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

[Fix] Logout user if a request errors with 401 #419

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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