Module: Auth::TwoFactorOtp

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

Constant Summary collapse

TYPHOEUS_TIMEOUT =

so this is complicated.

30
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



11
12
13
# 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

.send_transactional_sms_new(args) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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

.set_webhook_identifier(notification_response, last_response) ⇒ Object

after this put it into a background job.



17
18
19
20
21
22
23
24
25
26
# 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

.ttransObject



28
29
30
31
32
33
34
35
36
# 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

Instance Method Details

#auth_genObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# 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

#clear_redis_user_otp_hashObject



190
191
192
193
# 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

#log_error_to_redis(error) ⇒ Object



185
186
187
188
# 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

#resourceObject

the currently being used resource.



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

mattr_accessor :resource

#send_otp_responseObject

so we get the transactional working. then we get some more tests going for this.s



198
199
200
201
202
203
204
205
# 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.}/AUTOGEN", timeout: typhoeus_timeout, headers: {'Content-Type'=> "application/x-www-form-urlencoded"})
	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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# 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

#sms_webhook(params) ⇒ Object

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



228
229
230
231
232
233
234
235
236
237
238
239
240
# 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

#transactional_sms_delivered?(params) ⇒ Boolean

Returns:

  • (Boolean)


243
244
245
# File 'lib/auth/two_factor_otp.rb', line 243

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

#transactional_sms_failed?(params) ⇒ Boolean

Returns:

  • (Boolean)


251
252
253
# 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

#transactional_sms_pending?(params) ⇒ Boolean

Returns:

  • (Boolean)


247
248
249
# File 'lib/auth/two_factor_otp.rb', line 247

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

#typhoeus_timeoutObject

return the timeout in seconds.



256
257
258
# File 'lib/auth/two_factor_otp.rb', line 256

def typhoeus_timeout
	20
end

#verify(otp) ⇒ Object

so gotta first sort out two factor api issues. and get the basic sms transactionals working. and then done.



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# 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. = 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

#verify_otp_response(otp, otp_session_id) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# 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