File tree Expand file tree Collapse file tree 4 files changed +42
-1
lines changed Expand file tree Collapse file tree 4 files changed +42
-1
lines changed Original file line number Diff line number Diff line change @@ -18,7 +18,8 @@ class Configuration
18
18
:scope ,
19
19
:session_keys_to_persist ,
20
20
:success_redirect ,
21
- :user_model
21
+ :user_model ,
22
+ :setup
22
23
23
24
def initialize
24
25
@bypass_auth = false
Original file line number Diff line number Diff line change @@ -23,10 +23,12 @@ class Engine < ::Rails::Engine
23
23
initializer 'RpiAuth.add_middleware' do |app | # rubocop:disable Metrics/BlockLength
24
24
next unless RpiAuth . configuration
25
25
26
+ # rubocop:disable Metrics/BlockLength
26
27
app . middleware . use OmniAuth ::Builder do
27
28
provider (
28
29
:openid_connect ,
29
30
name : :rpi ,
31
+ setup : RpiAuth . configuration . setup ,
30
32
issuer : RpiAuth . configuration . issuer ,
31
33
scope : RpiAuth . configuration . scope ,
32
34
callback_path : CALLBACK_PATH ,
@@ -47,6 +49,7 @@ class Engine < ::Rails::Engine
47
49
allow_authorize_params : [ :login_options ] ,
48
50
origin_param : 'returnTo'
49
51
)
52
+ # rubocop:enable Metrics/BlockLength
50
53
51
54
OmniAuth . config . on_failure = RpiAuth ::AuthController . action ( :failure )
52
55
Original file line number Diff line number Diff line change 1
1
RpiAuth . configure do |config |
2
+ config . setup = lambda do |env |
3
+ request = Rack ::Request . new ( env )
4
+
5
+ if custom_scope = request . params [ 'add-custom-scope' ]
6
+ env [ 'omniauth.strategy' ] . options [ :scope ] += [ custom_scope ]
7
+ end
8
+ end
2
9
config . auth_url = 'http://localhost:9001'
3
10
config . auth_client_id = 'gem-dev'
4
11
config . auth_client_secret = 'secret'
Original file line number Diff line number Diff line change 312
312
end
313
313
end
314
314
end
315
+
316
+ describe 'and toggling the scope at runtime' do
317
+ let ( :custom_scope ) { 'custom-scope' }
318
+
319
+ before do
320
+ OmniAuth . config . test_mode = false
321
+ end
322
+
323
+ it 'does not append a custom scope' do
324
+ post '/auth/rpi'
325
+
326
+ scopes = extract_scopes_from_redirect_location ( response )
327
+
328
+ expect ( scopes ) . not_to include ( custom_scope )
329
+ end
330
+
331
+ it 'appends a custom scope' do
332
+ post "/auth/rpi?add-custom-scope=#{ custom_scope } "
333
+
334
+ scopes = extract_scopes_from_redirect_location ( response )
335
+
336
+ expect ( scopes ) . to include ( custom_scope )
337
+ end
338
+
339
+ def extract_scopes_from_redirect_location ( response )
340
+ location = response . headers [ 'location' ]
341
+ params = CGI . parse ( URI . parse ( location ) . query )
342
+ params [ 'scope' ] . first . split
343
+ end
344
+ end
315
345
end
316
346
end
You can’t perform that action at this time.
0 commit comments