Module: Auth::TwoFactorOtp

Included in:
OtpJob
Defined in:
lib/auth/two_factor_otp.rb

Constant Summary collapse

TWO_FACTOR_BASE_URL =
"http://2factor.in/API/V1/"
TWO_FACTOR_TRANSACTIONAL_SMS_URL =
"/ADDON_SERVICES/SEND/TSMS"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.check_errorsObject

returns the string value at the errors keys in the redis hash



10
11
12
# File 'lib/auth/two_factor_otp.rb', line 10

def self.check_errors
	$redis.hget(resource.id.to_s + "_two_factor_sms_otp","error")
end

.set_webhook_identifier(notification_response, last_response) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/auth/two_factor_otp.rb', line 14

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

Instance Method Details

#auth_genObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/auth/two_factor_otp.rb', line 57

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 "--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

#clear_redis_user_otp_hashObject



133
134
135
136
# File 'lib/auth/two_factor_otp.rb', line 133

def clear_redis_user_otp_hash
	#puts "--came to clear redis otp hash."
	$redis.del(resource.id.to_s + "_two_factor_sms_otp")
end

#log_error_to_redis(error) ⇒ Object



128
129
130
131
# File 'lib/auth/two_factor_otp.rb', line 128

def log_error_to_redis(error)
	#puts "redis error is:#{error}"
	$redis.hset(resource.id.to_s + "_two_factor_sms_otp","error",error)
end

#resourceObject

the currently being used resource.



5
# File 'lib/auth/two_factor_otp.rb', line 5

mattr_accessor :resource

#send_otp_responseObject



138
139
140
141
142
143
144
145
# File 'lib/auth/two_factor_otp.rb', line 138

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.}/AUTOGEN", timeout: typhoeus_timeout)
	end
end

#send_transactional_sms(args) ⇒ Object

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=#:two_factor_sms_api_key&to=#to_number&from=#template_sender_id&templatename=TemplateName&var1=VAR1_VALUE&var2=VAR2_VALUE” @return session_id



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/auth/two_factor_otp.rb', line 31

def send_transactional_sms(args)
	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

#sms_webhook(params) ⇒ Object

WEBHOOK #####################



168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/auth/two_factor_otp.rb', line 168

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

#transactional_sms_delivered?(params) ⇒ Boolean

Returns:

  • (Boolean)


183
184
185
# File 'lib/auth/two_factor_otp.rb', line 183

def transactional_sms_delivered?(params)
	params[:StatusGroupId] && params[:StatusGroupId].to_s == "3"
end

#transactional_sms_failed?(params) ⇒ Boolean

Returns:

  • (Boolean)


191
192
193
# File 'lib/auth/two_factor_otp.rb', line 191

def transactional_sms_failed?(params)
	!params[:StatusGroupId] || (params[:StatusGroupId] && params[:StatusGroupId].to_s =~ /2|4|5/)
end

#transactional_sms_pending?(params) ⇒ Boolean

Returns:

  • (Boolean)


187
188
189
# File 'lib/auth/two_factor_otp.rb', line 187

def transactional_sms_pending?(params)
	params[:StatusGroupId] && params[:StatusGroupId].to_s =~ /0|1/
end

#typhoeus_timeoutObject

return the timeout in seconds.



196
197
198
# File 'lib/auth/two_factor_otp.rb', line 196

def typhoeus_timeout
	10
end

#verify(otp) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/auth/two_factor_otp.rb', line 91

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
				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.

					resource.otp = otp

					resource. = 2
					
					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

#verify_otp_response(otp, otp_session_id) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/auth/two_factor_otp.rb', line 147

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)
	end
end