-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
82 lines (77 loc) · 2.75 KB
/
index.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
/* Copyright 2017 data.world, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the
* License.
*
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* This product includes software developed at
* data.world, Inc.(http://data.world/). */
const Hapi = require('hapi');
const axios = require('axios');
const server = new Hapi.Server();
server.connection({
port: process.env.PORT || 3001,
host: '0.0.0.0'
});
/**
* The entry point from the connector. It will immediately redirect to the
* data.world authenticator to start the oauth flow.
*/
server.route({
method: 'GET',
path: '/authorize',
handler: function (req, reply) {
const client_id = process.env.CLIENT_ID;
const redirect_uri = process.env.REDIRECT_URI;
const endpoint = process.env.AUTHORIZATION_ENDPOINT;
reply.redirect(`${endpoint}/oauth/authorize?client_id=${client_id}&redirect_uri=${redirect_uri}`);
}
});
/**
* The oauth callback endpoint. The data.world oauth flow will return here with a
* short term code. The code should be sent back to data.world to retrieve the long
* term code and then flow should be redirected to the connector.
*/
server.route({
method: 'GET',
path: '/callback',
handler: function (req, reply) {
const endpoint = process.env.AUTHORIZATION_ENDPOINT;
const params = {
code: req.query.code,
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET,
grant_type: 'authorization_code'
};
const queryString = Object.keys(params).map((k) => {
return `${k}=${params[k]}`;
}).join('&');
axios.post(`${endpoint}/oauth/access_token?${queryString}`).then((response) => {
if (response.data.access_token) {
reply().redirect(`${process.env.CONNECTOR_REDIRECT}?token=${response.data.access_token}`);
} else {
const errorMessage = response.data.message || 'UNKNOWN_ERROR';
reply().redirect(`${process.env.CONNECTOR_REDIRECT}?error=${errorMessage}`);
}
}).catch((err) => {
console.log('Error exchanging short term code for long term token: ', err);
const errorMessage = err.message || 'UNKNOWN_ERROR';
reply().redirect(`${process.env.CONNECTOR_REDIRECT}?error=${errorMessage}`);
});
}
})
server.start((err) => {
if (err) {
throw err;
}
console.log('Server running at: ', server.info.uri);
})