This repository has been archived by the owner on Apr 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 120
/
basket-proxy-server.js
222 lines (183 loc) · 6.19 KB
/
basket-proxy-server.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env node
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
var url = require('url');
var mozlog = require('mozlog');
var request = require('request');
var config = require('../lib/configuration');
mozlog.config(config.get('logging'));
var logger = require('mozlog')('server.basketproxy');
// Side effect - Adds default_fxa and dev_fxa to express.logger formats
var routeLogging = require('../lib/logging/route_logging');
var express = require('express');
var bodyParser = require('body-parser');
var cors = require('cors');
var CORS_ORIGIN = config.get('public_url');
var API_KEY = config.get('basket.api_key');
var API_URL = config.get('basket.api_url');
var API_TIMEOUT = config.get('basket.api_timeout');
var VERIFY_URL = config.get('oauth_url') + '/v1/verify';
// Error codes are defined in:
// https://github.com/mozilla/basket-client/blob/master/basket/errors.py
/* eslint-disable sorting/sort-object-props */
var BASKET_ERRORS = {
NETWORK_FAILURE: 1,
INVALID_EMAIL: 2,
UNKNOWN_EMAIL: 3,
UNKNOWN_TOKEN: 4,
USAGE_ERROR: 5,
EMAIL_PROVIDER_AUTH_FAILURE: 6,
AUTH_ERROR: 7,
SSL_REQUIRED: 8,
INVALID_NEWSLETTER: 9,
INVALID_LANGUAGE: 10,
EMAIL_NOT_CHANGED: 11,
CHANGE_REQUEST_NOT_FOUND: 12,
// If you get this, report it as a bug so we can add a more specific
// error code.
UNKNOWN_ERROR: 99
};
function errorResponse(desc, code) {
// Format from
// https://basket.readthedocs.org/en/latest/newsletter_api.html
return {
status: 'error',
desc: String(desc),
code: code || BASKET_ERRORS.UNKNOWN_ERROR
};
}
// Verify an OAuth token and return the associated credentials
function verifyOAuthToken() {
return function (req, res, next) {
var authHeader = req.headers && req.headers.authorization;
if (! authHeader) {
logger.error('auth.missing-authorization-header');
res.status(400).json(errorResponse('missing authorization header', BASKET_ERRORS.USAGE_ERROR));
return;
}
if (! authHeader.match(/^Bearer /)) {
logger.error('auth.invalid-authorization-header');
res.status(400).json(errorResponse('invalid authorization header', BASKET_ERRORS.USAGE_ERROR));
return;
}
var token = authHeader.replace(/^Bearer /, '');
logger.info('auth.valid.starting');
request.post({
url: VERIFY_URL,
json: {
token: token
}
}, function (err, _, body) {
if (err) {
logger.error('auth.error', err);
res.status(400).json(errorResponse(err, BASKET_ERRORS.UNKNOWN_ERROR));
return;
}
if (body.code >= 400) {
logger.error('auth.unauthorized', body);
res.status(body.code).json(errorResponse('unauthorized', BASKET_ERRORS.AUTH_ERROR));
return;
}
if (! body.email) {
logger.error('auth.missing-email', body);
res.status(400).json(errorResponse('missing email', BASKET_ERRORS.AUTH_ERROR));
return;
}
if (body.scope.indexOf('basket:write') === -1) {
logger.error('auth.invalid-scope', body);
res.status(400).json(errorResponse('invalid scope', BASKET_ERRORS.AUTH_ERROR));
return;
}
logger.info('auth.valid', body);
res.locals.creds = body;
next();
});
};
}
// Send a request to the Basket backend
function basketRequest(path, method, params, done) {
var req = request({
url: API_URL + path,
strictSSL: true,
method: method,
timeout: API_TIMEOUT,
headers: {
'X-API-Key': API_KEY
},
form: params
}, done);
return req;
}
function initApp() {
var app = express();
app.use(routeLogging());
app.use(bodyParser.json());
app.use(cors({
origin: CORS_ORIGIN
}));
app.use(verifyOAuthToken());
app.get('/lookup-user', function (req, res) {
var params = req.body;
var email = encodeURIComponent(res.locals.creds.email);
basketRequest('/lookup-user/?email=' + email, 'get', params)
.on('error', function (error) {
logger.error('lookup-user.error', error);
res.status(500).json(errorResponse(error, BASKET_ERRORS.UNKNOWN_ERROR));
})
.pipe(res);
});
app.post('/subscribe', function (req, res) {
var params = req.body;
params.email = res.locals.creds.email;
logger.info('subscribe.params', params);
basketRequest('/subscribe/', 'post', params)
.on('error', function (error) {
logger.error('subscribe.error', error);
res.status(500).json(errorResponse(error, BASKET_ERRORS.UNKNOWN_ERROR));
})
.pipe(res);
});
app.post('/unsubscribe', function (req, res) {
var creds = res.locals.creds;
var email = encodeURIComponent(creds.email);
basketRequest('/lookup-user/?email=' + email, 'get', {}, function (lookupError, httpRequest, body) {
if (lookupError) {
logger.error('lookup-user.error', lookupError);
res.status(400).json(errorResponse(lookupError, BASKET_ERRORS.UNKNOWN_ERROR));
return;
}
var responseData;
try {
responseData = JSON.parse(body);
} catch (parseError) {
logger.error('lookup-user.cannot-parse-response', parseError);
res.status(400).json(errorResponse(parseError, BASKET_ERRORS.UNKNOWN_ERROR));
return;
}
if (responseData.status !== 'ok') {
logger.error('lookup-user status not ok: ' + responseData.status);
res.status(httpRequest.statusCode).json(responseData);
return;
}
var params = req.body;
params.email = creds.email;
logger.info('unsubscribe.params', params);
basketRequest('/unsubscribe/' + responseData.token + '/', 'post', params)
.on('error', function (error) {
logger.error('unsubscribe.error', error);
res.status(500).json(errorResponse(error, BASKET_ERRORS.UNKNOWN_ERROR));
})
.pipe(res);
});
});
return app;
}
function listen(app) {
var serverUrl = url.parse(config.get('basket.proxy_url'));
app.listen(serverUrl.port, serverUrl.hostname);
logger.info('FxA Basket Proxy listening on port', serverUrl.port);
return true;
}
var app = initApp();
listen(app);