public function middleware($req, $res)
 {
     $this->app['oauth_server'] = function ($c) {
         $storage = new IdealistStorage();
         $storage->injectApp($c);
         $server = new Server($storage);
         // password grant type
         $grantType = new UserCredentials($storage);
         $server->addGrantType($grantType);
         // JWT access token response type
         $config = $c['config']->get('oauth2');
         $jwtResponseType = new JwtAccessToken($storage, $storage, null, $config);
         $server->addResponseType($jwtResponseType);
         return $server;
     };
     $this->app['oauth_resource'] = function ($c) {
         $server = new Server();
         // no private key is necessary for the resource server
         $keyStorage = new Memory(['keys' => ['public_key' => file_get_contents(INFUSE_BASE_DIR . '/jwt_pubkey.pem')]]);
         $storage = new JwtAccessTokenStorage($keyStorage);
         $server->addStorage($storage, 'access_token');
         return $server;
     };
     // attempt to authenticate the user when an API request is made
     if ($req->isApi()) {
         $this->authenticateApiRequest();
     }
 }
Example #2
0
 /**
  * function to create the OAuth2 Server Object
  */
 public function setup(Application $app)
 {
     // ensure our Sqlite database exists
     if (!file_exists($sqliteFile = __DIR__ . '/../../../data/oauth.sqlite')) {
         $this->generateSqliteDb();
     }
     // create PDO-based sqlite storage
     $storage = new Pdo(array('dsn' => 'sqlite:' . $sqliteFile));
     // create array of supported grant types
     $grantTypes = array('authorization_code' => new AuthorizationCode($storage), 'user_credentials' => new UserCredentials($storage), 'refresh_token' => new RefreshToken($storage, array('always_issue_new_refresh_token' => true)));
     // instantiate the oauth server
     $server = new OAuth2Server($storage, array('enforce_state' => true, 'allow_implicit' => true, 'use_openid_connect' => true, 'issuer' => $_SERVER['HTTP_HOST']), $grantTypes);
     $server->addStorage($this->getKeyStorage(), 'public_key');
     // add the server to the silex "container" so we can use it in our controllers (see src/OAuth2Demo/Server/Controllers/.*)
     $app['oauth_server'] = $server;
     /**
      * add HttpFoundataionBridge Response to the container, which returns a silex-compatible response object
      * @see (https://github.com/bshaffer/oauth2-server-httpfoundation-bridge)
      */
     $app['oauth_response'] = new BridgeResponse();
 }
 /**
  * @expectedException LogicException OAuth2\Storage\PublicKeyInterface
  **/
 public function testUsingJwtAccessTokensWithoutPublicKeyStorageThrowsException()
 {
     $server = new Server(array(), array('use_jwt_access_tokens' => true));
     $server->addGrantType($this->getMock('OAuth2\\GrantType\\GrantTypeInterface'));
     $server->addStorage($this->getMock('OAuth2\\Storage\\ClientCredentialsInterface'));
     $server->addStorage($this->getMock('OAuth2\\Storage\\ClientCredentialsInterface'));
     $server->getTokenController();
 }
 public function testAddingResponseType()
 {
     $storage = $this->getMock('OAuth2\\Storage\\Memory');
     $storage->expects($this->any())->method('getClientDetails')->will($this->returnValue(array('client_id' => 'some_client')));
     $storage->expects($this->any())->method('checkRestrictedGrantType')->will($this->returnValue(true));
     // add with the "code" key explicitly set
     $codeType = new AuthorizationCode($storage);
     $server = new Server();
     $server->addStorage($storage);
     $server->addResponseType($codeType);
     $request = new Request(array('response_type' => 'code', 'client_id' => 'some_client', 'redirect_uri' => 'http://example.com', 'state' => 'xyx'));
     $server->handleAuthorizeRequest($request, $response = new Response(), true);
     // the response is successful
     $this->assertEquals($response->getStatusCode(), 302);
     $parts = parse_url($response->getHttpHeader('Location'));
     parse_str($parts['query'], $query);
     $this->assertTrue(isset($query['code']));
     $this->assertFalse(isset($query['error']));
     // add with the "code" key not set
     $codeType = new AuthorizationCode($storage);
     $server = new Server(array($storage), array(), array(), array($codeType));
     $request = new Request(array('response_type' => 'code', 'client_id' => 'some_client', 'redirect_uri' => 'http://example.com', 'state' => 'xyx'));
     $server->handleAuthorizeRequest($request, $response = new Response(), true);
     // the response is successful
     $this->assertEquals($response->getStatusCode(), 302);
     $parts = parse_url($response->getHttpHeader('Location'));
     parse_str($parts['query'], $query);
     $this->assertTrue(isset($query['code']));
     $this->assertFalse(isset($query['error']));
 }