Allow pass a Proc to the setup option when you specify a strategy (#18)

Co-authored-by: Jose Luis Cambero <joseluis.cambero@sngular.com>
master
Cambero 2021-12-18 22:39:56 +01:00 committed by GitHub
parent cc236ae6fe
commit 09b68aab71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 2 deletions

View File

@ -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|
# ...

View File

@ -16,6 +16,8 @@ module OmniAuth
attr_reader :certs
def setup_phase
super
if @authorize_url.nil? || @token_url.nil?
prevent_site_option_mistake

View File

@ -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