@live-change/secret-link-service 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +144 -0
- package/package.json +28 -0
package/index.js
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
const nodemailer = require('nodemailer')
|
|
2
|
+
const app = require("@live-change/framework").app()
|
|
3
|
+
const { randomString } = require('@live-change/uid')
|
|
4
|
+
|
|
5
|
+
const definition = app.createServiceDefinition({
|
|
6
|
+
name: "secretLink"
|
|
7
|
+
})
|
|
8
|
+
const config = definition.config
|
|
9
|
+
|
|
10
|
+
const MessageAuthentication = definition.foreignModel('messageAuthentication', 'Authentication')
|
|
11
|
+
|
|
12
|
+
const targetProperties = {
|
|
13
|
+
authentication: {
|
|
14
|
+
type: MessageAuthentication,
|
|
15
|
+
validation: ['nonEmpty']
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const secretProperties = {
|
|
20
|
+
secretCode: {
|
|
21
|
+
type: String,
|
|
22
|
+
validation: ['nonEmpty']
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const Link = definition.model({
|
|
27
|
+
name: "Link",
|
|
28
|
+
properties: {
|
|
29
|
+
...targetProperties,
|
|
30
|
+
...secretProperties,
|
|
31
|
+
expire: {
|
|
32
|
+
type: Date,
|
|
33
|
+
validation: ['nonEmpty']
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
indexes: {
|
|
37
|
+
bySecretCode: {
|
|
38
|
+
property: 'secretCode'
|
|
39
|
+
},
|
|
40
|
+
byAuthentication: {
|
|
41
|
+
property: 'authentication'
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
definition.event({
|
|
47
|
+
name: 'linkCreated',
|
|
48
|
+
execute({ link, authentication, secretCode, expire }) {
|
|
49
|
+
return Link.create({ id: link, authentication, secretCode, expire })
|
|
50
|
+
}
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
definition.event({
|
|
54
|
+
name: 'linkExpired',
|
|
55
|
+
execute({ link }) {
|
|
56
|
+
return Link.update(link, { date: new Date(Date.now() - 1000) })
|
|
57
|
+
}
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
definition.trigger({
|
|
61
|
+
name: "authenticationSecret",
|
|
62
|
+
properties: {
|
|
63
|
+
...targetProperties
|
|
64
|
+
},
|
|
65
|
+
waitForEvents: true,
|
|
66
|
+
async execute({ authentication }, context, emit) {
|
|
67
|
+
const link = app.generateUid()
|
|
68
|
+
const secretCode = randomString(config.secretCodeLength || 16)
|
|
69
|
+
const expire = new Date()
|
|
70
|
+
expire.setTime(Date.now() + (config.expireTime || 24*60*60*1000))
|
|
71
|
+
emit({
|
|
72
|
+
type: 'linkCreated',
|
|
73
|
+
link,
|
|
74
|
+
authentication,
|
|
75
|
+
secretCode, expire
|
|
76
|
+
})
|
|
77
|
+
return {
|
|
78
|
+
type: 'link',
|
|
79
|
+
link,
|
|
80
|
+
expire,
|
|
81
|
+
secret: {
|
|
82
|
+
secretCode
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
definition.trigger({
|
|
89
|
+
name: "refreshAuthenticationSecret",
|
|
90
|
+
properties: {
|
|
91
|
+
...targetProperties
|
|
92
|
+
},
|
|
93
|
+
waitForEvents: true,
|
|
94
|
+
async execute({ authentication }, context, emit) {
|
|
95
|
+
const currentLink = await Link.indexObjectGet('byAuthentication', authentication)
|
|
96
|
+
if(currentLink) {
|
|
97
|
+
emit({ type: 'linkExpired', link: currentLink.id })
|
|
98
|
+
}
|
|
99
|
+
const link = app.generateUid()
|
|
100
|
+
const secretCode = randomString(config.secretCodeLength || 16)
|
|
101
|
+
const expire = new Date()
|
|
102
|
+
expire.setTime(Date.now() + (config.expireTime || 24*60*60*1000))
|
|
103
|
+
emit({
|
|
104
|
+
type: 'linkCreated',
|
|
105
|
+
link,
|
|
106
|
+
authentication,
|
|
107
|
+
secretCode, expire
|
|
108
|
+
})
|
|
109
|
+
return {
|
|
110
|
+
type: 'link',
|
|
111
|
+
link,
|
|
112
|
+
expire,
|
|
113
|
+
secret: {
|
|
114
|
+
secretCode
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
definition.view({
|
|
121
|
+
name: "link",
|
|
122
|
+
properties: {
|
|
123
|
+
...secretProperties
|
|
124
|
+
},
|
|
125
|
+
daoPath({ secretCode }, { client, context }) {
|
|
126
|
+
return Link.indexObjectPath('bySecretCode', secretCode)
|
|
127
|
+
}
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
definition.trigger({
|
|
131
|
+
name: "checkLinkSecret",
|
|
132
|
+
properties: {
|
|
133
|
+
...targetProperties,
|
|
134
|
+
...secretProperties,
|
|
135
|
+
},
|
|
136
|
+
async execute({ secret }, context, emit) {
|
|
137
|
+
const linkData = await Link.indexObjectGet('bySecretCode', secret)
|
|
138
|
+
if(!linkData) throw 'linkNotFound'
|
|
139
|
+
if(new Date().toISOString() > linkData.expire) throw "linkExpired"
|
|
140
|
+
return linkData.authentication
|
|
141
|
+
}
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
module.exports = definition
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@live-change/secret-link-service",
|
|
3
|
+
"version": "0.2.2",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "NODE_ENV=test tape tests/*"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/live-change/live-change-services.git"
|
|
12
|
+
},
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/live-change/live-change-services/issues"
|
|
16
|
+
},
|
|
17
|
+
"homepage": "https://github.com/live-change/live-change-services",
|
|
18
|
+
"author": {
|
|
19
|
+
"email": "michal@laszczewski.pl",
|
|
20
|
+
"name": "Michał Łaszczewski",
|
|
21
|
+
"url": "https://www.viamage.com/"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@live-change/framework": "^0.5.7",
|
|
25
|
+
"nodemailer": "^6.7.2"
|
|
26
|
+
},
|
|
27
|
+
"gitHead": "7691357c7569a18373668691eb6a81a9e161194d"
|
|
28
|
+
}
|