Skip to content

Commit 140a80b

Browse files
Implement auth using admin JWT token (#398)
* Implement auth using admin jwt token * address comments * fix undefined * add auth pkg * done * address comments * fix typo * fix typo
1 parent 789c1fb commit 140a80b

File tree

4 files changed

+109
-1
lines changed

4 files changed

+109
-1
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ Set these environment variables if you need to change their defaults
1919
| CADENCE_TCHANNEL_PEERS | Comma-delmited list of tchannel peers | 127.0.0.1:7933 |
2020
| CADENCE_TCHANNEL_SERVICE | Name of the cadence tchannel service to call | cadence-frontend |
2121
| CADENCE_WEB_PORT | HTTP port to serve on | 8088 |
22-
| CADENCE_EXTERNAL_SCRIPTS | Addtional JavaScript tags to serve in the UI | |
22+
| CADENCE_EXTERNAL_SCRIPTS | Addtional JavaScript tags to serve in the UI | |
23+
| ENABLE_AUTH | Enable auth feature | false |
24+
| AUTH_TYPE | concurrently supports ADMIN_JWT | '' |
25+
| AUTH_ADMIN_JWT_PRIVATE_KEY | JWT signing private key for ADMIN_JWT type | '' |
2326

2427
### Running locally
2528

package-lock.json

Lines changed: 84 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"html-webpack-plugin": "^2.30.1",
5151
"html-webpack-template": "^6.1.0",
5252
"is-ipv4-node": "^1.0.6",
53+
"jsonwebtoken": "^8.5.1",
5354
"koa": "^2.3.0",
5455
"koa-better-error-handler": "^1.3.0",
5556
"koa-bodyparser": "^4.2.0",

server/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const koaSend = require('koa-send');
2828
const koaStatic = require('koa-static');
2929
const koaWebpack = require('koa-webpack');
3030
const webpack = require('webpack');
31+
const jwt = require('jsonwebtoken');
3132

3233
const webpackConfig = require('../webpack.config');
3334
const tchannelClient = require('./middleware/tchannel-client');
@@ -54,6 +55,9 @@ app.init = function({
5455
serviceName = process.env.CADENCE_TCHANNEL_SERVICE || SERVICE_NAME_DEFAULT,
5556
timeout = REQUEST_TIMEOUT_DEFAULT,
5657
useWebpack = process.env.NODE_ENV !== 'production',
58+
enableAuth = process.env.ENABLE_AUTH === 'true',
59+
authType = process.env.AUTH_TYPE,
60+
authAdminJwtPrivateKey = process.env.AUTH_ADMIN_JWT_PRIVATE_KEY,
5761
} = {}) {
5862
const requestConfig = {
5963
retryFlags,
@@ -94,6 +98,22 @@ app.init = function({
9498
filter: contentType => !contentType.startsWith('text/event-stream'),
9599
})
96100
)
101+
.use(async function(ctx, next) {
102+
if (enableAuth && authType === 'ADMIN_JWT' && authAdminJwtPrivateKey) {
103+
ctx.authTokenHeaders = ctx.authTokenHeaders || {};
104+
const token = jwt.sign(
105+
{ admin: true, ttl: 10 },
106+
authAdminJwtPrivateKey,
107+
{
108+
algorithm: 'RS256',
109+
}
110+
);
111+
112+
ctx.authTokenHeaders['cadence-authorization'] = token;
113+
}
114+
115+
await next();
116+
})
97117
.use(tchannelClient({ peers, requestConfig }))
98118
.use(
99119
useWebpack

0 commit comments

Comments
 (0)