-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IMN-879 - Refactoring headers parsing and improving auth (#1079)
- Loading branch information
1 parent
9f4f333
commit e178c01
Showing
8 changed files
with
114 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,40 @@ | ||
import { Request } from "express"; | ||
import { P, match } from "ts-pattern"; | ||
import { badBearerToken, missingHeader } from "pagopa-interop-models"; | ||
import { z } from "zod"; | ||
import { genericLogger } from "../logging/index.js"; | ||
import { AuthData } from "./authData.js"; | ||
import { readAuthDataFromJwtToken } from "./jwt.js"; | ||
import { Logger } from "../logging/index.js"; | ||
|
||
export const Headers = z.object({ | ||
authorization: z.string().nullish(), | ||
"x-correlation-id": z.string().nullish(), | ||
}); | ||
export function parseCorrelationIdHeader(req: Request): string | undefined { | ||
const parsed = z | ||
.object({ "x-correlation-id": z.string() }) | ||
.safeParse(req.headers); | ||
|
||
export type Headers = z.infer<typeof Headers>; | ||
|
||
export const ParsedHeaders = z | ||
.object({ | ||
correlationId: z.string(), | ||
}) | ||
.and(AuthData); | ||
export type ParsedHeaders = z.infer<typeof ParsedHeaders>; | ||
|
||
export const readCorrelationIdHeader = (req: Request): string | undefined => | ||
match(req.headers) | ||
.with( | ||
{ "x-correlation-id": P.string }, | ||
(headers) => headers["x-correlation-id"] | ||
) | ||
.otherwise(() => undefined); | ||
if (parsed.success) { | ||
return parsed.data["x-correlation-id"]; | ||
} | ||
return undefined; | ||
} | ||
export function parseAuthHeader(req: Request): string | undefined { | ||
const parsed = z.object({ authorization: z.string() }).safeParse(req.headers); | ||
|
||
export const readHeaders = (req: Request): ParsedHeaders | undefined => { | ||
try { | ||
const headers = Headers.parse(req.headers); | ||
return match(headers) | ||
.with( | ||
{ | ||
authorization: P.string, | ||
"x-correlation-id": P.string, | ||
}, | ||
(headers) => { | ||
const authorizationHeader = headers.authorization.split(" "); | ||
if ( | ||
authorizationHeader.length !== 2 || | ||
authorizationHeader[0] !== "Bearer" | ||
) { | ||
return undefined; | ||
} | ||
if (parsed.success) { | ||
return parsed.data.authorization; | ||
} | ||
return undefined; | ||
} | ||
|
||
const jwtToken = authorizationHeader[1]; | ||
const authData = readAuthDataFromJwtToken(jwtToken, genericLogger); | ||
export function jwtFromAuthHeader(req: Request, logger: Logger): string { | ||
const authHeader = parseAuthHeader(req); | ||
if (!authHeader) { | ||
throw missingHeader("Authorization"); | ||
} | ||
|
||
return { | ||
...authData, | ||
correlationId: headers["x-correlation-id"], | ||
}; | ||
} | ||
) | ||
.otherwise(() => undefined); | ||
} catch (error) { | ||
return undefined; | ||
const authHeaderParts = authHeader.split(" "); | ||
if (authHeaderParts.length !== 2 || authHeaderParts[0] !== "Bearer") { | ||
logger.warn( | ||
`Invalid authentication provided for this call ${req.method} ${req.url}` | ||
); | ||
throw badBearerToken; | ||
} | ||
}; | ||
|
||
return authHeaderParts[1]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.