Types and methods to work with Kinde Infrastructure features
# npm
npm install @kinde/infrastructure
# yarn
yarn add @kinde/infrastructure
# pnpm
pnpm install @kinde/infrastructure
idTokenCustomClaims
- Define and set custom claims on the id token
accessCustomClaims
- Define and set custom claims on the access token
m2mCustomClaims
- Define and set custom claims on the m2m token
denyAccess
- Deny access to your application
fetch
- Sent a request to external API
getEnvironmentVariable
- Get Environment variable from Kinde secrets
createKindeAPI
- Create handler to call the Kinde management SDK
{
"request": {
"ip": "1.2.3.4",
"auth": {
"audience": ["https://api.example.com/v1"]
}
},
"context": {
"auth": {
"origin": "refresh_token_request",
"connectionId": "conn_0192b...",
"isExistingSession": false
},
"user": {
"id": "kp_6a071...",
"identityId": "identity_0192c..."
},
"domains": {
"kindeDomain": "https://mykindebusiness.kinde.com"
},
"workflow": {
"trigger": "user:tokens_generation"
},
"application": {
"clientId": "f77dbc..."
},
"organization": {
"code": "org_b5a9c8..."
}
}
}
{
"request": {
"ip": "1.2.3.4",
"auth": {
"audience": ["https://api.example.com/v1"]
}
},
"context": {
"domains": {
"kindeDomain": "https://mykindebusiness.kinde.com"
},
"workflow": {
"trigger": "m2m:tokens_generation"
},
"application": {
"clientId": "f77dbc..."
}
}
}
import {
onUserTokenGeneratedEvent,
getKindeAccessTokenHandle,
WorkflowSettings,
WorkflowTrigger,
} from "@kinde/infrastructure";
export const workflowSettings: WorkflowSettings = {
id: "addAccessTokenClaim",
trigger: WorkflowTrigger.UserTokenGeneration,
};
export default {
async handle(event: onUserTokenGeneratedEvent) {
const accessToken = accessTokenCustomClaims<{
hello: string;
ip: string;
}>();
accessToken.hello = "Hello there!";
accessToken.ipAddress = event.request.ip;
},
};
This will result with two new extra claims added to the AccessToken
{
"hello": "Hello there!",
"ipAddress": "1.2.3.4"
}
This example will prevent login from anyone accessing with an IP address starting with 192
async handle(event: onUserTokenGeneratedEvent) {
if (accessToken.ipAddress.startsWith('192')) {
denyAccess("You are not allowed to access this resource");
}
},
This example will get the api token from the kinde environment variables and call an API to get the timezone for the IP address and add it to the ID token
async handle(event: onUserTokenGeneratedEvent) {
const ipInfoToken = getEnvironmentVariable('IP_INFO_TOKEN')?.value
const ipDetails = await fetch(`https://ipinfo.io/${event.request.ip}?token=${ipInfoToken}`, {
method: "GET",
responseFormat: 'json',
headers: new Headers({
"Content-Type": "application/json",
})
});
const accessToken = ipTokenCustomClaims<
{
timezone: string;
}
>();
accessToken.timezone = ipDetails.timezone;
},
Warning
Some claims are prohibited to be updated from a workflow. (see Prohibited Claims)
No sensitive data should be added to tokens as these can be accessed publically
Kinde Documentation - Explore the Kinde docs
If you'd like to contribute to this project, please follow these steps:
- Fork the repository.
- Create a new branch.
- Make your changes.
- Submit a pull request.
By contributing to Kinde, you agree that your contributions will be licensed under its MIT License.