Exemplo n.º 1
0
 public function register(Application $app)
 {
     parent::register($app);
     $app[AuthorizationServer::class] = $app->share(function () use($app) {
         /** @var AuthorizationServer $server */
         $server = (new AuthorizationServer())->setAccessTokenStorage($app['oauth.accesstoken-storage'])->setSessionStorage($app['oauth.session-storage'])->setRefreshTokenStorage($app['oauth.refreshtoken-storage'])->setClientStorage($app['oauth.client-storage'])->setScopeStorage($app['oauth.scope-storage'])->setAuthCodeStorage($app['oauth.authcode-storage']);
         // standard auth code grant
         $authCodeGrant = new AuthCodeGrant();
         $server->addGrantType($authCodeGrant);
         // password grant used by our apps
         $passwordGrant = new PasswordGrant();
         $passwordGrant->setVerifyCredentialsCallback(function ($username, $password) use($app) {
             /** @var OAuth2AuthenticatorInterface $auth */
             $auth = $app['oauth.authenticator'];
             $user = $auth->findUser(['username' => $username]);
             if ($user) {
                 return $auth->authenticate($user, ['username' => $username, 'password' => $password]);
             }
             return false;
         });
         $server->addGrantType($passwordGrant);
         $refreshTokenGrant = new RefreshTokenGrant();
         $refreshTokenGrant->setRequireClientSecret(false);
         $server->addGrantType($refreshTokenGrant);
         return $server;
     });
     $app[ResourceServer::class] = $app->share(function () use($app) {
         return new ResourceServer($app['oauth.session-storage'], $app['oauth.accesstoken-storage'], $app['oauth.client-storage'], $app['oauth.scope-storage']);
     });
     $app['security.authentication_listener.factory.oauth'] = $app->protect(function ($name) use($app) {
         $app['security.authentication_provider.' . $name . '.oauth'] = $app->share(function ($app) {
             return $app[OAuth2Provider::class];
         });
         $app['security.authentication_listener.' . $name . '.oauth'] = $app->share(function ($app) {
             return $app[OAuth2Listener::class];
         });
         return ['security.authentication_provider.' . $name . '.oauth', 'security.authentication_listener.' . $name . '.oauth', null, 'pre_auth'];
     });
     $app['security.authentication_listener.factory.oauth-optional'] = $app->protect(function ($name) use($app) {
         $app['security.authentication_provider.' . $name . '.oauth-optional'] = $app->share(function ($app) {
             $provider = new OAuth2Provider();
             $provider->setContainer($app);
             return $provider;
         });
         $app['security.authentication_listener.' . $name . '.oauth-optional'] = $app->share(function ($app) {
             $provider = new OAuth2OptionalListener();
             $provider->setContainer($app);
             return $provider;
         });
         return ['security.authentication_provider.' . $name . '.oauth-optional', 'security.authentication_listener.' . $name . '.oauth-optional', null, 'pre_auth'];
     });
 }