Skip to content

Commit 377b58f

Browse files
committed
Better auth handling
1 parent 0b273b4 commit 377b58f

2 files changed

Lines changed: 39 additions & 9 deletions

File tree

src/auth/guards/scopes.guard.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,14 @@ export class ScopesGuard implements CanActivate {
3232
.map((s: string) => s.trim())
3333
.filter(Boolean);
3434

35-
const ok = required.some((s) => scopes.includes(s));
35+
const normalizedRequiredScopes = required.map((scope) =>
36+
scope.trim().toLowerCase(),
37+
);
38+
const normalizedScopes = scopes.map((scope) => scope.trim().toLowerCase());
39+
40+
const ok = normalizedScopes.some((scope) =>
41+
normalizedRequiredScopes.includes(scope),
42+
);
3643
if (ok) return true;
3744

3845
const fallbackRoles = this.reflector.getAllAndOverride<string[]>(
@@ -48,7 +55,16 @@ export class ScopesGuard implements CanActivate {
4855
.map((r: string) => r.trim())
4956
.filter(Boolean);
5057

51-
const roleOk = roles.some((r: string) => fallbackRoles.includes(r));
58+
const normalizedFallbackRoles = fallbackRoles.map((role) =>
59+
role.trim().toLowerCase(),
60+
);
61+
const normalizedRoles = roles.map((role: string) =>
62+
role.trim().toLowerCase(),
63+
);
64+
65+
const roleOk = normalizedRoles.some((role: string) =>
66+
normalizedFallbackRoles.includes(role),
67+
);
5268
if (roleOk) return true;
5369
}
5470

src/billing-accounts/billing-accounts.service.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,13 @@ import {
3333
} from "../auth/constants";
3434

3535
export interface BillingAccountsAuthUser {
36+
id?: number | string;
3637
role?: string;
3738
roles?: string[] | string;
39+
sub?: number | string;
40+
tcUserId?: number | string;
41+
user_id?: number | string;
42+
userID?: number | string;
3843
userId?: number | string;
3944
}
4045

@@ -112,24 +117,33 @@ function getNormalizedAuthUserRoles(
112117
/**
113118
* Resolves the caller user id as a trimmed string when present.
114119
*
120+
* Topcoder JWT middleware has used a few decoded claim names across services,
121+
* so this accepts the canonical `userId` first and then falls back to the other
122+
* common user-id claim spellings.
123+
*
115124
* @param authUser Authenticated caller context from `req.authUser`.
116125
* @returns Normalized user id or `undefined` when missing.
117126
*/
118127
function getNormalizedAuthUserId(
119128
authUser?: BillingAccountsAuthUser,
120129
): string | undefined {
121-
if (
122-
typeof authUser?.userId === "number" &&
123-
Number.isFinite(authUser.userId)
124-
) {
125-
return String(authUser.userId);
130+
const candidateUserId =
131+
authUser?.userId ??
132+
authUser?.user_id ??
133+
authUser?.userID ??
134+
authUser?.tcUserId ??
135+
authUser?.id ??
136+
authUser?.sub;
137+
138+
if (typeof candidateUserId === "number" && Number.isFinite(candidateUserId)) {
139+
return String(candidateUserId);
126140
}
127141

128-
if (typeof authUser?.userId !== "string") {
142+
if (typeof candidateUserId !== "string") {
129143
return undefined;
130144
}
131145

132-
const normalizedUserId = authUser.userId.trim();
146+
const normalizedUserId = candidateUserId.trim();
133147

134148
return normalizedUserId || undefined;
135149
}

0 commit comments

Comments
 (0)