Class: OmniAuth::Strategies::Identity

Inherits:
Object
  • Object
show all
Includes:
OmniAuth::Strategy
Defined in:
lib/omniauth/strategies/identity.rb

Overview

The identity strategy allows you to provide simple internal user authentication using the same process flow that you use for external OmniAuth providers.

Instance Method Summary collapse

Instance Method Details

#callback_phaseObject



36
37
38
39
40
# File 'lib/omniauth/strategies/identity.rb', line 36

def callback_phase
  return fail!(:invalid_credentials) unless identity

  super
end

#identityObject



107
108
109
110
111
112
113
114
115
# File 'lib/omniauth/strategies/identity.rb', line 107

def identity
  if options[:locate_conditions].is_a? Proc
    conditions = instance_exec(request, &options[:locate_conditions])
    conditions.to_hash
  else
    conditions = options[:locate_conditions].to_hash
  end
  @identity ||= model.authenticate(conditions, request['password'])
end

#modelObject



117
118
119
# File 'lib/omniauth/strategies/identity.rb', line 117

def model
  options[:model] || ::Identity
end

#on_registration_path?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/omniauth/strategies/identity.rb', line 103

def on_registration_path?
  on_path?(registration_path)
end

#other_phaseObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/omniauth/strategies/identity.rb', line 42

def other_phase
  if options[:enable_registration] && on_registration_path?
    if request.get?
      registration_form
    elsif request.post?
      registration_phase
    else
      call_app!
    end
  elsif options[:enable_login] && on_request_path?
    # OmniAuth, by default, disables "GET" requests for security reasons.
    # This effectively disables omniauth-identity tool's login form feature.
    # Because it is disabled by default, and because enabling it would desecuritize all the other
    #   OmniAuth strategies that may be implemented, we do not ask users to modify that setting.
    # Instead we hook in here in the "other_phase", with a config setting of our own: `enable_login`
    request_phase
  else
    call_app!
  end
end

#registration_formObject



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/omniauth/strategies/identity.rb', line 63

def registration_form
  if options[:on_registration]
    options[:on_registration].call(env)
  else
    OmniAuth::Form.build(title: 'Register Identity') do |f|
      options[:fields].each do |field|
        f.text_field field.to_s.capitalize, field.to_s
      end
      f.password_field 'Password', 'password'
      f.password_field 'Confirm Password', 'password_confirmation'
    end.to_response
  end
end

#registration_pathObject



99
100
101
# File 'lib/omniauth/strategies/identity.rb', line 99

def registration_path
  options[:registration_path] || "#{path_prefix}/#{name}/register"
end

#registration_phaseObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/omniauth/strategies/identity.rb', line 77

def registration_phase
  attributes = (options[:fields] + %i[password password_confirmation]).each_with_object({}) do |k, h|
    h[k] = request[k.to_s]
  end
  if model.respond_to?(:column_names) && model.column_names.include?('provider')
    attributes.reverse_merge!(provider: 'identity')
  end
  @identity = model.create(attributes)
  if @identity.persisted?
    env['PATH_INFO'] = callback_path
    callback_phase
  elsif options[:on_failed_registration]
    env['omniauth.identity'] = @identity
    options[:on_failed_registration].call(env)
  else
    registration_form
  end
end

#request_phaseObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/omniauth/strategies/identity.rb', line 19

def request_phase
  if options[:on_login]
    options[:on_login].call(env)
  else
    OmniAuth::Form.build(
      title: (options[:title] || 'Identity Verification'),
      url: callback_path
    ) do |f|
      f.text_field 'Login', 'auth_key'
      f.password_field 'Password', 'password'
      if options[:enable_registration]
        f.html "<p align='center'><a href='#{registration_path}'>Create an Identity</a></p>"
      end
    end.to_response
  end
end