module Auth::TwoFactorOtp
Constants
- TWO_FACTOR_BASE_URL
- TWO_FACTOR_TRANSACTIONAL_SMS_URL
- TYPHOEUS_TIMEOUT
so this is complicated.
Public Class Methods
returns the string value at the errors keys in the redis hash
# File lib/auth/two_factor_otp.rb, line 11 def self.check_errors $redis.hget(resource.id.to_s + "_two_factor_sms_otp","error") end
# File lib/auth/two_factor_otp.rb, line 38 def self.send_transactional_sms_new(args) if Auth.configuration.stub_otp_api_calls == true puts "stubbing transactional sms otp message, as stub_otp_api_calls is set to true" return {"stubbing_otp_transactions_sms_calls" => true}.to_json end puts "-- send transactional sms---" to_number = args[:to_number] template_name = args[:template_name] var_hash = args[:var_hash] template_sender_id = args[:template_sender_id] url = "http://2factor.in/API/V1/#{Auth.configuration.third_party_api_keys[:two_factor_sms_api_key]}/ADDON_SERVICES/SEND/TSMS" body = { To: to_number, From: template_sender_id, TemplateName: template_name, }.merge(var_hash) puts "url is: #{url}" puts "body is: #{body}" response = Typhoeus.post( url, body: body, timeout: TYPHOEUS_TIMEOUT ) #puts response.body response.body end
after this put it into a background job.
# File lib/auth/two_factor_otp.rb, line 17 def self.set_webhook_identifier(notification_response,last_response) last_response = JSON.parse(last_response) if last_response["Status"] && last_response["Status"] == "Success" notification_response.webhook_identifier = last_response["Details"] end end
# File lib/auth/two_factor_otp.rb, line 28 def self.ttrans args = { :to_number => "9561137096", :template_name => "Report Updated", :var_hash => {:VAR1 => "Bhargav", :VAR2 => "Raut", :VAR3 => "http://www.google.com", :VAR4 => "Plus Path Lab"}, :template_sender_id => "LABTST" } send_transactional_sms_new(args) end
Public Instance Methods
# File lib/auth/two_factor_otp.rb, line 106 def auth_gen #puts "--entered auth gen with params #{self.id} and phone number #{self.additional_login_param}" clear_redis_user_otp_hash #puts "--came after clearing the redis hash." if Auth.configuration.third_party_api_keys[:two_factor_sms_api_key].nil? #puts "--no api key found" log_error_to_redis("no api key found for two_factor_sms_otp") else #puts "--running request" response = send_otp_response if response.code == 200 #puts "-- send response code is 200" response_body = JSON.parse(response.body).symbolize_keys #puts "---send response body is:" #puts response_body.to_s if response_body[:Status] == "Success" puts "--send response status is success" puts "set the redis value to : #{response_body[:Details]}" $redis.hset(resource.id.to_s + "_two_factor_sms_otp","otp_session_id",response_body[:Details]) else puts "--otp response status is failure" log_error_to_redis(response_body[:Details]) end else #puts "--response code is non 200" log_error_to_redis("HTTP Error code:"+ response.code.to_s) end end end
# File lib/auth/two_factor_otp.rb, line 190 def clear_redis_user_otp_hash #puts "--came to clear redis otp hash." $redis.del(resource.id.to_s + "_two_factor_sms_otp") end
# File lib/auth/two_factor_otp.rb, line 185 def log_error_to_redis(error) #puts "redis error is:#{error}" $redis.hset(resource.id.to_s + "_two_factor_sms_otp","error",error) end
so we get the transactional working. then we get some more tests going for this.s
# File lib/auth/two_factor_otp.rb, line 198 def send_otp_response if Auth.configuration.stub_otp_api_calls == true OpenStruct.new({code: 200, body: JSON.generate({:Status => "Success", :Details => Faker::Name.name})}) else Typhoeus.get("https://2factor.in/API/V1/#{Auth.configuration.third_party_api_keys[:two_factor_sms_api_key]}/SMS/+91#{resource.additional_login_param}/AUTOGEN", timeout: typhoeus_timeout, headers: {'Content-Type'=> "application/x-www-form-urlencoded"}) end end
to_number : string, indian telephone number, without the preceeding 91 template : the two_factor_otp template example request should look like this “2factor.in/API/R1/?module=TRANS_SMS&apikey=#{Auth.configuration.third_party_api_keys[:two_factor_sms_api_key]}&to=#{to_number}&from=#{template_sender_id}&templatename=TemplateName&var1=VAR1_VALUE&var2=VAR2_VALUE” @return session_id
# File lib/auth/two_factor_otp.rb, line 74 def send_transactional_sms(args) if Auth.configuration.stub_otp_api_calls == true puts "stubbing transactional sms otp message, as stub_otp_api_calls is set to true" return {"stubbing_otp_transactions_sms_calls" => true}.to_json end puts "-- send transactional sms---" to_number = args[:to_number] template_name = args[:template_name] var_hash = args[:var_hash] template_sender_id = args[:template_sender_id] url = "https://2factor.in/API/R1/?module=TRANS_SMS" params = { apikey: Auth.configuration.third_party_api_keys[:two_factor_sms_api_key], to: to_number, from: template_sender_id, templatename: template_name, }.merge(var_hash) request = Typhoeus::Request.new( url, params: params, timeout: typhoeus_timeout ) response = request.run response.body end
WEBHOOK #####################
# File lib/auth/two_factor_otp.rb, line 228 def sms_webhook(params) Auth.configuration.notification_response_class.constantize.find_and_update_notification_response(params[:SessionId],JSON.generate(params)) do |notification_response| puts "found the sms notification response and triggered it." if transactional_sms_failed?(params) notification = notification_response.get_parent_notification resource = notification_response.get_resource notification.send_sms_background(resource) end end end
# File lib/auth/two_factor_otp.rb, line 243 def transactional_sms_delivered?(params) params[:StatusGroupId] && params[:StatusGroupId].to_s == "3" end
# File lib/auth/two_factor_otp.rb, line 251 def transactional_sms_failed?(params) !params[:StatusGroupId] || (params[:StatusGroupId] && params[:StatusGroupId].to_s =~ /2|4|5/) end
# File lib/auth/two_factor_otp.rb, line 247 def transactional_sms_pending?(params) params[:StatusGroupId] && params[:StatusGroupId].to_s =~ /0|1/ end
return the timeout in seconds.
# File lib/auth/two_factor_otp.rb, line 256 def typhoeus_timeout 20 end
so gotta first sort out two factor api issues. and get the basic sms transactionals working. and then done.
# File lib/auth/two_factor_otp.rb, line 144 def verify(otp) if Auth.configuration.third_party_api_keys[:two_factor_sms_api_key].nil? log_error_to_redis("no api key found for two_factor_sms_otp") else otp_session_id = $redis.hget(resource.id.to_s + "_two_factor_sms_otp","otp_session_id") if otp_session_id.nil? log_error_to_redis("No otp session id found, please click \"resend otp message\" and try again") else response = verify_otp_response(otp,otp_session_id) if response.code == 200 response_body = JSON.parse(response.body).symbolize_keys #puts "response body is:" #puts response_body.to_s if response_body[:Status] == "Success" ##suppose here we say additional parameter confirmed ##then when we have to sign in user, we just need to bypass the active_for_authentication, ##and dont touch anything else. #puts "successfully matched otp --- " resource.otp = otp resource.additional_login_param_status = 2 #puts "set the status as: #{resource.additional_login_param_status}" #puts "going for save." resource.save clear_redis_user_otp_hash else log_error_to_redis(response_body[:Details]) end else log_error_to_redis("HTTP Error code:"+ response.code.to_s) end end end end
# File lib/auth/two_factor_otp.rb, line 207 def verify_otp_response(otp,otp_session_id) if Auth.configuration.stub_otp_api_calls == true if Auth.configuration.simulate_invalid_otp == true OpenStruct.new({code: 200, body: JSON.generate({:Status => "failed", :Details => "your otp is invalid"})}) else ##check the otp, and derive the response based on that. ##this comparison of comparing the session id, with the opt is just for test purpose. ##in reality they have nothing to do with each other. #puts "otp session id is:#{otp_session_id}" #puts "otp is: #{otp}" OpenStruct.new({code: 200, body: JSON.generate({:Status => ((otp_session_id == otp) ? "Success" : "failed"), :Details => "location: two_factor_otp.rb#verify_otp_response, compares otp_session id to provided otp to decide failure or success"})}) end else Typhoeus.get("https://2factor.in/API/V1/#{Auth.configuration.third_party_api_keys[:two_factor_sms_api_key]}/SMS/VERIFY/#{otp_session_id}/#{otp}", timeout: typhoeus_timeout, headers: {'Content-Type'=> "application/x-www-form-urlencoded"}) end end