Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import decode from "jwt-decode";
import { ContextProvider } from "dom-context";
import { equal } from "@wry/equality";
import { getEnvironmentSDK } from "../environment";
import { getEnvironmentSDK, getTenantAlias } from "../environment";
import {
USER_CONTEXT_NAME,
UserIdentity,
Expand Down Expand Up @@ -62,13 +62,16 @@ export function userIdentityFromJwt(jwt?: string): UserIdentity | undefined {

let userId: string | undefined = undefined;
let accountId: string | undefined = undefined;
let tenantAlias: string | undefined = undefined;

if (isDecodedWidgetAPIJWT(decoded)) {
// Pull the accountId and userId from the subject and Base64-decode them
// This also applies to JWTs generated for microsite sessions
// NOTE: This is to support classic theme engine widget token generation
const matches = decoded.sub.match(/(.*):(.*)@(.*):users/);
if (matches?.[1]) accountId = atob(matches[1]);
if (matches?.[2]) userId = atob(matches[2]);
if (matches?.[3]) tenantAlias = matches[3];
} else if (isDecodedSquatchJWT(decoded)) {
accountId = decoded.user.accountId;
userId = decoded.user.id;
Expand All @@ -79,6 +82,11 @@ export function userIdentityFromJwt(jwt?: string): UserIdentity | undefined {
return undefined;
}

if (tenantAlias && getTenantAlias() && tenantAlias !== getTenantAlias()) {
debug("tenantAlias in JWT doesn't match environment");
return undefined;
}

// Check if the JWT has expired
if (exp && Date.now() >= exp * 1000) {
debug("JWT has expired");
Expand Down
Loading