diff --git a/README.md b/README.md index e621efe..dc32d90 100755 --- a/README.md +++ b/README.md @@ -30,6 +30,26 @@ Rails.application.config.middleware.use OmniAuth::Builder do end ``` +Or using a proc setup with a custom options: + +```ruby +Rails.application.config.middleware.use OmniAuth::Builder do + SETUP_PROC = lambda do |env| + request = Rack::Request.new(env) + organization = Organization.find_by(host: request.host) + provider_config = organization.enabled_omniauth_providers[:keycloakopenid] + + env["omniauth.strategy"].options[:client_id] = provider_config[:client_id] + env["omniauth.strategy"].options[:client_secret] = provider_config[:client_secret] + env["omniauth.strategy"].options[:client_options] = { site: provider_config[:site], realm: provider_config[:realm] } + end + + Rails.application.config.middleware.use OmniAuth::Builder do + provider :keycloak_openid, setup: SETUP_PROC + end +end +``` + This will allow a POST request to `auth/keycloak` ## Devise Usage @@ -46,7 +66,7 @@ end # config/initializers/devise.rb config.omniauth :keycloak_openid, "Example-Client-Name", "example-secret-if-configured", client_options: { site: "https://example.keycloak-url.com", realm: "example-realm" }, :strategy_class => OmniAuth::Strategies::KeycloakOpenId -# Below controller assumes callback route configuration following +# Below controller assumes callback route configuration following # in config/routes.rb Devise.setup do |config| # ... diff --git a/lib/omniauth/strategies/keycloak-openid.rb b/lib/omniauth/strategies/keycloak-openid.rb index e8301ca..5ea8c8c 100755 --- a/lib/omniauth/strategies/keycloak-openid.rb +++ b/lib/omniauth/strategies/keycloak-openid.rb @@ -16,6 +16,8 @@ module OmniAuth attr_reader :certs def setup_phase + super + if @authorize_url.nil? || @token_url.nil? prevent_site_option_mistake diff --git a/spec/omniauth/strategies/keycloak_spec.rb b/spec/omniauth/strategies/keycloak_spec.rb index 8960982..763b4ea 100755 --- a/spec/omniauth/strategies/keycloak_spec.rb +++ b/spec/omniauth/strategies/keycloak_spec.rb @@ -35,7 +35,7 @@ RSpec.describe OmniAuth::Strategies::KeycloakOpenId do OmniAuth::Strategies::KeycloakOpenId.new('keycloak-openid', 'Example-Client', 'b53c572b-9f3b-4e79-bf8b-f03c799ba6ec', client_options: {site: 'http://localhost:8080/', realm: 'example-realm'}) end - + it 'should have the correct keycloak token url' do subject.setup_phase expect(subject.token_url).to eq('/auth/realms/example-realm/protocol/openid-connect/token') @@ -47,6 +47,16 @@ RSpec.describe OmniAuth::Strategies::KeycloakOpenId do end end + context 'client setup with a proc' do + subject do + OmniAuth::Strategies::KeycloakOpenId.new('keycloak-openid', setup: proc { throw :setup_proc_was_called }) + end + + it 'should call the proc' do + expect { subject.setup_phase }.to throw_symbol :setup_proc_was_called + end + end + describe 'errors processing' do context 'when site contains /auth part' do subject do