forked from mekentosj/oauth2-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
96 lines (77 loc) · 2.7 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
var express = require('express');
var routes = require('./routes');
var config = require('./config');
var path = require('path');
var models = require('./models');
var middleware = require('./middleware');
var app = express();
var oauthserver = require('node-oauth2-server');
var User = models.User;
app.set('env', process.env.NODE_ENV || 'development');
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.cookieParser('ncie0fnft6wjfmgtjz8i'));
app.use(express.cookieSession());
app.locals.title = 'OAuth Example';
app.locals.pretty = true;
app.configure('development', 'production', function() {
app.use(express.logger('dev'));
});
app.use(express.bodyParser());
app.use(express.methodOverride());
app.oauth = oauthserver({
model: models.oauth,
grants: ['password', 'authorization_code', 'refresh_token'],
debug: true
});
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
app.use(function(err, req, res, next) {
if (process.env.NODE_ENV !== 'test')
console.error('Error:', err);
if (middleware.isValidationError(err)) {
res.status(400);
res.send(err.errors);
} else {
res.status(err.code || 500);
res.send('Error');
}
});
if ('development' === app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', middleware.loadUser, routes.index);
app.all('/oauth/token', app.oauth.grant());
app.get('/oauth/authorise', function(req, res, next) {
if (!req.session.userId) {
return res.redirect('/session?redirect=' + req.path + '&client_id=' +
req.query.client_id + '&redirect_uri=' + req.query.redirect_uri);
}
res.render('authorise', {
client_id: req.query.client_id,
redirect_uri: req.query.redirect_uri
});
});
// Handle authorise
app.post('/oauth/authorise', function(req, res, next) {
if (!req.session.userId) {
return res.redirect('/session?redirect=' + req.path + 'client_id=' +
req.query.client_id +'&redirect_uri=' + req.query.redirect_uri);
}
next();
}, app.oauth.authCodeGrant(function(req, next) {
// The first param should to indicate an error
// The second param should a bool to indicate if the user did authorise the app
// The third param should for the user/uid (only used for passing to saveAuthCode)
next(null, req.body.allow === 'yes', req.session.userId, null);
}));
app.get('/secret', middleware.requiresUser, function(req, res) {
res.send('Secret area');
});
app.use(app.oauth.errorHandler());
app.post('/v1/users', routes.users.create);
app.get('/account', middleware.requiresUser, routes.users.show);
app.post('/session', routes.session.create);
app.get('/session', routes.session.show);
module.exports = app;