forked from nrstott/bogart
-
Notifications
You must be signed in to change notification settings - Fork 1
/
oauth.js
111 lines (98 loc) · 3.61 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
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
var q = require('../q')
, oauth = require('oauth')
, bogart = require('../bogart')
, _ = require('underscore')
, url = require('url');
module.exports = function oauthMiddleware(config, nextApp) {
if (nextApp === undefined) {
return function (nextApp) {
return oauthMiddleware(config, nextApp);
};
}
var options = {
loginRoute: '/auth/login',
logoutRoute: '/auth/logout',
callbackRoute: '/auth/callback',
errorUrl: '/'
};
options = _.extend(options, config);
if (!options.host) {
throw new Error('host is a required option')
}
if (!options.host.match(/^http/)) {
throw new Error('host must include the protocal.');
}
options.host = options.host.replace(/\/$/, ''); //remove trailing slash
var OAuth = new oauth.OAuth(options.requestTokenURL, options.accessTokenURL, options.consumerKey, options.consumerSecret, '1.0A', null, 'HMAC-SHA1');
var authorized = function(req) {
if (!req.session('profile')) {
return bogart.redirect(options.loginRoute + '?returnUrl=' + req.pathInfo );
}
req.auth = req.auth || {};
req.auth.profile = req.session('profile');
req.auth.access_token = req.session('access_token');
return nextApp(req);
};
var router = bogart.router(authorized);
router.get(options.loginRoute, function (req) {
var deferred = q.defer();
var callbackRoute = options.host + options.callbackRoute;
if (req.params.returnUrl) {
callbackRoute += '?returnUrl=' + encodeURIComponent(req.params.returnUrl);
}
var params = {
oauth_callback: callbackRoute
};
OAuth.getOAuthRequestToken(params, function (error, token, tokenSecret, params) {
if (error) {
deferred.reject(error);
}
req.session('oauth_token_secret', tokenSecret);
var parsed = url.parse(options.authorizationURL, true);
parsed.query.oauth_token = token;
var location = url.format(parsed);
deferred.resolve(bogart.redirect(location));
});
return deferred.promise;
});
router.get(options.logoutRoute, function (req) {
req.session('profile', undefined);
return bogart.redirect('/');
});
router.get(options.callbackRoute, function (req) {
var deferred = q.defer();
var oAuthToken = req.params.oauth_token,
oAuthVerifier = req.params.oauth_verifier,
oAuthTokenSecret = req.session('oauth_token_secret');
OAuth.getOAuthAccessToken(oAuthToken, oAuthTokenSecret, oAuthVerifier, function (error, accessToken, tokenSecret, params) {
if (error) {
deferred.reject(error);
}
var parsed = url.parse(options.resourceURL, true);
options.resourceURLParams.forEach(function (resourceUrlParam) {
if (params[resourceUrlParam]) {
parsed.query[resourceUrlParam] = params[resourceUrlParam];
}
});
var location = url.format(parsed);
OAuth.getProtectedResource(location, 'get', accessToken, tokenSecret, function (error, body, res) {
if (error) {
deferred.reject(error);
}
var profile = q(options.parse(body, req)).then(function (profile) {
req.session('profile', profile);
req.session('access_token', accessToken);
return bogart.redirect(options.successUrl || req.params.returnUrl || '/');
}).fail(function (error) {
var parsedUrl = url.parse(options.errorUrl, true);
parsedUrl.query.message = error.message;
var redirectUrl = url.format(parsedUrl);
return bogart.redirect(redirectUrl);
});
deferred.resolve(profile);
});
});
return deferred.promise;
});
return router(nextApp);
};