/** * @test */ public function it_can_authenticate_a_basic_request() { $authorization = 'Basic ' . base64_encode(self::$apiKey->id . ':' . self::$apiKey->secret); $_SERVER['HTTP_AUTHORIZATION'] = $authorization; self::$apiKey->setStatus('ENABLED'); self::$apiKey->save(); self::$account->setStatus('ENABLED'); self::$account->save(); $auth = new BasicRequestAuthenticator(self::$application); $result = $auth->authenticate(Request::createFromGlobals()); $this->assertInstanceOf('Stormpath\\Authc\\Api\\BasicAuthenticationResult', $result); $this->assertInstanceOf('Stormpath\\Resource\\Application', $result->getApplication()); $this->assertInstanceOf('Stormpath\\Resource\\ApiKey', $result->getApiKey()); }
/** @codeCoverageIgnore */ private function doClientCredentialsGrantType($request) { if (!config('stormpath.web.oauth2.client_credentials.enabled')) { return $this->respondUnsupportedGrantType(); } try { $request = \Stormpath\Authc\Api\Request::createFromGlobals(); $result = (new OAuthClientCredentialsRequestAuthenticator(app('stormpath.application')))->authenticate($request); $tokenResponse = json_decode($result->getAccessToken()); return response()->json(['access_token' => $tokenResponse->access_token, 'token_type' => $tokenResponse->token_type, 'expires_in' => config('stormpath.web.oauth2.client_credentials.accessToken.ttl')]); } catch (\Exception $e) { return $this->respondWithInvalidRequest($e->getMessage()); } }
/** * @return array */ private function getAccessToken() { $authorization = 'Basic ' . base64_encode(self::$apiKey->id . ':' . self::$apiKey->secret); $_SERVER['HTTP_AUTHORIZATION'] = $authorization; $_SERVER['REQUEST_URI'] = 'http://test.com/?grant_type=client_credentials'; $_SERVER['QUERY_STRING'] = 'grant_type=client_credentials'; self::$apiKey->setStatus('ENABLED'); self::$apiKey->save(); self::$account->setStatus('ENABLED'); self::$account->save(); $auth = new OAuthClientCredentialsRequestAuthenticator(self::$application); $result = $auth->authenticate(Request::createFromGlobals()); $token = json_decode($result->getAccessToken()); $accessToken = $token->access_token; Request::tearDown(); return $accessToken; }
/** * @test */ public function it_can_authenticate_and_return_oauth_client_credentials_result() { $authorization = 'Basic ' . base64_encode(self::$apiKey->id . ':' . self::$apiKey->secret); $_SERVER['HTTP_AUTHORIZATION'] = $authorization; $_SERVER['REQUEST_URI'] = 'http://test.com/?grant_type=client_credentials'; $_SERVER['QUERY_STRING'] = 'grant_type=client_credentials'; self::$apiKey->setStatus('ENABLED'); self::$apiKey->save(); self::$account->setStatus('ENABLED'); self::$account->save(); $auth = new OAuthClientCredentialsRequestAuthenticator(self::$application); $result = $auth->authenticate(Request::createFromGlobals()); $token = json_decode($result->getAccessToken()); $this->assertInstanceOf('Stormpath\\Authc\\Api\\OAuthClientCredentialsAuthenticationResult', $result); $this->assertInstanceOf('Stormpath\\Resource\\Application', $result->getApplication()); $this->assertInstanceOf('Stormpath\\Resource\\ApiKey', $result->getApiKey()); $this->assertObjectHasAttribute('access_token', $token); $this->assertObjectHasAttribute('token_type', $token); $this->assertObjectHasAttribute('expires_in', $token); }