-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
51 lines (42 loc) · 1.57 KB
/
index.js
File metadata and controls
51 lines (42 loc) · 1.57 KB
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
'use strict';
global.APP_ID = process.env.APP_ID || undefined;
const bootstrap = require('@dronedeploy/function-wrapper');
const { createConfig } = require('./oauth-config');
const authorizationCode = require('./handlers/authorizationCode');
const clientCredentials = require('./handlers/clientCredentials');
exports.createOAuth = function(configuration) {
const { authorizationCodeSettings, clientCredentialsSettings } = parseConfig(configuration);
let paths = {};
if (authorizationCodeSettings) {
paths = authorizationCode.initHandler(createConfig(authorizationCodeSettings, false));
}
if (clientCredentialsSettings) {
paths = Object.assign(paths, clientCredentials.initHandler(createConfig(clientCredentialsSettings, true)));
}
return createHandler(paths);
};
const parseConfig = function (configuration) {
const newConfigFormat = configuration.clientCredentialsSettings || configuration.authorizationCodeSettings;
if (newConfigFormat) {
return {
clientCredentialsSettings: configuration.clientCredentialsSettings,
authorizationCodeSettings: configuration.authorizationCodeSettings,
}
}
return {
authorizationCodeSettings: configuration
};
};
function createHandler(paths) {
process.env.IGNORE_AUTH_ROUTES = ['/auth/callback'];
return bootstrap((ctx) =>
(req, res) => {
console.log(`Request received to endpoint: ${req.method} ${req.originalUrl}`);
const pathHandler = paths[req.path];
if (pathHandler) {
pathHandler(req, res, ctx);
} else {
res.status(404).send('Not Found');
}
});
}