Module: DeviseGauth::Views::Helpers

Defined in:
lib/devise_gauth/views/helpers.rb

Instance Method Summary collapse

Instance Method Details

#application_name_from(user) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/devise_gauth/views/helpers.rb', line 22

def application_name_from(user)
  user.class.ga_appname || if Rails.version < '6'
                             Rails.application.class.parent_name
                           else
                             Rails.application.class.module_parent_name
                           end
end

#build_qrcode_data_from(username, app, gauth_secret, qualifier = nil, issuer = nil) ⇒ Object



9
10
11
12
13
14
# File 'lib/devise_gauth/views/helpers.rb', line 9

def build_qrcode_data_from(username, app, gauth_secret, qualifier = nil, issuer = nil)
  data = "otpauth://totp/#{otpauth_user(username, app, qualifier)}"
  data += "?secret=#{gauth_secret}"
  data += "&issuer=#{issuer}" unless issuer.nil?
  data
end

#build_qrcode_from(data) ⇒ Object



16
17
18
19
20
# File 'lib/devise_gauth/views/helpers.rb', line 16

def build_qrcode_from(data)
  qrcode = RQRCode::QRCode.new(data, level: :m, mode: :byte_8bit)
  png = qrcode.as_png(fill: 'white', color: 'black', border_modules: 1, module_px_size: 4)
  "data:image/png;base64,#{Base64.encode64(png.to_s).strip}"
end

#google_authenticator_qrcode(user, qualifier = nil, issuer = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/devise_gauth/views/helpers.rb', line 30

def google_authenticator_qrcode(user, qualifier = nil, issuer = nil)
  data = build_qrcode_data_from(
    username_from_email(user.email),
    application_name_from(user),
    user.gauth_secret,
    qualifier,
    issuer
  )

  # data-uri is easier, so...
  image_tag(build_qrcode_from(data), alt: 'Google Authenticator QRCode')
end

#otpauth_user(username, app, qualifier = nil) ⇒ Object



43
44
45
# File 'lib/devise_gauth/views/helpers.rb', line 43

def otpauth_user(username, app, qualifier = nil)
  "#{username}@#{app}#{qualifier}"
end

#username_from_email(email) ⇒ Object



47
48
49
# File 'lib/devise_gauth/views/helpers.rb', line 47

def username_from_email(email)
  /^(.*)@/.match(email)[1]
end