Sha256: 1bcddfd1a3ae305b6404ce7537a5071c4556240f809f1c5a1b469a5fa9db602b
Contents?: true
Size: 1.75 KB
Versions: 1
Compression:
Stored size: 1.75 KB
Contents
module TFA module TfaHelper def require_tfa( url:, method: :post, http_params: {}, phone_number:, message: "Your two factor authentication code is:\n\n{code}", length: 6 ) @tfa = Tfa.new @tfa.phone = phone_number @tfa.used = false http_params_ = "" http_params.each do |p| http_params_ << "#{p[0]}:#{p[1].gsub(':', "\0001").gsub(',', "\0002").gsub('#', "\0003")}," end @tfa.after = "#{method}###{url}###{http_params_}" @tfa.code = rand((10**(length-1))..("9"*length).to_i) @tfa.save Twilio.send_msg( message.gsub("{code}", @tfa.code.to_s), to: phone_number ) controller.redirect_to Engine.routes.url_helpers.tfa_verify_path(@tfa) end def tfa_valid(expected_phone:nil) params[:tfa_id] && Tfa.find_by(id: params[:tfa_id]).code.to_s == params[:code].to_s && !Tfa.find_by(id: params[:tfa_id]).used && (Tfa.find_by(id: params[:tfa_id]).phone == expected_phone || expected_phone == nil) end def if_tfa(expected_phone:nil, &block) if tfa_valid(expected_phone: expected_phone) @tfa = Tfa.find_by(id: params[:tfa_id]) @tfa.used = true @tfa.save block.call(@tfa) end end def no_tfa(expected_phone:nil, &block) if !tfa_valid(expected_phone: expected_phone) block.call end end def tfa_friendly_params(p) p.permit! p = p.to_h out = {} p.each do |param| if param[1].class != ActiveSupport::HashWithIndifferentAccess out[param[0]] = param[1] else param[1].each do |p1| out["#{param[0]}[#{p1[0]}]"] = p1[1] end end end return out end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
2fa-0.0.2 | app/helpers/tfa/tfa_helper.rb |