Skip to content

Commit

Permalink
feat: update prxi dependency, minor tweak to the jwt claims extractio…
Browse files Browse the repository at this point in the history
…n logic
  • Loading branch information
vlad-tkachenko committed Nov 29, 2023
1 parent 4f4f8e5 commit 545d40f
Show file tree
Hide file tree
Showing 5 changed files with 575 additions and 490 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"node-graceful-shutdown": "^1.1.5",
"openid-client": "^5.4.3",
"pino": "^8.14.1",
"prxi": "^0.4.0"
"prxi": "^0.4.1"
},
"devDependencies": {
"@testdeck/mocha": "^0.3.3",
Expand Down
46 changes: 31 additions & 15 deletions src/config/Mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { HttpMethod } from "prxi";
export interface Mapping {
pattern: RegExp;
methods?: HttpMethod[];
exclude: {
pattern: RegExp;
methods?: HttpMethod[];
}[];
auth: {
required: boolean,
claims: Record<string, string[]>;
Expand Down Expand Up @@ -37,28 +41,34 @@ export const prepareMappings = (value: string): Mapping[] => {
return result;
}

/**
* Prepare single mapping
* @param value
* @param requireClaims
* @returns
*/
export const prepareMapping = (value: any): Mapping => {
let rawPattern = value.pattern;
if (!value.pattern) {
const preparePattern = (value: {pattern?: string}): RegExp => {
let { pattern } = value;
if (!pattern) {
throw new Error(`Unable to parse mappings for value: ${JSON.stringify(value)}`);
}

// add leading ^ character if missing to the pattern
if (value.pattern.indexOf('^') !== 0) {
value.pattern = '^' + value.pattern;
if (pattern.indexOf('^') !== 0) {
pattern = '^' + pattern;
}

// add trailing $ character if missing to the pattern
if (!value.pattern.endsWith('$')) {
value.pattern = value.pattern + '$';
if (!pattern.endsWith('$')) {
pattern = pattern + '$';
}

return new RegExp(pattern, 'i');
}

/**
* Prepare single mapping
* @param value
* @param requireClaims
* @returns
*/
export const prepareMapping = (value: any): Mapping => {
const pattern = preparePattern(value);

if (!value.auth) {
value.auth = {
required: false,
Expand All @@ -74,14 +84,20 @@ export const prepareMapping = (value: any): Mapping => {
// if no claims set, set default object
if (!value.auth.claims || JSON.stringify(value.auth.claims) === '{}') {
if (value.auth.required) {
throw new Error(`Invalid mapping provided for pattern: ${rawPattern}, configuration will cause rejection of all requests. Either provide auth.claims or set auth.required flag to false`);
throw new Error(`Invalid mapping provided for pattern: ${value.pattern}, configuration will cause rejection of all requests. Either provide auth.claims or set auth.required flag to false`);
}
value.auth.claims = {};
}

return {
pattern: new RegExp(value.pattern, 'i'),
pattern,
methods: value.methods?.map((m: string) => m.toUpperCase()),
auth: value.auth,
exclude: ([] || value.exclude).map((e: any) => {
return {
pattern: preparePattern(e),
methods: e.methods?.map((m: string) => m.toUpperCase()),
}
})
}
}
15 changes: 13 additions & 2 deletions src/handlers/ProxyHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export class ProxyHandler implements RequestHandlerConfig {
const outgoingCookieHeader = outgoingHeaders[setCookieName];
if (!outgoingCookieHeader) {
// when no need to merge cookies
outgoingHeaders[setCookieName] = setCookieHeader;
outgoingHeaders[setCookieName] = <string | string[]> setCookieHeader;
} else {
// merge cookies
const cookies: Array<string> = [];
Expand Down Expand Up @@ -290,7 +290,18 @@ export class ProxyHandler implements RequestHandlerConfig {
for (const mapping of mappings) {
const matchMethod = !mapping.methods || mapping.methods.find(m => m === method);
if (matchMethod && mapping.pattern.exec(path)) {
return mapping;
let exclude = false;
for (const excludeMapping of mapping.exclude) {
const excludeMethodMatch = !mapping.methods || mapping.methods.find(m => m === method);
exclude = excludeMethodMatch && !!excludeMapping.pattern.exec(path);
if (exclude) {
continue;
}
}

if (!exclude) {
return mapping;
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/utils/RequestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,11 @@ export class RequestUtils {
}

if (!fail) {
claims.push(...target);
if (Array.isArray(target)) {
claims.push(...target);
} else {
claims.push(target);
}
}
}
}
Expand Down
Loading

0 comments on commit 545d40f

Please sign in to comment.