Sha256: 878632232f4c2bf5bb8f1ed422a52800670198638a1a6dc34de10a6f474d559c

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

# Keycloak connect ruby

```ruby
require 'logger'

KeycloakConnectRuby.configure do |c|
  c.url = 'http://keycloak_server:8080'
  c.realm = 'my-company'
  c.client_id = 'my-sinatra-app'
  c.client_secret = 'dsfgfdgfdgfd'
  c.logger = Logger.new(STDOUT) # Or Rails.logger
  c.proxy = nil
end

class MyApiController < ApplicationController
  before_action :keycloak_authenticate_and_check_role!

  def current_user
    KeycloakConnectRuby::User.from_token(keycloak_token)
  end

  private

  def keycloak_token
    KeycloakConnectRuby::Token.validate_and_decode(bearer_token)
  end

  def bearer_token
    request.authorization&.to_s&.gsub(/^Bearer /, "")
  end

  def keycloak_authenticate_and_check_role!
    unless current_user.has_role?('my-role-name')
      render json: { error: 'your message here' }, status: :unauthorized
    end
  rescue KeycloakConnectRuby::Errors::Token::ExpiredError => e
    # ...
  rescue KeycloakConnectRuby::Errors::Token::NoProvidedError => e
    # ...
  rescue KeycloakConnectRuby::Errors::Token::InvalidFormatError => e
    # ...
  end

  def generate_keycloak_token_for_api_calls
    # Client Credentials Grant flow
    KeycloakConnectRuby::Token.generate_from_client_credentials

    # Resource Owner Password Credentials flow (or Direct Grant flow)
    KeycloakConnectRuby::Token.generate_from_password_credentials('username here', 'password here')
  end
end
```

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
keycloak-connect-ruby-1.0.0 README.md