Class: KeycloakAdmin::Configuration
- Inherits:
-
Object
- Object
- KeycloakAdmin::Configuration
- Defined in:
- lib/keycloak-admin/configuration.rb
Constant Summary collapse
- TOKEN_EXPIRY_SAFETY_MARGIN_SECONDS =
Treat a cached token as expired this many seconds before its real expiry, so it cannot go stale between the check below and the request that relies on it.
10- FILTERED_ATTRIBUTES =
%i[@client_secret @password @cached_token].freeze
- FILTERED_PLACEHOLDER =
"[FILTERED]".freeze
Instance Attribute Summary collapse
-
#client_id ⇒ Object
Returns the value of attribute client_id.
-
#client_realm_name ⇒ Object
Returns the value of attribute client_realm_name.
-
#client_secret ⇒ Object
Returns the value of attribute client_secret.
-
#faraday_adapter ⇒ Object
Returns the value of attribute faraday_adapter.
-
#faraday_options ⇒ Object
Returns the value of attribute faraday_options.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#password ⇒ Object
Returns the value of attribute password.
-
#server_domain ⇒ Object
Returns the value of attribute server_domain.
-
#server_url ⇒ Object
Returns the value of attribute server_url.
-
#use_service_account ⇒ Object
Returns the value of attribute use_service_account.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
- #body_for_token_retrieval ⇒ Object
- #cache_token(token_representation) ⇒ Object
-
#cached_token ⇒ Object
Returns the cached TokenRepresentation, or nil if there is none or it is (about to be) expired.
- #clear_cached_token! ⇒ Object
-
#fetch_token_once ⇒ Object
Returns the cached token, fetching one through the block if there is none.
- #headers_for_token_retrieval ⇒ Object
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
- #inspect ⇒ Object
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
15 16 17 |
# File 'lib/keycloak-admin/configuration.rb', line 15 def initialize @token_mutex = Mutex.new end |
Instance Attribute Details
#client_id ⇒ Object
Returns the value of attribute client_id.
6 7 8 |
# File 'lib/keycloak-admin/configuration.rb', line 6 def client_id @client_id end |
#client_realm_name ⇒ Object
Returns the value of attribute client_realm_name.
6 7 8 |
# File 'lib/keycloak-admin/configuration.rb', line 6 def client_realm_name @client_realm_name end |
#client_secret ⇒ Object
Returns the value of attribute client_secret.
6 7 8 |
# File 'lib/keycloak-admin/configuration.rb', line 6 def client_secret @client_secret end |
#faraday_adapter ⇒ Object
Returns the value of attribute faraday_adapter.
6 7 8 |
# File 'lib/keycloak-admin/configuration.rb', line 6 def faraday_adapter @faraday_adapter end |
#faraday_options ⇒ Object
Returns the value of attribute faraday_options.
6 7 8 |
# File 'lib/keycloak-admin/configuration.rb', line 6 def @faraday_options end |
#logger ⇒ Object
Returns the value of attribute logger.
6 7 8 |
# File 'lib/keycloak-admin/configuration.rb', line 6 def logger @logger end |
#password ⇒ Object
Returns the value of attribute password.
6 7 8 |
# File 'lib/keycloak-admin/configuration.rb', line 6 def password @password end |
#server_domain ⇒ Object
Returns the value of attribute server_domain.
6 7 8 |
# File 'lib/keycloak-admin/configuration.rb', line 6 def server_domain @server_domain end |
#server_url ⇒ Object
Returns the value of attribute server_url.
6 7 8 |
# File 'lib/keycloak-admin/configuration.rb', line 6 def server_url @server_url end |
#use_service_account ⇒ Object
Returns the value of attribute use_service_account.
6 7 8 |
# File 'lib/keycloak-admin/configuration.rb', line 6 def use_service_account @use_service_account end |
#username ⇒ Object
Returns the value of attribute username.
6 7 8 |
# File 'lib/keycloak-admin/configuration.rb', line 6 def username @username end |
Instance Method Details
#body_for_token_retrieval ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/keycloak-admin/configuration.rb', line 64 def body_for_token_retrieval if use_service_account body_for_service_account else body_for_username_and_password end end |
#cache_token(token_representation) ⇒ Object
52 53 54 55 56 57 |
# File 'lib/keycloak-admin/configuration.rb', line 52 def cache_token(token_representation) return token_representation unless token_representation.expires_in.is_a?(Numeric) @cached_token = token_representation @token_expires_at = Time.now + token_representation.expires_in - TOKEN_EXPIRY_SAFETY_MARGIN_SECONDS token_representation end |
#cached_token ⇒ Object
Returns the cached TokenRepresentation, or nil if there is none or it is (about to be) expired.
47 48 49 50 |
# File 'lib/keycloak-admin/configuration.rb', line 47 def cached_token return nil if @token_expires_at.nil? || Time.now >= @token_expires_at @cached_token end |
#clear_cached_token! ⇒ Object
59 60 61 62 |
# File 'lib/keycloak-admin/configuration.rb', line 59 def clear_cached_token! @token_expires_at = nil @cached_token = nil end |
#fetch_token_once ⇒ Object
Returns the cached token, fetching one through the block if there is none. The block runs at most once across threads.
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/keycloak-admin/configuration.rb', line 34 def fetch_token_once token = cached_token if token.nil? @token_mutex.synchronize do token = cached_token token.nil? ? cache_token(yield) : token end else token end end |
#headers_for_token_retrieval ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/keycloak-admin/configuration.rb', line 72 def headers_for_token_retrieval if use_service_account headers_for_service_account else headers_for_username_and_password end end |
#inspect ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/keycloak-admin/configuration.rb', line 19 def inspect rendered = instance_variables.map do |name| value = instance_variable_get(name) shown = if FILTERED_ATTRIBUTES.include?(name) && !value.nil? FILTERED_PLACEHOLDER else value.inspect end "#{name}=#{shown}" end "#<#{self.class.name} #{rendered.join(", ")}>" end |