/**
  * @param Request $request
  * @param $apiKey
  * @throws RequestAuthenticatorException
  */
 protected function isValidApiKey(Request $request, $apiKey)
 {
     if ($apiKey->getStatus() == 'DISABLED') {
         throw new RequestAuthenticatorException('The API Key is not allowed to make this request.');
     }
     if ($request->getScheme() == 'Bearer') {
         return true;
     }
     if ($apiKey === null || !!($request->getApiSecret() != $apiKey->getSecret())) {
         throw new RequestAuthenticatorException('The API Key is not valid for this request.');
     }
     return true;
 }
 public function authenticate(Request $request)
 {
     $authenticator = null;
     if ($request->hasAuthorizationHeader()) {
         if ($request->isBasicAuthorization()) {
             if ($request->hasGrantType()) {
                 $authenticator = new OAuthClientCredentialsRequestAuthenticator($this->application);
             } else {
                 $authenticator = new BasicRequestAuthenticator($this->application);
             }
         } else {
             if ($request->isBearerAuthorization()) {
                 $authenticator = new OAuthBearerRequestAuthenticator($this->application);
             }
         }
     }
     if ($authenticator) {
         $result = $authenticator->authenticate($request);
         $application = $result->getApplication();
         $apiKey = $result->getApiKey();
         $accessToken = null;
         if (method_exists($result, 'getAccessToken')) {
             $accessToken = $result->getAccessToken();
         }
         return new ApiAuthenticationResult($application, $apiKey, $accessToken);
     }
     throw new RequestAuthenticatorException('The method of authentication you are trying is not an allowed method.
                                              Please make sure you are using one of the following methods for
                                              Authentication: Basic, OAuth Bearer, or OAuth Client Credentials.');
 }
 /** @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;
 }
 protected function tearDown()
 {
     Request::tearDown();
 }