Skip to content

Commit 5840546

Browse files
Use runtime-helpers in apps (part 2/3) (#306)
* feat(lib): Add runtime-helpers library * feat(docs): Document runtime-helpers usage * feat(lib): Use runtime-helpers in 5 apps verified-broadcast, verify-dashboard, verify-push-backend, verify-retry, and video-token * fix(lib): runtime-helpers in package.json for 4 apps * fix(lib): verified-broadcast auth env variables * fix(docs): verified-broadcast README.md
1 parent 78019ad commit 5840546

25 files changed

+157
-222
lines changed

verified-broadcast/.env

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33
# required: true
44
TWILIO_PHONE_NUMBER=
55

6-
# description: Choose a passcode for your app. Users have to use this passcode to send SMS broadcasts or delete all subscriptions
6+
# description: Choose a username for your app. Users have to use this username and passcode to send SMS broadcasts or delete all subscriptions
7+
# format: text
8+
# required: true
9+
AUTH_USERNAME=
10+
11+
# description: Choose a passcode for your app. Users have to use this username and passcode to send SMS broadcasts or delete all subscriptions
712
# format: secret
813
# required: true
9-
PASSCODE=
14+
AUTH_PASSCODE=
1015

1116
# description: SID of a Twilio Messaging Service. Otherwise one will automatically be provisioned for you.
1217
# format: sid
@@ -25,4 +30,3 @@ BROADCAST_NOTIFY_SERVICE_SID=
2530
# link: https://www.twilio.com/console/verify/services
2631
# required: false
2732
VERIFY_SERVICE_SID=
28-

verified-broadcast/README.md

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,37 @@ This project requires some environment variables to be set. To keep your tokens
1010

1111
In your `.env` file, set the following values:
1212

13-
| Variable | Description | Required |
14-
| :--------------------------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------- |
15-
| TWILIO_PHONE_NUMBER | The Twilio phone number to send broadcast SMS from | Yes |
16-
| PASSCODE | Choose a passcode for your app. Users have to use this passcode to send SMS broadcasts or delete all subscriptions | Yes |
17-
| BROADCAST_NOTIFY_SERVICE_SID | SID of your Twilio Notify Service. Otherwise one will automatically be provisioned for you. | No |
18-
| MESSAGING_SERVICE_SID | SID of a Twilio Messaging Service. Otherwise one will automatically be provisioned for you. | No |
19-
| VERIFY_SERVICE_SID | SID of your Twilio Verify Service. Create one [here](https://www.twilio.com/console/verify/services). Otherwise one will automatically be provisioned for you. | No |
13+
| Variable | Description | Required |
14+
| :--------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------- |
15+
| TWILIO_PHONE_NUMBER | The Twilio phone number to send broadcast SMS from | Yes |
16+
| AUTH_USERNAME | Choose a passcode for your app. Users have to use this username and passcode to send SMS broadcasts or delete all subscriptions | Yes |
17+
| AUTH_PASSCODE | Choose a passcode for your app. Users have to use this username and passcode to send SMS broadcasts or delete all subscriptions | Yes |
18+
| BROADCAST_NOTIFY_SERVICE_SID | SID of your Twilio Notify Service. Otherwise one will automatically be provisioned for you. | No |
19+
| MESSAGING_SERVICE_SID | SID of a Twilio Messaging Service. Otherwise one will automatically be provisioned for you. | No |
20+
| VERIFY_SERVICE_SID | SID of your Twilio Verify Service. Create one [here](https://www.twilio.com/console/verify/services). Otherwise one will automatically be provisioned for you. | No |
2021

2122
### Function Parameters
2223

2324
`start-verify.js` expects the following parameters:
2425

25-
| Parameter | Description | Required |
26-
| :------------- | :------------------------------------------ | :------- |
27-
| `to` | Subscriber phone number. | Yes |
26+
| Parameter | Description | Required |
27+
| :-------- | :----------------------- | :------- |
28+
| `to` | Subscriber phone number. | Yes |
2829

29-
`broadcast.js` has to be authenticated using HTTP Basic Auth using `admin` as username an the configured `PASSCODE` as password. It expects the following parameters:
30+
`broadcast.js` has to be authenticated using HTTP Basic Auth using the configured `AUTH_USERNAME` as the username and the configured `AUTH_PASSCODE` as the password. It expects the following parameters:
3031

31-
| Parameter | Description | Required |
32-
| :------------- | :------------------------------------------ | :------- |
33-
| `body` | Message body to send to subscribers | Yes |
34-
| `tag` | Notify [tag](https://www.twilio.com/docs/notify/api/notification-resource#create-a-notification-resource) to filter broadcast. | No |
32+
| Parameter | Description | Required |
33+
| :-------- | :----------------------------------------------------------------------------------------------------------------------------- | :------- |
34+
| `body` | Message body to send to subscribers | Yes |
35+
| `tag` | Notify [tag](https://www.twilio.com/docs/notify/api/notification-resource#create-a-notification-resource) to filter broadcast. | No |
3536

3637
`subscribe.js` expects the following parameters:
3738

38-
| Parameter | Description | Required |
39-
| :------------- | :------------------------------------------ | :------- |
40-
| `to` | Subscriber phone number. Used for identity. | Yes |
41-
| `code` | One-time passcode sent via SMS. | Yes |
42-
| `tags` | Notify [tags](https://www.twilio.com/docs/notify/api/notification-resource#create-a-notification-resource) to filter broadcast. Defaults to 'all'. | No |
39+
| Parameter | Description | Required |
40+
| :-------- | :------------------------------------------------------------------------------------------------------------------------------------------------- | :------- |
41+
| `to` | Subscriber phone number. Used for identity. | Yes |
42+
| `code` | One-time passcode sent via SMS. | Yes |
43+
| `tags` | Notify [tags](https://www.twilio.com/docs/notify/api/notification-resource#create-a-notification-resource) to filter broadcast. Defaults to 'all'. | No |
4344

4445
## Setup
4546

@@ -84,7 +85,7 @@ You will need the generated SID for this service to configure your environment i
8485

8586
### Deploy the Function template
8687

87-
Add the Notify Service SID to the `.env` file as `BROADCAST_NOTIFY_SERVICE_SID` and the Verify Service SID as `VERIFY_SERVICE_SID`. Add any passcode for the admin that is permitted to send broadcast messages to the `.env` file as `PASSCODE`.
88+
Add the Notify Service SID to the `.env` file as `BROADCAST_NOTIFY_SERVICE_SID` and the Verify Service SID as `VERIFY_SERVICE_SID`. Add any username and passcode for the admin that is permitted to send broadcast messages to the `.env` file as `AUTH_USERNAME` and `AUTH_PASSCODE`, respectively.
8889

8990
Deploy your functions and assets with the following command. Note: you must run this command from inside your project folder. [More details in the docs.](https://www.twilio.com/docs/labs/serverless-toolkit)
9091

@@ -100,4 +101,4 @@ In a few moments your Function should be deployed! Grab its URL from the results
100101

101102
Once the Function is deployed, people can subscribe via the form on `/subscribe.html`. Users can respond `stop/start` to use Twilio's built-in stop handling to opt a user out from, or back in to, receiving messages (check out this article for [further details on Twilio's built-in features for handling opt-out/in keywords](https://support.twilio.com/hc/en-us/articles/223134027-Twilio-support-for-opt-out-keywords-SMS-STOP-filtering-)).
102103

103-
Administrators can use the form on `/broadcast.html` to send a message out to all subscribed users. This will send an OTP to the administrator for verification before broadcasting a message.
104+
Administrators can use the form on `/broadcast.html` to send a message out to all subscribed users. This will send an OTP to the administrator for verification before broadcasting a message.

verified-broadcast/assets/auth.private.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

verified-broadcast/functions/broadcast.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const { isAuthenticated } = require('@twilio-labs/runtime-helpers').auth;
2+
13
/**
24
* Broadcast a message
35
*
@@ -8,15 +10,23 @@
810
* }
911
*/
1012
exports.handler = async (context, event, callback) => {
11-
const { isAuthenticated } = require(Runtime.getAssets()['/auth.js'].path);
1213
const { setupResourcesIfRequired } = require(Runtime.getAssets()['/setup.js']
1314
.path);
1415

1516
const response = new Twilio.Response();
1617
response.appendHeader('Content-Type', 'application/json');
1718

18-
if (!isAuthenticated(context, event)) {
19-
response.setBody({ success: false, error: 'INVALID CREDENTIALS' });
19+
try {
20+
if (!isAuthenticated(context, event)) {
21+
response.setBody({ success: false, error: 'INVALID CREDENTIALS' });
22+
response.setStatusCode(401);
23+
return callback(null, response);
24+
}
25+
} catch (error) {
26+
response.setBody({
27+
success: false,
28+
error: `Authentication error: ${error.message}`,
29+
});
2030
response.setStatusCode(401);
2131
return callback(null, response);
2232
}

verified-broadcast/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"private": true,
44
"dependencies": {
55
"@twilio/runtime-handler": "1.2.0",
6+
"@twilio-labs/runtime-helpers": "^0.1.2",
67
"basic-auth": "^2.0.1",
78
"tsscmp": "^1.0.6",
89
"twilio": "^3.67.2"

verified-broadcast/tests/auth.test.js

Lines changed: 0 additions & 57 deletions
This file was deleted.

verified-broadcast/tests/broadcast.test.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const broadcastFunction = require('../functions/broadcast').handler;
22
const helpers = require('../../test/test-helper');
3+
const { isAuthenticated } = require('@twilio-labs/runtime-helpers').auth;
34

45
const mockVerificationCheck = {
56
verificationChecks: {
@@ -24,10 +25,11 @@ const mockClient = {
2425
},
2526
};
2627

27-
const mockIsAuthenticated = jest.fn(() => true);
28-
jest.mock('../assets/auth.private.js', () => {
28+
jest.mock('@twilio-labs/runtime-helpers', () => {
2929
return {
30-
isAuthenticated: mockIsAuthenticated,
30+
auth: {
31+
isAuthenticated: jest.fn(() => true),
32+
},
3133
};
3234
});
3335

@@ -42,7 +44,8 @@ const VERIFY_SERVICE_SID = 'default';
4244

4345
const testContext = {
4446
VERIFY_SERVICE_SID,
45-
PASSCODE: 'test-code',
47+
AUTH_USERNAME: 'admin',
48+
AUTH_PASSCODE: 'test-code',
4649
BROADCAST_NOTIFY_SERVICE_SID: 'placeholder',
4750
TWILIO_PHONE_NUMBER: '+12223334444',
4851
getTwilioClient: () => mockClient,
@@ -60,7 +63,7 @@ describe('verified-broadcast/broadcast', () => {
6063
});
6164

6265
test('does not allow broadcasting without valid code', (done) => {
63-
mockIsAuthenticated.mockReturnValueOnce(false);
66+
isAuthenticated.mockReturnValueOnce(false);
6467

6568
const callback = (err, result) => {
6669
expect(err).toEqual(null);

verified-broadcast/tests/start-verify.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ jest.mock('../assets/setup.private.js', () => {
2222

2323
const testContext = {
2424
VERIFY_SERVICE_SID: 'default',
25-
PASSCODE: 'test-code',
25+
AUTH_USERNAME: 'admin',
26+
AUTH_PASSCODE: 'test-code',
2627
BROADCAST_NOTIFY_SERVICE_SID: 'placeholder',
2728
TWILIO_PHONE_NUMBER: '+12223334444',
2829
getTwilioClient: () => mockClient,

verified-broadcast/tests/subscribe.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ const VERIFY_SERVICE_SID = 'default';
3838

3939
const testContext = {
4040
VERIFY_SERVICE_SID,
41-
PASSCODE: 'test-code',
41+
AUTH_USERNAME: 'admin',
42+
AUTH_PASSCODE: 'test-code',
4243
BROADCAST_NOTIFY_SERVICE_SID: 'placeholder',
4344
TWILIO_PHONE_NUMBER: '+12223334444',
4445
getTwilioClient: () => mockClient,

verify-dashboard/functions/check-verify.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@
1818
* }
1919
*/
2020

21+
const { createCORSResponse } = require('@twilio-labs/runtime-helpers').response;
22+
2123
// eslint-disable-next-line consistent-return
2224
exports.handler = function (context, event, callback) {
23-
const response = new Twilio.Response();
25+
// set to true to support CORS
26+
const supportCors = false;
27+
/* istanbul ignore next */
28+
const response = supportCors
29+
? createCORSResponse('*')
30+
: new Twilio.Response();
2431
response.appendHeader('Content-Type', 'application/json');
2532

26-
/*
27-
* uncomment to support CORS
28-
* response.appendHeader('Access-Control-Allow-Origin', '*');
29-
* response.appendHeader('Access-Control-Allow-Methods', 'POST, OPTIONS');
30-
* response.appendHeader('Access-Control-Allow-Headers', 'Content-Type');
31-
*/
32-
3333
if (
3434
typeof event.to === 'undefined' ||
3535
typeof event.verification_code === 'undefined'

0 commit comments

Comments
 (0)