Skip to content

Commit 0f9926c

Browse files
committed
Update frontend files and readme
1 parent 3cfa1e6 commit 0f9926c

File tree

6 files changed

+116
-10
lines changed

6 files changed

+116
-10
lines changed

README.md

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
# Twilio
1+
# Twilio Verify
22

3-
## Sample Registration page
3+
Twilio is a verification service that allows you to send a code to a user's phone or use a time-based one time password to verify their identity. You can find more information on Twilio at [https://www.twilio.com/](https://www.twilio.com/). This is not a free service, so you will need to sign up for a Twilio account. Each successful verification will cost you $0.05, so this is something to consider before implementing.
4+
5+
## Phone Verification
6+
7+
### Sample Registration page
48

59
```html
610
<link
@@ -66,7 +70,7 @@
6670
</script>
6771
```
6872

69-
## Sample Activation Page (&twilioActivationResourceId)
73+
### Sample Activation Page (&twilioActivationResourceId)
7074

7175
```html
7276
[[!TwilioGetPhone]]
@@ -107,4 +111,68 @@ Phone: [[!+twilio.phone]]
107111
<input type="submit" name="verify" value="Validate my phone" />
108112
</div>
109113
</form>
110-
```
114+
```
115+
116+
## Time-based One Time Password
117+
118+
### Sample Challenge Page
119+
120+
Create a challenge page and set the system setting `twilio.totp_challenge_page` to the page ID.
121+
122+
```html
123+
[[!FormIt?
124+
&hooks=`TwilioTOTPChallenge,TwilioVerify`
125+
&twilioRedirect=`4` // ID of the page to redirect to after verification
126+
&twilioFactorType=`totp`
127+
&validate=`code:required`
128+
]]
129+
<form method="post">
130+
<label>
131+
Enter 2FA Code
132+
<input name="code" value="" />
133+
</label>
134+
<button type="submit">Submit</button>
135+
</form>
136+
```
137+
138+
### Sample Create/Reset Token Page
139+
140+
Create a page with the following content:
141+
142+
```html
143+
[[TwilioTOTPCreate?twilioRedirect=`4`]]
144+
```
145+
146+
### Sample Profile Page
147+
148+
```html
149+
[[!TwilioTOTPqr]]
150+
151+
[[!+twilio.qr:ne=``:then=`
152+
<img src="[[!+twilio.qr]]" />
153+
<p>Secret [[!+twilio.secret]]</p>
154+
[[!+twilio.status:is=`unverified`:then=`
155+
<p><a href="[[~5]]"><strong>Verify 2FA Code Before Next Login</strong></a></p> <!-- link to challenge page -->
156+
`:else=``]]
157+
<p><a href="[[~6]]">Refresh 2FA</a><br /> <!-- link to create / refresh page -->
158+
<a href="[[~6?status=`disable_totp`]]">Disable 2FA</a></p>
159+
`:else=`
160+
<a href="[[~6]]">Enable 2FA</a>
161+
`]]
162+
```
163+
164+
## System Settings
165+
166+
| key | description |
167+
| --- |------------------------------------------------------------------------------------------------------------|
168+
| twilio.account_sid | Twilio Account SID - Found under Account Info here https://console.twilio.com/ |
169+
| twilio.account_token | Twilio Auth Token - Found under Account Info here https://console.twilio.com/ |
170+
| twilio.service_id | Twilio Service ID - Found under Services Page here https://console.twilio.com/us1/develop/verify/services |
171+
| twilio.totp_enforce | Enforce 2FA for all users |
172+
| twilio.totp_email_on_login | Email a code to the user when they login |
173+
| twilio.totp_challenge_page | Page ID of the challenge page |
174+
175+
## Manager Page
176+
177+
Twilio 2FA Verification can be enabled in the manager login as well. You can view the status of Twilio 2FA for each user in the menu under
178+
"Extras -> User Authentication"

_build/config.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,16 @@
3636
"file": "TwilioValidatePhone.php"
3737
},
3838
{
39-
"name": "TwilioCreateTOTP",
40-
"file": "TwilioCreateTOTP.php"
39+
"name": "TwilioTOTPChallenge",
40+
"file": "TwilioTOTPChallenge.php"
41+
},
42+
{
43+
"name": "TwilioTOTPCreate",
44+
"file": "TwilioTOTPCreate.php"
45+
},
46+
{
47+
"name": "TwilioTOTPqr",
48+
"file": "TwilioTOTPqr.php"
4149
}
4250
],
4351
"plugins": [
@@ -77,7 +85,7 @@
7785
},
7886
{
7987
"key": "totp_challenge_page",
80-
"value": "0",
88+
"value": ""
8189
}
8290
]
8391
},

core/components/twilio/model/twilio/src/Snippet/Snippet.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ public function base64urlDecode($str) {
5454

5555
protected function redirect()
5656
{
57-
$redirect = (int)$this->getOption('twilioRedirect', 0);
58-
if (!empty($redirect)) {
57+
$redirect = (int)$this->getOption('twilioRedirect', null);
58+
if ($redirect > 0) {
5959
$this->modx->sendRedirect($this->modx->makeUrl($redirect));
6060
}
6161
}

core/components/twilio/model/twilio/src/Snippet/TotpChallenge.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ public function process()
3131
$profile = $user->getOne('Profile');
3232
$extended = $profile->get('extended');
3333
$userTwilio = $extended['twilio_totp'];
34+
if ($userTwilio['status'] !== 'verified') {
35+
return true;
36+
}
3437
try {
3538
$twilio = new Client($this->sid, $this->token);
3639
$verification_check = $twilio->verify->v2->services($this->service)

core/components/twilio/model/twilio/src/Snippet/TotpCreate.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,36 @@ public function process()
1111
$sid = $this->modx->getOption('twilio.account_sid');
1212
$token = $this->modx->getOption('twilio.account_token');
1313
$service = $this->getOption('twilioServiceId', $this->modx->getOption('twilio.service_id'));
14+
$status = ($this->modx->getOption('status', $_REQUEST, null) === 'disable_totp') ? 0 : 1;
1415

1516
if (empty($sid) || empty($token) || empty($service)) {
1617
$this->modx->sendErrorPage();
1718
return false;
1819
}
1920
$user = $this->modx->user;
2021
if (!$user || $user->id === 0) {
22+
$this->modx->sendErrorPage();
2123
return false;
2224
}
2325

26+
$setting = $this->modx->getObject('modUserSetting', array('user' => $user->id, 'key' => 'twilio.totp'));
27+
if (!$setting) {
28+
$setting = $this->modx->newObject('modUserSetting');
29+
$setting->set('user', $user->id);
30+
$setting->set('key', 'twilio.totp');
31+
$setting->set('xtype', 'combo-boolean');
32+
}
33+
$this->modx->log(\xPDO::LOG_LEVEL_ERROR, "[Twilio Create TOTP] setting = ".$status." REQUEST = ".$_REQUEST['status']);
34+
if ($status === 0) {
35+
$setting->set('value', $status);
36+
if (!$setting->save()) {
37+
$this->modx->log(\xPDO::LOG_LEVEL_ERROR, "[Twilio Create TOTP] Failed to save user setting");
38+
return false;
39+
}
40+
$this->redirect();
41+
return true;
42+
}
43+
2444
try {
2545
$twilio = new Client($sid, $token);
2646
$site = $this->modx->getOption('site_name');
@@ -47,6 +67,12 @@ public function process()
4767
$this->modx->log(\xPDO::LOG_LEVEL_ERROR, "[Twilio Create TOTP] Failed to save profile");
4868
return false;
4969
}
70+
$setting->set('value', $status);
71+
if (!$setting->save()) {
72+
$this->modx->log(\xPDO::LOG_LEVEL_ERROR, "[Twilio Create TOTP] Failed to save user setting");
73+
return false;
74+
}
75+
$_SESSION['twilio_totp_verified'] = true;
5076
$this->redirect();
5177
return true;
5278
}

core/components/twilio/model/twilio/src/Snippet/TotpQR.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ public function process()
1717
$userTwilio = $extended['twilio_totp'];
1818
$uri = $userTwilio['binding']['uri'];
1919
$qr = 'https://chart.googleapis.com/chart?chs=200x200&chld=M|0&cht=qr&chl=' . urlencode($uri);
20-
$this->modx->setPlaceholder('twilio.secret', $userTwilio['binding']['secret']);
2120
$this->modx->setPlaceholder('twilio.qr', $qr);
21+
$this->modx->setPlaceholder('twilio.secret', $userTwilio['binding']['secret']);
22+
$this->modx->setPlaceholder('twilio.status', $userTwilio['status']);
2223
}
2324
}
2425
}

0 commit comments

Comments
 (0)