Completed
Pull Request — master (#736)
by
unknown
59:12 queued 56:55
created

OAuth2ServerServiceProvider::setupMigrations()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 4
nc 2
nop 1
crap 3
1
<?php
2
3
/*
4
 * This file is part of OAuth 2.0 Laravel.
5
 *
6
 * (c) Luca Degasperi <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LucaDegasperi\OAuth2Server;
13
14
use Illuminate\Contracts\Container\Container as Application;
15
use Illuminate\Foundation\Application as LaravelApplication;
16
use Illuminate\Support\ServiceProvider;
17
use Laravel\Lumen\Application as LumenApplication;
18
use League\OAuth2\Server\AuthorizationServer;
19
use League\OAuth2\Server\ResourceServer;
20
use League\OAuth2\Server\Storage\AccessTokenInterface;
21
use League\OAuth2\Server\Storage\AuthCodeInterface;
22
use League\OAuth2\Server\Storage\ClientInterface;
23
use League\OAuth2\Server\Storage\RefreshTokenInterface;
24
use League\OAuth2\Server\Storage\ScopeInterface;
25
use League\OAuth2\Server\Storage\SessionInterface;
26
use LucaDegasperi\OAuth2Server\Middleware\CheckAuthCodeRequestMiddleware;
27
use LucaDegasperi\OAuth2Server\Middleware\OAuthClientOwnerMiddleware;
28
use LucaDegasperi\OAuth2Server\Middleware\OAuthMiddleware;
29
use LucaDegasperi\OAuth2Server\Middleware\OAuthUserOwnerMiddleware;
30
31
/**
32
 * This is the oauth2 server service provider class.
33
 *
34
 * @author Luca Degasperi <[email protected]>
35
 */
36
class OAuth2ServerServiceProvider extends ServiceProvider
37
{
38
    /**
39
     * Boot the service provider.
40
     *
41
     * @return void
42
     */
43 108
    public function boot()
44
    {
45 108
        $this->setupConfig($this->app);
46 108
        $this->setupMigrations($this->app);
47 108
    }
48
49
    /**
50
     * Setup the config.
51
     *
52
     * @param \Illuminate\Contracts\Container\Container $app
53
     *
54
     * @return void
55
     */
56 108
    protected function setupConfig(Application $app)
57
    {
58 108
        $source = realpath(__DIR__.'/../config/oauth2.php');
59
60 108
        if ($app instanceof LaravelApplication && $app->runningInConsole()) {
61 108
            $this->publishes([$source => config_path('oauth2.php')]);
62 108
        } elseif ($app instanceof LumenApplication) {
0 ignored issues
show
Bug introduced by
The class Laravel\Lumen\Application does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
63
            $app->configure('oauth2');
64
        }
65
66 108
        $this->mergeConfigFrom($source, 'oauth2');
67 108
    }
68
69
    /**
70
     * Setup the migrations.
71
     *
72
     * @param \Illuminate\Contracts\Container\Container $app
73
     *
74
     * @return void
75
     */
76 108
    protected function setupMigrations(Application $app)
77
    {
78 108
        $source = realpath(__DIR__.'/../database/migrations/');
79
80 108
        if ($app instanceof LaravelApplication && $app->runningInConsole()) {
81 108
            $this->publishes([$source => database_path('migrations')], 'migrations');
82 108
        }
83 108
    }
84
85
    /**
86
     * Register the service provider.
87
     *
88
     * @return void
89
     */
90 108
    public function register()
91
    {
92 108
        $this->registerAuthorizer($this->app);
93 108
        $this->registerMiddlewareBindings($this->app);
94 108
    }
95
96
    /**
97
     * Register the Authorization server with the IoC container.
98
     *
99
     * @param \Illuminate\Contracts\Container\Container $app
100
     *
101
     * @return void
102
     */
103 108
    public function registerAuthorizer(Application $app)
104
    {
105
        $app->singleton('oauth2-server.authorizer', function ($app) {
106
            $config = $app['config']->get('oauth2');
107
            $issuer = $app->make(AuthorizationServer::class)
108
                ->setClientStorage($app->make(ClientInterface::class))
109
                ->setSessionStorage($app->make(SessionInterface::class))
110
                ->setAuthCodeStorage($app->make(AuthCodeInterface::class))
111
                ->setAccessTokenStorage($app->make(AccessTokenInterface::class))
112
                ->setRefreshTokenStorage($app->make(RefreshTokenInterface::class))
113
                ->setScopeStorage($app->make(ScopeInterface::class))
114
                ->requireScopeParam($config['scope_param'])
115
                ->setDefaultScope($config['default_scope'])
116
                ->requireStateParam($config['state_param'])
117
                ->setScopeDelimiter($config['scope_delimiter'])
118
                ->setAccessTokenTTL($config['access_token_ttl']);
119
120
            // add the supported grant types to the authorization server
121
            foreach ($config['grant_types'] as $grantIdentifier => $grantParams) {
122
                $grant = $app->make($grantParams['class']);
123
                $grant->setAccessTokenTTL($grantParams['access_token_ttl']);
124
125
                if (array_key_exists('callback', $grantParams)) {
126
                    list($className, $method) = array_pad(explode('@', $grantParams['callback']), 2, 'verify');
127
                    $verifier = $app->make($className);
128
                    $grant->setVerifyCredentialsCallback([$verifier, $method]);
129
                }
130
131
                if (array_key_exists('auth_token_ttl', $grantParams)) {
132
                    $grant->setAuthTokenTTL($grantParams['auth_token_ttl']);
133
                }
134
135
                if (array_key_exists('refresh_token_ttl', $grantParams)) {
136
                    $grant->setRefreshTokenTTL($grantParams['refresh_token_ttl']);
137
                }
138
139
                if (array_key_exists('rotate_refresh_tokens', $grantParams)) {
140
                    $grant->setRefreshTokenRotation($grantParams['rotate_refresh_tokens']);
141
                }
142
143
                $issuer->addGrantType($grant, $grantIdentifier);
144
            }
145
146
            $checker = $app->make(ResourceServer::class);
147
148
            $authorizer = new Authorizer($issuer, $checker);
149
            $authorizer->setRequest($app['request']);
150
            $authorizer->setTokenType($app->make($config['token_type']));
151
152
            $app->refresh('request', $authorizer, 'setRequest');
153
154
            return $authorizer;
155 108
        });
156
157 108
        $app->alias('oauth2-server.authorizer', Authorizer::class);
158 108
    }
159
160
    /**
161
     * Register the Middleware to the IoC container because
162
     * some middleware need additional parameters.
163
     *
164
     * @param \Illuminate\Contracts\Container\Container $app
165
     *
166
     * @return void
167
     */
168 108
    public function registerMiddlewareBindings(Application $app)
169
    {
170
        $app->singleton(CheckAuthCodeRequestMiddleware::class, function ($app) {
171
            return new CheckAuthCodeRequestMiddleware($app['oauth2-server.authorizer']);
172 108
        });
173
174
        $app->singleton(OAuthMiddleware::class, function ($app) {
175
            $httpHeadersOnly = $app['config']->get('oauth2.http_headers_only');
176
177
            return new OAuthMiddleware($app['oauth2-server.authorizer'], $httpHeadersOnly);
178 108
        });
179
180
        $app->singleton(OAuthClientOwnerMiddleware::class, function ($app) {
181
            return new OAuthClientOwnerMiddleware($app['oauth2-server.authorizer']);
182 108
        });
183
184 108
        $app->singleton(OAuthUserOwnerMiddleware::class, function ($app) {
185
            return new OAuthUserOwnerMiddleware($app['oauth2-server.authorizer']);
186 108
        });
187 108
    }
188
189
    /**
190
     * Get the services provided by the provider.
191
     *
192
     * @return string[]
193
     * @codeCoverageIgnore
194
     */
195
    public function provides()
196
    {
197
        return ['oauth2-server.authorizer'];
198
    }
199
}
200