Skip to content

Commit

Permalink
Move verification of IdToken to before accessing tokens (omniauth#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysiek1507 authored and m0n9oose committed Aug 2, 2019
1 parent bd8b57f commit c6c64a4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
13 changes: 6 additions & 7 deletions lib/omniauth/strategies/openid_connect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ def callback_phase
return fail!(:missing_code, OmniAuth::OpenIDConnect::MissingCodeError.new(params['error'])) unless params['code']

options.issuer = issuer if options.issuer.nil? || options.issuer.empty?

decode_id_token(params['id_token'])
.verify! issuer: options.issuer,
client_id: client_options.identifier,
nonce: stored_nonce

discover!
client.redirect_uri = redirect_uri
client.authorization_code = authorization_code
Expand Down Expand Up @@ -197,13 +203,6 @@ def access_token
scope: (options.scope if options.send_scope_to_token_endpoint),
client_auth_method: options.client_auth_method
)
id_token = decode_id_token(@access_token.id_token)
id_token.verify!(
issuer: options.issuer,
client_id: client_options.identifier,
nonce: stored_nonce
)
@access_token
end

def decode_id_token(id_token)
Expand Down
21 changes: 21 additions & 0 deletions test/lib/omniauth/strategies/openid_connect_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def test_callback_phase(session = {}, params = {})
id_token = stub('OpenIDConnect::ResponseObject::IdToken')
id_token.stubs(:verify!).with(issuer: strategy.options.issuer, client_id: @identifier, nonce: nonce).returns(true)
::OpenIDConnect::ResponseObject::IdToken.stubs(:decode).returns(id_token)
id_token.expects(:verify!)

strategy.unstub(:user_info)
access_token = stub('OpenIDConnect::AccessToken')
Expand Down Expand Up @@ -241,6 +242,11 @@ def test_callback_phase_with_timeout
strategy.stubs(:access_token).raises(::Timeout::Error.new('error'))
strategy.call!('rack.session' => { 'omniauth.state' => state, 'omniauth.nonce' => nonce })
strategy.expects(:fail!)

id_token = stub('OpenIDConnect::ResponseObject::IdToken')
id_token.stubs(:verify!).with(issuer: 'example.com', client_id: @identifier, nonce: nonce).returns(true)
::OpenIDConnect::ResponseObject::IdToken.stubs(:decode).returns(id_token)

strategy.callback_phase
end

Expand All @@ -256,6 +262,11 @@ def test_callback_phase_with_etimeout
strategy.stubs(:access_token).raises(::Errno::ETIMEDOUT.new('error'))
strategy.call!('rack.session' => { 'omniauth.state' => state, 'omniauth.nonce' => nonce })
strategy.expects(:fail!)

id_token = stub('OpenIDConnect::ResponseObject::IdToken')
id_token.stubs(:verify!).with(issuer: 'example.com', client_id: @identifier, nonce: nonce).returns(true)
::OpenIDConnect::ResponseObject::IdToken.stubs(:decode).returns(id_token)

strategy.callback_phase
end

Expand All @@ -271,6 +282,11 @@ def test_callback_phase_with_socket_error
strategy.stubs(:access_token).raises(::SocketError.new('error'))
strategy.call!('rack.session' => { 'omniauth.state' => state, 'omniauth.nonce' => nonce })
strategy.expects(:fail!)

id_token = stub('OpenIDConnect::ResponseObject::IdToken')
id_token.stubs(:verify!).with(issuer: 'example.com', client_id: @identifier, nonce: nonce).returns(true)
::OpenIDConnect::ResponseObject::IdToken.stubs(:decode).returns(id_token)

strategy.callback_phase
end

Expand All @@ -286,6 +302,11 @@ def test_callback_phase_with_rack_oauth2_client_error
strategy.stubs(:access_token).raises(::Rack::OAuth2::Client::Error.new('error', error: 'Unknown'))
strategy.call!('rack.session' => { 'omniauth.state' => state, 'omniauth.nonce' => nonce })
strategy.expects(:fail!)

id_token = stub('OpenIDConnect::ResponseObject::IdToken')
id_token.stubs(:verify!).with(issuer: 'example.com', client_id: @identifier, nonce: nonce).returns(true)
::OpenIDConnect::ResponseObject::IdToken.stubs(:decode).returns(id_token)

strategy.callback_phase
end

Expand Down

0 comments on commit c6c64a4

Please sign in to comment.