Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
rails_keycloak_authorization
Advanced tools
You can find more in this blog.
Rails middleware to authorize requests using Keycloak and gem keycloak-admin-ruby.
This gem uses JWT token to authorize requests. To read more how this gem works:
For the moment it only support permission_resource_format=uri. it does not support permission_resource_format=resource.
It does not support rails cookie-based-sessions, so it is only suitable for APIs.
This gem uses regular-expression for URLs matching, so it is very powerful and flexible.
This gem is a middleware that checks if the request is authorized by Keycloak. It will check if the request's token is valid and if the user has the required roles to access the requested resource.
Keycloak setup for authorization has many options, the following conventions were followed building this gem:
Rails component | Keycloak component |
---|---|
Controller | Authz Resource |
Controller Action | Authz Scope |
Route | permission subject |
sequenceDiagram
actor User
User->>Application: Request ${URL} with ${JWT_TOKEN}
create participant Keycloak
Application-->>Keycloak: is ${JWT_TOKEN} authorized for ${URL}?
note right of Keycloak: Keycloak will validate the token <br/> and check if the user has the required roles
destroy Keycloak
Keycloak-->>Application: ${JWT_TOKEN} is authorized for ${URL}
destroy Application
Application-->>User: Response ${URL} with ${DATA}
In order to use this gem, you need to configure it in an initializer file. You can create a new file in config/initializers/rails_keycloak_authorization.rb
with the following content:
RailsKeycloakAuthorization.keycloak_auth_client_realm_name = ENV.fetch("KEYCLOAK_AUTH_CLIENT_REALM_NAME", "dummy")
RailsKeycloakAuthorization.keycloak_auth_client_id = ENV.fetch("KEYCLOAK_AUTH_CLIENT_ID", "dummy-client")
RailsKeycloakAuthorization.keycloak_server_url = ENV.fetch("KEYCLOAK_SERVER_URL", "http://localhost:8080")
RailsKeycloakAuthorization.keycloak_server_domain = ENV.fetch("KEYCLOAK_ADMIN_SERVER_DOMAIN", "localhost")
RailsKeycloakAuthorization.keycloak_admin_realm_name = ENV.fetch("KEYCLOAK_ADMIN_REALM_NAME", "master")
RailsKeycloakAuthorization.keycloak_admin_client_id = ENV.fetch("KEYCLOAK_ADMIN_CLIENT_ID", "keycloak-admin")
RailsKeycloakAuthorization.keycloak_admin_client_secret = ENV.fetch("KEYCLOAK_ADMIN_CLIENT_SECRET", "keycloak-admin-client-secret-xxx")
RailsKeycloakAuthorization.match_patterns = [
/^\/organizations(\.json)?/,
/^\/api/,
/internal/
]
Add the route to the UI helper config/routes.rb
:
# make sure to change the constraint to suite your security
mount RailsKeycloakAuthorization::Engine, at: "/rka", constraints: lambda { |request| request.remote_ip == "127.0.0.1" }
Create development environment with Keycloak and Tofu:
git checkout https://github.com/tillawy/rails_keycloak_authorization.git
cd rails_keycloak_authorization
cd docker
docker-compose up
http://localhost:8080
, username: admin
, password: admin
brew install opentofu
cd ../tofu
tofu -chdir=tofu init
tofu -chdir=tofu apply -auto-approve
Running the previous steps should:
Dummy
dummy-client
in realm dummy
with:
dummy-client-super-secret-xxx
http://localhost:3000/*
test@test.com
with password test
keycloak-admin
in realm master
with:
keycloak-admin-client-secret-xxx
dummy
Run the server:
bundle exec rails s
make the first request (should fail) Authorization Failed
:
bash test/curl/test.curl.bash
How let us setup Authorization:
Rails Routes
/organizations(.:format)
, click inspectCreate Resource?
to create Authz Resource for controllerCreate Scope?
to create resource for controller actionAttach scope index to resource
to attach the scope (action: index) to the resource (controller: organizations_controller)Keycloak Policies
default-roles-dummy
Create
Keycloak Permissions
RKA-Policy
organization_controllers
index
Create
Now let us run the test bash test/curl/test.curl.bash
again, it should pass.
Add this line to your application's Gemfile:
gem "rails_keycloak_authorization"
And then execute:
$ bundle
Contribution directions go here.
The gem is available as open source under the terms of the MIT License.
FAQs
Unknown package
We found that rails_keycloak_authorization demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.