This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.js
183 lines (160 loc) · 6.28 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* App dependencies.
* Thanks to jamhul & donpark on github
*/
var express = require( 'express' )
, fs = require( 'fs' )
, hbs = require( 'hbs' )
, http = require( 'http' )
, https = require( 'https' )
, jwt = require( 'jwt-simple' )
, path = require( 'path' )
, routes = require( './routes' )
, platform = require( './routes/platform' ) // TODO: REMOVE -> Used ONLY for initial UI buildup
, soap = require( './routes/soap' )
, utils = require( './app/utils/utils' )
, blocks = {}
, endpoints = {}
, sessions = {}
// Arguments passed in from command line
var argv = []
, opts = {}
, options
;
for( var arg in process.argv ) {
if( '--' === arg.substr( 0, 2 ) ) {
var parts = arg.split( '=' );
opts[ parts[0].substr( 2 ).replace( '-', '_' ) ] = parts[1] || true;
} else {
argv.push( arg );
}
}
// Merge options from siteConf with passed in arguments
options = utils.mergeOptions( utils.getConfig(), opts );
// Define express as framework
var app = express();
// development ONLY
if( 'development' == app.get( 'env' ) ) {
console.log( 'APP IS IN DEVELOPMENT MODE' );
app.set( 'port', process.env.PORT || 3000 );
app.set( 'views', __dirname + '/views' );
app.set( 'view engine', 'hbs' );
app.set( 'APIKey', 'localNodeApp' );
app.use( express.favicon() );
app.use( express.logger('dev') );
app.use( express.bodyParser() );
app.use( express.cookieParser() );
app.use( express.session( { secret: '5458a100-1d06-11e2-892e-0800200c9a66' } ) ); // generic UUID
app.use( express.methodOverride() );
app.use( app.router );
app.use( express.static( path.join( __dirname, 'public') ) );
app.use( express.errorHandler() );
}
// production ONLY
if( 'production' == app.get( 'env' ) ) {
console.log( 'APP IS IN PRODUCTION MODE' );
app.set( 'port', process.env.PORT || 3000 );
app.set( 'views', __dirname + '/views' );
app.set( 'view engine', 'hbs' );
app.set( 'APIKey', 'nodeSampleApp' );
app.use( express.favicon() );
app.use( express.logger() );
app.use( express.bodyParser() );
app.use( express.cookieParser() );
app.use( express.session( { secret: '5458a100-1d06-11e2-892e-0800200c9a61' } ) ); // generic UUID
app.use( express.methodOverride() );
app.use( app.router );
app.use( express.static( path.join( __dirname, 'public') ) );
}
app.use( express.cookieSession() );
// Handlebars helpers
hbs.registerHelper( 'extend', function( name, context ) {
var block = blocks[name];
if( !block ) {
block = blocks[name] = [];
}
block.push( context( this ) );
});
hbs.registerHelper( 'block', function( name ) {
var val = ( blocks[name] || [] ).join( '\n' );
// clear the block
blocks[name] = [];
return val;
});
// Enable CORS
app.all( '/*', function( req, res, next ) {
// Process preflight if it is OPTIONS request
if( 'OPTIONS' == req.method ) {
res.header( 'Access-Control-Allow-Origin', '*' );
res.header( 'Access-Control-Allow-Method', 'POST, GET, PUT, DELETE, OPTIONS' );
res.header( 'Access-Control-Allow-Headers', 'origin, x-requested-with, x-file-name, content-type, cache-control' );
res.send( 203, 'No Content' );
}
next();
});
// DEFINE GET ROUTES
app.get( '/', routes.index );
app.get( '/defineEndpoints', platform.getEndpoints );
// DEFINE POST ROUTES
app.post( '/searchSubscribers', soap.getSubscribers );
app.post( '/updateSubscriberStatus', soap.updateSubscriberStatus );
// This route specifically handles SSO & JWT
app.post('/login', function( req, res ) {
// If we don't already have a token in the request session
if( !req.session.token ) {
var secret = options.settings.apiKeys[app.get('APIKey')].appSignature
, encodedJWT = req.body.jwt
, decodedJWT = jwt.decode(encodedJWT, secret)
, clientId = options.settings.apiKeys[app.get( 'APIKey' )].clientId
, clientSecret = options.settings.apiKeys[app.get( 'APIKey' )].clientSecret
;
// Sanity Check for SSO via HUB
if( decodedJWT ) {
//Used for inspecting the SOAP object
//console.log( JSON.stringify( decodedJWT, null, 4 ) );
req.session.token = decodedJWT.request.user.oauthToken;
req.session.internalOauthToken = decodedJWT.request.user.internalOauthToken;
req.session.refreshToken = decodedJWT.request.user.refreshToken;
req.session.tokenExpiration = decodedJWT.exp;
}
if( req.session.token ) {
// Make call to obtain the endpoints
var optionsObj = {
'hostname': 'www.exacttargetapis.com',
'port': 443,
'path': '/platform/v1/endpoints?access_token=' + req.session.token,
'method': 'GET',
'headers': {
'Content-Type': 'application/json',
'Encoding': 'utf-8'
}
};
// Mount the endpoints through this request in the session
var PlatformRequest = https.request( optionsObj, function( response ) {
var data = {}
responseData = {}
;
response.on( 'data', function( chunk ) {
data = JSON.parse( chunk );
for( var i = 0; i < data.items.length; i++ ) {
var type = data.items[i].type.replace( /\s+/g, '' );
responseData[ type ] = data.items[i].url;
}
});
response.on( 'end', function() {
req.session.soap = responseData.soap;
req.session.rest = responseData.rest;
res.redirect( '/' );
});
});
PlatformRequest.on( 'error', function( e ) {
res.json( 500, e );
});
PlatformRequest.end();
}
}
});
// Define the server and the handler
http.createServer( app ).listen( app.get( 'port' ), function() {
console.log( "Express server listening on port " + app.get( 'port' ) );
});