Skip to content

Commit

Permalink
Make uid label configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
iconoeugen committed Feb 7, 2019
1 parent 4c024ea commit 6d08609
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
*.rbc
.bundle
.config
.idea
.yardoc
InstalledFiles
_yardoc
Expand Down
12 changes: 10 additions & 2 deletions lib/omniauth/strategies/openid_connect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,16 @@ class OpenIDConnect
option :send_scope_to_token_endpoint, true
option :client_auth_method
option :post_logout_redirect_uri

uid { user_info.sub }
option :uid_field, 'sub'

uid do
begin
user_info.public_send(options.uid_field.to_s)
rescue NoMethodError => e
log :warn, "User sub:#{user_info.sub} missing info field: #{options.uid_field.to_s}"
user_info.sub
end
end

info do
{
Expand Down
18 changes: 18 additions & 0 deletions test/lib/omniauth/strategies/openid_connect_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ def test_request_phase_with_discovery

def test_uid
assert_equal user_info.sub, strategy.uid

strategy.options.uid_field = 'preferred_username'
assert_equal user_info.preferred_username, strategy.uid

strategy.options.uid_field = 'something'
assert_equal user_info.sub, strategy.uid
end

def test_callback_phase(session = {}, params = {})
Expand Down Expand Up @@ -213,6 +219,18 @@ def test_callback_phase_with_invalid_state
assert result.first == 401, "Expecting unauthorized"
end

def test_callback_phase_without_code
state = SecureRandom.hex(16)
nonce = SecureRandom.hex(16)
request.stubs(:params).returns('state' => state)
request.stubs(:path_info).returns('')

strategy.call!('rack.session' => { 'omniauth.state' => state, 'omniauth.nonce' => nonce })

strategy.expects(:fail!)
result = strategy.callback_phase
end

def test_callback_phase_with_timeout
code = SecureRandom.hex(16)
state = SecureRandom.hex(16)
Expand Down

0 comments on commit 6d08609

Please sign in to comment.