Skip to content

Commit 399e545

Browse files
committed
support external jwt claims to be passed downsrream
1 parent 0687d9a commit 399e545

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

extauth/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ extauth:
4848
exp: true # can be true or false, but defaults to true; use true to check for the expiry time and send an error if the token is expired.
4949
sendErr: true # can be either true or false, but defaults to true; set this to false if you want the extauth plugin to send an error if the JWT is invalid.
5050
keepAuthHeader: false # can be true or false; default is false; set this to true if you want to pass the Authorization header to the backend.
51+
extauth-claims-header: "header to be added with base64 encoded string of claims from authorization bearer jwt payload. Example value: x-extauth-claims" # default null for backward compatibility. When present, jwt payload claims are extracted and added as a request header of this name
52+
extauth-exclude-claims: "array of claims to be excluded from extauth-claims-header" # used only when `extauth-claims-header` is set. Example value: ['application_name', 'client_id', 'api_product_list', 'iat', 'exp']
5153
```
5254
5355
## Enable the plugin

extauth/index.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*
44
*/
55

6+
var _ = require('lodash');
67
var debug = require('debug')('plugin:extauth');
78
var request = require('request');
89
var rs = require('jsrsasign');
@@ -28,7 +29,11 @@ module.exports.init = function(config, logger, stats) {
2829
var sendErr = config.hasOwnProperty("sendErr") ? config.sendErr : true;
2930
//preserve or delete the auth header
3031
var keepAuthHeader = config.hasOwnProperty('keep-authorization-header') ? config['keep-authorization-header'] : false;
31-
32+
//extracts jwt claims from header authorization bearer jwt and adds them in a new header x-extauth-claims (default null for backward compatibility)
33+
var extauthClaimsHeader = config.hasOwnProperty('extauth-claims-header') ? config['extauth-claims-header'] : null;
34+
//sensitive claims to be omitted from extauth claims header, if enabled
35+
var PRIVATE_JWT_VALUES = config.hasOwnProperty('extauth-exclude-claims') ? config['extauth-exclude-claims'] : ['application_name', 'client_id', 'api_product_list', 'iat', 'exp'];
36+
3237
if (iss) {
3338
debug("Issuer " + iss);
3439
acceptField.iss = [];
@@ -107,6 +112,10 @@ module.exports.init = function(config, logger, stats) {
107112
debug("key type is PEM");
108113
isValid = validateJWT(publickeys, jwtpayload[1], exp);
109114
if (isValid) {
115+
if(extauthClaimsHeader) {
116+
var authClaims = _.omit(jwtdecode, PRIVATE_JWT_VALUES);
117+
req.headers[extauthClaimsHeader] = new Buffer(JSON.stringify(authClaims)).toString('base64');
118+
}
110119
if (!keepAuthHeader) {
111120
delete(req.headers['authorization']);
112121
}
@@ -145,6 +154,10 @@ module.exports.init = function(config, logger, stats) {
145154
isValid = validateJWT(pem, jwtpayload[1], exp);
146155
if (isValid) {
147156
debug("JWT is valid");
157+
if(extauthClaimsHeader) {
158+
var authClaims = _.omit(jwtdecode.payloadObj, PRIVATE_JWT_VALUES);
159+
req.headers[extauthClaimsHeader] = new Buffer(JSON.stringify(authClaims)).toString('base64');
160+
}
148161
if (!keepAuthHeader) {
149162
delete(req.headers['authorization']);
150163
}

0 commit comments

Comments
 (0)