Skip to content

Commit

Permalink
verifyJwtToken accept configuration as parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Viktor-K committed Oct 1, 2024
1 parent bab7433 commit 1fbd47e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ import {
USER_ROLES,
WithLogger,
decodeJwtToken,
getJwksClients,
userRoles,
verifyJwtToken,
getJwksClient,
} from "pagopa-interop-commons";
import { TenantId, genericError, unsafeBrandId } from "pagopa-interop-models";
import { PagoPAInteropBeClients } from "../clients/clientsProvider.js";
import { config } from "../config/config.js";
import {
missingClaim,
missingSelfcareId,
tenantLoginNotAllowed,
tokenVerificationFailed,
} from "../model/errors.js";
import { PagoPAInteropBeClients } from "../clients/clientsProvider.js";
import { validateSamlResponse } from "../utilities/samlValidator.js";
import { BffAppContext } from "../utilities/context.js";
import { validateSamlResponse } from "../utilities/samlValidator.js";

const SUPPORT_USER_ID = "5119b1fa-825a-4297-8c9c-152e055cabca";

Expand All @@ -54,7 +54,7 @@ export function authorizationServiceBuilder(
allowList: string[],
rateLimiter: RateLimiter
) {
const jwksClients = getJwksClient(config);
const jwksClients = getJwksClients(config);

const readJwt = async (
identityToken: string,
Expand All @@ -64,7 +64,7 @@ export function authorizationServiceBuilder(
sessionClaims: SessionClaims;
selfcareId: string;
}> => {
const verified = await verifyJwtToken(identityToken, jwksClients, logger);
const verified = await verifyJwtToken(identityToken, jwksClients, config, logger);

Check failure on line 67 in packages/backend-for-frontend/src/services/authorizationService.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `identityToken,·jwksClients,·config,·logger` with `⏎······identityToken,⏎······jwksClients,⏎······config,⏎······logger⏎····`
if (!verified) {
throw tokenVerificationFailed();
}
Expand Down
8 changes: 4 additions & 4 deletions packages/commons/src/auth/authenticationMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
missingHeader,
unauthorizedError,
} from "pagopa-interop-models";
import { P, match } from "ts-pattern";
import { ExpressContext, getJwksClient, JWTConfig } from "../index.js";
import { match, P } from "ts-pattern";
import { ExpressContext, getJwksClients, JWTConfig } from "../index.js";
import { Logger, logger } from "../logging/index.js";
import { AuthData } from "./authData.js";
import { Headers } from "./headers.js";
Expand All @@ -20,7 +20,7 @@ export const authenticationMiddleware: (
) => ZodiosRouterContextRequestHandler<ExpressContext> =
(config: JWTConfig) =>
async (req, res, next): Promise<unknown> => {
const jwksClients = getJwksClient(config);
const jwksClients = getJwksClients(config);

const addCtxAuthData = async (
authHeader: string,
Expand All @@ -38,7 +38,7 @@ export const authenticationMiddleware: (
}

const jwtToken = authorizationHeader[1];
const valid = await verifyJwtToken(jwtToken, jwksClients, logger);
const valid = await verifyJwtToken(jwtToken, jwksClients, config, logger);
if (!valid) {
throw unauthorizedError("Invalid token");
}
Expand Down
4 changes: 2 additions & 2 deletions packages/commons/src/auth/jwk.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import crypto, { JsonWebKey, KeyObject } from "crypto";
import jwksClient, { JwksClient } from "jwks-rsa";
import {
invalidKey,
jwkDecodingError,
notAllowedCertificateException,
notAllowedPrivateKeyException,
} from "pagopa-interop-models";
import jwksClient, { JwksClient } from "jwks-rsa";
import { JWTConfig } from "../config/index.js";

export const decodeBase64ToPem = (base64String: string): string => {
Expand Down Expand Up @@ -67,7 +67,7 @@ export function sortJWK(jwk: JsonWebKey): JsonWebKey {
);
}

export function getJwksClient(config: JWTConfig): JwksClient[] {
export function getJwksClients(config: JWTConfig): JwksClient[] {
return config.wellKnownUrls.map((url) =>
jwksClient({
cache: true,
Expand Down
3 changes: 2 additions & 1 deletion packages/commons/src/auth/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ const getKey = async (
export const verifyJwtToken = async (
jwtToken: string,
jwksClients: jwksClient.JwksClient[],
config: JWTConfig,
logger: Logger
): Promise<boolean> => {
try {
const { acceptedAudiences } = JWTConfig.parse(process.env);
const { acceptedAudiences } = config;

const jwtHeader = decodeJwtTokenHeaders(jwtToken, logger);
if (!jwtHeader?.kid) {
Expand Down

0 comments on commit 1fbd47e

Please sign in to comment.