Skip to content

Commit

Permalink
Merge pull request #15 from lawp09/feature/client-ip
Browse files Browse the repository at this point in the history
Feature/client ip
  • Loading branch information
livetocode authored Jun 5, 2024
2 parents fa88c23 + d51fa6f commit 0218c20
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@villedemontreal/jwt-validator",
"version": "5.9.2",
"version": "5.9.3",
"description": "Module to validate JWT (JSON Web Tokens)",
"main": "dist/src/index.js",
"typings": "dist/src",
Expand Down
21 changes: 21 additions & 0 deletions src/config/configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,23 @@ export class Configs {
*/
private _cacheDuration: number = constants.default.cacheDuration;

/**
* When this library is used as a dependency in a project, the source project name will be the property name in the package.json of this project
*
* @private
* @type {string}
* @memberof Configs
*/
private readonly _sourceProjectName: string;

private _loggerCreator: (name: string) => ILogger;
private _correlationIdProvider: () => string;

constructor() {
this.libRoot = path.normalize(__dirname + '/../../..');
this.isWindows = os.platform() === 'win32';
const sourcePackageJson = require(`${constants.appRoot}/package.json`);
this._sourceProjectName = sourcePackageJson?.name ? sourcePackageJson.name : '';
}

/**
Expand Down Expand Up @@ -129,6 +140,16 @@ export class Configs {
this._correlationIdProvider = correlationIdProvider;
}

/**
* Get the source project name where this library is imported
*
* @return {*} {string}
* @memberof Configs
*/
public getSourceProjectName(): string {
return this._sourceProjectName;
}

/**
* The Correlation Id provider
*/
Expand Down
17 changes: 16 additions & 1 deletion src/middleware/tokenTransformationMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { utils } from '@villedemontreal/general-utils';
import * as express from 'express';
import httpHeaderFieldsTyped from 'http-header-fields-typed';
import * as _ from 'lodash';
import { configs } from '../config/configs';
import { constants } from '../config/constants';
import { ITokenTtransformationMiddlewareConfig } from '../config/tokenTransformationMiddlewareConfig';
import { IInputAccessToken, IInputAccessTokenSource } from '../models/accessToken';
import { createInvalidJwtError } from '../models/customError';
import { createLogger } from '../utils/logger';
import superagent = require('superagent');
Expand Down Expand Up @@ -67,10 +69,23 @@ export const tokenTransformationMiddleware: (
return;
}

const source: IInputAccessTokenSource = {
url: `${req.protocol}://${req?.headers.host}${req.url}`,
method: req.method,
serviceName: configs.getSourceProjectName(),
clientIp: '10.0.0.1',
};

const inputAccessToken: IInputAccessToken = {
accessToken,
source,
extensions: config.extensions,
};

// Call the service endpoint to exchange the access token for a extended jwt
superagent
.post(config.service.uri)
.send({ accessToken, extensions: config.extensions })
.send(inputAccessToken)
.then((response) => {
const extendedJwt = response.body.jwts?.extended;
logger.debug(extendedJwt, 'Extended jwt content.');
Expand Down
35 changes: 35 additions & 0 deletions src/models/accessToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* An input access token
*/
export interface IInputAccessTokenExtensionsJwtCustomDataProvider {
uri: string;
method?: string;
options?: any;
name?: string;
useJwtInAuthHeader?: boolean;
}
export interface IInputAccessTokenExtensionsJwt {
customDataProvider: IInputAccessTokenExtensionsJwtCustomDataProvider;
}

export interface IInputAccessTokenExtensions {
jwt: IInputAccessTokenExtensionsJwt;
}

export interface IInputAccessTokenSource {
url: string;
method: string;
serviceName: string;
basicJwtCacheKey?: string; // the name of the cache key for the basic JWT, to use the same key as the Kong plugin
extendedJwtCacheKey?: string; // the name of the cache key for the extended JWT, to use the same key as the Kong plugin
extendedJwtConfigDigest?: string; // the digest that should be included in the extended JWT, to verify later that we retrieved a cached JWT matching the right config.
clientIp: string;
}

export interface IInputAccessToken {
accessToken: string;
accessTokenIssuer?: string;
source?: IInputAccessTokenSource;
jwt?: string;
extensions?: IInputAccessTokenExtensions;
}

0 comments on commit 0218c20

Please sign in to comment.