forked from nodeSolidServer/node-solid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken-service.js
More file actions
47 lines (35 loc) · 910 Bytes
/
Copy pathtoken-service.js
File metadata and controls
47 lines (35 loc) · 910 Bytes
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
'use strict'
const { ulid } = require('ulid')
class TokenService {
constructor () {
this.tokens = {}
}
generate (domain, data = {}) {
const token = ulid()
this.tokens[domain] = this.tokens[domain] || {}
const value = {
exp: new Date(Date.now() + 20 * 60 * 1000)
}
this.tokens[domain][token] = Object.assign({}, value, data)
return token
}
verify (domain, token) {
const now = new Date()
if (!this.tokens[domain]) {
throw new Error(`Invalid domain for tokens: ${domain}`)
}
const tokenValue = this.tokens[domain][token]
if (tokenValue && now < tokenValue.exp) {
return tokenValue
} else {
return false
}
}
remove (domain, token) {
if (!this.tokens[domain]) {
throw new Error(`Invalid domain for tokens: ${domain}`)
}
delete this.tokens[domain][token]
}
}
module.exports = TokenService