Completed
Pull Request — devel (#13)
by Philippe
02:33
created

Oauth::prepareServerConfig()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 2.0023

Importance

Changes 3
Bugs 0 Features 1
Metric Value
dl 0
loc 27
c 3
b 0
f 1
ccs 22
cts 24
cp 0.9167
rs 8.8571
cc 2
eloc 23
nc 2
nop 2
crap 2.0023
1
<?php
2
/**
3
 * Oauth.php
4
 *
5
 * PHP version 5.6+
6
 *
7
 * @author Philippe Gaultier <[email protected]>
8
 * @copyright 2010-2017 Philippe Gaultier
9
 * @license http://www.sweelix.net/license license
10
 * @version 1.1.0
11
 * @link http://www.sweelix.net
12
 * @package sweelix\oauth2\server\services
13
 */
14
15
namespace sweelix\oauth2\server\services;
16
17
use OAuth2\GrantType\AuthorizationCode;
18
use OAuth2\GrantType\ClientCredentials;
19
use OAuth2\GrantType\JwtBearer;
20
use OAuth2\GrantType\RefreshToken;
21
use OAuth2\GrantType\UserCredentials;
22
use OAuth2\Server;
23
use sweelix\oauth2\server\interfaces\ServiceBootstrapInterface;
24
use sweelix\oauth2\server\Module;
25
use yii\helpers\Url;
26
use Yii;
27
28
/**
29
 * This is the service loader for bshaffer oauth2 elements
30
 *
31
 * @author Philippe Gaultier <[email protected]>
32
 * @copyright 2010-2017 Philippe Gaultier
33
 * @license http://www.sweelix.net/license license
34
 * @version 1.1.0
35
 * @link http://www.sweelix.net
36
 * @package sweelix\oauth2\server\services
37
 * @since 1.0.0
38
 */
39
class Oauth implements ServiceBootstrapInterface
40
{
41
    /**
42
     * @inheritdoc
43
     */
44 52
    public static function register($app)
45
    {
46 52
        $module = Module::getInstance();
47
        Yii::$container->set('OAuth2\Server', function($container, $params, $config) use ($module) {
48 16
            $storage = Yii::createObject('sweelix\oauth2\server\storage\OauthStorage');
49 16
            $server = new Server($storage, self::prepareServerConfig($config, $module));
50 16
            return $server;
51 52
        });
52
53
        Yii::$container->set('OAuth2\GrantType\AuthorizationCode', function($container, $params, $config) {
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
54 4
            $storage = Yii::createObject('sweelix\oauth2\server\storage\OauthStorage');
55 4
            $grantType = new AuthorizationCode($storage);
56 4
            return $grantType;
57 52
        });
58
59
        Yii::$container->set('OAuth2\GrantType\ClientCredentials', function($container, $params, $config) use ($module) {
60 2
            $storage = Yii::createObject('sweelix\oauth2\server\storage\OauthStorage');
61 2
            $grantType = new ClientCredentials($storage, self::prepareServerConfig($config, $module));
62 2
            return $grantType;
63 52
        });
64
65
        Yii::$container->set('OAuth2\GrantType\JwtBearer', function($container, $params, $config) use ($module) {
66
            $storage = Yii::createObject('sweelix\oauth2\server\storage\OauthStorage');
67
            $audience = Url::to($module->jwtAudience, true);
68
            $grantType = new JwtBearer($storage, $audience, null, self::prepareServerConfig($config, $module));
69
            return $grantType;
70 52
        });
71
72
        Yii::$container->set('OAuth2\GrantType\RefreshToken', function($container, $params, $config) use ($module) {
73 1
            $storage = Yii::createObject('sweelix\oauth2\server\storage\OauthStorage');
74 1
            $grantType = new RefreshToken($storage, self::prepareServerConfig($config, $module));
75 1
            return $grantType;
76 52
        });
77
78 52
        Yii::$container->set('OAuth2\GrantType\UserCredentials', function($container, $params, $config) {
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79 4
            $storage = Yii::createObject('sweelix\oauth2\server\storage\OauthStorage');
80 4
            $grantType = new UserCredentials($storage);
81 4
            return $grantType;
82 52
        });
83
84 52
    }
85
86
    /**
87
     * @param mixed $config
88
     * @return array Oauth server configuration
89
     * @since 1.0.0
90
     */
91 16
    protected static function prepareServerConfig($config, $module)
92
    {
93
        $baseConfig = [
94 16
            'use_jwt_access_tokens' => $module->allowJwtAccessToken,
95 16
            'store_encrypted_token_string' => $module->storeEncryptedTokenString,
96 16
            'use_openid_connect' => $module->allowOpenIdConnect,
97 16
            'id_lifetime' => $module->idTTL,
98 16
            'access_lifetime' => $module->accessTokenTTL,
99 16
            'refresh_token_lifetime' => $module->refreshTokenTTL,
100 16
            'www_realm' => $module->realm,
101 16
            'token_param_name' => $module->tokenQueryName,
102 16
            'token_bearer_header_name' => $module->tokenBearerName,
103 16
            'enforce_state' => $module->enforceState,
104 16
            'require_exact_redirect_uri' => $module->allowOnlyRedirectUri,
105 16
            'allow_implicit' => $module->allowImplicit,
106 16
            'allow_credentials_in_request_body' => $module->allowCredentialsInRequestBody,
107 16
            'allow_public_clients' => $module->allowPublicClients,
108 16
            'always_issue_new_refresh_token' => $module->alwaysIssueNewRefreshToken,
109 16
            'unset_refresh_token_after_use' => $module->unsetRefreshTokenAfterUse,
110 16
            'enforce_redirect' => $module->enforceRedirect,
111 16
            'auth_code_lifetime' => $module->authorizationCodeTTL,
112 16
        ];
113 16
        if (is_array($config) === false) {
114
            $config = [];
115
        }
116 16
        return array_merge($baseConfig, $config);
117
    }
118
}
119