fix(api): resolve lint errors
This commit is contained in:
1 parent
96fede9fe5
commit
fd1fa2366c
42 files changed
+307
-199
No files matched your search
@@ -26,8 +26,7 @@ export class JwtAuthGuard implements CanActivate {
|
||||
METADATA_KEY_IS_PUBLIC_ACCESS_ALLOWED,
|
||||
[context.getHandler(), context.getClass()],
|
||||
);
|
||||
const authz = request.headers?.authorization;
|
||||
const authzHeader = Array.isArray(authz) ? authz[0] : authz;
|
||||
const authzHeader = request.get('authorization');
|
||||
|
||||
if (!authzHeader) {
|
||||
const fallback =
|
||||
@@ -74,7 +73,7 @@ export class JwtAuthGuard implements CanActivate {
|
||||
|
||||
private pickIdPFromToken(token: string): IdentityProviderName {
|
||||
try {
|
||||
const { iss } = decodeJwt(token) as { iss?: string };
|
||||
const { iss } = decodeJwt(token);
|
||||
if (!iss) {
|
||||
throw new Error('Token has no issuer (iss) claim');
|
||||
}
|
||||
|
||||
@@ -35,11 +35,13 @@ export class JwtService {
|
||||
throw new Error('Empty token');
|
||||
}
|
||||
|
||||
if (identityProvider !== 'MICROSOFT_ENTRAID') {
|
||||
throw new Error(`Identity provider not configured: ${identityProvider}`);
|
||||
const provider =
|
||||
identityProvider === 'MICROSOFT_ENTRAID'
|
||||
? await this.getEntraConfig()
|
||||
: undefined;
|
||||
if (!provider) {
|
||||
throw new Error('Identity provider not configured.');
|
||||
}
|
||||
|
||||
const provider = await this.getEntraConfig();
|
||||
const result = await jwtVerify(token, provider.jwks, {
|
||||
issuer: provider.issuer,
|
||||
audience: provider.audience,
|
||||
@@ -48,14 +50,22 @@ export class JwtService {
|
||||
});
|
||||
|
||||
const { payload, protectedHeader } = result;
|
||||
if (!provider.allowedAlgs.includes(protectedHeader.alg as string)) {
|
||||
if (!provider.allowedAlgs.includes(protectedHeader.alg)) {
|
||||
throw new Error(`Unexpected alg: ${protectedHeader.alg}`);
|
||||
}
|
||||
if (payload.ver && payload.ver !== '2.0') {
|
||||
throw new Error(`Unsupported Entra token version: ${payload.ver}`);
|
||||
if (
|
||||
payload.ver !== undefined &&
|
||||
(typeof payload.ver !== 'string' || payload.ver !== '2.0')
|
||||
) {
|
||||
throw new Error(
|
||||
`Unsupported Entra token version: ${formatClaimValue(payload.ver)}`,
|
||||
);
|
||||
}
|
||||
if (payload.tid && payload.tid !== provider.tenantId) {
|
||||
throw new Error(`Unexpected tenant: ${payload.tid}`);
|
||||
if (
|
||||
payload.tid !== undefined &&
|
||||
(typeof payload.tid !== 'string' || payload.tid !== provider.tenantId)
|
||||
) {
|
||||
throw new Error(`Unexpected tenant: ${formatClaimValue(payload.tid)}`);
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -98,3 +108,16 @@ export class JwtService {
|
||||
return this.entraConfig;
|
||||
}
|
||||
}
|
||||
|
||||
function formatClaimValue(value: unknown): string {
|
||||
if (
|
||||
typeof value === 'string' ||
|
||||
typeof value === 'number' ||
|
||||
typeof value === 'boolean' ||
|
||||
typeof value === 'bigint'
|
||||
) {
|
||||
return String(value);
|
||||
}
|
||||
if (value === null) return 'null';
|
||||
return JSON.stringify(value) ?? typeof value;
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import {
|
||||
} from '@nestjs/common';
|
||||
import { Reflector } from '@nestjs/core';
|
||||
import type { Request } from 'express';
|
||||
import { CommunityRole, UserDocument } from '../global/database/schema';
|
||||
import type { CommunityRole } from '../global/database/schema';
|
||||
import { ROLES_KEY } from './roles.decorator';
|
||||
import './auth-request';
|
||||
|
||||
@@ -23,7 +23,7 @@ export class RolesGuard implements CanActivate {
|
||||
if (!requiredRoles || requiredRoles.length === 0) return true;
|
||||
|
||||
const req = context.switchToHttp().getRequest<Request>();
|
||||
const user = req.user as UserDocument | undefined;
|
||||
const user = req.user;
|
||||
if (!user && req.identity) {
|
||||
throw new ForbiddenException('No membership in this community.');
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user