|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tequilarapido\Okta; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
|
6
|
|
|
use GuzzleHttp\ClientInterface; |
|
7
|
|
|
use Laravel\Socialite\Two\User; |
|
8
|
|
|
use Laravel\Socialite\Two\AbstractProvider; |
|
9
|
|
|
use Laravel\Socialite\Two\ProviderInterface; |
|
10
|
|
|
|
|
11
|
|
|
class OktaProvider extends AbstractProvider implements ProviderInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Scopes defintions. |
|
15
|
|
|
* |
|
16
|
|
|
* @see http://developer.okta.com/docs/api/resources/oidc.html#scopes |
|
17
|
|
|
*/ |
|
18
|
|
|
const SCOPE_OPENID = 'openid'; |
|
19
|
|
|
const SCOPE_PROFILE = 'profile'; |
|
20
|
|
|
const SCOPE_EMAIL = 'email'; |
|
21
|
|
|
const SCOPE_ADDRESS = 'address'; |
|
22
|
|
|
const SCOPE_PHONE = 'phone'; |
|
23
|
|
|
const SCOPE_OFFLINE_ACCESS = 'offline_access'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Okta organization url. |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $oktaUrl; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* {@inheritdoc} |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $scopes = [ |
|
36
|
|
|
'openid', |
|
37
|
|
|
'profile', |
|
38
|
|
|
'email', |
|
39
|
|
|
]; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritdoc} |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $scopeSeparator = ' '; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Set the okta base organization url. |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $oktaUrl |
|
50
|
|
|
*/ |
|
51
|
|
|
public function setOktaUrl($oktaUrl) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->oktaUrl = $oktaUrl; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritdoc} |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function getAuthUrl($state) |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->buildAuthUrlFromBase($this->oktaUrl.'/oauth2/v1/authorize', $state); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function getTokenUrl() |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->oktaUrl.'/oauth2/v1/token'; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function getTokenFields($code) |
|
76
|
|
|
{ |
|
77
|
|
|
return [ |
|
78
|
|
|
'grant_type' => 'authorization_code', |
|
79
|
|
|
'code' => $code, |
|
80
|
|
|
'redirect_uri' => $this->redirectUrl, |
|
81
|
|
|
]; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* {@inheritdoc} |
|
86
|
|
|
* |
|
87
|
|
|
* @see http://developer.okta.com/docs/api/resources/oidc.html#get-user-information |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function getUserByToken($token) |
|
90
|
|
|
{ |
|
91
|
|
|
$response = $this->getHttpClient()->get($this->oktaUrl.'/oauth2/v1/userinfo', [ |
|
92
|
|
|
'headers' => [ |
|
93
|
|
|
//'Accept' => 'application/json', |
|
94
|
|
|
'Authorization' => 'Bearer '.$token, |
|
95
|
|
|
], |
|
96
|
|
|
]); |
|
97
|
|
|
|
|
98
|
|
|
return json_decode($response->getBody(), true); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* {@inheritdoc} |
|
103
|
|
|
* |
|
104
|
|
|
* @see http://developer.okta.com/docs/api/resources/oidc.html#response-example-success |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function mapUserToObject(array $user) |
|
107
|
|
|
{ |
|
108
|
|
|
return (new User)->setRaw($user)->map([ |
|
109
|
|
|
'id' => Arr::get($user, 'sub'), |
|
110
|
|
|
'email' => Arr::get($user, 'email'), |
|
111
|
|
|
'email_verified' => Arr::get($user, 'email_verified', false), |
|
112
|
|
|
'nickname' => Arr::get($user, 'nickname'), |
|
113
|
|
|
'name' => Arr::get($user, 'name'), |
|
114
|
|
|
'first_name' => Arr::get($user, 'given_name'), |
|
115
|
|
|
'last_name' => Arr::get($user, 'family_name'), |
|
116
|
|
|
'profileUrl' => Arr::get($user, 'profile'), |
|
117
|
|
|
'address' => Arr::get($user, 'address'), |
|
118
|
|
|
'phone' => Arr::get($user, 'phone'), |
|
119
|
|
|
]); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* {@inheritdoc} |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getAccessTokenResponse($code) |
|
126
|
|
|
{ |
|
127
|
|
|
$postKey = (version_compare(ClientInterface::VERSION, '6') === 1) ? 'form_params' : 'body'; |
|
128
|
|
|
|
|
129
|
|
|
$options = [ |
|
130
|
|
|
'headers' => [ |
|
131
|
|
|
'Authorization' => 'Basic '.base64_encode($this->clientId.':'.$this->clientSecret), |
|
132
|
|
|
], |
|
133
|
|
|
$postKey => $this->getTokenFields($code), |
|
134
|
|
|
]; |
|
135
|
|
|
|
|
136
|
|
|
$response = $this->getHttpClient()->post($this->getTokenUrl(), $options); |
|
137
|
|
|
|
|
138
|
|
|
return json_decode($response->getBody(), true); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|