Class: KindeSdk::TokenManager

Inherits:
Object
  • Object
show all
Defined in:
lib/kinde_sdk/token_manager.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tokens = nil) ⇒ TokenManager

Returns a new instance of TokenManager.



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

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

Instance Attribute Details

#bearer_tokenObject (readonly)

Returns the value of attribute bearer_token.



3
4
5
# File 'lib/kinde_sdk/token_manager.rb', line 3

def bearer_token
  @bearer_token
end

#expires_atObject (readonly)

Returns the value of attribute expires_at.



3
4
5
# File 'lib/kinde_sdk/token_manager.rb', line 3

def expires_at
  @expires_at
end

#tokensObject (readonly)

Returns the value of attribute tokens.



3
4
5
# File 'lib/kinde_sdk/token_manager.rb', line 3

def tokens
  @tokens
end

Class Method Details

.clear_tokens(store, session = nil) ⇒ Object



63
64
65
66
67
# File 'lib/kinde_sdk/token_manager.rb', line 63

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



34
35
36
# File 'lib/kinde_sdk/token_manager.rb', line 34

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

.refresh_tokens(store, session = nil) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/kinde_sdk/token_manager.rb', line 38

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)


57
58
59
60
61
# File 'lib/kinde_sdk/token_manager.rb', line 57

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



47
48
49
50
51
52
53
54
55
# File 'lib/kinde_sdk/token_manager.rb', line 47

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



27
28
29
30
31
# File 'lib/kinde_sdk/token_manager.rb', line 27

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

#set_tokens(tokens) ⇒ Object



9
10
11
12
13
14
# File 'lib/kinde_sdk/token_manager.rb', line 9

def set_tokens(tokens)
  @tokens = tokens.transform_keys(&:to_sym).freeze
  @bearer_token = @tokens[:access_token]
  @expires_at = @tokens[:expires_at]
  @tokens
end

#token_expired?Boolean

Returns:

  • (Boolean)


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

def token_expired?
  return true unless @tokens.present?
  begin
    KindeSdk.validate_jwt_token(@tokens)
    @expires_at.to_i > 0 && (@expires_at <= Time.now.to_i)
  rescue Exception => e
    Rails.logger.error("Error checking token expiration: #{e.message}")
    true
  end
end