Class: Keycloak::Helper
- Inherits:
-
Object
- Object
- Keycloak::Helper
- Defined in:
- lib/keycloak-api-rails/helper.rb
Constant Summary collapse
- CURRENT_USER_ID_KEY =
"keycloak:keycloak_id"
- CURRENT_AUTHORIZED_PARTY_KEY =
"keycloak:authorized_party"
- CURRENT_USER_EMAIL_KEY =
"keycloak:email"
- CURRENT_USER_LOCALE_KEY =
"keycloak:locale"
- CURRENT_USER_ATTRIBUTES =
"keycloak:attributes"
- ROLES_KEY =
"keycloak:roles"
- RESOURCE_ROLES_KEY =
"keycloak:resource_roles"
- TOKEN_KEY =
"keycloak:token"
- QUERY_STRING_TOKEN_KEY =
"authorizationToken"
Class Method Summary collapse
- .assign_current_authorized_party(env, token) ⇒ Object
- .assign_current_user_custom_attributes(env, token, attribute_names) ⇒ Object
- .assign_current_user_email(env, token) ⇒ Object
- .assign_current_user_id(env, token) ⇒ Object
- .assign_current_user_locale(env, token) ⇒ Object
- .assign_keycloak_token(env, token) ⇒ Object
- .assign_realm_roles(env, token) ⇒ Object
- .assign_resource_roles(env, token) ⇒ Object
- .create_url_with_token(uri, token) ⇒ Object
- .current_authorized_party(env) ⇒ Object
- .current_resource_roles(env) ⇒ Object
- .current_user_custom_attributes(env) ⇒ Object
- .current_user_email(env) ⇒ Object
- .current_user_id(env) ⇒ Object
- .current_user_locale(env) ⇒ Object
- .current_user_roles(env) ⇒ Object
- .keycloak_token(env) ⇒ Object
- .read_token_from_headers(headers) ⇒ Object
- .read_token_from_query_string(uri) ⇒ Object
Class Method Details
.assign_current_authorized_party(env, token) ⇒ Object
34 35 36 |
# File 'lib/keycloak-api-rails/helper.rb', line 34 def self.(env, token) env[CURRENT_AUTHORIZED_PARTY_KEY] = token["azp"] end |
.assign_current_user_custom_attributes(env, token, attribute_names) ⇒ Object
73 74 75 |
# 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) ⇒ Object
42 43 44 |
# 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) ⇒ Object
18 19 20 |
# 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) ⇒ Object
50 51 52 |
# 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) ⇒ Object
26 27 28 |
# 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) ⇒ Object
58 59 60 |
# 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) ⇒ Object
66 67 68 69 70 71 |
# 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) ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/keycloak-api-rails/helper.rb', line 96 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) ⇒ Object
30 31 32 |
# File 'lib/keycloak-api-rails/helper.rb', line 30 def self.(env) env[CURRENT_AUTHORIZED_PARTY_KEY] end |
.current_resource_roles(env) ⇒ Object
62 63 64 |
# 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) ⇒ Object
77 78 79 |
# 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) ⇒ Object
38 39 40 |
# 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) ⇒ Object
14 15 16 |
# 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) ⇒ Object
46 47 48 |
# 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) ⇒ Object
54 55 56 |
# File 'lib/keycloak-api-rails/helper.rb', line 54 def self.current_user_roles(env) env[ROLES_KEY] end |
.keycloak_token(env) ⇒ Object
22 23 24 |
# File 'lib/keycloak-api-rails/helper.rb', line 22 def self.keycloak_token(env) env[TOKEN_KEY] end |
.read_token_from_headers(headers) ⇒ Object
104 105 106 |
# File 'lib/keycloak-api-rails/helper.rb', line 104 def self.read_token_from_headers(headers) headers["HTTP_AUTHORIZATION"]&.gsub(/^Bearer /, "") || "" end |
.read_token_from_query_string(uri) ⇒ Object
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/keycloak-api-rails/helper.rb', line 85 def self.read_token_from_query_string(uri) if uri.present? 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 else "" end end |