| 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 \Base; |
| 25: | use SimpleID\Protocols\CustomRedirectResponse; |
| 26: | use SimpleID\Protocols\FormResponse; |
| 27: | use SimpleID\Util\ArrayWrapper; |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | class Response extends ArrayWrapper { |
| 38: | |
| 39: | const QUERY_RESPONSE_MODE = 'query'; |
| 40: | |
| 41: | |
| 42: | const FRAGMENT_RESPONSE_MODE = 'fragment'; |
| 43: | |
| 44: | |
| 45: | const FORM_POST_RESPONSE_MODE = 'form_post'; |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | protected $response_mode = self::QUERY_RESPONSE_MODE; |
| 54: | |
| 55: | |
| 56: | protected $redirect_uri = null; |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | public function __construct($request = NULL, $data = []) { |
| 70: | if (isset($data['#response_mode'])) { |
| 71: | $this->response_mode = $data['#response_mode']; |
| 72: | unset($data['#response_mode']); |
| 73: | } |
| 74: | if (isset($data['#redirect_uri'])) { |
| 75: | $this->redirect_uri = $data['#redirect_uri']; |
| 76: | unset($data['#redirect_uri']); |
| 77: | } |
| 78: | |
| 79: | parent::__construct($data); |
| 80: | |
| 81: | if ($request != NULL) { |
| 82: | if (isset($request['state'])) $this->container['state'] = $request['state']; |
| 83: | if (isset($request['redirect_uri']) && ($this->redirect_uri == null)) $this->redirect_uri = $request['redirect_uri']; |
| 84: | } |
| 85: | } |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: | |
| 91: | |
| 92: | public function getResponseMode() { |
| 93: | return $this->response_mode; |
| 94: | } |
| 95: | |
| 96: | |
| 97: | |
| 98: | |
| 99: | |
| 100: | |
| 101: | |
| 102: | |
| 103: | public function setResponseMode($response_mode) { |
| 104: | $this->response_mode = $response_mode; |
| 105: | } |
| 106: | |
| 107: | |
| 108: | |
| 109: | |
| 110: | |
| 111: | |
| 112: | |
| 113: | public function setRedirectURI($redirect_uri) { |
| 114: | $this->redirect_uri = $redirect_uri; |
| 115: | } |
| 116: | |
| 117: | |
| 118: | |
| 119: | |
| 120: | |
| 121: | |
| 122: | public function getRedirectURI() { |
| 123: | return $this->redirect_uri; |
| 124: | } |
| 125: | |
| 126: | |
| 127: | |
| 128: | |
| 129: | |
| 130: | |
| 131: | |
| 132: | |
| 133: | |
| 134: | public function isError() { |
| 135: | return isset($this->container['error']); |
| 136: | } |
| 137: | |
| 138: | |
| 139: | |
| 140: | |
| 141: | |
| 142: | |
| 143: | |
| 144: | |
| 145: | |
| 146: | |
| 147: | |
| 148: | public function setError($error, $error_description = NULL, $additional = []) { |
| 149: | foreach (array_keys($this->container) as $key) { |
| 150: | if ($key != 'state') unset($this->container[$key]); |
| 151: | } |
| 152: | $this->container['error'] = $error; |
| 153: | if ($error_description != null) $this->container['error_description'] = $error_description; |
| 154: | $this->container = array_merge($this->container, $additional); |
| 155: | return $this; |
| 156: | } |
| 157: | |
| 158: | |
| 159: | |
| 160: | |
| 161: | |
| 162: | |
| 163: | |
| 164: | |
| 165: | |
| 166: | |
| 167: | public function renderRedirect($redirect_uri = NULL) { |
| 168: | $f3 = Base::instance(); |
| 169: | |
| 170: | if ($redirect_uri == NULL) $redirect_uri = $this->redirect_uri; |
| 171: | |
| 172: | if ($redirect_uri == NULL) { |
| 173: | $this->setError('server_error', 'Missing redirect_uri'); |
| 174: | $this->renderJSON(); |
| 175: | return; |
| 176: | } |
| 177: | |
| 178: | if ($this->response_mode == self::FORM_POST_RESPONSE_MODE) $this->renderFormPost($redirect_uri); |
| 179: | |
| 180: | $parts = parse_url($redirect_uri); |
| 181: | |
| 182: | |
| 183: | $query = str_replace([ '+', '%7E' ], [ '%20', '~' ], http_build_query($this->container)); |
| 184: | |
| 185: | |
| 186: | if (!$query) { |
| 187: | $url = $redirect_uri; |
| 188: | } else { |
| 189: | |
| 190: | |
| 191: | |
| 192: | if ($parts == false) { |
| 193: | $url = $redirect_uri; |
| 194: | } else { |
| 195: | $url = $parts['scheme'] . '://'; |
| 196: | if (isset($parts['user'])) { |
| 197: | $url .= $parts['user']; |
| 198: | if (isset($parts['pass'])) $url .= ':' . $parts['pass']; |
| 199: | $url .= '@'; |
| 200: | } |
| 201: | if (isset($parts['host'])) $url .= $parts['host']; |
| 202: | if (isset($parts['port'])) $url .= ':' . $parts['port']; |
| 203: | if (isset($parts['path'])) $url .= $parts['path']; |
| 204: | |
| 205: | if (($this->response_mode == self::QUERY_RESPONSE_MODE) || (strpos($url, '#') === FALSE)) { |
| 206: | $url .= '?' . ((isset($parts['query'])) ? $parts['query'] . '&' : '') . $query; |
| 207: | if (isset($parts['fragment'])) $url .= '#' . $parts['fragment']; |
| 208: | } elseif ($this->response_mode == self::FRAGMENT_RESPONSE_MODE) { |
| 209: | |
| 210: | |
| 211: | |
| 212: | if (!isset($parts['fragment'])) $parts['fragment'] = ''; |
| 213: | if (isset($parts['query'])) { |
| 214: | $url .= '?' . $parts['query'] . '#' . $parts['fragment'] . '&' . $query; |
| 215: | } else { |
| 216: | $url .= '#' . $parts['fragment'] . '&' . $query; |
| 217: | } |
| 218: | } |
| 219: | } |
| 220: | } |
| 221: | |
| 222: | if (isset($parts['scheme']) && ((strtolower($parts['scheme']) == 'https') || (strtolower($parts['scheme']) == 'http'))) { |
| 223: | $f3->status(303); |
| 224: | header('Location: ' . $url); |
| 225: | } else { |
| 226: | $redirect = new CustomRedirectResponse($url); |
| 227: | $redirect->render(); |
| 228: | } |
| 229: | } |
| 230: | |
| 231: | |
| 232: | |
| 233: | |
| 234: | |
| 235: | |
| 236: | |
| 237: | |
| 238: | |
| 239: | |
| 240: | public function renderJSON($status = NULL) { |
| 241: | $f3 = Base::instance(); |
| 242: | |
| 243: | if ($status == NULL) { |
| 244: | $status = ($this->isError()) ? 400 : 200; |
| 245: | } |
| 246: | $f3->status($status); |
| 247: | $f3->expire(0); |
| 248: | |
| 249: | header('Content-Type: application/json;charset=UTF-8'); |
| 250: | header('Pragma: no-cache'); |
| 251: | print json_encode($this->container); |
| 252: | } |
| 253: | |
| 254: | |
| 255: | |
| 256: | |
| 257: | |
| 258: | |
| 259: | |
| 260: | public function renderFormPost($url = NULL) { |
| 261: | $form = new FormResponse($this->container); |
| 262: | if ($url == NULL) $url = $this->redirect_uri; |
| 263: | $form->render($url); |
| 264: | } |
| 265: | |
| 266: | |
| 267: | |
| 268: | |
| 269: | public function toArray() { |
| 270: | $array = parent::toArray(); |
| 271: | $array['#response_mode'] = $this->response_mode; |
| 272: | $array['#redirect_uri'] = $this->redirect_uri; |
| 273: | return $array; |
| 274: | } |
| 275: | |
| 276: | |
| 277: | |
| 278: | |
| 279: | |
| 280: | |
| 281: | public static function getResponseModesSupported() { |
| 282: | return [ self::QUERY_RESPONSE_MODE, self::FRAGMENT_RESPONSE_MODE, self::FORM_POST_RESPONSE_MODE ]; |
| 283: | } |
| 284: | } |
| 285: | |
| 286: | ?> |