Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Twilio Verify API #8189

Merged
merged 24 commits into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env.alfajores
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ ATTESTATION_BOT_MAX_ATTESTATIONS=32
ATTESTATION_BOT_SKIP_ODIS_SALT=bot

ATTESTATION_SERVICE_DOCKER_IMAGE_REPOSITORY="us.gcr.io/celo-testnet/celo-monorepo"
ATTESTATION_SERVICE_DOCKER_IMAGE_TAG="attestation-service-v1.2.0"
ATTESTATION_SERVICE_DOCKER_IMAGE_TAG="attestation-service-v1.3.0"

TELEKOM_FROM="+14157389085"
SMS_PROVIDERS=twilio,nexmo,telekom
Expand Down
Binary file modified .env.mnemonic.alfajores.enc
Binary file not shown.
3 changes: 2 additions & 1 deletion packages/attestation-service/config/.env.development
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ATTESTATION_SIGNER_ADDRESS=x

# List all SMS providers here. They are tried from first to last, unless
# you specify SMS_PROVIDERS_RANDOMIZED=1, in which case they are tried in a random order.
SMS_PROVIDERS=messagebird,twilio,nexmo
SMS_PROVIDERS=twilio
# SMS_PROVIDERS_RANDOMIZED=0

# Optional: set a different list or order of providers for a specific country.
Expand Down Expand Up @@ -43,6 +43,7 @@ NEXMO_UNSUPPORTED_REGIONS=CU,SY,KP,IR,SD
# Twilio parameters (fill in values for 'x')
TWILIO_ACCOUNT_SID=x
TWILIO_MESSAGING_SERVICE_SID=x
TWILIO_VERIFY_SERVICE_SID=x
TWILIO_AUTH_TOKEN=x
TWILIO_UNSUPPORTED_REGIONS=CU,SY,KP,IR,SD

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict'
module.exports = {
up: async (queryInterface, Sequelize) => {
const transaction = await queryInterface.sequelize.transaction()

try {
await queryInterface.addColumn('Attestations', 'appSignature', {
type: Sequelize.STRING,
allowNull: true,
})
await queryInterface.addColumn('Attestations', 'language', {
type: Sequelize.STRING,
allowNull: true,
})

await transaction.commit()
} catch (error) {
await transaction.rollback()
throw error
}
},
down: async (queryInterface, Sequelize) => {
const transaction = await queryInterface.sequelize.transaction()
try {
await queryInterface.removeColumn('Attestations', 'appSignature')
await queryInterface.removeColumn('Attestations', 'language')
} catch (error) {
await transaction.rollback()
throw error
}
},
}
2 changes: 1 addition & 1 deletion packages/attestation-service/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@celo/attestation-service",
"version": "1.2.2",
"version": "1.3.0",
"description": "Issues attestation messages for Celo's identity protocol",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down
4 changes: 4 additions & 0 deletions packages/attestation-service/src/models/attestation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export interface AttestationModel extends Model {
recordError: (error: string) => void
failure: () => boolean
currentError: () => string | undefined
appSignature: string | undefined
language: string | undefined
}

export interface AttestationKey {
Expand Down Expand Up @@ -64,6 +66,8 @@ export default (sequelize: Sequelize) => {
errors: DataTypes.STRING,
createdAt: DataTypes.DATE,
completedAt: DataTypes.DATE,
appSignature: DataTypes.STRING,
language: DataTypes.STRING,
}) as AttestationStatic

