Class: KindeSdk::Client
- Inherits:
-
Object
- Object
- KindeSdk::Client
- Includes:
- Entitlements, FeatureFlags, Permissions, Roles, Logging
- Defined in:
- lib/kinde_sdk/client.rb,
lib/kinde_sdk/client/roles.rb,
lib/kinde_sdk/client/permissions.rb,
lib/kinde_sdk/client/entitlements.rb,
lib/kinde_sdk/client/feature_flags.rb
Defined Under Namespace
Modules: Entitlements, FeatureFlags, Permissions, Roles
Instance Attribute Summary collapse
-
#auto_refresh_tokens ⇒ Object
Returns the value of attribute auto_refresh_tokens.
-
#force_api ⇒ Object
Returns the value of attribute force_api.
-
#kinde_api_client ⇒ Object
Returns the value of attribute kinde_api_client.
-
#token_store ⇒ Object
Returns the value of attribute token_store.
Instance Method Summary collapse
-
#bearer_token ⇒ String
Returns the bearer token for backwards compatibility.
-
#enhanced_user_profile ⇒ OpenStruct
Get enhanced user profile information.
-
#entitlement(key) ⇒ OpenStruct?
Get a specific entitlement by key.
-
#entitlements(page_size: 10, starting_after: nil) ⇒ OpenStruct
Get entitlements for the authenticated user with pagination support.
-
#expires_at ⇒ Integer
Returns the token expiration time for backwards compatibility.
-
#frontend ⇒ Object
Frontend API access - Internal use only Uses user access tokens instead of M2M tokens.
-
#generate_portal_url(domain:, return_url:, sub_nav: PortalPage::PROFILE) ⇒ Hash
Generate a URL to the user profile portal.
-
#generatePortalUrl(return_url, sub_nav = PortalPage::PROFILE) ⇒ Hash
PHP SDK compatible portal URL generation method.
-
#get_claim(claim, token_type = :access_token) ⇒ Hash
token_type is one of: :access_token, :id_token.
-
#getAllEntitlements ⇒ Array
(also: #all_entitlements)
Get all entitlements for the authenticated user, handling pagination automatically.
-
#getEntitlement(key) ⇒ OpenStruct?
PHP SDK compatible alias for entitlement.
-
#getEntitlementLimit(key) ⇒ Integer?
(also: #entitlement_limit)
Get the maximum limit for a specific entitlement.
-
#has_entitlement?(feature_key) ⇒ Boolean
Check if the user has a specific entitlement.
-
#hasEntitlement(key) ⇒ Boolean
PHP SDK compatible alias for has_entitlement?.
-
#initialize(sdk_api_client, tokens_hash, auto_refresh_tokens, force_api = false) ⇒ Client
constructor
A new instance of Client.
-
#oauth ⇒ Object
Custom oauth method that provides backward compatibility.
-
#portal_link(return_url:, page: PortalPage::PROFILE) ⇒ Hash
Generate a URL to the user profile portal.
- #refresh_token ⇒ Object
- #token_expired? ⇒ Boolean
-
#tokens_hash ⇒ Hash
Returns the tokens hash for backwards compatibility.
-
#user_feature_flags(page_size: 10, starting_after: nil) ⇒ OpenStruct
Get user feature flags with pagination support.
-
#user_permissions(page_size: 10, starting_after: nil) ⇒ OpenStruct
Get user permissions with pagination support.
-
#user_properties(page_size: 10, starting_after: nil) ⇒ OpenStruct
Get user properties with pagination support.
-
#user_roles(page_size: 10, starting_after: nil) ⇒ OpenStruct
Get user roles with pagination support.
Methods included from Entitlements
#get_entitlements, #has_billing_entitlements?, #has_entitlements?
Methods included from Logging
#log_debug, #log_error, #log_info, #log_warning, resolve_logger, write_log
Methods included from Roles
#getRoles, #get_roles, #has_roles?
Methods included from Permissions
#getAllPermissions, #getPermissions, #get_permission, #get_permissions, #get_permissions_legacy, #permission_granted?
Methods included from FeatureFlags
#getFlags, #get_boolean_flag, #get_flag, #get_flags, #get_integer_flag, #get_string_flag, #has_feature_flags?
Constructor Details
#initialize(sdk_api_client, tokens_hash, auto_refresh_tokens, force_api = false) ⇒ Client
Returns a new instance of Client.
32 33 34 35 36 37 38 39 40 |
# File 'lib/kinde_sdk/client.rb', line 32 def initialize(sdk_api_client, tokens_hash, auto_refresh_tokens, force_api = false) @kinde_api_client = sdk_api_client @auto_refresh_tokens = auto_refresh_tokens @token_store = TokenManager.create_store(tokens_hash) @force_api = force_api # 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_tokens ⇒ Object
Returns the value of attribute auto_refresh_tokens.
30 31 32 |
# File 'lib/kinde_sdk/client.rb', line 30 def auto_refresh_tokens @auto_refresh_tokens end |
#force_api ⇒ Object
Returns the value of attribute force_api.
30 31 32 |
# File 'lib/kinde_sdk/client.rb', line 30 def force_api @force_api end |
#kinde_api_client ⇒ Object
Returns the value of attribute kinde_api_client.
30 31 32 |
# File 'lib/kinde_sdk/client.rb', line 30 def kinde_api_client @kinde_api_client end |
#token_store ⇒ Object
Returns the value of attribute token_store.
30 31 32 |
# File 'lib/kinde_sdk/client.rb', line 30 def token_store @token_store end |
Instance Method Details
#bearer_token ⇒ String
Returns the bearer token for backwards compatibility
44 45 46 |
# File 'lib/kinde_sdk/client.rb', line 44 def bearer_token @token_store.bearer_token end |
#enhanced_user_profile ⇒ OpenStruct
Get enhanced user profile information.
359 360 361 362 363 364 |
# File 'lib/kinde_sdk/client.rb', line 359 def enhanced_user_profile frontend.get_user_profile_v2 rescue StandardError => e log_error("Failed to fetch enhanced profile: #{e.}") raise KindeSdk::APIError, "Unable to fetch enhanced profile: #{e.}" end |
#entitlement(key) ⇒ OpenStruct?
Get a specific entitlement by key.
207 208 209 210 211 212 |
# File 'lib/kinde_sdk/client.rb', line 207 def entitlement(key) frontend.get_entitlement(key) rescue StandardError => e log_error("Failed to fetch entitlement for #{key}: #{e.}") raise KindeSdk::APIError, "Unable to fetch entitlement: #{e.}" end |
#entitlements(page_size: 10, starting_after: nil) ⇒ OpenStruct
Get entitlements for the authenticated user with pagination support.
177 178 179 180 181 182 |
# File 'lib/kinde_sdk/client.rb', line 177 def entitlements(page_size: 10, starting_after: nil) frontend.get_entitlements(page_size: page_size, starting_after: starting_after) rescue StandardError => e log_error("Failed to fetch entitlements: #{e.}") raise KindeSdk::APIError, "Unable to fetch entitlements: #{e.}" end |
#expires_at ⇒ Integer
Returns the token expiration time for backwards compatibility
56 57 58 |
# File 'lib/kinde_sdk/client.rb', line 56 def expires_at @token_store.expires_at end |
#frontend ⇒ Object
Frontend API access - Internal use only Uses user access tokens instead of M2M tokens
165 166 167 |
# File 'lib/kinde_sdk/client.rb', line 165 def frontend @frontend_client ||= KindeSdk::Internal::FrontendClient.new(@token_store, KindeSdk.config.domain) end |
#generate_portal_url(domain:, return_url:, sub_nav: PortalPage::PROFILE) ⇒ Hash
Generate a URL to the user profile portal
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/kinde_sdk/client.rb', line 96 def generate_portal_url(domain:, return_url:, sub_nav: PortalPage::PROFILE) refresh_token if auto_refresh_tokens && token_expired? unless return_url.start_with?('http') raise StandardError, 'generatePortalUrl: returnUrl must be an absolute URL' end params = { 'return_url' => return_url, 'sub_nav' => sub_nav } response = Faraday.get("#{domain}/account_api/v1/portal_link", params) do |req| req.headers['Authorization'] = "Bearer #{@token_store.bearer_token}" end unless response.success? raise StandardError, "Failed to fetch profile URL: #{response.status} #{response.reason_phrase}" end result = JSON.parse(response.body) unless result['url'].is_a?(String) raise StandardError, "Invalid URL received from API" end begin { url: URI.parse(result['url']) } rescue URI::InvalidURIError => e log_error(e.) raise StandardError, "Invalid URL format received from API: #{result['url']}" end end |
#generatePortalUrl(return_url, sub_nav = PortalPage::PROFILE) ⇒ Hash
PHP SDK compatible portal URL generation method.
340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/kinde_sdk/client.rb', line 340 def generatePortalUrl(return_url, sub_nav = PortalPage::PROFILE) unless token_store.bearer_token raise KindeSdk::APIError, 'generatePortalUrl: Access Token not found' end unless return_url.start_with?('http') raise ArgumentError, 'generatePortalUrl: returnUrl must be an absolute URL' end frontend.get_portal_link(subnav: sub_nav, return_url: return_url) rescue StandardError => e log_error("Failed to generate portal URL: #{e.}") raise KindeSdk::APIError, "Unable to generate portal URL: #{e.}" end |
#get_claim(claim, token_type = :access_token) ⇒ Hash
token_type is one of: :access_token, :id_token
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/kinde_sdk/client.rb', line 77 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 |
#getAllEntitlements ⇒ Array Also known as: all_entitlements
Get all entitlements for the authenticated user, handling pagination automatically.
188 189 190 191 192 193 194 195 196 197 |
# File 'lib/kinde_sdk/client.rb', line 188 def getAllEntitlements unless token_store.bearer_token raise KindeSdk::APIError, 'User must be authenticated to get entitlements' end paginate_all_results('entitlements') { |starting_after| entitlements(page_size: 100, starting_after: starting_after) } rescue StandardError => e log_error("Failed to fetch all entitlements: #{e.}") raise KindeSdk::APIError, "Unable to fetch all entitlements: #{e.}" end |
#getEntitlement(key) ⇒ OpenStruct?
PHP SDK compatible alias for entitlement
219 220 221 222 223 224 225 226 227 |
# File 'lib/kinde_sdk/client.rb', line 219 def getEntitlement(key) # Find the specific entitlement from all entitlements (like PHP SDK does) entitlements = getAllEntitlements entitlements.find { |entitlement| entitlement.feature_key == key } rescue StandardError => e log_error("Failed to get entitlement for #{key}: #{e.}") raise KindeSdk::APIError, "Unable to get entitlement: #{e.}" end |
#getEntitlementLimit(key) ⇒ Integer? Also known as: entitlement_limit
Get the maximum limit for a specific entitlement.
257 258 259 260 261 262 263 |
# File 'lib/kinde_sdk/client.rb', line 257 def getEntitlementLimit(key) entitlement = getEntitlement(key) entitlement ? entitlement.entitlement_limit_max : nil rescue StandardError => e log_error("Error getting entitlement limit for #{key}: #{e.}") nil end |
#has_entitlement?(feature_key) ⇒ Boolean
Check if the user has a specific entitlement.
233 234 235 236 237 238 239 |
# File 'lib/kinde_sdk/client.rb', line 233 def has_entitlement?(feature_key) entitlement_response = entitlement(feature_key) !entitlement_response&.data&.entitlement.nil? rescue StandardError => e log_error("Error checking entitlement for #{feature_key}: #{e.}") false end |
#hasEntitlement(key) ⇒ Boolean
PHP SDK compatible alias for has_entitlement?
245 246 247 248 249 250 |
# File 'lib/kinde_sdk/client.rb', line 245 def hasEntitlement(key) getEntitlement(key) != nil rescue StandardError => e log_error("Error checking entitlement for #{key}: #{e.}") false end |
#oauth ⇒ Object
Custom oauth method that provides backward compatibility
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/kinde_sdk/client.rb', line 138 def oauth oauth_api = init_instance_api(KindeApi::OAuthApi) # Add backward compatibility for get_user method unless oauth_api.respond_to?(:get_user) oauth_api.define_singleton_method(:get_user) do |opts = {}| # Call the new method user_profile_v2 = get_user_profile_v2(opts) # Convert UserProfileV2 to UserProfile format as a hash # This provides backward compatibility without depending on the UserProfile class { id: user_profile_v2.id || user_profile_v2.sub, preferred_email: user_profile_v2.email, provided_id: user_profile_v2.provided_id, last_name: user_profile_v2.family_name, first_name: user_profile_v2.given_name, picture: user_profile_v2.picture } end end oauth_api end |
#portal_link(return_url:, page: PortalPage::PROFILE) ⇒ Hash
Generate a URL to the user profile portal.
326 327 328 329 330 331 |
# File 'lib/kinde_sdk/client.rb', line 326 def portal_link(return_url:, page: PortalPage::PROFILE) frontend.get_portal_link(subnav: page, return_url: return_url) rescue StandardError => e log_error("Failed to get portal link: #{e.}") raise KindeSdk::APIError, "Unable to get portal link: #{e.}" end |
#refresh_token ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/kinde_sdk/client.rb', line 64 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
60 61 62 |
# File 'lib/kinde_sdk/client.rb', line 60 def token_expired? TokenManager.token_expired?(@token_store) end |
#tokens_hash ⇒ Hash
Returns the tokens hash for backwards compatibility
50 51 52 |
# File 'lib/kinde_sdk/client.rb', line 50 def tokens_hash @token_store.tokens end |
#user_feature_flags(page_size: 10, starting_after: nil) ⇒ OpenStruct
Get user feature flags with pagination support.
274 275 276 277 278 279 |
# File 'lib/kinde_sdk/client.rb', line 274 def user_feature_flags(page_size: 10, starting_after: nil) frontend.get_feature_flags(page_size: page_size, starting_after: starting_after) rescue StandardError => e log_error("Failed to fetch feature flags: #{e.}") raise KindeSdk::APIError, "Unable to fetch feature flags: #{e.}" end |
#user_permissions(page_size: 10, starting_after: nil) ⇒ OpenStruct
Get user permissions with pagination support.
287 288 289 290 291 292 |
# File 'lib/kinde_sdk/client.rb', line 287 def (page_size: 10, starting_after: nil) frontend.(page_size: page_size, starting_after: starting_after) rescue StandardError => e log_error("Failed to fetch permissions: #{e.}") raise KindeSdk::APIError, "Unable to fetch permissions: #{e.}" end |
#user_properties(page_size: 10, starting_after: nil) ⇒ OpenStruct
Get user properties with pagination support.
300 301 302 303 304 305 |
# File 'lib/kinde_sdk/client.rb', line 300 def user_properties(page_size: 10, starting_after: nil) frontend.get_user_properties(page_size: page_size, starting_after: starting_after) rescue StandardError => e log_error("Failed to fetch properties: #{e.}") raise KindeSdk::APIError, "Unable to fetch properties: #{e.}" end |
#user_roles(page_size: 10, starting_after: nil) ⇒ OpenStruct
Get user roles with pagination support.
313 314 315 316 317 318 |
# File 'lib/kinde_sdk/client.rb', line 313 def user_roles(page_size: 10, starting_after: nil) frontend.get_user_roles(page_size: page_size, starting_after: starting_after) rescue StandardError => e log_error("Failed to fetch roles: #{e.}") raise KindeSdk::APIError, "Unable to fetch roles: #{e.}" end |