| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | namespace SimpleID\Protocols\OAuth; |
| 23: | |
| 24: | use Branca\Branca; |
| 25: | use SimpleID\Crypt\Random; |
| 26: | use SimpleID\Store\StoreManager; |
| 27: | use SimpleID\Util\SecureString; |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | abstract class Token { |
| 41: | |
| 42: | const GRANT_REF_SEPARATOR = '~'; |
| 43: | |
| 44: | |
| 45: | const TTL_PERPETUAL = 0; |
| 46: | |
| 47: | const KEY_FQAID = 'a'; |
| 48: | const KEY_TYPE = 't'; |
| 49: | const KEY_CACHE_HASH = 'h'; |
| 50: | const KEY_ID = 'i'; |
| 51: | const KEY_GRANTREF = 'r'; |
| 52: | const KEY_SCOPEREF = 's'; |
| 53: | const KEY_EXPIRE = 'x'; |
| 54: | |
| 55: | |
| 56: | protected $branca; |
| 57: | |
| 58: | |
| 59: | protected $id; |
| 60: | |
| 61: | |
| 62: | protected $authorization; |
| 63: | |
| 64: | |
| 65: | protected $scope; |
| 66: | |
| 67: | |
| 68: | protected $expire = NULL; |
| 69: | |
| 70: | |
| 71: | protected $grant_ref = NULL; |
| 72: | |
| 73: | |
| 74: | protected $additional = []; |
| 75: | |
| 76: | |
| 77: | protected $encoded = NULL; |
| 78: | |
| 79: | |
| 80: | protected $is_parsed = false; |
| 81: | |
| 82: | |
| 83: | protected function __construct() { |
| 84: | $this->branca = new Branca((StoreManager::instance())->getKey('oauth-token', true)); |
| 85: | } |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: | |
| 91: | |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | |
| 98: | |
| 99: | protected function init($authorization, $scope = [], $expires_in = self::TTL_PERPETUAL, $grant = NULL, $additional = []) { |
| 100: | $rand = new Random(); |
| 101: | |
| 102: | $this->id = $rand->id(); |
| 103: | $this->authorization = $authorization; |
| 104: | if (is_string($scope)) $scope = explode(' ', $scope); |
| 105: | if (count($scope) == 0) { |
| 106: | $this->scope = $authorization->getScope(); |
| 107: | } else { |
| 108: | $this->scope = $authorization->filterScope($scope); |
| 109: | } |
| 110: | |
| 111: | if ($grant != null) $this->grant_ref = $grant->getGrantRef(); |
| 112: | if ($expires_in > 0) $this->expire = time() + $expires_in; |
| 113: | $this->additional = $additional; |
| 114: | } |
| 115: | |
| 116: | |
| 117: | |
| 118: | |
| 119: | |
| 120: | |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: | |
| 126: | |
| 127: | |
| 128: | public function isValid() { |
| 129: | if (!$this->is_parsed) return false; |
| 130: | if ($this->expire != null) return !$this->hasExpired(); |
| 131: | return true; |
| 132: | } |
| 133: | |
| 134: | |
| 135: | |
| 136: | |
| 137: | |
| 138: | |
| 139: | public function getID() { |
| 140: | return $this->id; |
| 141: | } |
| 142: | |
| 143: | |
| 144: | |
| 145: | |
| 146: | |
| 147: | |
| 148: | |
| 149: | abstract public function getType(): string; |
| 150: | |
| 151: | |
| 152: | |
| 153: | |
| 154: | |
| 155: | |
| 156: | public function getAuthorization() { |
| 157: | return $this->authorization; |
| 158: | } |
| 159: | |
| 160: | |
| 161: | |
| 162: | |
| 163: | |
| 164: | |
| 165: | public function getScope() { |
| 166: | return $this->scope; |
| 167: | } |
| 168: | |
| 169: | |
| 170: | |
| 171: | |
| 172: | |
| 173: | |
| 174: | |
| 175: | |
| 176: | |
| 177: | |
| 178: | |
| 179: | public function hasScope($scope) { |
| 180: | if (!is_array($scope)) $scope = explode(' ', $scope); |
| 181: | return (count(array_diff($scope, $this->scope)) == 0); |
| 182: | } |
| 183: | |
| 184: | |
| 185: | |
| 186: | |
| 187: | |
| 188: | |
| 189: | public function getAdditionalData() { |
| 190: | return $this->additional; |
| 191: | } |
| 192: | |
| 193: | |
| 194: | |
| 195: | |
| 196: | |
| 197: | |
| 198: | |
| 199: | public function hasExpired() { |
| 200: | if ($this->expire == null) return false; |
| 201: | return (time() >= $this->expire); |
| 202: | } |
| 203: | |
| 204: | |
| 205: | |
| 206: | |
| 207: | |
| 208: | |
| 209: | |
| 210: | public function getExpiry() { |
| 211: | return $this->expire; |
| 212: | } |
| 213: | |
| 214: | |
| 215: | |
| 216: | |
| 217: | |
| 218: | |
| 219: | public function getEncoded() { |
| 220: | return $this->encoded; |
| 221: | } |
| 222: | |
| 223: | |
| 224: | |
| 225: | |
| 226: | |
| 227: | |
| 228: | public function revoke() { |
| 229: | $cache = \Cache::instance(); |
| 230: | $cache->clear($this->getCacheKey()); |
| 231: | } |
| 232: | |
| 233: | |
| 234: | |
| 235: | |
| 236: | |
| 237: | |
| 238: | |
| 239: | |
| 240: | |
| 241: | |
| 242: | |
| 243: | public static function revokeAll($authorization, $grant = null) { |
| 244: | $cache = \Cache::instance(); |
| 245: | |
| 246: | if ($grant != null) { |
| 247: | if ($grant instanceof TokenGrantType) { |
| 248: | $grant_ref = $grant->getGrantRef(); |
| 249: | } elseif (is_string($grant)) { |
| 250: | $grant_ref = $grant; |
| 251: | } else { |
| 252: | |
| 253: | throw new \InvalidArgumentException('$grant must be TokenGrantType or string'); |
| 254: | } |
| 255: | $suffix = self::GRANT_REF_SEPARATOR . $grant_ref; |
| 256: | } else { |
| 257: | $suffix = ''; |
| 258: | } |
| 259: | |
| 260: | $suffix .= '.' . $authorization->getFullyQualifiedID() . '.oauth_token'; |
| 261: | $cache->reset($suffix); |
| 262: | } |
| 263: | |
| 264: | |
| 265: | |
| 266: | |
| 267: | |
| 268: | |
| 269: | protected function getCacheKey() { |
| 270: | $key = $this->id; |
| 271: | if ($this->grant_ref != NULL) { |
| 272: | $key .= self::GRANT_REF_SEPARATOR . $this->grant_ref; |
| 273: | } |
| 274: | $key .= '.' . $this->authorization->getFullyQualifiedID() . '.oauth_token'; |
| 275: | return $key; |
| 276: | } |
| 277: | |
| 278: | |
| 279: | |
| 280: | |
| 281: | |
| 282: | |
| 283: | protected function parse() { |
| 284: | $store = StoreManager::instance(); |
| 285: | $cache = \Cache::instance(); |
| 286: | |
| 287: | try { |
| 288: | $message = $this->branca->decode($this->encoded); |
| 289: | $token_data = json_decode($message, true); |
| 290: | |
| 291: | $this->id = $token_data[self::KEY_ID]; |
| 292: | if ($token_data[self::KEY_TYPE] != $this->getType()) return; |
| 293: | |
| 294: | list($auth_state, $aid) = explode('.', $token_data[self::KEY_FQAID]); |
| 295: | $this->scope = $this->resolveScope($token_data[self::KEY_SCOPEREF]); |
| 296: | if (isset($token_data[self::KEY_EXPIRE])) $this->expire = $token_data[self::KEY_EXPIRE]; |
| 297: | if (isset($token_data[self::KEY_GRANTREF])) $this->grant_ref = $token_data[self::KEY_GRANTREF]; |
| 298: | |
| 299: | |
| 300: | $authorization = $store->loadAuth($aid); |
| 301: | $this->authorization = $authorization; |
| 302: | if ($this->authorization == NULL) return; |
| 303: | if ($this->authorization->getAuthState() != $auth_state) return; |
| 304: | |
| 305: | $server_data = $cache->get($this->getCacheKey()); |
| 306: | if ($server_data === false) return; |
| 307: | if (base64_encode(hash('sha256', serialize($server_data), true)) !== $token_data[self::KEY_CACHE_HASH]) return; |
| 308: | $this->additional = $server_data['additional']; |
| 309: | |
| 310: | $this->is_parsed = true; |
| 311: | } catch (\RuntimeException $e) { |
| 312: | return; |
| 313: | } |
| 314: | } |
| 315: | |
| 316: | |
| 317: | |
| 318: | |
| 319: | |
| 320: | |
| 321: | |
| 322: | |
| 323: | protected function encode($server_data = [], $token_data = []) { |
| 324: | $cache = \Cache::instance(); |
| 325: | |
| 326: | $fqaid = $this->authorization->getFullyQualifiedID(); |
| 327: | |
| 328: | $server_data = array_merge([ |
| 329: | 'id' => $this->id, |
| 330: | 'type' => $this->getType(), |
| 331: | 'fqaid' => $fqaid, |
| 332: | 'scope' => $this->scope, |
| 333: | 'additional' => $this->additional |
| 334: | ], $server_data); |
| 335: | $token_data = array_merge([ |
| 336: | self::KEY_ID => $server_data['id'], |
| 337: | self::KEY_TYPE => $this->getType(), |
| 338: | self::KEY_FQAID => $server_data['fqaid'], |
| 339: | self::KEY_SCOPEREF => $this->getScopeRef($this->scope), |
| 340: | ], $token_data); |
| 341: | |
| 342: | if ($this->expire != NULL) { |
| 343: | $server_data['expire'] = $this->expire; |
| 344: | $token_data[self::KEY_EXPIRE] = $this->expire; |
| 345: | } |
| 346: | |
| 347: | if ($this->grant_ref != NULL) { |
| 348: | $server_data['grant_ref'] = $this->grant_ref; |
| 349: | $token_data[self::KEY_GRANTREF] = $this->grant_ref; |
| 350: | } |
| 351: | |
| 352: | $cache->set($this->getCacheKey(), $server_data, ($this->expire != NULL) ? $this->expire - time() : 0); |
| 353: | $token_data[self::KEY_CACHE_HASH] = base64_encode(hash('sha256', serialize($server_data), true)); |
| 354: | |
| 355: | $json = json_encode($token_data); |
| 356: | assert($json != false); |
| 357: | $this->encoded = $this->branca->encode($json); |
| 358: | } |
| 359: | |
| 360: | |
| 361: | |
| 362: | |
| 363: | |
| 364: | |
| 365: | |
| 366: | |
| 367: | |
| 368: | |
| 369: | |
| 370: | protected function getScopeRef($scope) { |
| 371: | $ref = []; |
| 372: | |
| 373: | $store = StoreManager::instance(); |
| 374: | $scope_map = $store->getSetting('oauth_scope', []); |
| 375: | |
| 376: | foreach ($scope as $item) { |
| 377: | $i = array_search($item, $scope_map); |
| 378: | if ($i === false) { |
| 379: | $scope_map[] = $item; |
| 380: | $i = count($scope_map) - 1; |
| 381: | } |
| 382: | $ref[] = '\\' . $i; |
| 383: | } |
| 384: | |
| 385: | $store->setSetting('oauth_scope', $scope_map); |
| 386: | return implode(' ', $ref); |
| 387: | } |
| 388: | |
| 389: | |
| 390: | |
| 391: | |
| 392: | |
| 393: | |
| 394: | |
| 395: | |
| 396: | |
| 397: | protected function resolveScope($ref) { |
| 398: | $scope = []; |
| 399: | |
| 400: | $store = StoreManager::instance(); |
| 401: | $scope_map = $store->getSetting('oauth_scope', []); |
| 402: | |
| 403: | $refs = explode(' ', $ref); |
| 404: | foreach ($refs as $item) { |
| 405: | if (preg_match('/\\\\(\d+)/', $item, $matches)) { |
| 406: | $scope[] = $scope_map[$matches[1]]; |
| 407: | } |
| 408: | } |
| 409: | |
| 410: | return $scope; |
| 411: | } |
| 412: | |
| 413: | |
| 414: | |
| 415: | |
| 416: | |
| 417: | |
| 418: | |
| 419: | static function getScopeRefMap() { |
| 420: | $store = StoreManager::instance(); |
| 421: | return $store->getSetting('oauth_scope', []); |
| 422: | } |
| 423: | } |
| 424: | |
| 425: | ?> |