model.prototype.key = function (): AttestationKey {
Expand Down
42 changes: 25 additions & 17 deletions packages/attestation-service/src/requestHandlers/attestation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,35 +152,41 @@ class AttestationRequestHandler {
// Main process for handling an attestation.
async doAttestation() {
Counters.attestationRequestsTotal.inc()
if (
!this.attestationRequest.securityCodePrefix ||
this.attestationRequest.securityCodePrefix.length !== 1
) {
throw new ErrorWithResponse('Invalid securityCodePrefix', 422)
}

let attestation = await this.findOrValidateRequest()

if (attestation && attestation.message) {
// Re-request existing attestation. In this case, security code prefix is ignored (the message sent is the same as before)
attestation = await rerequestAttestation(this.key, this.logger, this.sequelizeLogger)
attestation = await rerequestAttestation(
this.key,
this.attestationRequest.smsRetrieverAppSig,
this.attestationRequest.language,
this.attestationRequest.securityCodePrefix,
this.logger,
this.sequelizeLogger
)
} else {
// New attestation: create new attestation code, new delivery.
const attestationCode = await this.signAttestation()
await this.validateAttestationCode(attestationCode)
// This attestation code is stored in the attestation object
// and will be returned to the user with the get_attestation call
const attestationCodeDeeplink = `celo://wallet/v/${toBase64(attestationCode)}`

// Determine if we're sending a security code, or the full deep link.
let messageBase, securityCode
if (this.attestationRequest.securityCodePrefix) {
if (this.attestationRequest.securityCodePrefix.length !== 1) {
throw new ErrorWithResponse('Invalid securityCodePrefix', 422)
}

// Client is requesting a security code SMS. Generate a challenge and just store the deeplink.
securityCode = randomBytes(7)
.map((x) => x % 10)
.join('')
messageBase = `${getSecurityCodeText(this.attestationRequest.language)}: ${
this.attestationRequest.securityCodePrefix
}${securityCode}`
} else {
// Client is requesting direct SMS with the deeplink.
messageBase = attestationCodeDeeplink
}
// Generate a security code to be sent over SMS
securityCode = randomBytes(7)
.map((x) => x % 10)
.join('')
securityCode = `${this.attestationRequest.securityCodePrefix}${securityCode}`
messageBase = `${getSecurityCodeText(this.attestationRequest.language)}: ${securityCode}`

let textMessage

Expand All @@ -200,6 +206,8 @@ class AttestationRequestHandler {
textMessage,
securityCode,
attestationCodeDeeplink,
this.attestationRequest.smsRetrieverAppSig,
this.attestationRequest.language,
aslawson marked this conversation as resolved.
Show resolved Hide resolved
this.logger,
this.sequelizeLogger
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ class GetAttestationRequestHandler {
}

// Security code is supplied. Check it's correct.
if (attestation.securityCode === this.getRequest.securityCode) {
// Check with both methods (can remove second method after 1.3.0)
if (
attestation.securityCode &&
(attestation.securityCode.slice(1) === this.getRequest.securityCode ||
attestation.securityCode === this.getRequest.securityCode)
) {
callback(attestation, attestation.attestationCode)
await transaction.commit()
return
Expand Down
1 change: 1 addition & 0 deletions packages/attestation-service/src/requestHandlers/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export async function handleStatusRequest(
10
),
maxRerequestMins: parseInt(fetchEnvOrDefault('MAX_REREQUEST_MINS', '55'), 10),
twilioVerifySidProvided: !!fetchEnvOrDefault('TWILIO_VERIFY_SERVICE_SID', ''),
})
)
.status(200)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ export async function handleTestAttestationRequest(
key,
testRequest.phoneNumber,
testRequest.message,
null,
'12345678',
testRequest.message,
undefined,
undefined,
logger,
sequelizeLogger,
testRequest.provider
Expand Down
19 changes: 19 additions & 0 deletions packages/attestation-service/src/sms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ export async function startSendSms(
messageToSend: string,
securityCode: string | null = null,
attestationCode: string | null = null,
appSignature: string | undefined,
language: string | undefined,
logger: Logger,
sequelizeLogger: SequelizeLogger,
onlyUseProvider: string | null = null
Expand Down Expand Up @@ -243,6 +245,8 @@ export async function startSendSms(
ongoingDeliveryId: null,
securityCode,
securityCodeAttempt: 0,
appSignature,
language,
},
transaction
)
Expand Down Expand Up @@ -285,6 +289,9 @@ export async function startSendSms(
// immediately if there are sufficient attempts remaining.
export async function rerequestAttestation(
key: AttestationKey,
appSignature: string | undefined,
language: string | undefined,
securityCodePrefix: string,
logger: Logger,
sequelizeLogger: SequelizeLogger
): Promise<AttestationModel> {
Expand All @@ -301,6 +308,18 @@ export async function rerequestAttestation(
if (!attestation) {
throw new Error('Cannot retrieve attestation')
}
// For backward compatibility
// Can be removed after 1.3.0
if (!attestation.appSignature) {
attestation.appSignature = appSignature
}
if (!attestation.language) {
attestation.language = language
}
// Old security code approach did not store the prefix
if (attestation.securityCode?.length === 7) {
attestation.securityCode = `${securityCodePrefix}${attestation.securityCode}`
}

if (attestation.completedAt) {
const completedAgo = Date.now() - attestation.completedAt!.getTime()
Expand Down
120 changes: 112 additions & 8 deletions packages/attestation-service/src/sms/twilio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import bodyParser from 'body-parser'
import Logger from 'bunyan'
import express from 'express'
import twilio, { Twilio } from 'twilio'
import { fetchEnv } from '../env'
import { fetchEnv, fetchEnvOrDefault } from '../env'
import { AttestationModel, AttestationStatus } from '../models/attestation'
import { readUnsupportedRegionsFromEnv, SmsProvider, SmsProviderType } from './base'
import { receivedDeliveryReport } from './index'
Expand All @@ -12,25 +12,68 @@ export class TwilioSmsProvider extends SmsProvider {
return new TwilioSmsProvider(
fetchEnv('TWILIO_ACCOUNT_SID'),
fetchEnv('TWILIO_MESSAGING_SERVICE_SID'),
fetchEnvOrDefault('TWILIO_VERIFY_SERVICE_SID', ''),
fetchEnv('TWILIO_AUTH_TOKEN'),
readUnsupportedRegionsFromEnv('TWILIO_UNSUPPORTED_REGIONS', 'TWILIO_BLACKLIST')
)
}

client: Twilio
messagingServiceSid: string
verifyServiceSid: string
type = SmsProviderType.TWILIO
deliveryStatusURL: string | undefined
// https://www.twilio.com/docs/verify/api/verification#start-new-verification
twilioSupportedLocales = [
'af',
'ar',
'ca',
'cs',
'da',
'de',
'el',
'en',
'en-gb',
'es',
'fi',
'fr',
'he',
'hi',
'hr',
'hu',
'id',
'it',
'ja',
'ko',
'ms',
'nb',
'nl',
'pl',
'pt',
'pr-br',
'ro',
'ru',
'sv',
'th',
'tl',
'tr',
'vi',
'zh',
'zh-cn',
'zh-hk',
]

constructor(
twilioSid: string,
messagingServiceSid: string,
verifyServiceSid: string,
twilioAuthToken: string,
unsupportedRegionCodes: string[]
) {
super()
this.client = twilio(twilioSid, twilioAuthToken)
this.messagingServiceSid = messagingServiceSid
this.verifyServiceSid = verifyServiceSid
this.unsupportedRegionCodes = unsupportedRegionCodes
}

Expand Down Expand Up @@ -76,15 +119,76 @@ export class TwilioSmsProvider extends SmsProvider {
} catch (error) {
throw new Error(`Twilio Messaging Service could not be fetched: ${error}`)
}
if (this.verifyServiceSid) {
aslawson marked this conversation as resolved.
Show resolved Hide resolved
try {
await this.client.verify.services
.get(this.verifyServiceSid)
.fetch()
.then((service) => {
if (!service.customCodeEnabled) {
// Make sure that custom code is enabled
throw new Error(
'TWILIO_VERIFY_SERVICE_SID is specified, but customCode is not enabled. Please contact Twilio support to enable it.'
)
}
})
} catch (error) {
throw new Error(`Twilio Verify Service could not be fetched: ${error}`)
}
}
}

async sendSms(attestation: AttestationModel) {
const m = await this.client.messages.create({
body: attestation.message,
to: attestation.phoneNumber,
from: this.messagingServiceSid,
statusCallback: this.deliveryStatusURL,
})
return m.sid
// Prefer Verify API if Verify Service is present
if (this.verifyServiceSid) {
const requestParams: any = {
to: attestation.phoneNumber,
channel: 'sms',
customCode: attestation.securityCode,
}

// This param tells Twilio to add the <#> prefix and app hash postfix
if (attestation.appSignature) {
requestParams.appHash = attestation.appSignature
aslawson marked this conversation as resolved.
Show resolved Hide resolved
}
// Normalize to locales that Twilio supports
// If locale is not supported, Twilio API will throw an error
if (attestation.language) {
const locale = attestation.language.toLocaleLowerCase()
if (['es-419', 'es-us', 'es-la'].includes(locale)) {
attestation.language = 'es'
}
if (this.twilioSupportedLocales.includes(locale)) {
requestParams.locale = locale
codyborn marked this conversation as resolved.
Show resolved Hide resolved
aslawson marked this conversation as resolved.
Show resolved Hide resolved
}
}
try {
const m = await this.client.verify
.services(this.verifyServiceSid)
.verifications.create(requestParams)
return m.sid
} catch (e) {
// Verify landlines using voice
if (e.message.includes('SMS is not supported by landline phone number')) {
requestParams.appHash = undefined
requestParams.channel = 'call'
aslawson marked this conversation as resolved.
Show resolved Hide resolved
const m = await this.client.verify
.services(this.verifyServiceSid)
.verifications.create(requestParams)
return m.sid
} else {
throw e
}
}
} else {
// Send using the message service
const m = await this.client.messages.create({
body: attestation.message,
to: attestation.phoneNumber,
from: this.messagingServiceSid,
statusCallback: this.deliveryStatusURL,
})
return m.sid
}
}
}
Loading