Class: KindeSdk::TokenManager

Inherits:
Object
  • Object
show all
Extended by:
Logging
Includes:
Logging
Defined in:
lib/kinde_sdk/token_manager.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

log_debug, log_error, log_info, log_warning, resolve_logger, write_log

Constructor Details

#initialize(tokens = nil) ⇒ TokenManager

Returns a new instance of TokenManager.



7
8
9
# File 'lib/kinde_sdk/token_manager.rb', line 7

def initialize(tokens = nil)
  set_tokens(tokens) if tokens
end

Instance Attribute Details

#bearer_tokenObject (readonly)

Returns the value of attribute bearer_token.



5
6
7
# File 'lib/kinde_sdk/token_manager.rb', line 5

def bearer_token
  @bearer_token
end

#expires_atObject (readonly)

Returns the value of attribute expires_at.



5
6
7
# File 'lib/kinde_sdk/token_manager.rb', line 5

def expires_at
  @expires_at
end

#tokensObject (readonly)

Returns the value of attribute tokens.



5
6
7
# File 'lib/kinde_sdk/token_manager.rb', line 5

def tokens
  @tokens
end

Class Method Details

.clear_tokens(store, session = nil) ⇒ Object



67
68
69
70
71
# File 'lib/kinde_sdk/token_manager.rb', line 67

def clear_tokens(store, session = nil)
  store.set_tokens(nil)
  target_session = session || Current.session
  target_session&.delete(:kinde_token_store)
end

.create_store(tokens = nil) ⇒ Object



38
39
40
# File 'lib/kinde_sdk/token_manager.rb', line 38

def create_store(tokens = nil)
  TokenStore.new(tokens)
end

.refresh_tokens(store, session = nil) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/kinde_sdk/token_manager.rb', line 42

def refresh_tokens(store, session = nil)
  return nil unless store&.tokens
  new_tokens = safe_refresh(store.tokens)
  return nil unless new_tokens
  store.set_tokens(new_tokens)
  sync_session(store, session)
  new_tokens
end

.token_expired?(store) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
# File 'lib/kinde_sdk/token_manager.rb', line 61

def token_expired?(store)
  return true unless store&.tokens
  return true unless validate_tokens(store)
  store.expires_at.to_i > 0 && (store.expires_at <= Time.now.to_i)
end

.validate_tokens(store) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/kinde_sdk/token_manager.rb', line 51

def validate_tokens(store)
  return false unless store&.tokens
  begin
    KindeSdk.validate_jwt_token(store.tokens)
    true
  rescue JWT::DecodeError, StandardError
    false
  end
end

Instance Method Details

#clear_tokensObject



29
30
31
32
33
# File 'lib/kinde_sdk/token_manager.rb', line 29

def clear_tokens
  @tokens = nil
  @bearer_token = nil
  @expires_at = nil
end

#set_tokens(tokens) ⇒ Object



11
12
13
14
15
16
# File 'lib/kinde_sdk/token_manager.rb', line 11

def set_tokens(tokens)
  @tokens = TokenHash.normalize(tokens).freeze
  @bearer_token = @tokens[:access_token]
  @expires_at = @tokens[:expires_at]
  @tokens
end

#token_expired?Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
27
# File 'lib/kinde_sdk/token_manager.rb', line 18

def token_expired?
  return true if @tokens.nil? || @tokens.empty?
  begin
    KindeSdk.validate_jwt_token(@tokens)
    @expires_at.to_i > 0 && (@expires_at <= Time.now.to_i)
  rescue StandardError => e
    log_error("Error checking token expiration: #{e.message}")
    true
  end
end