-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthentication.js
More file actions
77 lines (69 loc) · 2.15 KB
/
authentication.js
File metadata and controls
77 lines (69 loc) · 2.15 KB
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
const crypto = require('crypto')
const RPC = require('./rpc')
const LocalDatabase = require('./local-database')
const Session = require('./session')
const authentication = LocalDatabase('authentication')
module.exports = class Authentication extends RPC {
constructor(options = {}) {
options.id = options.code || crypto.randomBytes(8).toString('hex')
super(options)
this.sessions = {}
}
async bindReady() {
const { authority, code } = this.options
if (authority) {
await authentication.update({ code }, { code }, { upsert: true })
this.emit('ready')
} else {
this.emit('ready')
}
}
createSession(remote) {
const { code } = this.options
const hash = crypto.createHash('sha256')
hash.update(Buffer.from(code, 'hex'))
hash.update(crypto.randomBytes(8))
const id = hash.digest().toString('hex')
const peer = remote.$nodeId
const algorithm = 'aes256'
const key = crypto.randomBytes(32)
const iv = crypto.randomBytes(16)
const session = new Session({ id, peer, algorithm, key, iv })
this.sessions[peer] = peer
this.emit(`session.${peer}`, session)
return session
}
async handleRemote(remote) {
const { authority, code } = this.options
if (!authority) {
const options = await remote.authenticate(code)
if (options) {
options.peer = remote.$nodeId
options.key = Buffer.from(options.key, 'hex')
options.iv = Buffer.from(options.iv, 'hex')
const session = new Session(options)
this.sessions[options.peer] = options.peer
this.emit(`session.${options.peer}`, session)
}
}
}
getProtocol(methods = {}) {
const self = this
return super.getProtocol(
Object.assign(methods, {
async authenticate(code) {
const exists = await authentication.count({ code })
if (!exists) return
const session = self.createSession(this.remote)
const { id, algorithm, key, iv } = session.options
return {
id,
algorithm,
key: key.toString('hex'),
iv: iv.toString('hex'),
}
},
}),
)
}
}