-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathoauth.js
78 lines (74 loc) · 2.92 KB
/
oauth.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
var rest = require('restler');
exports.refresh = function refresh(options) {
console.log('refresh');
rest.post(options.oauth.loginServer+'/services/oauth2/token', {
data: {
grant_type: 'refresh_token',
client_id: options.oauth.clientId,
client_secret: options.oauth.clientSecret,
refresh_token: options.oauth.refresh_token
},
}).on('complete', function(data, response) {
if (response.statusCode == 200) {
console.log('refreshed: '+data.access_token);
options.callback(data);
}
}).on('error', function(e) {
console.error(e);
});
}
exports.oauth = function oauth(options) {
var loginServer = options.loginServer || 'https://login.salesforce.com/',
clientId = options.clientId,
clientSecret = options.clientSecret,
redirectUri = options.redirectUri;
return function oauth(req, res, next){
console.log('oauth');
console.log('url :'+req.url);
if (req.session && req.session.oauth) {
// We're done - decorate the request with the oauth object
req.oauth = req.session.oauth;
req.oauth.loginServer = loginServer;
req.oauth.clientId = clientId;
req.oauth.clientSecret = clientSecret;
console.log(req.session.oauth);
next();
} else if (req.query.code){
// Callback from the Authorization Server
console.log('code: '+req.query.code);
rest.post(loginServer+'/services/oauth2/token', {
data: {
code: req.query.code,
grant_type: 'authorization_code',
client_id: clientId,
redirect_uri: redirectUri,
client_secret: clientSecret
},
}).on('complete', function(data, response) {
if (response.statusCode == 200) {
req.session.oauth = data;
state = req.session.oauth_state;
delete req.session.oauth_state;
console.log('oauth done - redirecting to '+state);
res.redirect(state);
}
}).on('error', function(e) {
console.error(e);
});
} else {
// Test for req.session - browser requests favicon.ico but doesn't
// bother sending cookie?
if ( req.session ) {
// We have nothing - redirect to the Authorization Server
req.session.oauth_state = req.url;
var oauthURL = loginServer + "/services/oauth2/authorize?response_type=code&" +
"client_id=" + clientId + "&redirect_uri=" + redirectUri + "&display=touch";
console.log('redirecting: '+oauthURL);
res.redirect(oauthURL); // Redirect to salesforce.com
res.end();
} else {
next();
}
}
};
};