public function testRequest() { $testCase = $this; $http = new Http(new Callback(function (RequestInterface $request) use($testCase) { // api request if ($request->getUri()->getPath() == '/api') { $testCase->assertEquals('Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW', (string) $request->getHeader('Authorization')); $testCase->assertEquals('application/x-www-form-urlencoded', (string) $request->getHeader('Content-Type')); $testCase->assertEquals('grant_type=authorization_code&code=SplxlOBeZQQYbYS6WxSbIA', (string) $request->getBody()); $response = <<<TEXT HTTP/1.1 200 OK Content-Type: application/json;charset=UTF-8 Cache-Control: no-store Pragma: no-cache { "access_token":"2YotnFZFEjr1zCsicMWpAA", "token_type":"example", "expires_in":3600, "example_parameter":"example_value" } TEXT; } else { throw new \RuntimeException('Invalid path'); } return ResponseParser::convert($response, ResponseParser::MODE_LOOSE)->toString(); })); $oauth = new AuthorizationCode($http, new Url('http://127.0.0.1/api'), Environment::getService('importer')); $oauth->setClientPassword(self::CLIENT_ID, self::CLIENT_SECRET); $accessToken = $oauth->getAccessToken('SplxlOBeZQQYbYS6WxSbIA'); $this->assertEquals('2YotnFZFEjr1zCsicMWpAA', $accessToken->getAccessToken()); $this->assertEquals('example', $accessToken->getTokenType()); $this->assertEquals(3600, $accessToken->getExpiresIn()); }
protected function getAuthorizationCode($code, $state) { $testCase = $this->testCase; $http = new Http(new Callback(function (RequestInterface $request) use($testCase) { // api request if ($request->getUri()->getPath() == '/api') { $testCase->assertEquals('Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW', (string) $request->getHeader('Authorization')); $testCase->assertEquals('application/x-www-form-urlencoded', (string) $request->getHeader('Content-Type')); $response = <<<TEXT HTTP/1.1 200 OK Content-Type: application/json;charset=UTF-8 Cache-Control: no-store Pragma: no-cache { "access_token":"2YotnFZFEjr1zCsicMWpAA", "token_type":"example", "expires_in":3600, "refresh_token":"tGzv3JOkF0XG5Qx2TlKWIA", "example_parameter":"example_value" } TEXT; } else { throw new \RuntimeException('Invalid path'); } return ResponseParser::convert($response, ResponseParser::MODE_LOOSE)->toString(); })); $oauth = new AuthorizationCode($http, new Url('http://127.0.0.1/api'), Environment::getService('importer')); $oauth->setClientPassword(ClientCredentialsTest::CLIENT_ID, ClientCredentialsTest::CLIENT_SECRET); return $oauth; }
public function callback($code, $state, Closure $callback) { $params = $this->store->load('openid_connect_request'); if (empty($params)) { throw new Exception('Request was not initialized'); } if (empty($state)) { throw new Exception('State parameter not set'); } if ($params->getState() != $state) { throw new Exception('Invalid state'); } $auth = new AuthorizationCode($this->http, $this->creds->getAccessTokenUrl()); $auth->setClientPassword($this->creds->getClientId(), $this->creds->getClientSecret(), AuthorizationAbstract::AUTH_POST); $auth->setAccessTokenClass('PSX\\OpenId\\Connect\\AccessToken'); $token = $auth->getAccessToken($code, $params->getRedirectUri()); $webToken = $token->getIdToken(); if ($webToken instanceof WebToken) { $claim = Json::decode($webToken->getPayload()); $callback($claim); } else { throw new Exception('No id token given'); } }
public function callback() { $code = new AuthorizationCode($this->http, new Url(self::ACCESS_TOKEN)); $code->setClientPassword(self::CLIENT_ID, self::CLIENT_SECRET, AuthorizationCode::AUTH_POST); $accessToken = $code->getAccessToken($this->pageUrl . '/callback/facebook'); // request user informations $url = new Url(self::VERIFY_ACCOUNT); $header = array('Authorization' => $this->oauth->getAuthorizationHeader($accessToken)); $request = new GetRequest($url, $header); $response = $this->http->request($request); if ($response->getCode() == 200) { $acc = Json::decode($response->getBody()); if (empty($acc)) { throw new Exception('No user informations provided'); } if (empty($acc['id'])) { throw new Exception('No user id provided'); } $identity = $acc['id']; $con = new Condition(array('identity', '=', sha1($this->config['amun_salt'] . $identity))); $userId = $this->hm->getTable('AmunService\\User\\Account')->getField('id', $con); if (empty($userId)) { // user doesnt exist so register a new user check whether // registration is enabled if (!$this->registry['login.registration_enabled']) { throw new Exception('Registration is disabled'); } if (empty($acc['username'])) { throw new Exception('No username provided'); } $name = $this->normalizeName($acc['username']); // create user account $security = new Security($this->registry); $handler = $this->hm->getHandler('AmunService\\User\\Account', $this->user); $account = $handler->getRecord(); $account->setGroupId($this->registry['core.default_user_group']); $account->setStatus(Account\Record::NORMAL); $account->setIdentity($identity); $account->setName($name); $account->setPw($security->generatePw()); $account->profileUrl = isset($acc['link']) ? $acc['link'] : null; $account->thumbnailUrl = 'http://graph.facebook.com/' . $identity . '/picture'; $account = $handler->create($account); $userId = $account->id; // if the id is not set the account was probably added to // the approval table if (!empty($userId)) { $this->setUserId($userId); } else { throw new Exception('Could not create account'); } } else { $this->setUserId($userId); } // redirect header('Location: ' . $this->config['psx_url']); exit; } else { throw new Exception('Authentication failed'); } }