-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
130 lines (108 loc) · 3.26 KB
/
server.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
const express = require('express')
const Eauth = require('express-eauth');
const expressNunjucks = require('express-nunjucks')
const session = require('express-session')
const rp = require('request-promise')
const async = require('async')
const app = express()
app.set('views', __dirname + '/dist');
const isDev = app.get('env') === 'development'
const njk = expressNunjucks(app, {
watch: isDev,
noCache: isDev
})
/* ----------- OAuth example----------- */
const OAUTH_CLIENT_ID = 'eauth'
const OAUTH_URL_PREFIX = 'http://eauth.local:8080/oauth'
const OAUTH_CREDIENTALS = Buffer.from(`${OAUTH_CLIENT_ID}:SEEEECRET`).toString('base64')
const OAUTH_REDIRECT_URI = 'http://localhost:59011/oauth_success'
app.use(session({
secret: 'SSSEEEcret',
resave: false,
saveUninitialized: true,
}))
app.get('/', (req, resp) => {
const cxt = {
session: req.session,
}
if (!req.session.address) {
Object.assign(cxt, {
clientID: OAUTH_CLIENT_ID,
oauthURL: `${OAUTH_URL_PREFIX}/authorize`,
redirectURI: OAUTH_REDIRECT_URI,
sessionID: req.sessionID,
})
resp.render('index', cxt)
} else {
resp.render('index', cxt)
}
})
app.get('/oauth_success', (req, resp) => {
if (req.query.state != req.sessionID) {
resp.status(400)
.send('Invalid session')
return
}
const rpJson = rp.defaults({ json: true })
Promise.resolve(null)
.then(() => {
return rpJson(`${OAUTH_URL_PREFIX}/token`, {
method: 'POST',
headers: { 'Authorization': `Basic ${OAUTH_CREDIENTALS}` },
form: {
grant_type: 'authorization_code',
code: req.query.code,
redirect_uri: OAUTH_REDIRECT_URI,
},
json: true,
})
})
.then(authResult => {
return rpJson(`${OAUTH_URL_PREFIX}/user`, {
headers: { 'Authorization': `Bearer ${authResult.access_token}` },
})
})
.then(userData => {
const { id, ens, address } = userData
req.session.ens = ens
req.session.address = address
console.log(req.session)
resp.redirect('/')
})
.catch(err => {
console.log(err)
resp.status(500)
.send('Error occurs while authenticating...')
})
})
/* ----------- SDK example----------- */
const eauthMiddleware = new Eauth({ banner: 'Eauth Sample', method: 'eth_signTypedData_v4' })
/* --- Step 1: authentication request --- */
app.get('/auth/:Address', eauthMiddleware, (req, res) => {
return req.eauth.message ? res.send(req.eauth.message) : res.status(400).send()
});
/* --- Step 2: challenge response --- */
app.post('/auth/:Message/:Signature', eauthMiddleware, (req, res) => {
const address = req.eauth.recoveredAddress
if (!address) res.status(400).send()
else {
req.session.address = address
console.log(req.session)
res.json({
success: true,
message: 'Eauth Success',
address: address,
})
}
});
app.get('/logout', (req, resp) => {
req.session.destroy()
resp.redirect('/')
})
app.use(express.static(__dirname + '/dist'))
Promise.resolve()
.then(() => {
app.listen(59011, () => {
console.log('Server is up')
})
})