forked from reneweb/oauth2orize_authorization_grant_example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
46 lines (38 loc) · 1.76 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
//Module dependencies
var express = require('express')
, http = require('http')
, passport = require('passport')
, util = require('util')
, session = require('express-session')
, cookieParser = require('cookie-parser')
, bodyParser = require('body-parser')
, expressValidator = require('express-validator')
, auth = require("./auth")
, oauth = require("./oauth")
, registration = require("./registration")
// Express configuration
var app = express()
app.set('views', __dirname + '/views')
app.set('view engine', 'jade')
app.use(bodyParser())
app.use(expressValidator())
app.use(cookieParser())
app.use(session({ secret: 'keyboard cat'}))
app.use(passport.initialize())
app.use(passport.session())
app.get('/client/registration', function(req, res) { res.render('clientRegistration') })
app.post('/client/registration', registration.registerClient)
app.get('/registration', function(req, res) { res.render('userRegistration') })
app.post('/registration', registration.registerUser)
app.get('/login', function(req, res) { res.render('login', {clientId : req.query.clientId, redirectUri: req.query.redirectUri}) })
app.post('/login', passport.authenticate('local', { failureRedirect: '/login' }), function(req, res) {
res.redirect('/oauth/authorization?response_type=code&client_id=' + req.body.clientId + '&redirect_uri=' + req.body.redirectUri)
})
app.get('/oauth/authorization', oauth.authorization)
app.post('/decision', oauth.decision)
app.post('/oauth/token', oauth.token)
app.get('/restricted', passport.authenticate('accessToken', { session: false }), function (req, res) {
res.send("Yay, you successfully accessed the restricted resource!")
})
//Start
http.createServer(app).listen(process.env.PORT || 3000, process.env.IP || "0.0.0.0")