Class: KindeSdk::Client

Inherits:
Object
  • Object
show all
Includes:
FeatureFlags, Permissions
Defined in:
lib/kinde_sdk/client.rb,
lib/kinde_sdk/client/permissions.rb,
lib/kinde_sdk/client/feature_flags.rb

Defined Under Namespace

Modules: FeatureFlags, Permissions

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Permissions

#get_permission, #get_permissions, #permission_granted?

Methods included from FeatureFlags

#get_boolean_flag, #get_flag, #get_integer_flag, #get_string_flag

Constructor Details

#initialize(sdk_api_client, tokens_hash, auto_refresh_tokens) ⇒ Client

Returns a new instance of Client.



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

def initialize(sdk_api_client, tokens_hash, auto_refresh_tokens)
  @kinde_api_client = sdk_api_client
  @auto_refresh_tokens = auto_refresh_tokens
  @token_store = TokenManager.create_store(tokens_hash)

  # refresh the token if it's expired and auto_refresh_tokens is enabled
  refresh_token if auto_refresh_tokens && TokenManager.token_expired?(@token_store)
end

Instance Attribute Details

#auto_refresh_tokensObject

Returns the value of attribute auto_refresh_tokens.



11
12
13
# File 'lib/kinde_sdk/client.rb', line 11

def auto_refresh_tokens
  @auto_refresh_tokens
end

#kinde_api_clientObject

Returns the value of attribute kinde_api_client.



11
12
13
# File 'lib/kinde_sdk/client.rb', line 11

def kinde_api_client
  @kinde_api_client
end

#token_storeObject

Returns the value of attribute token_store.



11
12
13
# File 'lib/kinde_sdk/client.rb', line 11

def token_store
  @token_store
end

Instance Method Details

#bearer_tokenString

Returns the bearer token for backwards compatibility

Returns:

  • (String)


24
25
26
# File 'lib/kinde_sdk/client.rb', line 24

def bearer_token
  @token_store.bearer_token
end

#expires_atInteger

Returns the token expiration time for backwards compatibility

Returns:

  • (Integer)


36
37
38
# File 'lib/kinde_sdk/client.rb', line 36

def expires_at
  @token_store.expires_at
end

#get_claim(claim, token_type = :access_token) ⇒ Hash

token_type is one of: :access_token, :id_token

Examples:

“scp”, value: [“openid”, “offline”]


Returns:

  • (Hash)


57
58
59
60
61
62
63
64
65
66
67
# File 'lib/kinde_sdk/client.rb', line 57

def get_claim(claim, token_type = :access_token)
  refresh_token if auto_refresh_tokens && token_expired?

  token = @token_store.tokens[token_type.to_sym]
  return unless token

  value = JWT.decode(token, nil, false)[0][claim]
  return unless value

  { name: claim, value: value }
end

#refresh_tokenObject



44
45
46
47
48
49
50
51
# File 'lib/kinde_sdk/client.rb', line 44

def refresh_token
  new_tokens_hash = TokenManager.refresh_tokens(@token_store, Current.session)
  return nil unless new_tokens_hash

  @token_store.set_tokens(new_tokens_hash)
  @kinde_api_client = KindeSdk.api_client(@token_store.bearer_token)
  new_tokens_hash
end

#token_expired?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/kinde_sdk/client.rb', line 40

def token_expired?
  TokenManager.token_expired?(@token_store)
end

#tokens_hashHash

Returns the tokens hash for backwards compatibility

Returns:

  • (Hash)


30
31
32
# File 'lib/kinde_sdk/client.rb', line 30

def tokens_hash
  @token_store.tokens
end