@@ -27,7 +27,7 @@ import passport from 'passport';
27
27
import environmentController from './controllers/environment.controller.js' ;
28
28
import accountController from './controllers/account.controller.js' ;
29
29
import type { Response , Request } from 'express' ;
30
- import { isCloud , isEnterprise , flagHasAuth , isBasicAuthEnabled , isTest , flagHasManagedAuth } from '@nangohq/utils' ;
30
+ import { isCloud , isEnterprise , isBasicAuthEnabled , isTest , isLocal , basePublicUrl , baseUrl , flagHasAuth , flagHasManagedAuth } from '@nangohq/utils' ;
31
31
import { errorManager } from '@nangohq/shared' ;
32
32
import tracer from 'dd-trace' ;
33
33
import { getConnection as getConnectionWeb } from './controllers/v1/connection/get.js' ;
@@ -66,12 +66,15 @@ import { patchUser } from './controllers/v1/user/patchUser.js';
66
66
import { getInvite } from './controllers/v1/invite/getInvite.js' ;
67
67
import { declineInvite } from './controllers/v1/invite/declineInvite.js' ;
68
68
import { acceptInvite } from './controllers/v1/invite/acceptInvite.js' ;
69
+ import { securityMiddlewares } from './middleware/security.js' ;
69
70
import { getMeta } from './controllers/v1/meta/getMeta.js' ;
70
71
import { postManagedSignup } from './controllers/v1/account/managed/postSignup.js' ;
71
72
import { getManagedCallback } from './controllers/v1/account/managed/getCallback.js' ;
72
73
73
74
export const router = express . Router ( ) ;
74
75
76
+ router . use ( ...securityMiddlewares ( ) ) ;
77
+
75
78
const apiAuth = [ authMiddleware . secretKeyAuth . bind ( authMiddleware ) , rateLimiterMiddleware ] ;
76
79
const adminAuth = [ authMiddleware . secretKeyAuth . bind ( authMiddleware ) , authMiddleware . adminKeyAuth . bind ( authMiddleware ) , rateLimiterMiddleware ] ;
77
80
const apiPublicAuth = [ authMiddleware . publicKeyAuth . bind ( authMiddleware ) , authCheck , rateLimiterMiddleware ] ;
@@ -95,71 +98,95 @@ router.use(
95
98
} )
96
99
) ;
97
100
router . use ( bodyParser . raw ( { type : 'text/xml' } ) ) ;
98
- router . use ( cors ( ) ) ;
99
101
router . use ( express . urlencoded ( { extended : true } ) ) ;
100
102
101
103
const upload = multer ( { storage : multer . memoryStorage ( ) } ) ;
102
104
105
+ // -------
103
106
// API routes (no/public auth).
104
107
router . get ( '/health' , ( _ , res ) => {
105
108
res . status ( 200 ) . send ( { result : 'ok' } ) ;
106
109
} ) ;
107
110
108
- router . route ( '/oauth/callback' ) . get ( oauthController . oauthCallback . bind ( oauthController ) ) ;
109
- router . route ( '/webhook/:environmentUuid/:providerConfigKey' ) . post ( webhookController . receive . bind ( proxyController ) ) ;
110
- router . route ( '/app-auth/connect' ) . get ( appAuthController . connect . bind ( appAuthController ) ) ;
111
- router . route ( '/oauth/connect/:providerConfigKey' ) . get ( apiPublicAuth , oauthController . oauthRequest . bind ( oauthController ) ) ;
112
- router . route ( '/oauth2/auth/:providerConfigKey' ) . post ( apiPublicAuth , oauthController . oauth2RequestCC . bind ( oauthController ) ) ;
113
- router . route ( '/api-auth/api-key/:providerConfigKey' ) . post ( apiPublicAuth , apiAuthController . apiKey . bind ( apiAuthController ) ) ;
114
- router . route ( '/api-auth/basic/:providerConfigKey' ) . post ( apiPublicAuth , apiAuthController . basic . bind ( apiAuthController ) ) ;
115
- router . route ( '/app-store-auth/:providerConfigKey' ) . post ( apiPublicAuth , appStoreAuthController . auth . bind ( appStoreAuthController ) ) ;
116
- router . route ( '/auth/tba/:providerConfigKey' ) . post ( apiPublicAuth , tbaAuthorization ) ;
117
- router . route ( '/unauth/:providerConfigKey' ) . post ( apiPublicAuth , unAuthController . create . bind ( unAuthController ) ) ;
111
+ // -------
112
+ // Public API routes
113
+ const publicAPI = express . Router ( ) ;
114
+ const publicAPICorsHandler = cors ( {
115
+ maxAge : 600 ,
116
+ exposedHeaders : 'Authorization, Etag, Content-Type, Content-Length, X-Nango-Signature, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset' ,
117
+ allowedHeaders : 'Nango-Activity-Log-Id, Nango-Is-Dry-Run, Nango-Is-Sync, Provider-Config-Key, Connection-Id' ,
118
+ origin : '*'
119
+ } ) ;
120
+ publicAPI . use ( publicAPICorsHandler ) ;
121
+ publicAPI . options ( '*' , publicAPICorsHandler ) ; // Pre-flight
122
+
123
+ publicAPI . route ( '/oauth/callback' ) . get ( oauthController . oauthCallback . bind ( oauthController ) ) ;
124
+ publicAPI . route ( '/webhook/:environmentUuid/:providerConfigKey' ) . post ( webhookController . receive . bind ( proxyController ) ) ;
125
+ publicAPI . route ( '/app-auth/connect' ) . get ( appAuthController . connect . bind ( appAuthController ) ) ;
126
+ publicAPI . route ( '/oauth/connect/:providerConfigKey' ) . get ( apiPublicAuth , oauthController . oauthRequest . bind ( oauthController ) ) ;
127
+ publicAPI . route ( '/oauth2/auth/:providerConfigKey' ) . post ( apiPublicAuth , oauthController . oauth2RequestCC . bind ( oauthController ) ) ;
128
+ publicAPI . route ( '/api-auth/api-key/:providerConfigKey' ) . post ( apiPublicAuth , apiAuthController . apiKey . bind ( apiAuthController ) ) ;
129
+ publicAPI . route ( '/api-auth/basic/:providerConfigKey' ) . post ( apiPublicAuth , apiAuthController . basic . bind ( apiAuthController ) ) ;
130
+ publicAPI . route ( '/app-store-auth/:providerConfigKey' ) . post ( apiPublicAuth , appStoreAuthController . auth . bind ( appStoreAuthController ) ) ;
131
+ publicAPI . route ( '/auth/tba/:providerConfigKey' ) . post ( apiPublicAuth , tbaAuthorization ) ;
132
+ publicAPI . route ( '/unauth/:providerConfigKey' ) . post ( apiPublicAuth , unAuthController . create . bind ( unAuthController ) ) ;
118
133
119
134
// API Admin routes
120
- router . route ( '/admin/flow/deploy/pre-built' ) . post ( adminAuth , flowController . adminDeployPrivateFlow . bind ( flowController ) ) ;
121
- router . route ( '/admin/customer' ) . patch ( adminAuth , accountController . editCustomer . bind ( accountController ) ) ;
135
+ publicAPI . route ( '/admin/flow/deploy/pre-built' ) . post ( adminAuth , flowController . adminDeployPrivateFlow . bind ( flowController ) ) ;
136
+ publicAPI . route ( '/admin/customer' ) . patch ( adminAuth , accountController . editCustomer . bind ( accountController ) ) ;
122
137
123
138
// API routes (API key auth).
124
- router . route ( '/provider' ) . get ( apiAuth , providerController . listProviders . bind ( providerController ) ) ;
125
- router . route ( '/provider/:provider' ) . get ( apiAuth , providerController . getProvider . bind ( providerController ) ) ;
126
- router . route ( '/config' ) . get ( apiAuth , configController . listProviderConfigs . bind ( configController ) ) ;
127
- router . route ( '/config/:providerConfigKey' ) . get ( apiAuth , configController . getProviderConfig . bind ( configController ) ) ;
128
- router . route ( '/config' ) . post ( apiAuth , configController . createProviderConfig . bind ( configController ) ) ;
129
- router . route ( '/config' ) . put ( apiAuth , configController . editProviderConfig . bind ( configController ) ) ;
130
- router . route ( '/config/:providerConfigKey' ) . delete ( apiAuth , configController . deleteProviderConfig . bind ( configController ) ) ;
131
- router . route ( '/connection/:connectionId' ) . get ( apiAuth , connectionController . getConnectionCreds . bind ( connectionController ) ) ;
132
- router . route ( '/connection' ) . get ( apiAuth , connectionController . listConnections . bind ( connectionController ) ) ;
133
- router . route ( '/connection/:connectionId' ) . delete ( apiAuth , connectionController . deleteConnection . bind ( connectionController ) ) ;
134
- router . route ( '/connection/:connectionId/metadata' ) . post ( apiAuth , connectionController . setMetadataLegacy . bind ( connectionController ) ) ;
135
- router . route ( '/connection/:connectionId/metadata' ) . patch ( apiAuth , connectionController . updateMetadataLegacy . bind ( connectionController ) ) ;
136
- router . route ( '/connection/metadata' ) . post ( apiAuth , setMetadata ) ;
137
- router . route ( '/connection/metadata' ) . patch ( apiAuth , updateMetadata ) ;
138
- router . route ( '/connection' ) . post ( apiAuth , connectionController . createConnection . bind ( connectionController ) ) ;
139
- router . route ( '/environment-variables' ) . get ( apiAuth , environmentController . getEnvironmentVariables . bind ( connectionController ) ) ;
140
- router . route ( '/sync/deploy' ) . post ( apiAuth , postDeploy ) ;
141
- router . route ( '/sync/deploy/confirmation' ) . post ( apiAuth , postDeployConfirmation ) ;
142
- router . route ( '/sync/update-connection-frequency' ) . put ( apiAuth , syncController . updateFrequencyForConnection . bind ( syncController ) ) ;
143
- router . route ( '/records' ) . get ( apiAuth , syncController . getAllRecords . bind ( syncController ) ) ;
144
- router . route ( '/sync/trigger' ) . post ( apiAuth , syncController . trigger . bind ( syncController ) ) ;
145
- router . route ( '/sync/pause' ) . post ( apiAuth , syncController . pause . bind ( syncController ) ) ;
146
- router . route ( '/sync/start' ) . post ( apiAuth , syncController . start . bind ( syncController ) ) ;
147
- router . route ( '/sync/provider' ) . get ( apiAuth , syncController . getSyncProvider . bind ( syncController ) ) ;
148
- router . route ( '/sync/status' ) . get ( apiAuth , syncController . getSyncStatus . bind ( syncController ) ) ;
149
- router . route ( '/sync/:syncId' ) . delete ( apiAuth , syncController . deleteSync . bind ( syncController ) ) ;
150
- router . route ( '/flow/attributes' ) . get ( apiAuth , syncController . getFlowAttributes . bind ( syncController ) ) ;
151
- router . route ( '/flow/configs' ) . get ( apiAuth , flowController . getFlowConfig . bind ( flowController ) ) ;
152
- router . route ( '/scripts/config' ) . get ( apiAuth , flowController . getFlowConfig . bind ( flowController ) ) ;
153
- router . route ( '/action/trigger' ) . post ( apiAuth , syncController . triggerAction . bind ( syncController ) ) ; //TODO: to deprecate
154
-
155
- router . route ( '/v1/*' ) . all ( apiAuth , syncController . actionOrModel . bind ( syncController ) ) ;
156
-
157
- router . route ( '/proxy/*' ) . all ( apiAuth , upload . any ( ) , proxyController . routeCall . bind ( proxyController ) ) ;
139
+ publicAPI . route ( '/provider' ) . get ( apiAuth , providerController . listProviders . bind ( providerController ) ) ;
140
+ publicAPI . route ( '/provider/:provider' ) . get ( apiAuth , providerController . getProvider . bind ( providerController ) ) ;
141
+ publicAPI . route ( '/config' ) . get ( apiAuth , configController . listProviderConfigs . bind ( configController ) ) ;
142
+ publicAPI . route ( '/config/:providerConfigKey' ) . get ( apiAuth , configController . getProviderConfig . bind ( configController ) ) ;
143
+ publicAPI . route ( '/config' ) . post ( apiAuth , configController . createProviderConfig . bind ( configController ) ) ;
144
+ publicAPI . route ( '/config' ) . put ( apiAuth , configController . editProviderConfig . bind ( configController ) ) ;
145
+ publicAPI . route ( '/config/:providerConfigKey' ) . delete ( apiAuth , configController . deleteProviderConfig . bind ( configController ) ) ;
146
+ publicAPI . route ( '/connection/:connectionId' ) . get ( apiAuth , connectionController . getConnectionCreds . bind ( connectionController ) ) ;
147
+ publicAPI . route ( '/connection' ) . get ( apiAuth , connectionController . listConnections . bind ( connectionController ) ) ;
148
+ publicAPI . route ( '/connection/:connectionId' ) . delete ( apiAuth , connectionController . deleteConnection . bind ( connectionController ) ) ;
149
+ publicAPI . route ( '/connection/:connectionId/metadata' ) . post ( apiAuth , connectionController . setMetadataLegacy . bind ( connectionController ) ) ;
150
+ publicAPI . route ( '/connection/:connectionId/metadata' ) . patch ( apiAuth , connectionController . updateMetadataLegacy . bind ( connectionController ) ) ;
151
+ publicAPI . route ( '/connection/metadata' ) . post ( apiAuth , setMetadata ) ;
152
+ publicAPI . route ( '/connection/metadata' ) . patch ( apiAuth , updateMetadata ) ;
153
+ publicAPI . route ( '/connection' ) . post ( apiAuth , connectionController . createConnection . bind ( connectionController ) ) ;
154
+ publicAPI . route ( '/environment-variables' ) . get ( apiAuth , environmentController . getEnvironmentVariables . bind ( connectionController ) ) ;
155
+ publicAPI . route ( '/sync/deploy' ) . post ( apiAuth , postDeploy ) ;
156
+ publicAPI . route ( '/sync/deploy/confirmation' ) . post ( apiAuth , postDeployConfirmation ) ;
157
+ publicAPI . route ( '/sync/update-connection-frequency' ) . put ( apiAuth , syncController . updateFrequencyForConnection . bind ( syncController ) ) ;
158
+ publicAPI . route ( '/records' ) . get ( apiAuth , syncController . getAllRecords . bind ( syncController ) ) ;
159
+ publicAPI . route ( '/sync/trigger' ) . post ( apiAuth , syncController . trigger . bind ( syncController ) ) ;
160
+ publicAPI . route ( '/sync/pause' ) . post ( apiAuth , syncController . pause . bind ( syncController ) ) ;
161
+ publicAPI . route ( '/sync/start' ) . post ( apiAuth , syncController . start . bind ( syncController ) ) ;
162
+ publicAPI . route ( '/sync/provider' ) . get ( apiAuth , syncController . getSyncProvider . bind ( syncController ) ) ;
163
+ publicAPI . route ( '/sync/status' ) . get ( apiAuth , syncController . getSyncStatus . bind ( syncController ) ) ;
164
+ publicAPI . route ( '/sync/:syncId' ) . delete ( apiAuth , syncController . deleteSync . bind ( syncController ) ) ;
165
+ publicAPI . route ( '/flow/attributes' ) . get ( apiAuth , syncController . getFlowAttributes . bind ( syncController ) ) ;
166
+ publicAPI . route ( '/flow/configs' ) . get ( apiAuth , flowController . getFlowConfig . bind ( flowController ) ) ;
167
+ publicAPI . route ( '/scripts/config' ) . get ( apiAuth , flowController . getFlowConfig . bind ( flowController ) ) ;
168
+ publicAPI . route ( '/action/trigger' ) . post ( apiAuth , syncController . triggerAction . bind ( syncController ) ) ; //TODO: to deprecate
169
+
170
+ publicAPI . route ( '/v1/*' ) . all ( apiAuth , syncController . actionOrModel . bind ( syncController ) ) ;
158
171
172
+ publicAPI . route ( '/proxy/*' ) . all ( apiAuth , upload . any ( ) , proxyController . routeCall . bind ( proxyController ) ) ;
173
+
174
+ router . use ( publicAPI ) ;
175
+
176
+ // -------
159
177
// Webapp routes (session auth).
160
178
const web = express . Router ( ) ;
161
179
setupAuth ( web ) ;
162
180
181
+ const webCorsHandler = cors ( {
182
+ maxAge : 600 ,
183
+ exposedHeaders : 'Authorization, Etag, Content-Type, Content-Length, Set-Cookie' ,
184
+ origin : isLocal ? '*' : [ basePublicUrl , baseUrl ] ,
185
+ credentials : true
186
+ } ) ;
187
+ web . use ( webCorsHandler ) ;
188
+ web . options ( '*' , webCorsHandler ) ; // Pre-flight
189
+
163
190
// Webapp routes (no auth).
164
191
if ( flagHasAuth ) {
165
192
web . route ( '/api/v1/account/signup' ) . post ( rateLimiterMiddleware , signup ) ;
0 commit comments