Completed
Push — develop ( fb7b18...537899 )
by
unknown
15:14
created

OAuth2Service   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 4
dl 0
loc 119
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A issueAccessToken() 0 4 1
A validateAccessToken() 0 4 1
A getResourceOwnerId() 0 7 1
A getResourceOwnerType() 0 4 1
A getClientId() 0 4 1
A checkAuthorizeParams() 0 10 2
A newAuthorizeRequest() 0 10 2
1
<?php namespace Nord\Lumen\OAuth2;
2
3
use League\OAuth2\Server\Grant\AuthCodeGrant;
4
use Nord\Lumen\OAuth2\Contracts\OAuth2Service as OAuth2ServiceContract;
5
use League\OAuth2\Server\AuthorizationServer;
6
use League\OAuth2\Server\ResourceServer;
7
8
class OAuth2Service implements OAuth2ServiceContract
9
{
10
11
    /**
12
     * @var AuthorizationServer
13
     */
14
    private $authorizationServer;
15
16
    /**
17
     * @var ResourceServer
18
     */
19
    private $resourceServer;
20
21
22
    /**
23
     * OAuth2Server constructor.
24
     *
25
     * @param AuthorizationServer $authorizationServer
26
     * @param ResourceServer      $resourceServer
27
     */
28
    public function __construct(AuthorizationServer $authorizationServer, ResourceServer $resourceServer)
29
    {
30
        $this->authorizationServer = $authorizationServer;
31
        $this->resourceServer      = $resourceServer;
32
    }
33
34
35
    /**
36
     * @return array
37
     * @throws \League\OAuth2\Server\Exception\InvalidRequestException
38
     * @throws \League\OAuth2\Server\Exception\UnsupportedGrantTypeException
39
     */
40
    public function issueAccessToken()
41
    {
42
        return $this->authorizationServer->issueAccessToken();
43
    }
44
45
46
    /**
47
     * @param bool        $headersOnly
48
     * @param null|string $accessToken
49
     *
50
     * @return bool
51
     * @throws \League\OAuth2\Server\Exception\AccessDeniedException
52
     */
53
    public function validateAccessToken($headersOnly = true, $accessToken = null)
54
    {
55
        return $this->resourceServer->isValidRequest($headersOnly, $accessToken);
0 ignored issues
show
Bug introduced by
It seems like $accessToken defined by parameter $accessToken on line 53 can also be of type string; however, League\OAuth2\Server\Res...erver::isValidRequest() does only seem to accept object<League\OAuth2\Ser...AccessTokenEntity>|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
56
    }
57
58
59
    /**
60
     * @return string
61
     */
62
    public function getResourceOwnerId()
63
    {
64
        // TODO: Calling validateAccessToken is kind of a hack, but it is necessary in order to load the access token.
65
        $this->validateAccessToken();
66
67
        return $this->resourceServer->getAccessToken()->getSession()->getOwnerId();
68
    }
69
70
71
    /**
72
     * @return string
73
     */
74
    public function getResourceOwnerType()
75
    {
76
        return $this->resourceServer->getAccessToken()->getSession()->getOwnerType();
77
    }
78
79
80
    /**
81
     * @return string
82
     */
83
    public function getClientId()
84
    {
85
        return $this->resourceServer->getAccessToken()->getSession()->getClient()->getId();
86
    }
87
88
    /**
89
     * Check authorize parameters
90
     *
91
     * @return array Authorize request parameters
92
     * @throws \League\OAuth2\Server\Exception\InvalidRequestException
93
     * @throws \League\OAuth2\Server\Exception\InvalidClientException
94
     * @throws \League\OAuth2\Server\Exception\UnsupportedResponseTypeException
95
     */
96
    public function checkAuthorizeParams()
97
    {
98
        if ($this->authorizationServer->hasGrantType('authorization_code')) {
99
            /** @var AuthCodeGrant $authGrant */
100
            $authGrant = $this->authorizationServer->getGrantType('authorization_code');
101
            return $authGrant->checkAuthorizeParams();
102
        }
103
104
        return [];
105
    }
106
107
    /**
108
     * Parse a new authorize request
109
     *
110
     * @param string $type The session owner's type
111
     * @param string $typeId The session owner's ID
112
     * @param array $authParams The authorize request $_GET parameters
113
     *
114
     * @return string
115
     */
116
    public function newAuthorizeRequest($type, $typeId, $authParams = [])
117
    {
118
        if ($this->authorizationServer->hasGrantType('authorization_code')) {
119
            /** @var AuthCodeGrant $authGrant */
120
            $authGrant = $this->authorizationServer->getGrantType('authorization_code');
121
            return $authGrant->newAuthorizeRequest($type, $typeId, $authParams);
122
        }
123
124
        return '';
125
    }
126
}
127