-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathSSO.php
More file actions
257 lines (216 loc) · 7.34 KB
/
SSO.php
File metadata and controls
257 lines (216 loc) · 7.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php
namespace WorkOS;
use WorkOS\Exception;
/**
* Class SSO.
* This class facilitates the use of WorkOS SSO.
*/
class SSO
{
/**
* Generates an OAuth 2.0 authorization URL used to initiate the SSO flow with WorkOS.
*
* @param null|string $domain Domain of the user that will be going through SSO @deprecated 1.5.0 Use $connection or $organization instead.
* @param null|string $redirectUri URI to direct the user to upon successful completion of SSO
* @param null|array $state Associative array containing state that will be returned from WorkOS as a json encoded string
* @param null|string $provider Service provider that handles the identity of the user
* @param null|string $connection Unique identifier for a WorkOS Connection
* @param null|string $organization Unique identifier for a WorkOS Organization
* @param null|string $domainHint DDomain hint that will be passed as a parameter to the IdP login page
* @param null|string $loginHint Username/email hint that will be passed as a parameter to the to IdP login page
*
* @throws Exception\UnexpectedValueException
* @throws Exception\ConfigurationException
*
* @return string
*/
public function getAuthorizationUrl(
$domain,
$redirectUri,
$state,
$provider = null,
$connection = null,
$organization = null,
$domainHint = null,
$loginHint = null
) {
$authorizationPath = "sso/authorize";
if (!isset($domain) && !isset($provider) && !isset($connection) && !isset($organization)) {
$msg = "Either \$domain, \$provider, \$connection, or \$organization is required.";
throw new Exception\UnexpectedValueException($msg);
}
if (isset($domain)) {
$msg = "'domain' is being deprecated, please switch to using 'connection' or 'organization'.";
trigger_error($msg, E_USER_DEPRECATED);
}
$params = [
"client_id" => WorkOS::getClientId(),
"response_type" => "code"
];
if ($domain) {
$params["domain"] = $domain;
}
if ($redirectUri) {
$params["redirect_uri"] = $redirectUri;
}
if (null !== $state && !empty($state)) {
$params["state"] = \json_encode($state);
}
if ($provider) {
$params["provider"] = $provider;
}
if ($connection) {
$params["connection"] = $connection;
}
if ($organization) {
$params["organization"] = $organization;
}
if ($domainHint) {
$params["domain_hint"] = $domainHint;
}
if ($loginHint) {
$params["login_hint"] = $loginHint;
}
return Client::generateUrl($authorizationPath, $params);
}
/**
* Verify that SSO has been completed successfully and retrieve the identity of the user.
*
* @param string $code Code returned by WorkOS on completion of OAuth 2.0 flow
*
* @throws Exception\WorkOSException
*
* @return Resource\ProfileAndToken
*/
public function getProfileAndToken($code)
{
$profilePath = "sso/token";
$params = [
"client_id" => WorkOS::getClientId(),
"client_secret" => WorkOS::getApikey(),
"code" => $code,
"grant_type" => "authorization_code"
];
$response = Client::request(Client::METHOD_POST, $profilePath, null, $params);
return Resource\ProfileAndToken::constructFromResponse($response);
}
/**
* Verify that SSO has been completed successfully and retrieve the identity of the user.
*
* @param string $accessToken, the token used to authenticate the API call
*
* @throws Exception\GenericException
*
* @return Resource\Profile
*/
public function getProfile($accessToken)
{
$getProfilePath = "sso/profile";
$params = [
"access_token" => $accessToken
];
$method = Client::METHOD_GET;
$url = "https://api.workos.com/sso/profile";
$requestHeaders = ["Authorization: Bearer " . $accessToken];
list($result) = Client::requestClient()->request(
$method,
$url,
$requestHeaders,
null
);
$decodedResponse = json_decode($result, true);
$profile = Resource\Profile::constructFromResponse($decodedResponse);
return $profile->json();
}
/**
* Delete a Connection.
*
* @param string $connection Connection ID
*
* @throws Exception\WorkOSException
*
* @return Resource\Response
*/
public function deleteConnection($connection)
{
$connectionPath = "connections/{$connection}";
$response = Client::request(
Client::METHOD_DELETE,
$connectionPath,
null,
null,
true
);
return $response;
}
/**
* Get a Connection.
*
* @param string $connection Connection ID
*
* @throws Exception\WorkOSException
*
* @return Resource\Connection
*/
public function getConnection($connection)
{
$connectionPath = "connections/{$connection}";
$response = Client::request(
Client::METHOD_GET,
$connectionPath,
null,
null,
true
);
return Resource\Connection::constructFromResponse($response);
}
public const DEFAULT_PAGE_SIZE = 10;
/**
* List Connections.
*
* @param null|string $domain Domain of a Connection
* @param null|string $connectionType Authentication service provider descriptor
* @param null|string $organizationId Organization ID of the Connection(s)
* @param int $limit Maximum number of records to return
* @param null|string $before Connection ID to look before
* @param null|string $after Connection ID to look after
* @param Resource\Order $order The Order in which to paginate records
*
* @return array{?string, ?string, Resource\Connection[]} An array containing the Directory Connection ID to use as before and after cursor, and an array of Connection instances
*
* @throws Exception\WorkOSException
*/
public function listConnections(
$domain = null,
$connectionType = null,
$organizationId = null,
$limit = self::DEFAULT_PAGE_SIZE,
$before = null,
$after = null,
$order = null
) {
$connectionsPath = "connections";
$params = [
"limit" => $limit,
"before" => $before,
"after" => $after,
"domain" => $domain,
"connection_type" => $connectionType,
"organization_id" => $organizationId,
"order" => $order
];
$response = Client::request(
Client::METHOD_GET,
$connectionsPath,
null,
$params,
true
);
$connections = [];
list($before, $after) = Util\Request::parsePaginationArgs($response);
foreach ($response["data"] as $responseData) {
\array_push($connections, Resource\Connection::constructFromResponse($responseData));
}
return [$before, $after, $connections];
}
}