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.');
 }
 /**
  * @test
  */
 public function it_can_authenticate_a_valid_bearer_request()
 {
     $accessToken = $this->getAccessToken();
     $_SERVER['HTTP_AUTHORIZATION'] = "Bearer {$accessToken}";
     $auth = new OAuthBearerRequestAuthenticator(self::$application);
     $result = $auth->authenticate(Request::createFromGlobals());
     $this->assertInstanceOf('Stormpath\\Authc\\Api\\OAuthBearerAuthenticationResult', $result);
     $this->assertInstanceOf('Stormpath\\Resource\\Application', $result->getApplication());
     $this->assertInstanceOf('Stormpath\\Resource\\ApiKey', $result->getApiKey());
 }