Module: KindeSdk

Defined in:
lib/kinde_sdk.rb,
lib/kinde_sdk/client.rb,
lib/kinde_sdk/version.rb,
lib/kinde_sdk/configuration.rb

Defined Under Namespace

Classes: Client, Configuration

Constant Summary collapse

VERSION =
"1.1.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject

Returns the value of attribute config.



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

def config
  @config
end

Class Method Details

.api_client(bearer_token) ⇒ KindeApi::ApiClient

init sdk api client by bearer token

Returns:

  • (KindeApi::ApiClient)


93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/kinde_sdk.rb', line 93

def api_client(bearer_token)
  config = KindeApi::Configuration.default
  config.configure do |c|
    c.access_token = bearer_token
    c.server_variables = { businessName: business_name }
    c.host = @config.domain
    c.debugging = @config.debugging
    c.logger = @config.logger
  end

  KindeApi::ApiClient.new(config)
end

.auth_url(**kwargs) ⇒ Hash

receive url for authorization in Kinde itself

Returns:

  • (Hash)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/kinde_sdk.rb', line 27

def auth_url(**kwargs)
  params = {
    redirect_uri: @config.callback_url,
    state: SecureRandom.hex,
    scope: @config.scope
  }.merge(**kwargs)
  return { url: @config.oauth_client.auth_code.authorize_url(params) } unless @config.pkce_enabled

  pkce_challenge = PkceChallenge.challenge(char_length: 128)
  params.merge!(code_challenge_method: 'S256', code_challenge: pkce_challenge.code_challenge)
  {
    url: @config.oauth_client.auth_code.authorize_url(params),
    code_verifier: pkce_challenge.code_verifier
  }
end

.client(bearer_token) ⇒ KindeSdk::Client

Returns:



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

def client(bearer_token)
  sdk_api_client = api_client(bearer_token)
  KindeSdk::Client.new(sdk_api_client, bearer_token)
end

.client_credentials_access(client_id: @config.client_id, client_secret: @config.client_secret, audience: "#{@config.domain}/api") ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/kinde_sdk.rb', line 65

def client_credentials_access(
  client_id: @config.client_id,
  client_secret: @config.client_secret,
  audience: "#{@config.domain}/api"
)
  Faraday.new(url: @config.domain) do |faraday|
    faraday.response :json
    faraday.use Faraday::FollowRedirects::Middleware
  end
    .post(@config.token_url) do |req|
    req.headers[:content_type] = 'application/x-www-form-urlencoded'
    req.body =
      "grant_type=client_credentials&client_id=#{client_id}&client_secret=#{client_secret}&audience=#{audience}"
  end.body
end

.configureObject



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

def configure
  if block_given?
    yield(Configuration.default)
  else
    Configuration.default
  end

  @config = Configuration.default
end

.fetch_tokens(params_or_code, code_verifier = nil) ⇒ Hash

when callback processor receives code, it needs to be used for fetching bearer token

Returns:

  • (Hash)


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

def fetch_tokens(params_or_code, code_verifier = nil)
  code = params_or_code.kind_of?(Hash) ? params.fetch("code") : params_or_code
  params = { redirect_uri: @config.callback_url }
  params[:code_verifier] = code_verifier if code_verifier
  @config.oauth_client.auth_code.get_token(code.to_s, params).to_hash
end

.logout_urlObject



59
60
61
62
63
# File 'lib/kinde_sdk.rb', line 59

def logout_url
  query = @config.logout_url ? URI.encode_www_form(redirect: @config.logout_url) : nil
  host = URI::parse(@config.domain).host
  URI::HTTP.build(host: host, path: '/logout', query: query).to_s
end

.refresh_token(hash) ⇒ Hash

Returns:

  • (Hash)


86
87
88
# File 'lib/kinde_sdk.rb', line 86

def refresh_token(hash)
  OAuth2::AccessToken.from_hash(@config.oauth_client, hash).refresh.to_hash
end

.token_expired?(hash) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/kinde_sdk.rb', line 81

def token_expired?(hash)
  OAuth2::AccessToken.from_hash(@config.oauth_client, hash).expired?
end