Module: KindeSdk::TokenHash

Defined in:
lib/kinde_sdk/token_hash.rb

Overview

Normalizes token hash key types for oauth2 round-trips and SDK consumers. Accepts string or symbol keys on input; internal storage uses symbol keys.

Constant Summary collapse

PUBLIC_KEYS =
%i[
  access_token refresh_token expires_at id_token scope token_type expires_in
].freeze

Class Method Summary collapse

Class Method Details

.access_token(raw) ⇒ Object



23
24
25
# File 'lib/kinde_sdk/token_hash.rb', line 23

def access_token(raw)
  normalize(raw)[:access_token]
end

.for_refresh_response(raw) ⇒ Object

Full oauth2 hash with string keys for refresh_token responses.



50
51
52
# File 'lib/kinde_sdk/token_hash.rb', line 50

def for_refresh_response(raw)
  normalize(raw).transform_keys(&:to_s)
end

.for_session(raw) ⇒ Object

String keys for session storage and documented public API responses.



45
46
47
# File 'lib/kinde_sdk/token_hash.rb', line 45

def for_session(raw)
  public_tokens(raw).transform_keys(&:to_s)
end

.from_access_token(token) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/kinde_sdk/token_hash.rb', line 32

def from_access_token(token)
  public_tokens(
    access_token: token.token,
    refresh_token: token.refresh_token,
    expires_at: token.expires_at,
    id_token: token_param(token, "id_token"),
    scope: token_param(token, "scope"),
    token_type: token_param(token, "token_type"),
    expires_in: token_param(token, "expires_in")
  )
end

.normalize(raw) ⇒ Object

Symbolize keys while preserving the full oauth2 hash contract.



12
13
14
15
16
17
18
19
20
21
# File 'lib/kinde_sdk/token_hash.rb', line 12

def normalize(raw)
  return {} if raw.nil?
  return {} if raw.respond_to?(:empty?) && raw.empty?

  raw.each_with_object({}) do |(key, value), memo|
    memo[key.to_sym] = value
  end.tap do |hash|
    hash[:access_token] = hash[:access_token].to_s if hash[:access_token]
  end
end

.public_tokens(raw) ⇒ Object

Reduced shape for documented public SDK responses (e.g. fetch_tokens).



28
29
30
# File 'lib/kinde_sdk/token_hash.rb', line 28

def public_tokens(raw)
  normalize(raw).slice(*PUBLIC_KEYS).compact
end

.token_param(token, key) ⇒ Object



54
55
56
# File 'lib/kinde_sdk/token_hash.rb', line 54

def token_param(token, key)
  token.params[key] || token.params[key.to_sym]
end