1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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 56
|
# frozen_string_literal: true
module OmniAuth
module MultiPassword
module Base
def self.included(base)
base.class_eval do
option :title, 'Restricted Access'
option :fields, %i[username password]
uid { username }
end
end
def username_id
options[:fields][0] || 'username'
end
def password_id
options[:fields][1] || 'password'
end
def username
@username || request[username_id].to_s
end
def init_authenticator(request, env, username)
@request = request
@env = env
@username = username
end
def callback_phase
if authenticate(username, request[password_id])
super
else
fail!(:invalid_credentials)
end
end
def request_phase
OmniAuth::Form.build(title: options.title, url: callback_url) do |f|
f.text_field 'Username', username_id
f.password_field 'Password', password_id
end.to_response
end
def other_phase
# OmniAuth, by default, disables "GET" requests for security reasons.
# This effectively disables showing a password form on a GET request to
# the `request_phase`. Instead, we hook the GET requests here.
return request_phase if on_request_path?
end
end
end
end
|