class Keycloak::Helper

Constants

CURRENT_AUTHORIZED_PARTY_KEY
CURRENT_USER_ATTRIBUTES
CURRENT_USER_EMAIL_KEY
CURRENT_USER_ID_KEY
CURRENT_USER_LOCALE_KEY
QUERY_STRING_TOKEN_KEY
RESOURCE_ROLES_KEY
ROLES_KEY
TOKEN_KEY

Public Class Methods

assign_current_authorized_party(env, token) click to toggle source
# File lib/keycloak-api-rails/helper.rb, line 34
def self.assign_current_authorized_party(env, token)
  env[CURRENT_AUTHORIZED_PARTY_KEY] = token["azp"]
end
assign_current_user_custom_attributes(env, token, attribute_names) click to toggle source
# File lib/keycloak-api-rails/helper.rb, line 73
def self.assign_current_user_custom_attributes(env, token, attribute_names)
  env[CURRENT_USER_ATTRIBUTES] = token.select { |key, value| attribute_names.include?(key) }
end
assign_current_user_email(env, token) click to toggle source
# File lib/keycloak-api-rails/helper.rb, line 42
def self.assign_current_user_email(env, token)
  env[CURRENT_USER_EMAIL_KEY] = token["email"]
end
assign_current_user_id(env, token) click to toggle source
# File lib/keycloak-api-rails/helper.rb, line 18
def self.assign_current_user_id(env, token)
  env[CURRENT_USER_ID_KEY] = token["sub"]
end
assign_current_user_locale(env, token) click to toggle source
# File lib/keycloak-api-rails/helper.rb, line 50
def self.assign_current_user_locale(env, token)
  env[CURRENT_USER_LOCALE_KEY] = token["locale"]
end
assign_keycloak_token(env, token) click to toggle source
# File lib/keycloak-api-rails/helper.rb, line 26
def self.assign_keycloak_token(env, token)
  env[TOKEN_KEY] = token
end
assign_realm_roles(env, token) click to toggle source
# File lib/keycloak-api-rails/helper.rb, line 58
def self.assign_realm_roles(env, token)
  env[ROLES_KEY] = token.dig("realm_access", "roles")
end
assign_resource_roles(env, token) click to toggle source
# File lib/keycloak-api-rails/helper.rb, line 66
def self.assign_resource_roles(env, token)
  env[RESOURCE_ROLES_KEY] = token.fetch("resource_access", {}).inject({}) do |resource_roles, (name, resource_attributes)|
    resource_roles[name] = resource_attributes.fetch("roles", [])
    resource_roles
  end
end
create_url_with_token(uri, token) click to toggle source
# File lib/keycloak-api-rails/helper.rb, line 92
def self.create_url_with_token(uri, token)
  uri       = URI(uri)
  params    = URI.decode_www_form(uri.query || "").reject { |query_string| query_string.first == QUERY_STRING_TOKEN_KEY }
  params    << [QUERY_STRING_TOKEN_KEY, token]
  uri.query = URI.encode_www_form(params)
  uri.to_s
end
current_authorized_party(env) click to toggle source
# File lib/keycloak-api-rails/helper.rb, line 30
def self.current_authorized_party(env)
  env[CURRENT_AUTHORIZED_PARTY_KEY]
end
current_resource_roles(env) click to toggle source
# File lib/keycloak-api-rails/helper.rb, line 62
def self.current_resource_roles(env)
  env[RESOURCE_ROLES_KEY]
end
current_user_custom_attributes(env) click to toggle source
# File lib/keycloak-api-rails/helper.rb, line 77
def self.current_user_custom_attributes(env)
  env[CURRENT_USER_ATTRIBUTES]
end
current_user_email(env) click to toggle source
# File lib/keycloak-api-rails/helper.rb, line 38
def self.current_user_email(env)
  env[CURRENT_USER_EMAIL_KEY]
end
current_user_id(env) click to toggle source
# File lib/keycloak-api-rails/helper.rb, line 14
def self.current_user_id(env)
  env[CURRENT_USER_ID_KEY]
end
current_user_locale(env) click to toggle source
# File lib/keycloak-api-rails/helper.rb, line 46
def self.current_user_locale(env)
  env[CURRENT_USER_LOCALE_KEY]
end
current_user_roles(env) click to toggle source
# File lib/keycloak-api-rails/helper.rb, line 54
def self.current_user_roles(env)
  env[ROLES_KEY]
end
keycloak_token(env) click to toggle source
# File lib/keycloak-api-rails/helper.rb, line 22
def self.keycloak_token(env)
  env[TOKEN_KEY]
end
read_token_from_headers(headers) click to toggle source
# File lib/keycloak-api-rails/helper.rb, line 100
def self.read_token_from_headers(headers)
  headers["HTTP_AUTHORIZATION"]&.gsub(/^Bearer /, "") || ""
end
read_token_from_query_string(uri) click to toggle source
# File lib/keycloak-api-rails/helper.rb, line 85
def self.read_token_from_query_string(uri)
  parsed_uri         = URI.parse(uri)
  query              = URI.decode_www_form(parsed_uri.query || "")
  query_string_token = query.detect { |param| param.first == QUERY_STRING_TOKEN_KEY }
  query_string_token&.second
end