Completed
Push — devel ( 7508db...540322 )
by Philippe
03:02
created

Oauth   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 10

Test Coverage

Coverage 88.24%

Importance

Changes 0
Metric Value
wmc 3
c 0
b 0
f 0
lcom 0
cbo 10
dl 0
loc 78
ccs 45
cts 51
cp 0.8824
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B register() 0 41 1
B prepareServerConfig() 0 25 2
1
<?php
2
/**
3
 * Oauth.php
4
 *
5
 * PHP version 5.6+
6
 *
7
 * @author Philippe Gaultier <[email protected]>
8
 * @copyright 2010-2016 Philippe Gaultier
9
 * @license http://www.sweelix.net/license license
10
 * @version XXX
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-2016 Philippe Gaultier
33
 * @license http://www.sweelix.net/license license
34
 * @version XXX
35
 * @link http://www.sweelix.net
36
 * @package sweelix\oauth2\server\services
37
 * @since XXX
38
 */
39
class Oauth implements ServiceBootstrapInterface
40
{
41
    /**
42
     * @inheritdoc
43
     */
44 45
    public static function register($app)
45
    {
46 45
        $module = Module::getInstance();
47
        Yii::$container->set('OAuth2\Server', function($container, $params, $config) use ($module) {
48 15
            $storage = Yii::createObject('sweelix\oauth2\server\storage\OauthStorage');
49 15
            $server = new Server($storage, self::prepareServerConfig($config, $module));
50 15
            return $server;
51 45
        });
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 3
            $storage = Yii::createObject('sweelix\oauth2\server\storage\OauthStorage');
55 3
            $grantType = new AuthorizationCode($storage);
56 3
            return $grantType;
57 45
        });
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 45
        });
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 45
        });
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 45
        });
77
78 45
        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 45
        });
83
84 45
    }
85
86
    /**
87
     * @param mixed $config
88
     * @return array Oauth server configuration
89
     * @since XXX
90
     */
91 15
    protected static function prepareServerConfig($config, $module)
92
    {
93
        $baseConfig = [
94 15
            'use_jwt_access_tokens' => $module->allowJwtAccesToken,
95 15
            'store_encrypted_token_string' => $module->storeEncryptedTokenString,
96 15
            'use_openid_connect' => $module->allowOpenIdConnect,
97 15
            'id_lifetime' => $module->idTTL,
98 15
            'access_lifetime' => $module->accessTokenTTL,
99 15
            'refresh_token_lifetime' => $module->refreshTokenTTL,
100 15
            'www_realm' => $module->realm,
101 15
            'token_param_name' => $module->tokenQueryName,
102 15
            'token_bearer_header_name' => $module->tokenBearerName,
103 15
            'enforce_state' => $module->enforceState,
104 15
            'require_exact_redirect_uri' => $module->allowOnlyRedirectUri,
105 15
            'allow_implicit' => $module->allowImplicit,
106 15
            'allow_credentials_in_request_body' => $module->allowCredentialsInRequestBody,
107 15
            'allow_public_clients' => $module->allowPublicClients,
108 15
            'always_issue_new_refresh_token' => $module->alwaysIssueNewRefreshToken,
109 15
            'unset_refresh_token_after_use' => $module->unsetRefreshTokenAfterUse,
110 15
        ];
111 15
        if (is_array($config) === false) {
112
            $config = [];
113
        }
114 15
        return array_merge($baseConfig, $config);
115
    }
116
}
117