Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add entitlements to claims available from access token #32

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ export const loader = (args: LoaderFunctionArgs) => authkitLoader(args);
export function App() {

// Retrieves the user from the session or returns `null` if no user is signed in
// Other supported values include sessionId, accessToken, organizationId, role, permissions, impersonator and oauthTokens
// Other supported values include sessionId, accessToken, organizationId,
// role, permissions, entitlements, impersonator and oauthTokens
const { user, signInUrl, signUpUrl } = useLoaderData<typeof loader>();

return (
Expand Down
3 changes: 3 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface AccessToken {
org_id?: string;
role?: string;
permissions?: string[];
entitlements?: string[];
}

export interface GetAuthURLOptions {
Expand All @@ -42,6 +43,7 @@ export interface AuthorizedData {
organizationId: string | null;
role: string | null;
permissions: string[];
entitlements: string[];
impersonator: Impersonator | null;
oauthTokens: OauthTokens | null;
sealedSession: string;
Expand All @@ -54,6 +56,7 @@ export interface UnauthorizedData {
organizationId: null;
role: null;
permissions: null;
entitlements: null;
impersonator: null;
oauthTokens: null;
sealedSession: null;
Expand Down
12 changes: 11 additions & 1 deletion src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ async function authkitLoader<Data = unknown>(
oauthTokens: null,
organizationId: null,
permissions: null,
entitlements: null,
role: null,
sessionId: null,
sealedSession: null,
Expand All @@ -146,6 +147,7 @@ async function authkitLoader<Data = unknown>(
organizationId = null,
role = null,
permissions = [],
entitlements = [],
} = getClaimsFromAccessToken(session.accessToken);

const cookieSession = await getSession(request.headers.get('Cookie'));
Expand All @@ -157,6 +159,7 @@ async function authkitLoader<Data = unknown>(
organizationId,
role,
permissions,
entitlements,
impersonator: session.impersonator ?? null,
oauthTokens: session.oauthTokens ?? null,
sealedSession: cookieSession.get('jwt'),
Expand Down Expand Up @@ -227,13 +230,20 @@ async function terminateSession(request: Request) {
}

function getClaimsFromAccessToken(accessToken: string) {
const { sid: sessionId, org_id: organizationId, role, permissions } = decodeJwt<AccessToken>(accessToken);
const {
sid: sessionId,
org_id: organizationId,
role,
permissions,
entitlements,
} = decodeJwt<AccessToken>(accessToken);

return {
sessionId,
organizationId,
role,
permissions,
entitlements,
};
}

Expand Down