-
-
Notifications
You must be signed in to change notification settings - Fork 976
/
omniauth.rb
182 lines (152 loc) · 4.94 KB
/
omniauth.rb
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
105
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
139
140
141
142
143
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
# TODO: Fixed in https://github.com/rack/rack/pull/1610 for Rack 3
if defined?(RUBY_ENGINE) && RUBY_ENGINE == "jruby"
require 'delegate'
end
require 'rack'
require 'singleton'
require 'logger'
module OmniAuth
class Error < StandardError; end
module Strategies
autoload :Developer, 'omniauth/strategies/developer'
end
autoload :Builder, 'omniauth/builder'
autoload :Strategy, 'omniauth/strategy'
autoload :Test, 'omniauth/test'
autoload :Form, 'omniauth/form'
autoload :AuthHash, 'omniauth/auth_hash'
autoload :FailureEndpoint, 'omniauth/failure_endpoint'
autoload :AuthenticityTokenProtection, 'omniauth/authenticity_token_protection'
def self.strategies
@strategies ||= []
end
class Configuration
include Singleton
def self.default_logger
logger = Logger.new(STDOUT)
logger.progname = 'omniauth'
logger
end
def self.defaults # rubocop:disable MethodLength
@defaults ||= {
:camelizations => {},
:path_prefix => '/auth',
:on_failure => OmniAuth::FailureEndpoint,
:failure_raise_out_environments => ['development'],
:request_validation_phase => OmniAuth::AuthenticityTokenProtection,
:before_request_phase => nil,
:before_callback_phase => nil,
:before_options_phase => nil,
:form_css => Form::DEFAULT_CSS,
:test_mode => false,
:logger => default_logger,
:allowed_request_methods => %i[post],
:mock_auth => {:default => AuthHash.new('provider' => 'default', 'uid' => '1234', 'info' => {'name' => 'Example User'})},
:silence_get_warning => false
}
end
def initialize
self.class.defaults.each_pair { |k, v| send("#{k}=", v) }
end
def on_failure(&block)
if block_given?
@on_failure = block
else
@on_failure
end
end
def before_callback_phase(&block)
if block_given?
@before_callback_phase = block
else
@before_callback_phase
end
end
def before_options_phase(&block)
if block_given?
@before_options_phase = block
else
@before_options_phase
end
end
def request_validation_phase(&block)
if block_given?
@request_validation_phase = block
else
@request_validation_phase
end
end
def before_request_phase(&block)
if block_given?
@before_request_phase = block
else
@before_request_phase
end
end
def add_mock(provider, original = {})
# Create key-stringified new hash from given auth hash
mock = {}
original.each_pair do |key, val|
mock[key.to_s] = if val.is_a? Hash
Hash[val.each_pair { |k, v| [k.to_s, v] }]
else
val
end
end
# Merge with the default mock and ensure provider is correct.
mock = mock_auth[:default].dup.merge(mock)
mock['provider'] = provider.to_s
# Add it to the mocks.
mock_auth[provider.to_sym] = mock
end
# This is a convenience method to be used by strategy authors
# so that they can add special cases to the camelization utility
# method that allows OmniAuth::Builder to work.
#
# @param name [String] The underscored name, e.g. `oauth`
# @param camelized [String] The properly camelized name, e.g. 'OAuth'
def add_camelization(name, camelized)
camelizations[name.to_s] = camelized.to_s
end
attr_writer :on_failure, :before_callback_phase, :before_options_phase, :before_request_phase, :request_validation_phase
attr_accessor :failure_raise_out_environments, :path_prefix, :allowed_request_methods, :form_css,
:test_mode, :mock_auth, :full_host, :camelizations, :logger, :silence_get_warning
end
def self.config
Configuration.instance
end
def self.configure
yield config
end
def self.logger
config.logger
end
def self.mock_auth_for(provider)
config.mock_auth[provider.to_sym] || config.mock_auth[:default]
end
module Utils
module_function # rubocop:disable Layout/IndentationWidth
def form_css
"<style type='text/css'>#{OmniAuth.config.form_css}</style>"
end
def deep_merge(hash, other_hash)
target = hash.dup
other_hash.each_key do |key|
if other_hash[key].is_a?(::Hash) && hash[key].is_a?(::Hash)
target[key] = deep_merge(target[key], other_hash[key])
next
end
target[key] = other_hash[key]
end
target
end
def camelize(word, first_letter_in_uppercase = true)
return OmniAuth.config.camelizations[word.to_s] if OmniAuth.config.camelizations[word.to_s]
if first_letter_in_uppercase
word.to_s.gsub(%r{/(.?)}) { '::' + Regexp.last_match[1].upcase }.gsub(/(^|_)(.)/) { Regexp.last_match[2].upcase }
else
camelize(word).tap { |w| w[0] = w[0].downcase }
end
end
end
end