-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
147 lines (135 loc) · 4.38 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
'use strict';
require('dotenv').config();
const express = require('express');
const cookieSession = require('cookie-session');
const axios = require('axios');
const moment = require('moment');
const ClientOAuth2 = require('client-oauth2');
// After registering your OAuth client you will be given the following credentials
const CLIENT_ID = process.env.OAUTH_CLIENT_ID;
const CLIENT_SECRET = process.env.OAUTH_CLIENT_SECRET;
const LD_DOMAIN = process.env.LD_DOMAIN || 'https://app.launchdarkly.com';
const PORT = process.env.PORT || 4000;
const REDIRECT_URI = process.env.REDIRECT_URI || `http://localhost:${PORT}/redirect`;
const COOKIE_SESSION_SECRET = process.env.COOKIE_SESSION_SECRET || 'Super Secure Cookie Session Secret';
const app = express();
app.set('view engine', 'pug');
app.use(express.static('public'));
app.use(
cookieSession({
name: 'session',
secret: COOKIE_SESSION_SECRET,
maxAge: 30 * 24 * 60 * 60 * 1000, // 30 Days
}),
);
var launchDarklyAuth = new ClientOAuth2({
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
accessTokenUri: `${LD_DOMAIN}/trust/oauth/token`,
authorizationUri: `${LD_DOMAIN}/trust/oauth/authorize`,
redirectUri: REDIRECT_URI,
scopes: ['writer'],
});
app.get('/', (req, res) => {
let context = {
loggedIn: false,
message: 'Hello LaunchDarkly OAuth',
apiUrl: `${LD_DOMAIN}/api/v2`,
};
if (req.session.oauthTokenData) {
context.loggedIn = true;
context.message = `Your OAuth token is: ${req.session.oauthTokenData.access}`;
context.token = req.session.oauthTokenData.access;
context.expiresIn = moment(req.session.oauthTokenData.expires).fromNow();
}
res.render('index', context);
});
// Delete cookie-session data and go home
app.get('/logout', (req, res) => {
delete req.session.oauthTokenData;
res.redirect('/');
});
// Begin the OAuth 2.0 flow
app.get('/auth', function(req, res) {
var uri = launchDarklyAuth.code.getUri();
res.redirect(uri);
});
// Make any GET request to LaunchDarkly's API using URL parameters
app.get('/get/:path*', function(req, res) {
if (req.session.oauthTokenData === undefined) {
res.redirect('/');
}
const endpoint = req.params.path + req.params[0];
const token = launchDarklyAuth.createToken(
req.session.oauthTokenData.access,
req.session.oauthTokenData.refresh,
'bearer',
);
const ldReq = token.sign({
method: 'get',
url: `${LD_DOMAIN}/api/v2/${endpoint}`,
});
axios(ldReq)
.then(testRes => {
return res.send(testRes.data);
})
.catch(e => {
return res.send(e.toJSON());
});
});
app.get('/redirect', function(req, res) {
// LaunchDarkly's OAuth implementation requires secret credentials to be placed in the POST body or as query parameters
launchDarklyAuth.code
.getToken(req.originalUrl, {
query: {
client_id: launchDarklyAuth.options.clientId,
client_secret: launchDarklyAuth.options.clientSecret,
},
})
.then(function(token) {
console.log(token); //=> { accessToken: '...', tokenType: 'bearer', ... }
// The token should ideally be saved in the database at this point
req.session.oauthTokenData = {
access: token.accessToken,
refresh: token.refreshToken,
expires: token.expires,
};
res.redirect('/');
})
.catch(e => {
res.send(e.message);
});
});
app.get('/refresh', function(req, res) {
if (req.session.oauthTokenData === undefined) {
res.redirect('/');
}
const token = launchDarklyAuth.createToken(
req.session.oauthTokenData.access,
req.session.oauthTokenData.refresh,
'bearer',
);
token
.refresh({
query: {
client_id: launchDarklyAuth.options.clientId,
client_secret: launchDarklyAuth.options.clientSecret,
},
})
.then(updatedToken => {
console.log('Token successfully updated:', updatedToken !== token); //=> true
console.log('New OAuth Token:', updatedToken.accessToken);
// The token should ideally be saved in the database at this point
// This example stores the token information in the cookie-session
req.session.oauthTokenData = {
access: updatedToken.accessToken,
refresh: updatedToken.refreshToken,
expires: updatedToken.expires,
};
res.redirect('/');
})
.catch(e => {
res.send(e.message);
});
});
app.listen(PORT, () => console.log(`Example app listening on port ${PORT}!`));