Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use jwks caching feature of openid_connect gem #124

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions lib/omniauth/strategies/openid_connect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,12 @@ def authorize_uri
client.authorization_uri(opts.reject { |_k, v| v.nil? })
end

def public_key
return config.jwks if options.discovery

key_or_secret || config.jwks
def public_key_or_config
if options.discovery || key_or_secret.blank?
config
else
key_or_secret
end
end

private
Expand Down Expand Up @@ -231,7 +233,7 @@ def access_token
end

def decode_id_token(id_token)
::OpenIDConnect::ResponseObject::IdToken.decode(id_token, public_key)
::OpenIDConnect::ResponseObject::IdToken.decode(id_token, public_key_or_config)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nov In #134, I'm fixing how this gem handles HS256 signatures. I'm wondering if you see any issues with the approach there.

I was actually just looking at the changes made in the openid-connect gem in https://github.com/omniauth/omniauth_openid_connect/pull/134/files#r1027569274. I think that change assumes the JWT includes the kid when HS256 is used. It's been a while since I've played with Keycloak, but if I recall correctly it may not be included. I'll have to try this again.

end

def client_options
Expand Down
2 changes: 1 addition & 1 deletion omniauth_openid_connect.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Gem::Specification.new do |spec|

spec.add_dependency 'addressable', '~> 2.5'
spec.add_dependency 'omniauth', '>= 1.9', '< 3'
spec.add_dependency 'openid_connect', '~> 1.1'
spec.add_dependency 'openid_connect', '~> 1.4'
spec.add_development_dependency 'faker', '~> 2.0'
spec.add_development_dependency 'guard', '~> 2.14'
spec.add_development_dependency 'guard-bundler', '~> 2.2'
Expand Down
34 changes: 30 additions & 4 deletions test/lib/omniauth/strategies/openid_connect_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -568,11 +568,37 @@ def test_option_client_auth_method
assert(strategy.send(:access_token))
end

def test_with_no_key_nor_discovery
config = ::OpenIDConnect::Discovery::Provider::Config::Response.new(
issuer: 'https://example.com/',
authorization_endpoint: 'https://example.com/authorization',
jwks_uri: 'https://example.com/jwks'
)
::OpenIDConnect::Discovery::Provider::Config.stubs(:discover!).with('https://example.com/').returns(config)
strategy.options.issuer = 'https://example.com/'
strategy.options.discovery = true

assert_equal config, strategy.public_key_or_config
end

def test_public_key_with_discovery
config = ::OpenIDConnect::Discovery::Provider::Config::Response.new(
issuer: 'https://example.com/',
authorization_endpoint: 'https://example.com/authorization',
jwks_uri: 'https://example.com/jwks'
)
::OpenIDConnect::Discovery::Provider::Config.stubs(:discover!).with('https://example.com/').returns(config)
strategy.options.issuer = 'https://example.com/'
strategy.options.discovery = true

assert_equal config, strategy.public_key_or_config
end

def test_public_key_with_jwks
strategy.options.client_signing_alg = :RS256
strategy.options.client_jwk_signing_key = File.read('./test/fixtures/jwks.json')

assert_equal JSON::JWK::Set, strategy.public_key.class
assert_equal JSON::JWK::Set, strategy.public_key_or_config.class
end

def test_public_key_with_jwk
Expand All @@ -582,19 +608,19 @@ def test_public_key_with_jwk
jwk = jwks['keys'].first
strategy.options.client_jwk_signing_key = jwk.to_json

assert_equal JSON::JWK, strategy.public_key.class
assert_equal JSON::JWK, strategy.public_key_or_config.class
end

def test_public_key_with_x509
strategy.options.client_signing_alg = :RS256
strategy.options.client_x509_signing_key = File.read('./test/fixtures/test.crt')
assert_equal OpenSSL::PKey::RSA, strategy.public_key.class
assert_equal OpenSSL::PKey::RSA, strategy.public_key_or_config.class
end

def test_public_key_with_hmac
strategy.options.client_options.secret = 'secret'
strategy.options.client_signing_alg = :HS256
assert_equal strategy.options.client_options.secret, strategy.public_key
assert_equal strategy.options.client_options.secret, strategy.public_key_or_config
end

def test_id_token_auth_hash
Expand Down