2.0.0 • Published 5 years ago

flex-auth v2.0.0

Weekly downloads
111
License
ISC
Repository
github
Last release
5 years ago

twilio-auth

Service for authenticating requests to/from Twilio.

API

Static Properties

twilioAuthHeaderName: string

The name of the header where the Twilio signature should exist.

twilioUserHeaderName: string

The name of the header where Twilio user tokens should be stored.

constructor(accountSid: string, authToken: string)

Creates a new TwilioAuth instance that will validate requests using authToken as the secret.

PropertyTypeDescription
accountSidstringThe SID of the Twilio account.
authTokenstringThe secret that should be used to sign requests.

Methods

async authenticateAWSRequest(event: { body?: string, path: string, headers: { name: string : string }, queryStringParameters: { name: string : string } }): Promise

Attempts to authenticate the request received by an AWS Lambda behind an ALB.

Returns a Promise that resolves to true if the request can be authenticated or false if it cannot be authenticated.

PropertyTypeDescription
eventanyThe event object passed to the AWS Lambda

async authenticateExpressRequest(req: Request): Promise

Attempts to authenticate the request received by an Express server.

Returns a Promise that resolves to true if the request can be authenticated or false if it cannot be authenticated.

PropertyTypeDescription
reqexpress.RequestThe Express Request object

async authenticateTwilioRequest({ userToken }: { userToken?: string }): Promise

Authenticates a request to a Twilio Function from an external agent.

Note: Internal requests should be authenticated with the builtin Twilio header check. If HTTP headers are made available, this method may be updated to handle both user- and Twilio-authenticated requests.

Returns a Promise that resolves to true if the user is authenticated and false otherwise.

PropertyTypeDescription
event{ userToken: string }The Twilio event object passed to the Function

createToken(url: string, body?: { key: string : any }): Promise

Creates a token that can be included in the Twilio auth header.

Returns a Promise that resolves with the token that should be included in the header.

PropertyTypeDescription
urlstringThe full URL of the requested resource
body{ key: string : any }If the request has a body, the body of the request

isSignatureValid(signature: string, url: string, body?: { key: string : any }): Promise

Validates the signature in a Twilio-authenticated request.

Returns a Promise that resolves to true iff the the signature is valid.

PropertyTypeDescription
signaturestringThe signature provided in the Twilio auth header
urlstringThe full URL of the requested resource
body{ key: string : any }If the request has a body, the body of the request

async isUserTokenValid(token: string): Promise

Checks if the user token provided is valid.

Returns a Promise that resolves to true if provided token is valid; otherwise resolves to false

PropertyTypeDescription
tokenstringThe user auth token generated by Twilio

async fetchUserData(token: string): Promise<{workerSid: string, roles: string[], isValid: boolean, expiration: Date, identity: string}>

Fetches the user data attached to the provided token.

PropertyTypeDescription
tokenstringThe user auth token generated by Twilio

Express Middleware

Building off the helper method for authenticating an Express request, we also provide middleware that you can just hook into express app.

Usage

For twilio-auth-middleware to work, the request object must have the following fields added to it:

{
  "twilio": {
    "accountSid": "string",
    "authToken": "string"
  }
}

To add this data to the request object, you might define middleware that is executed before twilio-auth-middlware that loads the required data and adds it to the request object. Your express app might look something like:

import { twilioAuthMiddleware, TwilioRequest } from 'twilio-auth';

app.use(async (req: TwilioRequest, _res: Response, next: NextFunction) => {
  req.twilio = {
    accountSid: await loadAccountSid(),
    authToken:  await loadAuthToken(),
  };

  next();
});

app.use(twilioAuthMiddleware());