| 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\Crypt; |
| 23: | |
| 24: | use Branca\Branca; |
| 25: | use JsonException; |
| 26: | use SimpleID\Store\StoreManager; |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 47: | |
| 48: | class SecurityToken { |
| 49: | |
| 50: | static private $site_token = null; |
| 51: | |
| 52: | const OPTION_DEFAULT = 0; |
| 53: | |
| 54: | |
| 55: | const OPTION_BIND_SESSION = 1; |
| 56: | |
| 57: | |
| 58: | const OPTION_NONCE = 2; |
| 59: | |
| 60: | |
| 61: | private $branca; |
| 62: | |
| 63: | |
| 64: | |
| 65: | private $data = null; |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | function __construct($key = null) { |
| 77: | if ($key == null) { |
| 78: | if (self::$site_token === null) self::$site_token = (StoreManager::instance())->getKey('site-token'); |
| 79: | $key = self::$site_token; |
| 80: | } |
| 81: | |
| 82: | |
| 83: | $this->branca = new Branca(base64_decode(strtr($key, '-_', '+/'))); |
| 84: | } |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: | |
| 91: | |
| 92: | |
| 93: | |
| 94: | public function getPayload($token, $ttl = null) { |
| 95: | try { |
| 96: | $message = $this->branca->decode($token, $ttl); |
| 97: | } catch (\RuntimeException $e) { |
| 98: | return null; |
| 99: | } |
| 100: | |
| 101: | $decompressed = gzuncompress($message); |
| 102: | if ($decompressed == false) return null; |
| 103: | |
| 104: | $this->data = json_decode($decompressed, true); |
| 105: | |
| 106: | if (($this->data['o'] & self::OPTION_BIND_SESSION) == self::OPTION_BIND_SESSION) { |
| 107: | if (!isset($this->data['s'])) return null; |
| 108: | if ($this->data['s'] != session_id()) return null; |
| 109: | } |
| 110: | |
| 111: | if (($this->data['o'] & self::OPTION_NONCE) == self::OPTION_NONCE) { |
| 112: | if (!isset($this->data['i'])) return null; |
| 113: | |
| 114: | $cache = \Cache::instance(); |
| 115: | $cache_name = rawurlencode($this->data['i']) . '.token'; |
| 116: | |
| 117: | if (!$cache->exists($cache_name)) return null; |
| 118: | $cache_token = $cache->get($cache_name); |
| 119: | $cache->clear($cache_name); |
| 120: | if ($token != $cache_token) return null; |
| 121: | } |
| 122: | |
| 123: | if (!isset($this->data['p'])) return null; |
| 124: | return $this->data['p']; |
| 125: | } |
| 126: | |
| 127: | |
| 128: | |
| 129: | |
| 130: | |
| 131: | |
| 132: | |
| 133: | |
| 134: | |
| 135: | |
| 136: | |
| 137: | |
| 138: | public function verify($token, $expected, $ttl = null) { |
| 139: | return ($this->getPayload($token, $ttl) == $expected); |
| 140: | } |
| 141: | |
| 142: | |
| 143: | |
| 144: | |
| 145: | |
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: | |
| 151: | |
| 152: | |
| 153: | |
| 154: | public function generate($payload, $options = self::OPTION_DEFAULT) { |
| 155: | $rand = new Random(); |
| 156: | $this->data = [ |
| 157: | 'i' => $rand->id(), |
| 158: | 'o' => $options, |
| 159: | 'p' => $payload |
| 160: | ]; |
| 161: | if (($options & self::OPTION_BIND_SESSION) == self::OPTION_BIND_SESSION) { |
| 162: | $this->data['s'] = session_id(); |
| 163: | } |
| 164: | |
| 165: | try { |
| 166: | $encoded = json_encode($this->data, JSON_THROW_ON_ERROR); |
| 167: | |
| 168: | $compressed = gzcompress($encoded); |
| 169: | if ($compressed == false) return new \RuntimeException(); |
| 170: | |
| 171: | $token = $this->branca->encode($compressed); |
| 172: | |
| 173: | if (($options & self::OPTION_NONCE) == self::OPTION_NONCE) { |
| 174: | $cache = \Cache::instance(); |
| 175: | $cache_name = rawurlencode($this->data['i']) . '.token'; |
| 176: | $cache->set($cache_name, $token, SIMPLEID_HUMAN_TOKEN_EXPIRES_IN); |
| 177: | } |
| 178: | |
| 179: | return $token; |
| 180: | } catch (\JsonException $e) { |
| 181: | return new \RuntimeException($e->getMessage(), 0, $e); |
| 182: | } |
| 183: | } |
| 184: | } |
| 185: | |
| 186: | ?> |