@@ -28,6 +28,7 @@ const koaSend = require('koa-send');
2828const koaStatic = require ( 'koa-static' ) ;
2929const koaWebpack = require ( 'koa-webpack' ) ;
3030const webpack = require ( 'webpack' ) ;
31+ const jwt = require ( 'jsonwebtoken' ) ;
3132
3233const webpackConfig = require ( '../webpack.config' ) ;
3334const 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