1 | <?php |
||
2 | |||
3 | namespace Javis\OAuth2; |
||
4 | |||
5 | use League\OAuth2\Client\Provider\Exception\IdentityProviderException; |
||
6 | use League\OAuth2\Client\Provider\GenericProvider; |
||
7 | use League\OAuth2\Client\Token\AccessToken; |
||
8 | |||
9 | /** |
||
10 | * Provides authentication method to get an AccessToken from the configured |
||
11 | * provider. |
||
12 | */ |
||
13 | class PasswordGrantClient |
||
14 | { |
||
15 | protected $provider; |
||
16 | |||
17 | public function __construct(GenericProvider $provider) |
||
18 | { |
||
19 | $this->provider = $provider; |
||
20 | } |
||
21 | |||
22 | /** |
||
23 | * retrieves from endpoint, session or refreshes the Token |
||
24 | * for a given user. |
||
25 | * |
||
26 | * @param [type] $username [description] |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
27 | * @param [type] $password [description] |
||
28 | * |
||
29 | * @return AccessToken |
||
30 | */ |
||
31 | public function getAccessToken($username, $password) |
||
32 | { |
||
33 | // get token from session |
||
34 | if ($token = $this->retrievePersistedAccessToken($username)) { |
||
35 | try { |
||
36 | $token = $this->refreshTokenIfNecessary($username, $token); |
||
37 | } catch (IdentityProviderException $e) { |
||
38 | $this->removePersistedToken($username); |
||
39 | $token = false; |
||
40 | } |
||
41 | } |
||
42 | |||
43 | if (!$token) { |
||
44 | $token = $this->requestAccessToken($username, $password); |
||
45 | } |
||
46 | |||
47 | return $token; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * forgets stored token. |
||
52 | * |
||
53 | * @param string $username |
||
54 | */ |
||
55 | public function forgetToken($username) |
||
56 | { |
||
57 | $this->removePersistedToken($username); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * request access token for a specific user from endpoint. |
||
62 | * |
||
63 | * @param [type] $username [description] |
||
0 ignored issues
–
show
|
|||
64 | * @param [type] $password [description] |
||
65 | * |
||
66 | * @return [type] [description] |
||
0 ignored issues
–
show
|
|||
67 | */ |
||
68 | public function requestAccessToken($username, $password) |
||
69 | { |
||
70 | // Try to get an access token using the resource owner password credentials grant. |
||
71 | $token = $this->provider->getAccessToken('password', [ |
||
72 | 'username' => $username, |
||
73 | 'password' => $password, |
||
74 | ]); |
||
75 | |||
76 | // save to session |
||
77 | $this->persistAccessToken($username, $token); |
||
78 | |||
79 | return $token; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * attempt to refresh a given token. |
||
84 | * |
||
85 | * @param AccessToken $token [description] |
||
86 | * @param mixed $username |
||
87 | * |
||
88 | * @return AccessToken [description] |
||
89 | */ |
||
90 | public function refreshAccessToken($username, AccessToken $token) |
||
91 | { |
||
92 | $token = $this->provider->getAccessToken('refresh_token', [ |
||
93 | 'refresh_token' => $token->getRefreshToken(), |
||
94 | ]); |
||
95 | |||
96 | // save to session |
||
97 | $this->persistAccessToken($username, $token); |
||
98 | |||
99 | // return |
||
100 | return $token; |
||
101 | } |
||
102 | |||
103 | protected function getPersistingKey($username) |
||
104 | { |
||
105 | return 'token_'.md5($this->provider->getBaseAccessTokenUrl([]).$username); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * saves token in session. |
||
110 | * |
||
111 | * @param AccessToken $token |
||
112 | * @param mixed $username |
||
113 | */ |
||
114 | protected function persistAccessToken($username, AccessToken $token) |
||
115 | { |
||
116 | // basic session storage |
||
117 | $_SESSION[$this->getPersistingKey($username)] = json_encode($token); |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * [retrievePersistedAccessToken description]. |
||
122 | * |
||
123 | * @param mixed $username |
||
124 | * |
||
125 | * @return AccessToken |
||
126 | */ |
||
127 | protected function retrievePersistedAccessToken($username) |
||
128 | { |
||
129 | $key = $this->getPersistingKey($username); |
||
130 | if (!empty($_SESSION[$key])) { |
||
131 | return new AccessToken(json_decode($_SESSION[$key], true)); |
||
132 | } |
||
133 | |||
134 | return false; |
||
0 ignored issues
–
show
|
|||
135 | } |
||
136 | |||
137 | protected function removePersistedToken($username) |
||
138 | { |
||
139 | unset($_SESSION[$this->getPersistingKey($username)]); |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @param AccessToken $token |
||
144 | * @param mixed $username |
||
145 | * |
||
146 | * @throws IdentityProviderException |
||
147 | * |
||
148 | * @return AccessToken |
||
149 | */ |
||
150 | protected function refreshTokenIfNecessary($username, AccessToken $token) |
||
151 | { |
||
152 | if ($token->hasExpired() && $token->getRefreshToken()) { |
||
153 | $token = $this->refreshAccessToken($username, $token); |
||
154 | } |
||
155 | |||
156 | return $token; |
||
157 | } |
||
158 | } |
||
159 |