| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | namespace SimpleID\Protocols\OpenID; |
| 24: | |
| 25: | use SimpleID\Protocols\XRDS\XRDSDiscovery; |
| 26: | use SimpleID\Models\Client; |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | class RelyingParty extends Client { |
| 36: | |
| 37: | |
| 38: | |
| 39: | protected $dynamic = true; |
| 40: | |
| 41: | |
| 42: | private $store_id; |
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 47: | public function __construct($realm) { |
| 48: | parent::__construct([ |
| 49: | 'openid' => [ 'realm' => $realm, 'services' => NULL, 'discovery_time' => 0 ] |
| 50: | ]); |
| 51: | $this->cid = $realm; |
| 52: | $this->store_id = self::buildID($realm); |
| 53: | } |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: | public function getRealm() { |
| 61: | return $this->container['openid']['realm']; |
| 62: | } |
| 63: | |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | public function getServices() { |
| 73: | return $this->container['openid']['services']; |
| 74: | } |
| 75: | |
| 76: | |
| 77: | |
| 78: | |
| 79: | |
| 80: | |
| 81: | |
| 82: | public function getDiscoveryTime() { |
| 83: | return $this->container['openid']['discovery_time']; |
| 84: | } |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: | |
| 91: | public function discover() { |
| 92: | $discovery = XRDSDiscovery::instance(); |
| 93: | $url = self::getDiscoveryURL($this->getRealm()); |
| 94: | $this->container['openid']['services'] = $discovery->discover($url); |
| 95: | } |
| 96: | |
| 97: | |
| 98: | |
| 99: | |
| 100: | |
| 101: | |
| 102: | |
| 103: | |
| 104: | |
| 105: | |
| 106: | |
| 107: | |
| 108: | |
| 109: | protected static function getDiscoveryURL($realm) { |
| 110: | $parts = parse_url($realm); |
| 111: | if ($parts == false) return $realm; |
| 112: | $host = strtr($parts['host'], [ '*.' => 'www.' ]); |
| 113: | |
| 114: | $url = $parts['scheme'] . '://'; |
| 115: | if (isset($parts['user'])) { |
| 116: | $url .= $parts['user']; |
| 117: | if (isset($parts['pass'])) $url .= ':' . $parts['pass']; |
| 118: | $url .= '@'; |
| 119: | } |
| 120: | $url .= $host; |
| 121: | if (isset($parts['port'])) $url .= ':' . $parts['port']; |
| 122: | if (isset($parts['path'])) $url .= $parts['path']; |
| 123: | if (isset($parts['query'])) $url .= '?' . $parts['query']; |
| 124: | if (isset($parts['fragment'])) $url .= '#' . $parts['fragment']; |
| 125: | return $url; |
| 126: | } |
| 127: | |
| 128: | |
| 129: | |
| 130: | |
| 131: | |
| 132: | public static function buildID($realm) { |
| 133: | $url = self::getDiscoveryURL($realm); |
| 134: | return '_' . trim(strtr(base64_encode(sha1($url, true)), '+/', '-_'), '=') . '.openid'; |
| 135: | } |
| 136: | |
| 137: | public function getStoreID() { |
| 138: | return $this->store_id; |
| 139: | } |
| 140: | |
| 141: | public function setStoreID($id) { |
| 142: | $this->store_id = $id; |
| 143: | } |
| 144: | |
| 145: | public function getDisplayName() { |
| 146: | return preg_replace('@^https?://(www\.|\*\.)?@', '', $this->getRealm()); |
| 147: | } |
| 148: | |
| 149: | public function getDisplayHTML() { |
| 150: | return preg_replace('@^https?://(www\.|\*\.)?@', '<span class="url-elide">$0</span>', $this->getRealm()); |
| 151: | } |
| 152: | } |
| 153: | |
| 154: | ?> |