1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | module.exports = auth
|
11 |
|
12 | function auth(req, res, next, options) {
|
13 | var authorization = req.headers["authorization"]
|
14 |
|
15 | if (authorization) {
|
16 | authorization = authorization.split(/\s+/)
|
17 |
|
18 | if (authorization[0] == "Session") {
|
19 | options.sessionStorage.get(authorization[1], handleSession)
|
20 | } else if (authorization[0] == "Basic") {
|
21 | return res.sendStatus(501)
|
22 |
|
23 | var user_and_pass = Buffer.from(authorization[1], "base64").toString()
|
24 | , name = user_and_pass.split(":")[0]
|
25 | , token = crypto.createHash("sha1").update(user_and_pass).digest("hex")
|
26 |
|
27 | } else if (authorization[0] == "Bearer") {
|
28 | return res.sendStatus(501)
|
29 | } else if (authorization[0] == "MAC") {
|
30 |
|
31 | return res.sendStatus(501)
|
32 | } else {
|
33 |
|
34 | res.end()
|
35 | }
|
36 | } else {
|
37 | req.session = {}
|
38 | next()
|
39 | }
|
40 |
|
41 | function handleSession(err, session) {
|
42 | req.session = session = session || {}
|
43 | session.seen = +req.date
|
44 | if (session && session.userId) {
|
45 | options.userStorage.get(session.userId, handleUser)
|
46 | } else {
|
47 | next()
|
48 | }
|
49 | }
|
50 |
|
51 | function handleUser(err, user) {
|
52 | req.user = user
|
53 | next()
|
54 | }
|
55 | }
|
56 |
|
57 |
|
58 |
|