/** * Register the authorization server. * * @return void */ protected function registerAuthorizationServer() { $this->app['microweber.oauth.authorization'] = $this->app->share(function ($app) { $server = new Authorization($app['microweber.oauth.storage'], $app['request']); // Set the access token and refresh token expirations on the server. $server->setAccessTokenExpiration($app['config']['oauth.expirations.access']); $server->setRefreshTokenExpiration($app['config']['oauth.expirations.refresh']); // Spin through each of the grants listed in the configuration file and // build an array of grants since some grants can be given options. foreach ($app['config']['oauth.grants'] as $key => $value) { if (!is_string($key)) { list($key, $value) = [$value, []]; } elseif (!is_array($value)) { $value = [$value]; } $grants[$key] = $value; } // We'll create an array of mappings to each of the grants class so that // users can use the shorthand name of the grant in the configuration // file. $mappings = ['password' => 'Microweber\\OAuth2\\Grant\\Password', 'client' => 'Microweber\\OAuth2\\Grant\\ClientCredentials', 'authorization' => 'Microweber\\OAuth2\\Grant\\AuthorizationCode', 'implicit' => 'Microweber\\OAuth2\\Grant\\Implicit', 'refresh' => 'Microweber\\OAuth2\\Grant\\RefreshToken']; // Spin through each of the grants and if it isn't set in the mappings // then we'll error out. Otherwise we'll get an instance of the // grant and register it on the server. foreach ($grants as $grant => $options) { if (!isset($mappings[$grant])) { throw new RuntimeException("Supplied grant [{$grant}] is invalid."); } $instance = new $mappings[$grant](); if ($grant == 'password') { $instance->setAuthenticationCallback(array_pop($options)); } elseif ($grant == 'authorization' and !empty($options)) { $instance->setAuthorizedCallback(array_pop($options)); } $server->registerGrant($instance); } return $server; }); }
public function testHandlingAuthorizationRequestSucceeds() { $storage = $this->getStorageMock(); $authorization = new Authorization($storage, Request::create('test', 'GET', ['response_type' => 'code', 'state' => 'teststate'])); $authorization->registerGrant(new AuthorizationCodeGrantStub()); $this->assertEquals(['code' => 'test', 'state' => 'teststate', 'scope' => 'testscope'], $authorization->handleAuthorizationRequest('testclient', 1, 'test', ['testscope' => true])); }