diff --git a/app/site.rb b/app/site.rb index 546dfd0e..412a9af6 100644 --- a/app/site.rb +++ b/app/site.rb @@ -304,6 +304,13 @@ get '/site/:username/confirm_phone' do erb :'site/confirm_phone' end +def restart_phone_verification + current_site.phone_verification_sent_at = nil + current_site.phone_verification_sid = nil + current_site.save_changes validate: false + redirect "/site/#{current_site.username}/confirm_phone" +end + post '/site/:username/confirm_phone' do require_login redirect '/' unless current_site.phone_verification_needed? @@ -335,19 +342,34 @@ post '/site/:username/confirm_phone' do flash[:success] = 'Validation message sent! Check your phone and enter the code below.' else - # Check code - vc = $twilio.verify - .v2 - .services($config['twilio_service_sid']) - .verification_checks - .create(verification_sid: current_site.phone_verification_sid, code: params[:code]) - # puts vc.status (pending if failed, approved if it passed) - if vc.status == 'approved' - current_site.phone_verified = true - current_site.save_changes validate: false - else - flash[:error] = 'Code was not correct, please re-enter.' + restart_phone_verification if current_site.phone_verification_sent_at < Time.now - Site::PHONE_VERIFICATION_EXPIRATION_TIME + minutes_remaining = ((current_site.phone_verification_sent_at - (Time.now - Site::PHONE_VERIFICATION_EXPIRATION_TIME))/60).round + + begin + # Check code + vc = $twilio.verify + .v2 + .services($config['twilio_service_sid']) + .verification_checks + .create(verification_sid: current_site.phone_verification_sid, code: params[:code]) + + # puts vc.status (pending if failed, approved if it passed) + if vc.status == 'approved' + current_site.phone_verified = true + current_site.save_changes validate: false + else + flash[:error] = "Code was not correct, please try again. If the phone number you entered was incorrect, you can re-enter the number after #{minutes_remaining} more minutes have passed." + end + + rescue Twilio::REST::RestError => e + if e.message =~ /60202/ + flash[:error] = "You have exhausted your check attempts. Please try again in #{minutes_remaining} minutes." + elsif e.message =~ /20404/ # Unable to create record + restart_phone_verification + else + raise e + end end end diff --git a/config.yml.ci b/config.yml.ci index 7425457d..7587484e 100644 --- a/config.yml.ci +++ b/config.yml.ci @@ -20,4 +20,7 @@ cache_control_ips: - 1.2.3.4 - 4.5.6.7 hcaptcha_site_key: "10000000-ffff-ffff-ffff-000000000001" -hcaptcha_secret_key: "0x0000000000000000000000000000000000000000" \ No newline at end of file +hcaptcha_secret_key: "0x0000000000000000000000000000000000000000" +twilio_account_sid: ACEDERPDERP +twilio_auth_token: derpderpderp +twilio_service_sid: VADERPDERPDERP \ No newline at end of file diff --git a/config.yml.template b/config.yml.template index be470036..1fc968fc 100644 --- a/config.yml.template +++ b/config.yml.template @@ -55,3 +55,6 @@ test: cache_control_ips: - 1.2.3.4 - 4.5.6.7 + twilio_account_sid: ACEDERPDERP + twilio_auth_token: derpderpderp + twilio_service_sid: VADERPDERPDERP \ No newline at end of file diff --git a/migrations/120_fix_phone_sent_at.rb b/migrations/120_fix_phone_sent_at.rb new file mode 100644 index 00000000..a5d3a937 --- /dev/null +++ b/migrations/120_fix_phone_sent_at.rb @@ -0,0 +1,11 @@ +Sequel.migration do + up { + DB.drop_column :sites, :phone_verification_sent_at + DB.add_column :sites, :phone_verification_sent_at, Time + } + + down { + DB.drop_column :sites, :phone_verification_sent_at + DB.add_column :sites, :phone_verification_sent_at, :time + } +end \ No newline at end of file diff --git a/models/site.rb b/models/site.rb index 30864524..b819fc3d 100644 --- a/models/site.rb +++ b/models/site.rb @@ -167,6 +167,9 @@ class Site < Sequel::Model BLACK_BOX_WAIT_TIME = 10.seconds MAX_DISPLAY_FOLLOWS = 56*3 + PHONE_VERIFICATION_EXPIRATION_TIME = 10.minutes + PHONE_VERIFICATION_LOCKOUT_ATTEMPTS = 3 + many_to_many :tags one_to_many :profile_comments diff --git a/views/site/confirm_phone.erb b/views/site/confirm_phone.erb index ba0287c0..c29f277b 100644 --- a/views/site/confirm_phone.erb +++ b/views/site/confirm_phone.erb @@ -2,7 +2,7 @@

Verify your phone number

- You're almost ready!
+ Last thing!
To prevent spam and keep the searchability of your site high, we have one last step:
please verify your mobile phone number.

@@ -27,13 +27,13 @@ <% if current_site.phone_verification_sid %>
- +
<% else %>
- +