@@ -13,19 +13,19 @@ class ::OmniAuth::Strategies::Oauth2Basic < ::OmniAuth::Strategies::OAuth2
13
13
option :name , "oauth2_basic"
14
14
15
15
uid do
16
- if path = SiteSetting . oauth2_callback_user_id_path . split ( '.' )
16
+ if path = SiteSetting . oauth2_callback_user_id_path . split ( "." )
17
17
recurse ( access_token , [ *path ] ) if path . present?
18
18
end
19
19
end
20
20
21
21
info do
22
- if paths = SiteSetting . oauth2_callback_user_info_paths . split ( '|' )
22
+ if paths = SiteSetting . oauth2_callback_user_info_paths . split ( "|" )
23
23
result = Hash . new
24
24
paths . each do |p |
25
- segments = p . split ( ':' )
25
+ segments = p . split ( ":" )
26
26
if segments . length == 2
27
27
key = segments . first
28
- path = [ *segments . last . split ( '.' ) ]
28
+ path = [ *segments . last . split ( "." ) ]
29
29
result [ key ] = recurse ( access_token , path )
30
30
end
31
31
end
@@ -45,7 +45,7 @@ def recurse(obj, keys)
45
45
end
46
46
end
47
47
48
- require ' faraday/logging/formatter'
48
+ require " faraday/logging/formatter"
49
49
class OAuth2FaradayFormatter < Faraday ::Logging ::Formatter
50
50
def request ( env )
51
51
warn <<~LOG
@@ -76,7 +76,7 @@ def response(env)
76
76
77
77
class ::OAuth2BasicAuthenticator < Auth ::ManagedAuthenticator
78
78
def name
79
- ' oauth2_basic'
79
+ " oauth2_basic"
80
80
end
81
81
82
82
def can_revoke?
@@ -90,52 +90,66 @@ def can_connect_existing_user?
90
90
def register_middleware ( omniauth )
91
91
omniauth . provider :oauth2_basic ,
92
92
name : name ,
93
- setup : lambda { |env |
94
- opts = env [ 'omniauth.strategy' ] . options
95
- opts [ :client_id ] = SiteSetting . oauth2_client_id
96
- opts [ :client_secret ] = SiteSetting . oauth2_client_secret
97
- opts [ :provider_ignores_state ] = SiteSetting . oauth2_disable_csrf
98
- opts [ :client_options ] = {
99
- authorize_url : SiteSetting . oauth2_authorize_url ,
100
- token_url : SiteSetting . oauth2_token_url ,
101
- token_method : SiteSetting . oauth2_token_url_method . downcase . to_sym
102
- }
103
- opts [ :authorize_options ] = SiteSetting . oauth2_authorize_options . split ( "|" ) . map ( &:to_sym )
104
-
105
- if SiteSetting . oauth2_authorize_signup_url . present? &&
106
- ActionDispatch ::Request . new ( env ) . params [ "signup" ] . present?
107
- opts [ :client_options ] [ :authorize_url ] = SiteSetting . oauth2_authorize_signup_url
108
- end
109
-
110
- if SiteSetting . oauth2_send_auth_header? && SiteSetting . oauth2_send_auth_body?
111
- # For maximum compatibility we include both header and body auth by default
112
- # This is a little unusual, and utilising multiple authentication methods
113
- # is technically disallowed by the spec (RFC2749 Section 5.2)
114
- opts [ :client_options ] [ :auth_scheme ] = :request_body
115
- opts [ :token_params ] = { headers : { 'Authorization' => basic_auth_header } }
116
- elsif SiteSetting . oauth2_send_auth_header?
117
- opts [ :client_options ] [ :auth_scheme ] = :basic_auth
118
- else
119
- opts [ :client_options ] [ :auth_scheme ] = :request_body
120
- end
121
-
122
- unless SiteSetting . oauth2_scope . blank?
123
- opts [ :scope ] = SiteSetting . oauth2_scope
124
- end
125
-
126
- opts [ :client_options ] [ :connection_build ] = lambda { |builder |
127
- if SiteSetting . oauth2_debug_auth && defined? OAuth2FaradayFormatter
128
- builder . response :logger , Rails . logger , { bodies : true , formatter : OAuth2FaradayFormatter }
93
+ setup :
94
+ lambda { |env |
95
+ opts = env [ "omniauth.strategy" ] . options
96
+ opts [ :client_id ] = SiteSetting . oauth2_client_id
97
+ opts [ :client_secret ] = SiteSetting . oauth2_client_secret
98
+ opts [ :provider_ignores_state ] = SiteSetting . oauth2_disable_csrf
99
+ opts [ :client_options ] = {
100
+ authorize_url : SiteSetting . oauth2_authorize_url ,
101
+ token_url : SiteSetting . oauth2_token_url ,
102
+ token_method : SiteSetting . oauth2_token_url_method . downcase . to_sym ,
103
+ }
104
+ opts [ :authorize_options ] = SiteSetting
105
+ . oauth2_authorize_options
106
+ . split ( "|" )
107
+ . map ( &:to_sym )
108
+
109
+ if SiteSetting . oauth2_authorize_signup_url . present? &&
110
+ ActionDispatch ::Request . new ( env ) . params [ "signup" ] . present?
111
+ opts [ :client_options ] [
112
+ :authorize_url
113
+ ] = SiteSetting . oauth2_authorize_signup_url
114
+ end
115
+
116
+ if SiteSetting . oauth2_send_auth_header? &&
117
+ SiteSetting . oauth2_send_auth_body?
118
+ # For maximum compatibility we include both header and body auth by default
119
+ # This is a little unusual, and utilising multiple authentication methods
120
+ # is technically disallowed by the spec (RFC2749 Section 5.2)
121
+ opts [ :client_options ] [ :auth_scheme ] = :request_body
122
+ opts [ :token_params ] = {
123
+ headers : {
124
+ "Authorization" => basic_auth_header ,
125
+ } ,
126
+ }
127
+ elsif SiteSetting . oauth2_send_auth_header?
128
+ opts [ :client_options ] [ :auth_scheme ] = :basic_auth
129
+ else
130
+ opts [ :client_options ] [ :auth_scheme ] = :request_body
131
+ end
132
+
133
+ unless SiteSetting . oauth2_scope . blank?
134
+ opts [ :scope ] = SiteSetting . oauth2_scope
129
135
end
130
136
131
- builder . request :url_encoded # form-encode POST params
132
- builder . adapter FinalDestination ::FaradayAdapter # make requests with FinalDestination::HTTP
137
+ opts [ :client_options ] [ :connection_build ] = lambda do |builder |
138
+ if SiteSetting . oauth2_debug_auth && defined? ( OAuth2FaradayFormatter )
139
+ builder . response :logger ,
140
+ Rails . logger ,
141
+ { bodies : true , formatter : OAuth2FaradayFormatter }
142
+ end
143
+
144
+ builder . request :url_encoded # form-encode POST params
145
+ builder . adapter FinalDestination ::FaradayAdapter # make requests with FinalDestination::HTTP
146
+ end
133
147
}
134
- }
135
148
end
136
149
137
150
def basic_auth_header
138
- "Basic " + Base64 . strict_encode64 ( "#{ SiteSetting . oauth2_client_id } :#{ SiteSetting . oauth2_client_secret } " )
151
+ "Basic " +
152
+ Base64 . strict_encode64 ( "#{ SiteSetting . oauth2_client_id } :#{ SiteSetting . oauth2_client_secret } " )
139
153
end
140
154
141
155
def walk_path ( fragment , segments , seg_index = 0 )
@@ -182,19 +196,21 @@ def parse_segments(path)
182
196
quoted = false
183
197
escaped = false
184
198
185
- path . split ( "" ) . each do |char |
186
- next_char_escaped = false
187
- if !escaped && ( char == '"' )
188
- quoted = !quoted
189
- elsif !escaped && !quoted && ( char == '.' )
190
- segments . append +""
191
- elsif !escaped && ( char == '\\' )
192
- next_char_escaped = true
193
- else
194
- segments . last << char
199
+ path
200
+ . split ( "" )
201
+ . each do |char |
202
+ next_char_escaped = false
203
+ if !escaped && ( char == '"' )
204
+ quoted = !quoted
205
+ elsif !escaped && !quoted && ( char == "." )
206
+ segments . append +""
207
+ elsif !escaped && ( char == '\\' )
208
+ next_char_escaped = true
209
+ else
210
+ segments . last << char
211
+ end
212
+ escaped = next_char_escaped
195
213
end
196
- escaped = next_char_escaped
197
- end
198
214
199
215
segments
200
216
end
@@ -204,14 +220,14 @@ def log(info)
204
220
end
205
221
206
222
def fetch_user_details ( token , id )
207
- user_json_url = SiteSetting . oauth2_user_json_url . sub ( ' :token' , token . to_s ) . sub ( ' :id' , id . to_s )
223
+ user_json_url = SiteSetting . oauth2_user_json_url . sub ( " :token" , token . to_s ) . sub ( " :id" , id . to_s )
208
224
user_json_method = SiteSetting . oauth2_user_json_url_method . downcase . to_sym
209
225
210
226
log ( "user_json_url: #{ user_json_method } #{ user_json_url } " )
211
227
212
228
bearer_token = "Bearer #{ token } "
213
229
connection = Faraday . new { |f | f . adapter FinalDestination ::FaradayAdapter }
214
- headers = { ' Authorization' => bearer_token , ' Accept' => ' application/json' }
230
+ headers = { " Authorization" => bearer_token , " Accept" => " application/json" }
215
231
user_json_response = connection . run_request ( user_json_method , user_json_url , nil , headers )
216
232
217
233
log ( "user_json_response: #{ user_json_response . inspect } " )
@@ -243,7 +259,7 @@ def fetch_user_details(token, id)
243
259
244
260
def primary_email_verified? ( auth )
245
261
return true if SiteSetting . oauth2_email_verified
246
- verified = auth [ ' info' ] [ ' email_verified' ]
262
+ verified = auth [ " info" ] [ " email_verified" ]
247
263
verified = true if verified == "true"
248
264
verified = false if verified == "false"
249
265
verified
@@ -254,19 +270,25 @@ def always_update_user_email?
254
270
end
255
271
256
272
def after_authenticate ( auth , existing_account : nil )
257
- log ( "after_authenticate response: \n \n creds: #{ auth [ 'credentials' ] . to_hash } \n uid: #{ auth [ 'uid' ] } \n info: #{ auth [ 'info' ] . to_hash } \n extra: #{ auth [ 'extra' ] . to_hash } " )
273
+ log (
274
+ "after_authenticate response: \n \n creds: #{ auth [ "credentials" ] . to_hash } \n uid: #{ auth [ "uid" ] } \n info: #{ auth [ "info" ] . to_hash } \n extra: #{ auth [ "extra" ] . to_hash } " ,
275
+ )
258
276
259
277
if SiteSetting . oauth2_fetch_user_details?
260
- if fetched_user_details = fetch_user_details ( auth [ 'credentials' ] [ 'token' ] , auth [ 'uid' ] )
261
- auth [ 'uid' ] = fetched_user_details [ :user_id ] if fetched_user_details [ :user_id ]
262
- auth [ 'info' ] [ 'nickname' ] = fetched_user_details [ :username ] if fetched_user_details [ :username ]
263
- auth [ 'info' ] [ 'image' ] = fetched_user_details [ :avatar ] if fetched_user_details [ :avatar ]
264
- [ 'name' , 'email' , 'email_verified' ] . each do |property |
265
- auth [ 'info' ] [ property ] = fetched_user_details [ property . to_sym ] if fetched_user_details [ property . to_sym ]
278
+ if fetched_user_details = fetch_user_details ( auth [ "credentials" ] [ "token" ] , auth [ "uid" ] )
279
+ auth [ "uid" ] = fetched_user_details [ :user_id ] if fetched_user_details [ :user_id ]
280
+ auth [ "info" ] [ "nickname" ] = fetched_user_details [ :username ] if fetched_user_details [
281
+ :username
282
+ ]
283
+ auth [ "info" ] [ "image" ] = fetched_user_details [ :avatar ] if fetched_user_details [ :avatar ]
284
+ %w[ name email email_verified ] . each do |property |
285
+ auth [ "info" ] [ property ] = fetched_user_details [ property . to_sym ] if fetched_user_details [
286
+ property . to_sym
287
+ ]
266
288
end
267
289
268
290
DiscoursePluginRegistry . oauth2_basic_additional_json_paths . each do |detail |
269
- auth [ ' extra' ] [ detail ] = fetched_user_details [ "extra:#{ detail } " ]
291
+ auth [ " extra" ] [ detail ] = fetched_user_details [ "extra:#{ detail } " ]
270
292
end
271
293
else
272
294
result = Auth ::Result . new
@@ -284,7 +306,9 @@ def enabled?
284
306
end
285
307
end
286
308
287
- auth_provider title_setting : "oauth2_button_title" ,
288
- authenticator : OAuth2BasicAuthenticator . new
309
+ auth_provider title_setting : "oauth2_button_title" , authenticator : OAuth2BasicAuthenticator . new
289
310
290
- load File . expand_path ( "../lib/validators/oauth2_basic/oauth2_fetch_user_details_validator.rb" , __FILE__ )
311
+ load File . expand_path (
312
+ "../lib/validators/oauth2_basic/oauth2_fetch_user_details_validator.rb" ,
313
+ __FILE__ ,
314
+ )
0 commit comments