-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
82 lines (60 loc) · 1.67 KB
/
index.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
import express from 'express'
import bodyParser from 'body-parser'
import OAuth2Server from 'oauth2-server'
import AccessDeniedError from 'oauth2-server/lib/errors/access-denied-error'
import views from 'co-views'
import util from 'util'
import swig from 'swig'
import model from './model'
// const Request = OAuth2Server.Request
// const Response = OAuth2Server.Response
let PORT = 3002
let tobi = {
name: 'Infinite Team',
species: 'Open Source Technology co,.ltd'
}
const app = express()
app.oauth = new OAuth2Server({
model,
grants: ['password', 'authorization_code', 'refresh_token'],
debug: true,
allowBearerTokensInQueryString: true,
accessTokenLifetime: model.JWT_ACCESS_TOKEN_EXPIRY_SECONDS,
refreshTokenLifetime: model.JWT_REFRESH_TOKEN_EXPIRY_SECONDS
})
app.use(bodyParser.urlencoded({
extended: true
}))
app.use(bodyParser.json())
// let request = new Request({
// method: 'GET',
// query: {},
// headers: {
// Authorization: 'Bearer foobar'
// }
// })
// let response = new Response({
// headers: {}
// })
app.use(app.oauth.authorize())
app.post('/oauth/token', app.oauth.token()) //same grant()
app.engine('html', swig.renderFile)
app.set('view engine', 'html')
app.set('views', __dirname + '/views')
app.get('/login', (req, res) => {
res.render('login', {
user: tobi,
redirect: req.query.redirect,
client_id: req.query.client_id,
redirect_uri: req.query.redirect_uri
})
})
console.log(model)
// Get secret.
app.get('/secret', app.oauth.authenticate(), function (req, res) {
// Will require a valid access_token.
res.send('Secret area');
})
app.listen(PORT, () => {
console.log(`Server is runing on port ${PORT}`)
})