@@ -6,6 +6,7 @@ import debug from 'debug';
66import https from 'https' ;
77import http from 'http' ;
88import superagent from 'superagent' ;
9+ import { BitGo , BitGoOptions } from 'bitgo' ;
910
1011import { MasterExpressConfig , config , isMasterExpressConfig } from './config' ;
1112import {
@@ -19,8 +20,22 @@ import {
1920 readCertificates ,
2021 setupHealthCheckRoutes ,
2122} from './shared/appUtils' ;
23+ import { ApiResponseError } from './errors' ;
24+ import bodyParser from 'body-parser' ;
25+ import { ProxyAgent } from 'proxy-agent' ;
26+ import { promiseWrapper } from './routes' ;
27+ import pjson from '../package.json' ;
28+ import { createEnclavedExpressClient } from './masterBitgoExpress/enclavedExpressClient' ;
2229
2330const debugLogger = debug ( 'master-express:express' ) ;
31+ const { version } = require ( 'bitgo/package.json' ) ;
32+ const BITGOEXPRESS_USER_AGENT = `BitGoExpress/${ pjson . version } BitGoJS/${ version } ` ;
33+
34+ // Add this interface before the startup function
35+ interface BitGoRequest extends express . Request {
36+ bitgo : BitGo ;
37+ config : MasterExpressConfig ;
38+ }
2439
2540/**
2641 * Create a startup function which will be run upon server initialization
@@ -43,6 +58,75 @@ function isSSL(config: MasterExpressConfig): boolean {
4358 return Boolean ( ( keyPath && crtPath ) || ( sslKey && sslCert ) ) ;
4459}
4560
61+ /**
62+ *
63+ * @param status
64+ * @param result
65+ * @param message
66+ */
67+ function apiResponse ( status : number , result : any , message : string ) : ApiResponseError {
68+ return new ApiResponseError ( message , status , result ) ;
69+ }
70+
71+ const expressJSONParser = bodyParser . json ( { limit : '20mb' } ) ;
72+
73+ /**
74+ * Perform body parsing here only on routes we want
75+ */
76+ function parseBody ( req : express . Request , res : express . Response , next : express . NextFunction ) {
77+ // Set the default Content-Type, in case the client doesn't set it. If
78+ // Content-Type isn't specified, Express silently refuses to parse the
79+ // request body.
80+ req . headers [ 'content-type' ] = req . headers [ 'content-type' ] || 'application/json' ;
81+ return expressJSONParser ( req , res , next ) ;
82+ }
83+
84+ /**
85+ * Create the bitgo object in the request
86+ * @param config
87+ */
88+ function prepareBitGo ( config : MasterExpressConfig ) {
89+ const { env, customRootUri } = config ;
90+
91+ return function prepBitGo (
92+ req : express . Request ,
93+ res : express . Response ,
94+ next : express . NextFunction ,
95+ ) {
96+ // Get access token
97+ let accessToken ;
98+ if ( req . headers . authorization ) {
99+ const authSplit = req . headers . authorization . split ( ' ' ) ;
100+ if ( authSplit . length === 2 && authSplit [ 0 ] . toLowerCase ( ) === 'bearer' ) {
101+ accessToken = authSplit [ 1 ] ;
102+ }
103+ }
104+ const userAgent = req . headers [ 'user-agent' ]
105+ ? BITGOEXPRESS_USER_AGENT + ' ' + req . headers [ 'user-agent' ]
106+ : BITGOEXPRESS_USER_AGENT ;
107+
108+ const useProxyUrl = process . env . BITGO_USE_PROXY ;
109+ const bitgoConstructorParams : BitGoOptions = {
110+ env,
111+ customRootURI : customRootUri ,
112+ accessToken,
113+ userAgent,
114+ ...( useProxyUrl
115+ ? {
116+ customProxyAgent : new ProxyAgent ( {
117+ getProxyForUrl : ( ) => useProxyUrl ,
118+ } ) ,
119+ }
120+ : { } ) ,
121+ } ;
122+
123+ ( req as BitGoRequest ) . bitgo = new BitGo ( bitgoConstructorParams ) ;
124+ ( req as BitGoRequest ) . config = config ;
125+
126+ next ( ) ;
127+ } ;
128+ }
129+
46130async function createHttpsServer (
47131 app : express . Application ,
48132 config : MasterExpressConfig ,
@@ -98,7 +182,6 @@ function setupMasterExpressRoutes(app: express.Application): void {
98182 // Add enclaved express ping route
99183 app . get ( '/ping/enclavedExpress' , async ( req , res ) => {
100184 const cfg = config ( ) as MasterExpressConfig ;
101-
102185 try {
103186 console . log ( 'Pinging enclaved express' ) ;
104187 console . log ( 'SSL Enabled:' , cfg . enableSSL ) ;
0 commit comments