Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nikkothari22 committed Jun 3, 2021
0 parents commit d5b5620
Show file tree
Hide file tree
Showing 12 changed files with 6,682 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
66 changes: 66 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
firebase-debug.log*
firebase-debug.*.log*

# Firebase cache
.firebase/

# Firebase config

# Uncomment this if you'd like others to create their own Firebase project.
# For a team working on the same Firebase project(s), it is recommended to leave
# it commented so all members can deploy to the same project(s) in .firebaserc.
# .firebaserc

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
48 changes: 48 additions & 0 deletions POSTINSTALL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
## Before you use the extension

Before you can use this extension, follow these steps to make the functions deployed for this extension publicly accessible:
1. Go to the Cloud Functions dashboard for your project in the [Google Cloud console](https://console.cloud.google.com/functions/list).
2. Click the checkbox next to the function called "ext-${param:EXT_INSTANCE_ID}-twilioSendOTP".
3. If it's not already expanded, click **Show Info Panel** (in the top-right corner) to show the *Permissions* tab.
4. Click **Add Member**. Then, in the *New members* field, enter the user "allUsers".
5. Select the role **Cloud Functions Invoker** from the role dropdown list. You may need to type in this role's name to pull it into the list.
6. Click **Save**.
7. Repeat steps 2 to 6 for the function called "ext-${param:EXT_INSTANCE_ID}-twilioCheckOTP".

The above steps are required to prevent a CORS issue. Hence it is recommended, that you enforce authentication in the extension's configuration.


## Using the extension

You can test the functions right away!
(The code examples below use the Firebase JavaScript SDK. Please refer to the [documentation](https://firebase.google.com/docs/functions/callable) to call functions from your app.

#### Request the OTP

1. Call the `ext-${param:EXT_INSTANCE_ID}-twilioSendOTP` function from your app like this:

```js
functions.httpsCallable('ext-${param:EXT_INSTANCE_ID}-twilioSendOTP')({
number: "+919898989898", //E.164 Formatted number
channel: "sms" //Can be "sms" or "call"
}).then(result => console.log(result.data.status))
```

#### Validate the OTP

2. When you receive the code, you can then call the `ext-${param:EXT_INSTANCE_ID}-twilioCheckOTP` function like this:

```js
functions.httpsCallable('ext-${param:EXT_INSTANCE_ID}-twilioCheckOTP')({
number: "+919898989898", //E.164 Formatted number
code: "123456" //Code entered by the user
}).then(result => console.log(result.data.status))
```

The function will authenticate the code with Twilio Verify and then return an object with `status` - which can be either "approved" or "pending".

Usage of this extension also requires a Twilio account with the Twilio Verify service. You are responsible for any associated costs with your usage of Twilio and Firebase.

### Monitoring

As a best practice, you can [monitor the activity](https://firebase.google.com/docs/extensions/manage-installed-extensions#monitor) of your installed extension, including checks on its health, usage, and logs.
45 changes: 45 additions & 0 deletions PREINSTALL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
Use this extension to send one time verification codes to your users and verify them. The extension uses [Twilio's Verify service](https://www.twilio.com/verify) to send and check codes.

There are two functions in the extension - one to send the code and the other validate the code. Both of these are meant to be called from your app (they are onCall() functions).

The function `twilioSendOTP` needs to be called with two variables: `channel` ("sms" or "call") and `number` - a phone number in E.164 format. On calling, the function checks for authentication depending on the configuration of the extension. If the function is called without authentication from your Firebase app and the extension is configured to only allow authenticated calls, it will throw an error.

The function then returns an object with `status` - which can be either "approved" or "pending".

Here's a basic example how you can call this function from your app to send a one time verification code:

```js
functions.httpsCallable('ext-${EXT_INSTANCE_ID}-twilioSendOTP')({
number: "+919898989898", //E.164 Formatted number
channel: "sms" //Can be "sms" or "call"
}).then(result => console.log(result.data.status))
```

The function `twilioCheckOTP` needs to be called with two variables: `code` - the verification code entered by the user, and `number` - a phone number in E.164 format. On calling, the function checks for authentication depending on the configuration of the extension. If the function is called without authentication from your Firebase app and the extension is configured to only allow authenticated calls, it will throw an error.

The function will authenticate the code with Twilio Verify and then return an object with `status` - which can be either "approved" or "pending".

Here's a basic example how you can call this function from your app to send a one time verification code:

```js
functions.httpsCallable('ext-${EXT_INSTANCE_ID}-twilioCheckOTP')({
number: "+919898989898", //E.164 Formatted number
code: "123456" //Code entered by the user
}).then(result => console.log(result.data.status))
```
***EXT_INSTANCE_ID* is the instance ID of the extension and will be shown to you post installation.**

When you configure this extension, you'll need to provide your **Twilio Account SID, Auth Token and a Twilio Verify Service SID**. You can sign up and configure Twilio [here](https://www.twilio.com/verify).

#### Other Configuration Options
- Cloud Function Location (default : Iowa (us-central1))
- User authentication required? - Whether function should only run for authenticated users or anyone.

#### Billing
To install an extension, your project must be on the [Blaze (pay as you go) plan](https://firebase.google.com/pricing)

- You will be charged a small amount (typically around $0.01/month) for the Firebase resources required by this extension (even if it is not used).
- This extension uses other Firebase and Google Cloud Platform services, which have associated charges if you exceed the service’s free tier:
- Cloud Functions (Node.js 12+ runtime. [See FAQs](https://firebase.google.com/support/faq#expandable-24))

Usage of this extension also requires a Twilio account with the Twilio Verify service. You are responsible for any associated costs with your usage of Twilio.
77 changes: 77 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Custom Twilio Verify Extension

**Author**: Nikhil Kothari (**[https://github.com/nikkothari22](https://github.com/nikkothari22)**)

**Description**: Functions to send users one time passwords and verify them using Twilio Verify. (Not an official extension)

**This is not an official Twilio extension and the author is not associated with Twilio or Firebase in any manner whatsoever. All trademarks and copyrights belong to the respective companies.**

**Details**: Use this extension to send one time verification codes to your users and verify them. The extension uses [Twilio's Verify service](https://www.twilio.com/verify) to send and check codes.

There are two functions in the extension - one to send the code and the other validate the code. Both of these are meant to be called from your app (they are onCall() functions).

The function `twilioSendOTP` needs to be called with two variables: `channel` ("sms" or "call") and `number` - a phone number in E.164 format. On calling, the function checks for authentication depending on the configuration of the extension. If the function is called without authentication from your Firebase app and the extension is configured to only allow authenticated calls, it will throw an error.

The function then returns an object with `status` - which can be either "approved" or "pending".

Here's a basic example how you can call this function from your app to send a one time verification code:

```js
functions.httpsCallable('ext-${EXT_INSTANCE_ID}-twilioSendOTP')({
number: "+919898989898", //E.164 Formatted number
channel: "sms" //Can be "sms" or "call"
}).then(result => console.log(result.data.status))
```

The function `twilioCheckOTP` needs to be called with two variables: `code` - the verification code entered by the user, and `number` - a phone number in E.164 format. On calling, the function checks for authentication depending on the configuration of the extension. If the function is called without authentication from your Firebase app and the extension is configured to only allow authenticated calls, it will throw an error.

The function will authenticate the code with Twilio Verify and then return an object with `status` - which can be either "approved" or "pending".

Here's a basic example how you can call this function from your app to send a one time verification code:

```js
functions.httpsCallable('ext-${EXT_INSTANCE_ID}twilioCheckOTP')({
number: "+919898989898", //E.164 Formatted number
code: "123456" //Code entered by the user
}).then(result => console.log(result.data.status))
```

***EXT_INSTANCE_ID* is the instance ID of the extension and will be shown to you post installation.**

When you configure this extension, you'll need to provide your **Twilio Account SID, Auth Token and a Twilio Verify Service SID**. You can sign up and configure Twilio [here](https://www.twilio.com/verify).

#### Other Configuration Options
- Cloud Function Location (default : Iowa (us-central1))
- User authentication required? - Whether function should only run for authenticated users or anyone.

#### Billing
To install an extension, your project must be on the [Blaze (pay as you go) plan](https://firebase.google.com/pricing)

- You will be charged a small amount (typically around $0.01/month) for the Firebase resources required by this extension (even if it is not used).
- This extension uses other Firebase and Google Cloud Platform services, which have associated charges if you exceed the service’s free tier:
- Cloud Functions (Node.js 12+ runtime. [See FAQs](https://firebase.google.com/support/faq#expandable-24))

Usage of this extension also requires a Twilio account with the Twilio Verify service. You are responsible for any associated costs with your usage of Twilio.




**Configuration Parameters:**

* Cloud Functions location: Where do you want to deploy the functions created for this extension? You usually want a location close to your customers. For help selecting a location, refer to the [location selection guide](https://firebase.google.com/docs/functions/locations).

* Twilio Account SID: You can find your Twilio Account SID in the [Twilio Console](https://www.twilio.com/console)

* Twilio Auth Token: You can find your Twilio Auth Token in the [Twilio Console](https://www.twilio.com/console)

* Twilio Verify Service SID: Add your Twilio Verification Service SID here. If you do not have an existing Service, you need to create one in the [Twilio Verify Console](https://www.twilio.com/console/verify/services).

* Only allow requests from authenticated users?: The extension creates two cloud functions which are meant to be called from your apps. If set to yes, only authenticated requests will be served.



**Cloud Functions:**

* **twilioCheckOTP:** Function which checks the one time verification code/token using Twilio's Verify API and returns the status back to the client. To be called from the app (onCall()).

* **twilioSendOTP:** Function which sends a one time verification code to the phone number using Twilio's Verify API and returns the status back to the client. To be called from the app (onCall()).
89 changes: 89 additions & 0 deletions extension.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
name: custom-twilio-verify-extension
version: 0.0.1
specVersion: v1beta # Firebase Extensions specification version (do not edit)

displayName: Twilio Verify Extension (Unofficial)
description: Functions to send users one time passwords and verify them using Twilio Verify. (Not an official extension)

license: Apache-2.0 # The license you want for the extension

author:
authorName: Nikhil Kothari
email: [email protected]
url: https://github.com/nikkothari22 # Author URL

billingRequired: true

params:
- param: LOCATION
label: Cloud Functions location
description: >-
Where do you want to deploy the functions created for this extension?
You usually want a location close to your customers. For help selecting a
location, refer to the [location selection
guide](https://firebase.google.com/docs/functions/locations).
type: select
options:
- label: Iowa (us-central1)
value: us-central1
- label: South Carolina (us-east1)
value: us-east1
- label: Northern Virginia (us-east4)
value: us-east4
- label: Belgium (europe-west1)
value: europe-west1
- label: London (europe-west2)
value: europe-west2
- label: Frankfurt (europe-west3)
value: europe-west3
- label: Hong Kong (asia-east2)
value: asia-east2
- label: Tokyo (asia-northeast1)
value: asia-northeast1
default: us-central1
required: true
immutable: true
- param: TWILIO_ACCOUNT_SID
label: Twilio Account SID
description: You can find your Twilio Account SID in the [Twilio Console](https://www.twilio.com/console)
example: ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
type: string
required: true
- param: TWILIO_AUTH_TOKEN
label: Twilio Auth Token
description: You can find your Twilio Auth Token in the [Twilio Console](https://www.twilio.com/console)
type: string
required: true
- param: TWILIO_VERIFY_SID
label: Twilio Verify Service SID
description: Add your Twilio Verification Service SID here. If you do not have an existing Service, you need to create one in the [Twilio Verify Console](https://www.twilio.com/console/verify/services).
example: VAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
type: string
required: true
- param: USER_AUTHENTICATION_REQUIRED
label: Only allow requests from authenticated users?
description: The extension creates two cloud functions which are meant to be called from your apps. If set to yes, only authenticated requests will be served.
type: select
options:
- label: Yes (recommended)
value: true
- label: No (allow anyone to call this function)
value: false
default: true
required: true

resources:
- name: twilioCheckOTP
type: firebaseextensions.v1beta.function
description: Function which checks the one time verification code/token using Twilio's Verify API and returns the status back to the client. To be called from the app (onCall()).
properties:
location: ${LOCATION}
runtime: nodejs12
httpsTrigger: {}
- name: twilioSendOTP
type: firebaseextensions.v1beta.function
description: Function which sends a one time verification code to the phone number using Twilio's Verify API and returns the status back to the client. To be called from the app (onCall()).
properties:
location: ${LOCATION}
runtime: nodejs12
httpsTrigger: {}
15 changes: 15 additions & 0 deletions firebase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"functions": {
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint"
]
},
"emulators": {
"functions": {
"port": 5001
},
"ui": {
"enabled": true
}
}
}
13 changes: 13 additions & 0 deletions functions/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
root: true,
env: {
es6: true,
node: true,
},
extends: [
"eslint:recommended"
],
rules: {
quotes: ["error", "double"],
},
};
1 change: 1 addition & 0 deletions functions/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
Loading

0 comments on commit d5b5620

Please sign in to comment.