-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathauthentication.js
117 lines (102 loc) · 3.36 KB
/
authentication.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
var request = require('request-promise');
var dateUtils = require('date-utils');
var passport = require('passport');
var OnshapeStrategy = require('passport-onshape').Strategy;
var oauthClientId;
var oauthClientSecret;
var callbackUrl = "https://onshape-app-stl.herokuapp.com/oauthRedirect";
var oauthUrl = 'https://oauth.onshape.com';
var apiUrl = 'https://cad.onshape.com';
if (process.env.OAUTH_CLIENT_ID) {
oauthClientId = process.env.OAUTH_CLIENT_ID;
}
if (process.env.OAUTH_CLIENT_SECRET) {
oauthClientSecret = process.env.OAUTH_CLIENT_SECRET;
}
if (process.env.OAUTH_URL) {
oauthUrl = process.env.OAUTH_URL;
}
if (process.env.API_URL) {
apiUrl = process.env.API_URL;
}
if (process.env.OAUTH_CALLBACK_URL) {
callbackUrl = process.env.OAUTH_CALLBACK_URL;
}
function init() {
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
passport.use(new OnshapeStrategy({
clientID: oauthClientId,
clientSecret: oauthClientSecret,
// Replace the callbackURL string with your own deployed servers path to handle the OAuth redirect
callbackURL: callbackUrl,
authorizationURL: oauthUrl + "/oauth/authorize",
tokenURL: oauthUrl + "/oauth/token",
userProfileURL: apiUrl + "/api/users/sessioninfo"
},
function(accessToken, refreshToken, profile, done) {
// asynchronous verification, for effect...
process.nextTick(function () {
profile.accessToken = accessToken;
profile.refreshToken = refreshToken;
// To keep the example simple, the user's Onshape profile is returned to
// represent the logged-in user. In a typical application, you would want
// to associate the Onshape account with a user record in your database,
// and return that user instead.
return done(null, profile);
});
}
));
}
function onOAuthTokenReceived(body, req) {
var jsonResponse;
jsonResponse = JSON.parse(body);
if (jsonResponse) {
req.user.accessToken = jsonResponse.access_token;
req.user.refreshToken = jsonResponse.refresh_token;
}
}
var pendingTokenRefreshes = {};
function refreshOAuthToken(req, res, next) {
if (pendingTokenRefreshes[req.session.id]) {
return pendingTokenRefreshes[req.session.id]
}
var refreshToken = req.user.refreshToken;
if (refreshToken) {
pendingTokenRefreshes[req.session.id] = request.post({
uri: oauthUrl + '/oauth/token',
form: {
'client_id': oauthClientId,
'client_secret': oauthClientSecret,
'grant_type': 'refresh_token',
'refresh_token': refreshToken
}
}).then(function(body) {
delete pendingTokenRefreshes[req.session.id];
return onOAuthTokenReceived(body, req);
}).catch(function(error) {
delete pendingTokenRefreshes[req.session.id];
console.log('Error refreshing OAuth Token: ', error);
res.status(401).send({
authUri: getAuthUri(),
msg: 'Authentication required.'
});
throw(error);
});
return pendingTokenRefreshes[req.session.id];
} else {
return Promise.reject('No refresh_token');
}
}
function getAuthUri() {
return oauthUrl + '/oauth/authorize?response_type=code&client_id=' + oauthClientId;
}
module.exports = {
'init': init,
'refreshOAuthToken': refreshOAuthToken,
'getAuthUri': getAuthUri
};