Ruby SDK
The Kinde Ruby SDK gem allows developers to integrate Kinde API into any ruby-based applications, Rails or non-Rails.
The gem contains all the related oauth2 authorization, and 3 pre-built OAuth flows: client credentials, authorization code and authorization code with PKCE code verifier.
You can also view the Ruby docs and Ruby starter kit in GitHub.
Register for Kinde
Link to this sectionIf you haven’t already got a Kinde account, register for free here (no credit card required). Registering gives you a Kinde domain, which you need to get started, e.g. yourapp.kinde.com
Install
Link to this sectionAdd this line into your Gemfile and run a bundler or install manually through a gem command.
Set callback URLs
Link to this section- In Kinde, go to Settings > Applications > [Your app] > View details.
- Add your callback URLs in the relevant fields. For example:
- Allowed callback URLs (also known as redirect URIs) - for example,
http://localhost:8000/api/auth/kinde_callback
- Allowed logout redirect URLs - for example,
http://localhost:8000
- Allowed callback URLs (also known as redirect URIs) - for example,
- Select Save.
Add environments
Link to this sectionKinde comes with a production environment, but you can set up other environments if you want to. Note that each environment needs to be set up independently, so you need to use the Environment subdomain in the code block above for those new environments.
Configure your app
Link to this sectionEnvironment variables
Link to this sectionThe following variables need to be replaced in the code snippets below.
KINDE_DOMAIN
- your Kinde domain - e.g.https://your_kinde_domain.kinde.com
KINDE_REDIRECT_URL
- your callback url, make sure this URL is under your allowed callback redirect URLs. - e.g.http://localhost:8000/api/auth/kinde_callback
KINDE_POST_LOGOUT_REDIRECT_URL
- where you want users to be redirected to after logging out, make sure this URL is under your allowed logout redirect URLs. - e.g.http://localhost:8000
KINDE_CLIENT_ID
- you can find this on the Application details page - e.g.your_kinde_client_id
KINDE_CLIENT_SECRET
- you can find this on the Application details page - e.g.your_kinde_client_secret
Integrate with your app
Link to this sectionYou can easily configure via the gem. For example, in a typical Rails-app it can be configured through the initializer file:
The snippet above contains all the possible configuration values. Here is a detailed explanation of them:
Domain
refers to your organization - for example,your-biz.kinde.com
.Client id
andClient secret
can be found in Kinde. Go to Settings > Applications > [yourapplication] > Details.Callback url
(or redirect URI) refers to the callback processing action. The URL must be defined in the Allowed callback URLs section of your application.Logout url
will open when the user signs out. The URL must be defined in the Allowed callback URLs section of your application.Scope
is an OAuth special parameter which is used to limit some rights.PKCE enabled
is a flag that can turn off PKCE auth flow. By default it is activated to improve security.Authorize url
andToken url
are paths to Oauth2 methods in kinde.Debugging
set toTrue
for long request logs. Can be useful while developing your application.Business name
is a parameter which is used in requests building. By default it is extracted from your Kindedomain
endpoint. For example, if your domain isyour-biz.kinde.com
, then business name will be set toyour-biz
. You don’t need to change it.Logger
set to whichever kind of loggers you are using. By default it is set toRails.logger
if gem is used in rails application orLogger.new(STDOUT)
if it is not a rails app.auto_refresh_tokens
defines the default behaviour on API instance method calls. If the config is set to false, there will not be any auto refreshes during method calling, otherwise each time the client will try to refresh expired tokens ifexpires_at
is present (see token expiration and refreshing section).
These variables can be handled with any system you want: .env files, settings.yml or any type of config files. For example, .env file (you can name variables yourself):
This can be used as:
The KINDE_MANAGEMENT_CLIENT_ID
and the KINDE_MANAGEMENT_CLIENT_SECRET
can be used for accessing the Kinde management API using the client_credentials
grant, without redirection, see details in the management API section.
Sign in and registration
Link to this sectionThe Kinde client provides methods for easy login and registration. For this, you need to acquire an auth URL by calling:
By default, gem uses the PKCE verification flow. This means that the code challenge
param will be added to your auth url, and the method returns verification string for the code. This can also be used in token requests.
You can disable PKCE by setting pkce_enabled
to false in your configuration. In this case, KindeSdk.auth_url
will only return a url:
If you are about to use PCKE, our recommendation is to save the code verifier output somewhere near your later tokens output.
The #auth_url
method can have another redirect url just in runtime. Use it with the argument:
You can put the link right in your web-application page or you can use it under the hood through redirection. After visiting the link you’ll be redirected to Kinde’s sign in/sign up form. And after authorizing in Kinde, you’ll be redirected to callback url.
Manage redirects
Link to this sectionThe next step is to extract code from the callback redirect. Your callback endpoint should contain logic to call the exchange method.
Callback is triggered in the body with the code. Use the whole params
object or to extract code from params["code"]
.
Next, exchange access and refresh tokens. You will receive code
as the parameter in the callback endpoint, and code_verifier
(if PKCE enabled) as per the previous step.
Save the whole hash in your session, redis or any other storage, and use it to build your client.
Fetch tokens
Link to this sectionThe #fetch_tokens
method can have another callback url (just lake the #auth_url
method), just use it in a same way:
Token expiration and refreshing
Link to this sectionFor proper refreshing you’ll need to use access_token
, refresh_token
and probably expires_in
if you want to know if your access token still actual. Use these two methods to work with refreshing:
Or from your client instance:
If you are calling #refresh_token
on a client instance, the instance token data will be automatically updated. If you are calling KindeSdk#refresh_token
, you’ll need to store new token data in your configured storage (redis, session, etc).
Audience
Link to this sectionAn audience
is the intended recipient of an access token - for example the API for your application. The audience argument can be passed to the Kinde #auth_url
method to request an audience be added to the provided token:
For details on how to connect, see Register an API
Overriding scope
Link to this sectionBy default KindeSdk
requests the following scopes:
- profile
- offline
- openid
You are able to change it - by configuring as per the integration instructions above - or by direct param passing into auth_url
method:
Getting claims
Link to this sectionWe have provided a helper to grab any claim from your ID or access tokens. The helper defaults to access tokens:
By default claim data is fetched from access_token, but you can also do it with id_token as well:
User permissions
Link to this sectionAfter a user signs in and they are verified, the token return includes permissions for that user. User permissions are set in Kinde, but you must also configure your application to unlock these functions.
We provide helper functions to more easily access permissions:
Feature flags
Link to this sectionKinde provides feature flag functionality. So, the SDK provides methods to work with them. Here’s an example:
Note that t
refers to type (b
- boolean, i
- integer, s
- string) and v
refers to value. You can fetch these flags with methods below:
If you try to call an call undefined flag, you will get an exception.
In addition to fetch existing flags, you can use fallbacks. For example:
Set the type explicitly (output omitted except value):
In the example above if you try to set default_value of different types (for example: get_flag("flag", {default_value: 1}, "s")
), you’ll get an exception.
Also you have wrapper methods, for example:
Client usage
Link to this sectionThe API part is mounted in the KindeSdk::Client
instance, so the short usage is:
The method name will be the same as API module from the SDK without -Api
part. Alternatively, you can initialize each API module:
Logout
Link to this sectionFor logout you need to call in the controller (in the case of a rails app):
Your app should handle the logout callback url (which was configured separately). After calling redirect to logout_url (if set), Kinde redirects it back to logout callback path, where you need to clear your session:
If you configured a logout redirect url in Kinde, you’ll receive a logout callback. Otherwise a Kinde logout message will be shown.
Organizations
Link to this sectionCreate an organization
Link to this sectionTo have a new organization created within your application, you will need to run something like:
Sign up and sign in to organizations
Link to this sectionKinde has a unique code for every organization. If you want a user to sign in to a particular organization, call the #auth_url
method with org_code
param passing:
Following authentication, Kinde provides a json web token (jwt) to your application. Along with the standard information we also include the org_code
and the permissions for that organization (this is important as a user can belong to multiple organizations and have different permissions for each).
Example of a returned token:
The id_token
will also contain an array of organizations that a user belongs to - this is useful if you wanted to build out an organization switcher for example:
For more information about how organizations work in Kinde, see Kinde organizations for developers.
Get user info
Link to this sectionKinde management API
Link to this sectionTo get started, you will need an access token, the Ruby SDK includes a helper to get one. Or see @kinde/management-api-js
This token can then be used to call any of the endpoints in the Kinde Management API.
Organizations handling
Link to this sectionCreate new user
Link to this sectionAlternatively, using model instances:
Add organization users
Link to this sectionToken expiration and refreshing
Link to this sectionFor proper refreshing you’ll need to use access_token
, refresh_token
and probably expires_in
if you want to know if your access token is still active. Use these two methods to work with refreshing:
KindeApi#refresh_token
returns a new token hash, so it needs to be updated in your storage.
SDK API reference
Link to this sectionEither your Kinde URL or your custom domain. e.g https://yourapp.kinde.com
.
Type: string
Required: Yes
redirectUri
Link to this sectionThe URL that the user will be returned to after authentication.
Type: string
Required: Yes
clientId
Link to this sectionThe unique ID of your application in Kinde.
Type: string
Required: Yes
clientSecret
Link to this sectionThe unique ID key or secret of your application in Kinde.
Type: string
Required: Yes
logoutRedirectUri
Link to this sectionWhere your user will be redirected when they sign out.
Type: string
Required: Yes
scope
Link to this sectionThe scopes to be requested from Kinde.
Type: boolean
Required: No
Default: openid profile email offline
additionalParameters
Link to this sectionAdditional parameters that will be passed in the authorization request.
Type: object
Required: No
Default: {}
additionalParameters
- audience
Link to this sectionThe audience claim for the JWT.
Type: string
Required: No
Kinde SDK methods
Link to this sectionlogin
Link to this sectionConstructs a redirect URL and sends the user to Kinde to sign in.
Arguments:
Usage:
Allow org_code
to be provided if a specific organization is being signed into.
register
Link to this sectionConstructs a redirect URL and sends the user to Kinde to sign up.
Arguments:
Usage:
logout
Link to this sectionLogs the user out of Kinde.
Usage:
getToken
Link to this sectionReturns the raw Access token from URL after logged from Kinde.
Usage:
Sample output:
createOrg
Link to this sectionConstructs a redirect URL and sends the user to Kinde to sign up and create a new organization in your business.
Arguments:
Usage:
Allow org_name
to be provided if you want a specific organization name when you create.
Sample output:
getClaim
Link to this sectionGets a claim from an access or ID token.
Arguments:
Usage:
Sample output:
getPermission
Link to this sectionReturns the state of a given permission.
Arguments: key: string
Usage:
Sample output:
getPermissions
Link to this sectionReturns all permissions for the current user for the organization they are signed into.
Usage:
Sample output:
getOrganization
Link to this sectionGet details for the organization your user is signed into.
Usage:
Sample output:
getUserDetails
Link to this sectionReturns the profile for the current user.
Usage:
Sample output:
getUserOrganizations
Link to this sectionGets an array of all organizations the user has access to.
If you need help connecting to Kinde, contact us at support@kinde.com.