-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathMFA.php
More file actions
222 lines (188 loc) · 6.58 KB
/
MFA.php
File metadata and controls
222 lines (188 loc) · 6.58 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
<?php
namespace WorkOS;
/**
* Class MFA.
*
* This class facilitates the use of WorkOS MFA.
*/
class MFA
{
/**
* Enrolls a new Authentication Factor
*
* @param string $type - Type of factor to be enrolled (sms or totp)
* @param null|string $totpIssuer - Name of the Organization
* @param null|string $totpUser - Email of user
* @param null|string $phoneNumber - Phone number of user
*
* @throws Exception\WorkOSException
*/
public function enrollFactor(
$type,
$totpIssuer = null,
$totpUser = null,
$phoneNumber = null
) {
$enrollPath = "auth/factors/enroll";
if (!isset($type)) {
$msg = "Incomplete arguments: Need to specify a type of factor.";
throw new Exception\UnexpectedValueException($msg);
}
if ($type != "sms" && $type != "totp") {
$msg = "Type Parameter must either be 'sms' or 'totp'.";
throw new Exception\UnexpectedValueException($msg);
}
if ($type == "sms" && !isset($phoneNumber)) {
$msg = "Incomplete arguments: phoneNumber needs to be specified when using 'sms' as type.";
throw new Exception\UnexpectedValueException($msg);
}
if ($type == "totp" && (!isset($totpIssuer) || !isset($totpUser))) {
$msg = "Incomplete arguments: totpIssuer and totpUser need to be specified when using 'totp' as type.";
throw new Exception\UnexpectedValueException($msg);
}
$params = [
"type" => $type,
"totp_issuer" => $totpIssuer,
"totp_user" => $totpUser,
"phone_number" => $phoneNumber
];
$response = Client::request(
Client::METHOD_POST,
$enrollPath,
null,
$params,
true
);
if ($type == "totp") {
return Resource\AuthenticationFactorTotp::constructFromResponse($response);
} elseif ($type == "sms") {
return Resource\AuthenticationFactorSms::constructFromResponse($response);
}
}
/**
* Initiates the authentication process (a challenge) for an authentication factor
*
* @param string $authenticationFactorId - ID of the authentication factor
* @param string|null $smsTemplate - Optional parameter to customize the message for sms type factors. Must include "{{code}}" if used.
*
* @return Resource\AuthenticationChallengeTotp|Resource\AuthenticationChallengeSms
*/
public function challengeFactor(
$authenticationFactorId,
$smsTemplate = null
) {
if (!isset($authenticationFactorId)) {
$msg = "Incomplete arguments: 'authentication_factor_id' is a required parameter.";
throw new Exception\UnexpectedValueException($msg);
}
$challengePath = "auth/factors/{$authenticationFactorId}/challenge";
$params = [
"sms_template" => $smsTemplate
];
$response = Client::request(
Client::METHOD_POST,
$challengePath,
null,
$params,
true
);
if (isset($response['expires_at'])) {
return Resource\AuthenticationChallengeSms::constructFromResponse($response);
} else {
return Resource\AuthenticationChallengeTotp::constructFromResponse($response);
}
}
/**
* @deprecated 1.12.0 Use `verifyChallenge` instead. This method will be removed in a future major version.
* Verifies the one time password provided by the end-user.
*
* @param string $authenticationChallengeId - The ID of the authentication challenge that provided the user the verification code.
* @param string $code - The verification code sent to and provided by the end user.
*/
public function verifyFactor(
$authenticationChallengeId,
$code
) {
if (!isset($authenticationChallengeId) || !isset($code)) {
$msg = "Incomplete arguments: 'authenticationChallengeId' and 'code' are required parameters.";
throw new Exception\UnexpectedValueException($msg);
}
$msg = "'verifyFactor' is deprecated. Please use 'verifyChallenge' instead.";
trigger_error($msg, E_USER_DEPRECATED);
$response = (new \WorkOS\MFA())
->verifyChallenge(
$authenticationChallengeId,
$code
);
return $response;
}
/**
* Verifies the one time password provided by the end-user.
*
* @param string $authenticationChallengeId - The ID of the authentication challenge that provided the user the verification code.
* @param string $code - The verification code sent to and provided by the end user.
*
* @throws Exception\WorkOSException
*/
public function verifyChallenge(
$authenticationChallengeId,
$code
) {
if (!isset($authenticationChallengeId) || !isset($code)) {
$msg = "Incomplete arguments: 'authenticationChallengeId' and 'code' are required parameters.";
throw new Exception\UnexpectedValueException($msg);
}
$verifyPath = "auth/challenges/{$authenticationChallengeId}/verify";
$params = [
"code" => $code
];
$response = Client::request(
Client::METHOD_POST,
$verifyPath,
null,
$params,
true
);
return Resource\VerificationChallenge::constructFromResponse($response);
}
/**
* Returns a Factor.
*
* @param string $authenticationFactorId - WorkOS Factor ID
*
* @throws Exception\WorkOSException
*/
public function getFactor($authenticationFactorId)
{
$getFactorPath = "auth/factors/{$authenticationFactorId}";
$response = Client::request(
Client::METHOD_GET,
$getFactorPath,
null,
null,
true
);
return Resource\AuthenticationFactorTotp::constructFromResponse($response);
}
/**
* Deletes a Factor.
*
* @param string $authenticationFactorId - WorkOS Factor ID
*
* @return Resource\Response
*
* @throws Exception\WorkOSException
*/
public function deleteFactor($authenticationFactorId)
{
$deleteFactorPath = "auth/factors/{$authenticationFactorId}";
$response = Client::request(
Client::METHOD_DELETE,
$deleteFactorPath,
null,
null,
true
);
return $response;
}
}