Skip to content

Commit

Permalink
Fixes jayywolff#3 - Allow usage with non-US phone numbers
Browse files Browse the repository at this point in the history
- Remove e164 formatting method from TwilioVerifyService, User#mobile_phone must be stored in -e164 format.
- Note required format of mobile_phone column in README
  • Loading branch information
jaredmoody committed Apr 18, 2023
1 parent ad65718 commit 15db130
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 12 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Migrate Authy to Twilio Verify API (for SMS and TOTP 2FA)

### This gem is meant to be a drop-in replacement for devise-authy in a Rails app (minus the following features)
- Currently only support mobile phones with US country codes
- Removed Onetouch support
- Removed ability to request a phone call

Expand All @@ -26,7 +25,7 @@

```
- you can also delete the `users.authy_id` column if you choose
- Twilio Verify service sms will be sent to `users.mobile_phone`, so make sure you store the users 2fa phone number in this column, can make this field name dynamic in the future
- Twilio Verify service sms will be sent to `users.mobile_phone`, so make sure you store the users 2fa phone number in this column in [e164 format](https://www.twilio.com/docs/glossary/what-e164), can make this field name dynamic in the future
- Do a project code wide search & replace of these terms
- `devise-authy` -> `devise-twilio-verify`
- `authy_` -> `twilio_verify_`
Expand Down
12 changes: 2 additions & 10 deletions app/services/twilio_verify_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ class TwilioVerifyService
attr_reader :twilio_client, :twilio_account_sid, :twilio_auth_token, :twilio_verify_service_sid

def self.send_sms_token(phone_number)
new.twilio_verify_service.verifications.create(to: e164_format(phone_number), channel: 'sms')
new.twilio_verify_service.verifications.create(to: phone_number, channel: 'sms')
end

def self.verify_sms_token(phone_number, token)
new.twilio_verify_service.verification_checks.create(to: e164_format(phone_number), code: token)
new.twilio_verify_service.verification_checks.create(to: phone_number, code: token)
end

def self.verify_totp_token(user, token)
Expand Down Expand Up @@ -38,10 +38,6 @@ def self.register_totp_service(user, token)
.update(auth_payload: token)
end

def self.e164_format(phone_number)
"+1#{phone_number.gsub(/[^0-9a-z\\s]/i, '')}"
end

def initialize
@twilio_account_sid = Rails.application.credentials.twilio_account_sid || ENV['TWILIO_ACCOUNT_SID']
@twilio_auth_token = Rails.application.credentials.twilio_auth_token || ENV['TWILIO_AUTH_TOKEN']
Expand All @@ -59,8 +55,4 @@ def twilio_verify_service
def twilio_verify_service_v2
twilio_client.verify.v2.services(twilio_verify_service_sid)
end

def e164_format(phone_number)
self.class.e164_format(phone_number)
end
end

0 comments on commit 15db130

Please sign in to comment.