Class: Keycloak::Helper

Inherits:
Object
  • Object
show all
Defined in:
lib/keycloak-api-rails/helper.rb

Constant Summary collapse

CURRENT_USER_ID_KEY =
"keycloak:keycloak_id"
CURRENT_USER_EMAIL_KEY =
"keycloak:email"
CURRENT_USER_LOCALE_KEY =
"keycloak:locale"
CURRENT_USER_ATTRIBUTES =
"keycloak:attributes"
ROLES_KEY =
"keycloak:roles"
QUERY_STRING_TOKEN_KEY =
"authorizationToken"

Class Method Summary collapse

Class Method Details

.assign_current_user_custom_attributes(env, token, attribute_names) ⇒ Object



43
44
45
# File 'lib/keycloak-api-rails/helper.rb', line 43

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) ⇒ Object



23
24
25
# File 'lib/keycloak-api-rails/helper.rb', line 23

def self.assign_current_user_email(env, token)
  env[CURRENT_USER_EMAIL_KEY] = token["email"]
end

.assign_current_user_id(env, token) ⇒ Object



15
16
17
# File 'lib/keycloak-api-rails/helper.rb', line 15

def self.assign_current_user_id(env, token)
  env[CURRENT_USER_ID_KEY] = token["sub"]
end

.assign_current_user_locale(env, token) ⇒ Object



31
32
33
# File 'lib/keycloak-api-rails/helper.rb', line 31

def self.assign_current_user_locale(env, token)
  env[CURRENT_USER_LOCALE_KEY] = token["locale"]
end

.assign_realm_roles(env, token) ⇒ Object



39
40
41
# File 'lib/keycloak-api-rails/helper.rb', line 39

def self.assign_realm_roles(env, token)
  env[ROLES_KEY] = token.dig("realm_access", "roles")
end

.create_url_with_token(uri, token) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/keycloak-api-rails/helper.rb', line 62

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_user_custom_attributes(env) ⇒ Object



47
48
49
# File 'lib/keycloak-api-rails/helper.rb', line 47

def self.current_user_custom_attributes(env)
  env[CURRENT_USER_ATTRIBUTES]
end

.current_user_email(env) ⇒ Object



19
20
21
# File 'lib/keycloak-api-rails/helper.rb', line 19

def self.current_user_email(env)
  env[CURRENT_USER_EMAIL_KEY]
end

.current_user_id(env) ⇒ Object



11
12
13
# File 'lib/keycloak-api-rails/helper.rb', line 11

def self.current_user_id(env)
  env[CURRENT_USER_ID_KEY]
end

.current_user_locale(env) ⇒ Object



27
28
29
# File 'lib/keycloak-api-rails/helper.rb', line 27

def self.current_user_locale(env)
  env[CURRENT_USER_LOCALE_KEY]
end

.current_user_roles(env) ⇒ Object



35
36
37
# File 'lib/keycloak-api-rails/helper.rb', line 35

def self.current_user_roles(env)
  env[ROLES_KEY]
end

.read_token_from_headers(headers) ⇒ Object



70
71
72
# File 'lib/keycloak-api-rails/helper.rb', line 70

def self.read_token_from_headers(headers)
  headers["HTTP_AUTHORIZATION"]&.gsub(/^Bearer /, "") || ""
end

.read_token_from_query_string(uri) ⇒ Object



55
56
57
58
59
60
# File 'lib/keycloak-api-rails/helper.rb', line 55

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