-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
132 lines (104 loc) · 3.29 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const async = require('async')
const express = require('express')
const path = require('path')
const util = require('util')
const session = require('express-session')
const bodyParser = require('body-parser')
const morgan = require('morgan')
const jwt = require('jsonwebtoken')
// initialize sequelize with session store
const SequelizeStore = require('connect-session-sequelize')(session.Store)
const app = express()
const server = require('http').Server(app)
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'pug')
// LOG
if (app.get('env') === 'development') app.use(morgan('dev'))
let ens = null
if (process.env.EAUTH_COMPONENTS_ENS === 'true') {
const ENS = require('./components/ens')
ens = new ENS()
}
// issue, dev // maybe add salt with secret
app.set('secret', process.env.EAUTH_SECRET)
// initialize database
const db = require('./models')
const { User, Session } = db
// create database if not exist // if force == true : drop table
async function initialize() {
try {
await User.sync()
await Session.sync({ force: true })
} catch (e) {
console.error(e)
setTimeout(() => {
initialize()
}, 5000)
}
}
initialize()
const sequelizeStore = new SequelizeStore({
db: db.sequelize,
table: 'Session',
})
app.use(session({
secret: app.get('secret'),
store: sequelizeStore,
resave: false,
saveUninitialized: true,
}))
// Add body parser.
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(express.static(path.join(__dirname, 'public')))
function oauthMiddleware(req, res, next) {
if (req.method == 'GET') {
if (req.path === '/oauth/authorize' && req.session.previousPath === '/oauth/authorize') {
return req.session.destroy((err) => {
let location = '/'
if (req.url) location = util.format('/?url=%s', encodeURIComponent(req.url))
return res.redirect(location)
})
}
req.session.previousPath = req.path
}
next()
}
app.use(oauthMiddleware)
function apiMiddleware(req, res, next) {
const { token } = req.session
if (token) {
// issue case: after server restart will pass verify cond,but token is expire, maybe should check database
jwt.verify(token, app.get('secret'), (err, decoded) => {
if (err) {
return res.json({ success: false, message: 'Failed to authenticate token.' })
}
req.decoded = decoded
return next()
})
} else {
const url = req.url ? util.format('/?url=%s', encodeURIComponent(req.url)) : '/'
res.redirect(url)
}
}
const api = express.Router()
// api middleware
api.use(apiMiddleware)
require('./components/eauth')(app, api, User, ens)
if (process.env.EAUTH_COMPONENTS_CONTRACT === 'true')
require('./components/contract')(app, User, ens)
if (process.env.EAUTH_COMPONENTS_OAUTH === 'true')
require('./components/oauth')(app, api, User, ens)
if (process.env.EAUTH_COMPONENTS_QRCODE === 'true')
require('./components/qrcode')(app, api, sequelizeStore, server)
// error handler
app.use((err, req, res, next) => {
res.status(err.status || 500)
res.render('error', {
message: err.message,
error: app.get('env') === 'development' ? err : {},
})
})
const listener = server.listen(process.env.EAUTH_PORT || 8080, () => {
console.log('Listening on port ' + listener.address().port)
})