@live-change/message-authentication-service 0.2.8 → 0.2.14
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/authentication.js +220 -0
- package/definition.js +10 -0
- package/index.js +3 -408
- package/package.json +2 -2
- package/sign.js +220 -0
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
const app = require("@live-change/framework").app()
|
|
2
|
+
const definition = require('./definition.js')
|
|
3
|
+
const config = definition.config
|
|
4
|
+
|
|
5
|
+
const contactProperties = {
|
|
6
|
+
contactType: {
|
|
7
|
+
type: String,
|
|
8
|
+
validation: ['nonEmpty']
|
|
9
|
+
},
|
|
10
|
+
contact: {
|
|
11
|
+
type: String,
|
|
12
|
+
validation: ['nonEmpty']
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const targetProperties = {
|
|
17
|
+
action: {
|
|
18
|
+
type: String,
|
|
19
|
+
validation: ['nonEmpty'],
|
|
20
|
+
},
|
|
21
|
+
actionProperties: {
|
|
22
|
+
type: Object
|
|
23
|
+
},
|
|
24
|
+
targetPage: {
|
|
25
|
+
type: Object,
|
|
26
|
+
validation: ['nonEmpty'],
|
|
27
|
+
properties: {
|
|
28
|
+
name: {
|
|
29
|
+
type: String,
|
|
30
|
+
validation: ['nonEmpty']
|
|
31
|
+
},
|
|
32
|
+
params: {
|
|
33
|
+
type: Object
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const messageProperties = {
|
|
40
|
+
messageData: {
|
|
41
|
+
type: Object
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const Authentication = definition.model({
|
|
46
|
+
name: 'Authentication',
|
|
47
|
+
properties: {
|
|
48
|
+
...contactProperties,
|
|
49
|
+
...targetProperties,
|
|
50
|
+
...messageProperties,
|
|
51
|
+
state: {
|
|
52
|
+
type: "String",
|
|
53
|
+
validation: ['nonEmpty'],
|
|
54
|
+
options: ['created', 'used']
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
definition.event({
|
|
60
|
+
name: 'authenticationCreated',
|
|
61
|
+
execute({ authentication, contactType, contact, action, actionProperties, targetPage, messageData }) {
|
|
62
|
+
return Authentication.create({
|
|
63
|
+
id: authentication,
|
|
64
|
+
contactType, contact,
|
|
65
|
+
action, actionProperties, targetPage,
|
|
66
|
+
messageData,
|
|
67
|
+
state: 'created'
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
definition.event({
|
|
73
|
+
name: 'authenticationUsed',
|
|
74
|
+
execute({ authentication, targetPage }) {
|
|
75
|
+
return Authentication.update(authentication, { state: 'used', targetPage })
|
|
76
|
+
}
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
definition.view({
|
|
80
|
+
name: "authentication",
|
|
81
|
+
properties: {
|
|
82
|
+
authentication: {
|
|
83
|
+
type: Authentication
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
daoPath({ authentication }, { client, context }) {
|
|
87
|
+
return Authentication.path(authentication)
|
|
88
|
+
}
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
definition.trigger({
|
|
92
|
+
name: 'authenticateWithMessage',
|
|
93
|
+
waitForEvents: true,
|
|
94
|
+
properties: {
|
|
95
|
+
...contactProperties,
|
|
96
|
+
...targetProperties,
|
|
97
|
+
...messageProperties
|
|
98
|
+
},
|
|
99
|
+
async execute({ contactType, contact, action, actionProperties, targetPage, messageData },
|
|
100
|
+
{ client, service }, emit) {
|
|
101
|
+
const authentication = app.generateUid()
|
|
102
|
+
const secrets = await service.trigger({
|
|
103
|
+
type: 'authenticationSecret',
|
|
104
|
+
authentication
|
|
105
|
+
})
|
|
106
|
+
if(secrets.length == 0) throw new Error('no secrets generated!')
|
|
107
|
+
const contactTypeUpperCase = contactType[0].toUpperCase() + contactType.slice(1)
|
|
108
|
+
await service.trigger({
|
|
109
|
+
type: 'send' + contactTypeUpperCase + 'Message',
|
|
110
|
+
render: {
|
|
111
|
+
action,
|
|
112
|
+
contactType,
|
|
113
|
+
contact,
|
|
114
|
+
secrets,
|
|
115
|
+
...messageData
|
|
116
|
+
}
|
|
117
|
+
})
|
|
118
|
+
emit({
|
|
119
|
+
type: "authenticationCreated",
|
|
120
|
+
authentication,
|
|
121
|
+
contactType,
|
|
122
|
+
contact,
|
|
123
|
+
action,
|
|
124
|
+
actionProperties,
|
|
125
|
+
targetPage,
|
|
126
|
+
messageData
|
|
127
|
+
})
|
|
128
|
+
return {
|
|
129
|
+
type: 'sent',
|
|
130
|
+
authentication,
|
|
131
|
+
secrets: secrets.map(({ secret, ...notSecret }) => notSecret)
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
definition.action({
|
|
137
|
+
name: 'finishMessageAuthentication',
|
|
138
|
+
waitForEvents: true,
|
|
139
|
+
properties: {
|
|
140
|
+
secretType: {
|
|
141
|
+
type: String,
|
|
142
|
+
validation: ['nonEmpty']
|
|
143
|
+
},
|
|
144
|
+
secret: {
|
|
145
|
+
type: String,
|
|
146
|
+
validation: ['nonEmpty']
|
|
147
|
+
},
|
|
148
|
+
authentication: {
|
|
149
|
+
type: Authentication
|
|
150
|
+
}
|
|
151
|
+
},
|
|
152
|
+
async execute({ secretType, secret, authentication = undefined }, { client, service }, emit) {
|
|
153
|
+
const secretTypeUpperCase = secretType[0].toUpperCase() + secretType.slice(1)
|
|
154
|
+
const checkResults = await service.trigger({
|
|
155
|
+
type: 'check' + secretTypeUpperCase + 'Secret',
|
|
156
|
+
secret,
|
|
157
|
+
authentication,
|
|
158
|
+
client
|
|
159
|
+
})
|
|
160
|
+
authentication = checkResults[0]
|
|
161
|
+
const authenticationData = await Authentication.get(authentication)
|
|
162
|
+
if(authenticationData.state == 'used') throw 'authenticationUsed'
|
|
163
|
+
const actionName = authenticationData.action
|
|
164
|
+
const actionResults = await service.trigger({
|
|
165
|
+
type: actionName+'Authenticated',
|
|
166
|
+
...authenticationData.actionProperties,
|
|
167
|
+
contactType: authenticationData.contactType,
|
|
168
|
+
contact: authenticationData.contact,
|
|
169
|
+
session: client.session
|
|
170
|
+
})
|
|
171
|
+
const result = actionResults[0]
|
|
172
|
+
const targetPage = (typeof result == 'object')
|
|
173
|
+
? { ...authenticationData.targetPage, params: { ...authenticationData.targetPage.params, ...result } }
|
|
174
|
+
: authenticationData.targetPage
|
|
175
|
+
emit({
|
|
176
|
+
type: 'authenticationUsed',
|
|
177
|
+
authentication,
|
|
178
|
+
targetPage
|
|
179
|
+
})
|
|
180
|
+
return { result, targetPage }
|
|
181
|
+
}
|
|
182
|
+
})
|
|
183
|
+
|
|
184
|
+
definition.action({
|
|
185
|
+
name: 'resendMessageAuthentication',
|
|
186
|
+
waitForEvents: true,
|
|
187
|
+
properties: {
|
|
188
|
+
authentication: {
|
|
189
|
+
type: Authentication,
|
|
190
|
+
validation: ['nonEmpty']
|
|
191
|
+
}
|
|
192
|
+
},
|
|
193
|
+
async execute({ authentication }, { client, service }, emit) {
|
|
194
|
+
const authenticationData = await Authentication.get(authentication)
|
|
195
|
+
if(!authenticationData) throw 'notFound'
|
|
196
|
+
const { contactType, contact, action } = authenticationData
|
|
197
|
+
const secrets = await service.trigger({
|
|
198
|
+
type: 'refreshAuthenticationSecret',
|
|
199
|
+
authentication
|
|
200
|
+
})
|
|
201
|
+
if(secrets.length == 0) throw new Error('no secrets generated!')
|
|
202
|
+
const contactTypeUpperCase = contactType[0].toUpperCase() + contactType.slice(1)
|
|
203
|
+
await service.trigger({
|
|
204
|
+
type: 'send' + contactTypeUpperCase + 'Message',
|
|
205
|
+
render: {
|
|
206
|
+
action,
|
|
207
|
+
contactType,
|
|
208
|
+
contact,
|
|
209
|
+
secrets,
|
|
210
|
+
...authentication.messageData
|
|
211
|
+
}
|
|
212
|
+
})
|
|
213
|
+
return {
|
|
214
|
+
authentication,
|
|
215
|
+
secrets: secrets.map(({ secret, ...notSecret }) => notSecret)
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
})
|
|
219
|
+
|
|
220
|
+
module.exports = { Authentication, contactProperties, targetProperties, messageProperties }
|
package/definition.js
ADDED
package/index.js
CHANGED
|
@@ -1,414 +1,9 @@
|
|
|
1
|
-
const autoValidation = require('@live-change/framework/lib/processors/autoValidation')
|
|
2
|
-
const nodemailer = require('nodemailer')
|
|
3
1
|
const app = require("@live-change/framework").app()
|
|
4
2
|
|
|
5
|
-
const definition =
|
|
6
|
-
name: "messageAuthentication"
|
|
7
|
-
})
|
|
3
|
+
const definition = require('./definition.js')
|
|
8
4
|
const config = definition.config
|
|
9
5
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
type: String,
|
|
13
|
-
validation: ['nonEmpty']
|
|
14
|
-
},
|
|
15
|
-
contact: {
|
|
16
|
-
type: String,
|
|
17
|
-
validation: ['nonEmpty']
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const targetProperties = {
|
|
22
|
-
action: {
|
|
23
|
-
type: String,
|
|
24
|
-
validation: ['nonEmpty'],
|
|
25
|
-
},
|
|
26
|
-
actionProperties: {
|
|
27
|
-
type: Object
|
|
28
|
-
},
|
|
29
|
-
targetPage: {
|
|
30
|
-
type: Object,
|
|
31
|
-
validation: ['nonEmpty'],
|
|
32
|
-
properties: {
|
|
33
|
-
name: {
|
|
34
|
-
type: String,
|
|
35
|
-
validation: ['nonEmpty']
|
|
36
|
-
},
|
|
37
|
-
params: {
|
|
38
|
-
type: Object
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const messageProperties = {
|
|
45
|
-
messageData: {
|
|
46
|
-
type: Object
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const Authentication = definition.model({
|
|
51
|
-
name: 'Authentication',
|
|
52
|
-
properties: {
|
|
53
|
-
...contactProperties,
|
|
54
|
-
...targetProperties,
|
|
55
|
-
...messageProperties,
|
|
56
|
-
state: {
|
|
57
|
-
type: "String",
|
|
58
|
-
validation: ['nonEmpty'],
|
|
59
|
-
options: ['created', 'used']
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
})
|
|
63
|
-
|
|
64
|
-
definition.view({
|
|
65
|
-
name: "authentication",
|
|
66
|
-
properties: {
|
|
67
|
-
authentication: {
|
|
68
|
-
type: Authentication
|
|
69
|
-
}
|
|
70
|
-
},
|
|
71
|
-
daoPath({ authentication }, { client, context }) {
|
|
72
|
-
return Authentication.path(authentication)
|
|
73
|
-
}
|
|
74
|
-
})
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
definition.event({
|
|
78
|
-
name: 'authenticationCreated',
|
|
79
|
-
execute({ authentication, contactType, contact, action, actionProperties, targetPage, messageData }) {
|
|
80
|
-
return Authentication.create({
|
|
81
|
-
id: authentication,
|
|
82
|
-
contactType, contact,
|
|
83
|
-
action, actionProperties, targetPage,
|
|
84
|
-
messageData,
|
|
85
|
-
state: 'created'
|
|
86
|
-
})
|
|
87
|
-
}
|
|
88
|
-
})
|
|
89
|
-
|
|
90
|
-
definition.event({
|
|
91
|
-
name: 'authenticationUsed',
|
|
92
|
-
execute({ authentication }) {
|
|
93
|
-
return Authentication.update(authentication, { state: 'used' })
|
|
94
|
-
}
|
|
95
|
-
})
|
|
96
|
-
|
|
97
|
-
definition.trigger({
|
|
98
|
-
name: 'authenticateWithMessage',
|
|
99
|
-
waitForEvents: true,
|
|
100
|
-
properties: {
|
|
101
|
-
...contactProperties,
|
|
102
|
-
...targetProperties,
|
|
103
|
-
...messageProperties
|
|
104
|
-
},
|
|
105
|
-
async execute({ contactType, contact, action, actionProperties, targetPage, messageData },
|
|
106
|
-
{ client, service }, emit) {
|
|
107
|
-
const authentication = app.generateUid()
|
|
108
|
-
const secrets = await service.trigger({
|
|
109
|
-
type: 'authenticationSecret',
|
|
110
|
-
authentication
|
|
111
|
-
})
|
|
112
|
-
if(secrets.length == 0) throw new Error('no secrets generated!')
|
|
113
|
-
const contactTypeUpperCase = contactType[0].toUpperCase() + contactType.slice(1)
|
|
114
|
-
await service.trigger({
|
|
115
|
-
type: 'send' + contactTypeUpperCase + 'Message',
|
|
116
|
-
render: {
|
|
117
|
-
action,
|
|
118
|
-
contactType,
|
|
119
|
-
contact,
|
|
120
|
-
secrets,
|
|
121
|
-
...messageData
|
|
122
|
-
}
|
|
123
|
-
})
|
|
124
|
-
emit({
|
|
125
|
-
type: "authenticationCreated",
|
|
126
|
-
authentication,
|
|
127
|
-
contactType,
|
|
128
|
-
contact,
|
|
129
|
-
action,
|
|
130
|
-
actionProperties,
|
|
131
|
-
targetPage,
|
|
132
|
-
messageData
|
|
133
|
-
})
|
|
134
|
-
return {
|
|
135
|
-
type: 'sent',
|
|
136
|
-
authentication,
|
|
137
|
-
secrets: secrets.map(({ secret, ...notSecret }) => notSecret)
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
})
|
|
141
|
-
|
|
142
|
-
definition.action({
|
|
143
|
-
name: 'finishMessageAuthentication',
|
|
144
|
-
waitForEvents: true,
|
|
145
|
-
properties: {
|
|
146
|
-
secretType: {
|
|
147
|
-
type: String,
|
|
148
|
-
validation: ['nonEmpty']
|
|
149
|
-
},
|
|
150
|
-
secret: {
|
|
151
|
-
type: String,
|
|
152
|
-
validation: ['nonEmpty']
|
|
153
|
-
},
|
|
154
|
-
authentication: {
|
|
155
|
-
type: Authentication
|
|
156
|
-
}
|
|
157
|
-
},
|
|
158
|
-
async execute({ secretType, secret, authentication = undefined }, { client, service }, emit) {
|
|
159
|
-
const secretTypeUpperCase = secretType[0].toUpperCase() + secretType.slice(1)
|
|
160
|
-
const checkResults = await service.trigger({
|
|
161
|
-
type: 'check' + secretTypeUpperCase + 'Secret',
|
|
162
|
-
secret,
|
|
163
|
-
authentication
|
|
164
|
-
})
|
|
165
|
-
authentication = checkResults[0]
|
|
166
|
-
const authenticationData = await Authentication.get(authentication)
|
|
167
|
-
if(authenticationData.state == 'used') throw 'authenticationUsed'
|
|
168
|
-
const actionName = authenticationData.action
|
|
169
|
-
const actionResults = await service.trigger({
|
|
170
|
-
type: actionName+'Authenticated',
|
|
171
|
-
...authenticationData.actionProperties,
|
|
172
|
-
contactType: authenticationData.contactType,
|
|
173
|
-
contact: authenticationData.contact,
|
|
174
|
-
session: client.session
|
|
175
|
-
})
|
|
176
|
-
emit({
|
|
177
|
-
type: 'authenticationUsed',
|
|
178
|
-
authentication
|
|
179
|
-
})
|
|
180
|
-
return {
|
|
181
|
-
result: actionResults[0],
|
|
182
|
-
targetPage: authenticationData.targetPage
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
})
|
|
186
|
-
|
|
187
|
-
definition.action({
|
|
188
|
-
name: 'resendMessageAuthentication',
|
|
189
|
-
waitForEvents: true,
|
|
190
|
-
properties: {
|
|
191
|
-
authentication: {
|
|
192
|
-
type: Authentication,
|
|
193
|
-
validation: ['nonEmpty']
|
|
194
|
-
}
|
|
195
|
-
},
|
|
196
|
-
async execute({ authentication }, { client, service }, emit) {
|
|
197
|
-
const authenticationData = await Authentication.get(authentication)
|
|
198
|
-
if(!authenticationData) throw 'notFound'
|
|
199
|
-
const { contactType, contact, action } = authenticationData
|
|
200
|
-
const secrets = await service.trigger({
|
|
201
|
-
type: 'refreshAuthenticationSecret',
|
|
202
|
-
authentication
|
|
203
|
-
})
|
|
204
|
-
if(secrets.length == 0) throw new Error('no secrets generated!')
|
|
205
|
-
const contactTypeUpperCase = contactType[0].toUpperCase() + contactType.slice(1)
|
|
206
|
-
await service.trigger({
|
|
207
|
-
type: 'send' + contactTypeUpperCase + 'Message',
|
|
208
|
-
render: {
|
|
209
|
-
action,
|
|
210
|
-
contactType,
|
|
211
|
-
contact,
|
|
212
|
-
secrets,
|
|
213
|
-
...authentication.messageData
|
|
214
|
-
}
|
|
215
|
-
})
|
|
216
|
-
return {
|
|
217
|
-
authentication,
|
|
218
|
-
secrets: secrets.map(({ secret, ...notSecret }) => notSecret)
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
})
|
|
222
|
-
|
|
223
|
-
definition.trigger({
|
|
224
|
-
name: 'signUpWithMessageAuthenticated',
|
|
225
|
-
waitForEvents: true,
|
|
226
|
-
properties: {
|
|
227
|
-
...contactProperties
|
|
228
|
-
},
|
|
229
|
-
async execute({ contactType, contact, session }, { service }, emit) {
|
|
230
|
-
const contactTypeUpperCase = contactType[0].toUpperCase() + contactType.slice(1)
|
|
231
|
-
const user = app.generateUid()
|
|
232
|
-
await service.trigger({
|
|
233
|
-
type: 'connect' + contactTypeUpperCase,
|
|
234
|
-
[contactType]: contact,
|
|
235
|
-
user
|
|
236
|
-
})
|
|
237
|
-
await service.trigger({
|
|
238
|
-
type: 'signUpAndSignIn',
|
|
239
|
-
user, session
|
|
240
|
-
})
|
|
241
|
-
return user
|
|
242
|
-
}
|
|
243
|
-
})
|
|
244
|
-
|
|
245
|
-
definition.trigger({
|
|
246
|
-
name: 'signInWithMessageAuthenticated',
|
|
247
|
-
waitForEvents: true,
|
|
248
|
-
properties: {
|
|
249
|
-
...contactProperties
|
|
250
|
-
},
|
|
251
|
-
async execute({ contactType, contact, session }, { client, service }, emit) {
|
|
252
|
-
const contactTypeUpperCase = contactType[0].toUpperCase() + contactType.slice(1)
|
|
253
|
-
const user = await service.trigger({
|
|
254
|
-
type: 'signIn' + contactTypeUpperCase,
|
|
255
|
-
[contactType]: contact,
|
|
256
|
-
session
|
|
257
|
-
})
|
|
258
|
-
return user
|
|
259
|
-
}
|
|
260
|
-
})
|
|
261
|
-
|
|
262
|
-
definition.trigger({
|
|
263
|
-
name: 'connectWithMessageAuthenticated',
|
|
264
|
-
waitForEvents: true,
|
|
265
|
-
properties: {
|
|
266
|
-
...contactProperties
|
|
267
|
-
},
|
|
268
|
-
async execute({ contactType, contact, user }, { client, service }, emit) {
|
|
269
|
-
const contactTypeUpperCase = contactType[0].toUpperCase() + contactType.slice(1)
|
|
270
|
-
await service.trigger({
|
|
271
|
-
type: 'connect' + contactTypeUpperCase,
|
|
272
|
-
[contactType]: contact,
|
|
273
|
-
user
|
|
274
|
-
})
|
|
275
|
-
return user
|
|
276
|
-
}
|
|
277
|
-
})
|
|
278
|
-
|
|
279
|
-
for(const contactType of config.contactTypes) {
|
|
280
|
-
const contactTypeUpperCaseName = contactType[0].toUpperCase() + contactType.slice(1)
|
|
281
|
-
|
|
282
|
-
const contactConfig = (typeof contactType == "string") ? { name: contactType } : contactType
|
|
283
|
-
|
|
284
|
-
const contactTypeName = contactConfig.name
|
|
285
|
-
const contactTypeUName = contactTypeName[0].toUpperCase() + contactTypeName.slice(1)
|
|
286
|
-
|
|
287
|
-
const contactTypeProperties = {
|
|
288
|
-
[contactType]: {
|
|
289
|
-
type: String,
|
|
290
|
-
validation: ['nonEmpty', contactTypeName]
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
if(contactConfig.signUp || config.signUp) {
|
|
295
|
-
definition.action({
|
|
296
|
-
name: 'signUp' + contactTypeUpperCaseName,
|
|
297
|
-
waitForEvents: true,
|
|
298
|
-
properties: {
|
|
299
|
-
...contactTypeProperties
|
|
300
|
-
},
|
|
301
|
-
async execute({ [contactTypeName]: contact }, { client, service }, emit) {
|
|
302
|
-
await service.trigger({
|
|
303
|
-
type: 'checkNew' + contactTypeUName,
|
|
304
|
-
[contactType]: contact,
|
|
305
|
-
})
|
|
306
|
-
return service.triggerService(definition.name, {
|
|
307
|
-
type: 'authenticateWithMessage',
|
|
308
|
-
contactType,
|
|
309
|
-
contact,
|
|
310
|
-
action: 'signUpWithMessage',
|
|
311
|
-
targetPage: config.signUpTargetPage || { name: 'user:signUpFinished' }
|
|
312
|
-
})
|
|
313
|
-
}
|
|
314
|
-
})
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
if(contactConfig.signUp || config.signUp || contactConfig.signIn || config.signIn) {
|
|
318
|
-
definition.action({
|
|
319
|
-
name: 'signIn' + contactTypeUpperCaseName,
|
|
320
|
-
waitForEvents: true,
|
|
321
|
-
properties: {
|
|
322
|
-
...contactTypeProperties
|
|
323
|
-
},
|
|
324
|
-
async execute({ [contactTypeName]: contact }, { client, service }, emit) {
|
|
325
|
-
const contactData = await service.trigger({
|
|
326
|
-
type: 'get' + contactTypeUName,
|
|
327
|
-
[contactType]: contact,
|
|
328
|
-
})
|
|
329
|
-
const messageData = {
|
|
330
|
-
user: contactData.user
|
|
331
|
-
}
|
|
332
|
-
return service.triggerService(definition.name, {
|
|
333
|
-
type: 'authenticateWithMessage',
|
|
334
|
-
contactType,
|
|
335
|
-
contact,
|
|
336
|
-
messageData,
|
|
337
|
-
action: 'signInWithMessage',
|
|
338
|
-
targetPage: config.signInTargetPage || { name: 'user:signInFinished' }
|
|
339
|
-
})
|
|
340
|
-
}
|
|
341
|
-
})
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
if(contactConfig.connect || config.connect ) {
|
|
345
|
-
definition.action({
|
|
346
|
-
name: 'connect' + contactTypeUpperCaseName,
|
|
347
|
-
properties: {
|
|
348
|
-
...contactTypeProperties
|
|
349
|
-
},
|
|
350
|
-
access: (params, { client }) => {
|
|
351
|
-
return !!client.user
|
|
352
|
-
},
|
|
353
|
-
async execute({ [contactTypeName]: contact }, { client, service }, emit) {
|
|
354
|
-
await service.trigger({
|
|
355
|
-
type: 'checkNew' + contactTypeUName,
|
|
356
|
-
[contactType]: contact,
|
|
357
|
-
})
|
|
358
|
-
return service.triggerService(definition.name, {
|
|
359
|
-
type: 'authenticateWithMessage',
|
|
360
|
-
contactType,
|
|
361
|
-
contact,
|
|
362
|
-
action: 'connectWithMessage',
|
|
363
|
-
actionProperties: {
|
|
364
|
-
user: client.user
|
|
365
|
-
},
|
|
366
|
-
messageData: {
|
|
367
|
-
user: client.user
|
|
368
|
-
},
|
|
369
|
-
targetPage: config.signUpTargetPage || { name: 'user:connectFinished' }
|
|
370
|
-
})
|
|
371
|
-
}
|
|
372
|
-
})
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
if(contactConfig.signUp || config.signUp) {
|
|
376
|
-
definition.action({
|
|
377
|
-
name: 'signInOrSignUp' + contactTypeUpperCaseName,
|
|
378
|
-
waitForEvents: true,
|
|
379
|
-
properties: {
|
|
380
|
-
...contactTypeProperties
|
|
381
|
-
},
|
|
382
|
-
async execute({ [contactType]: contact }, { client, service }, emit) {
|
|
383
|
-
const contactData = await service.trigger({
|
|
384
|
-
type: 'get' + contactTypeUName + 'OrNull',
|
|
385
|
-
[contactType]: contact,
|
|
386
|
-
})
|
|
387
|
-
if(contactData) {
|
|
388
|
-
const messageData = {
|
|
389
|
-
user: contactData.user
|
|
390
|
-
}
|
|
391
|
-
return service.triggerService(definition.name, {
|
|
392
|
-
type: 'authenticateWithMessage',
|
|
393
|
-
contactType,
|
|
394
|
-
contact,
|
|
395
|
-
messageData,
|
|
396
|
-
action: 'signInWithMessage',
|
|
397
|
-
targetPage: config.signInTargetPage || { name: 'user:signInFinished' }
|
|
398
|
-
})
|
|
399
|
-
} else {
|
|
400
|
-
return service.triggerService(definition.name, {
|
|
401
|
-
type: 'authenticateWithMessage',
|
|
402
|
-
contactType,
|
|
403
|
-
contact,
|
|
404
|
-
action: 'signUpWithMessage',
|
|
405
|
-
targetPage: config.signUpTargetPage || { name: 'user:signUpFinished' }
|
|
406
|
-
})
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
})
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
}
|
|
6
|
+
require('./authentication.js')
|
|
7
|
+
require('./sign.js')
|
|
413
8
|
|
|
414
9
|
module.exports = definition
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@live-change/message-authentication-service",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.14",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -24,5 +24,5 @@
|
|
|
24
24
|
"@live-change/framework": "^0.5.7",
|
|
25
25
|
"nodemailer": "^6.7.2"
|
|
26
26
|
},
|
|
27
|
-
"gitHead": "
|
|
27
|
+
"gitHead": "93745ce6f6f819a9c77fb49fa6fd02fcaaf2b349"
|
|
28
28
|
}
|
package/sign.js
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
const app = require("@live-change/framework").app()
|
|
2
|
+
const definition = require('./definition.js')
|
|
3
|
+
const config = definition.config
|
|
4
|
+
|
|
5
|
+
const { contactProperties } = require('./authentication.js')
|
|
6
|
+
|
|
7
|
+
definition.trigger({
|
|
8
|
+
name: 'signUpWithMessageAuthenticated',
|
|
9
|
+
waitForEvents: true,
|
|
10
|
+
properties: {
|
|
11
|
+
...contactProperties
|
|
12
|
+
},
|
|
13
|
+
async execute({ contactType, contact, session }, { service }, emit) {
|
|
14
|
+
const contactTypeUpperCase = contactType[0].toUpperCase() + contactType.slice(1)
|
|
15
|
+
const user = app.generateUid()
|
|
16
|
+
await service.trigger({
|
|
17
|
+
type: 'connect' + contactTypeUpperCase,
|
|
18
|
+
[contactType]: contact,
|
|
19
|
+
user
|
|
20
|
+
})
|
|
21
|
+
await service.trigger({
|
|
22
|
+
type: 'signUpAndSignIn',
|
|
23
|
+
user, session
|
|
24
|
+
})
|
|
25
|
+
return user
|
|
26
|
+
}
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
definition.trigger({
|
|
30
|
+
name: 'signInWithMessageAuthenticated',
|
|
31
|
+
waitForEvents: true,
|
|
32
|
+
properties: {
|
|
33
|
+
...contactProperties
|
|
34
|
+
},
|
|
35
|
+
async execute({ contactType, contact, session }, { client, service }, emit) {
|
|
36
|
+
const contactTypeUpperCase = contactType[0].toUpperCase() + contactType.slice(1)
|
|
37
|
+
const user = await service.trigger({
|
|
38
|
+
type: 'signIn' + contactTypeUpperCase,
|
|
39
|
+
[contactType]: contact,
|
|
40
|
+
session
|
|
41
|
+
})
|
|
42
|
+
return user
|
|
43
|
+
}
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
definition.trigger({
|
|
47
|
+
name: 'connectWithMessageAuthenticated',
|
|
48
|
+
waitForEvents: true,
|
|
49
|
+
properties: {
|
|
50
|
+
...contactProperties
|
|
51
|
+
},
|
|
52
|
+
async execute({ contactType, contact, user }, { client, service }, emit) {
|
|
53
|
+
const contactTypeUpperCase = contactType[0].toUpperCase() + contactType.slice(1)
|
|
54
|
+
await service.trigger({
|
|
55
|
+
type: 'connect' + contactTypeUpperCase,
|
|
56
|
+
[contactType]: contact,
|
|
57
|
+
user
|
|
58
|
+
})
|
|
59
|
+
return user
|
|
60
|
+
}
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
for(const contactType of config.contactTypes) {
|
|
64
|
+
|
|
65
|
+
const contactConfig = (typeof contactType == "string") ? { name: contactType } : contactType
|
|
66
|
+
const contactTypeName = contactConfig.name
|
|
67
|
+
const contactTypeUName = contactTypeName[0].toUpperCase() + contactTypeName.slice(1)
|
|
68
|
+
|
|
69
|
+
const contactTypeProperties = {
|
|
70
|
+
[contactTypeName]: {
|
|
71
|
+
type: String,
|
|
72
|
+
validation: ['nonEmpty', contactTypeName]
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if(contactConfig.signUp || config.signUp) {
|
|
77
|
+
definition.action({
|
|
78
|
+
name: 'signUp' + contactTypeUName,
|
|
79
|
+
waitForEvents: true,
|
|
80
|
+
properties: {
|
|
81
|
+
...contactTypeProperties
|
|
82
|
+
},
|
|
83
|
+
async execute({ [contactTypeName]: contact }, { client, service }, emit) {
|
|
84
|
+
await service.trigger({
|
|
85
|
+
type: 'checkNew' + contactTypeUName,
|
|
86
|
+
[contactTypeName]: contact,
|
|
87
|
+
})
|
|
88
|
+
return service.triggerService(definition.name, {
|
|
89
|
+
type: 'authenticateWithMessage',
|
|
90
|
+
contactType,
|
|
91
|
+
contact,
|
|
92
|
+
action: 'signUpWithMessage',
|
|
93
|
+
targetPage: config.signUpTargetPage || { name: 'user:signUpFinished' }
|
|
94
|
+
})
|
|
95
|
+
}
|
|
96
|
+
})
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if(contactConfig.signUp || config.signUp || contactConfig.signIn || config.signIn) {
|
|
100
|
+
definition.action({
|
|
101
|
+
name: 'signIn' + contactTypeUName,
|
|
102
|
+
waitForEvents: true,
|
|
103
|
+
properties: {
|
|
104
|
+
...contactTypeProperties
|
|
105
|
+
},
|
|
106
|
+
async execute({ [contactTypeName]: contact }, { client, service }, emit) {
|
|
107
|
+
const contactData = await service.trigger({
|
|
108
|
+
type: 'get' + contactTypeUName,
|
|
109
|
+
[contactTypeName]: contact,
|
|
110
|
+
})
|
|
111
|
+
const messageData = {
|
|
112
|
+
user: contactData.user
|
|
113
|
+
}
|
|
114
|
+
return service.triggerService(definition.name, {
|
|
115
|
+
type: 'authenticateWithMessage',
|
|
116
|
+
contactType,
|
|
117
|
+
contact,
|
|
118
|
+
messageData,
|
|
119
|
+
action: 'signInWithMessage',
|
|
120
|
+
targetPage: config.signInTargetPage || { name: 'user:signInFinished' }
|
|
121
|
+
})
|
|
122
|
+
}
|
|
123
|
+
})
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if(contactConfig.connect || config.connect ) {
|
|
127
|
+
definition.action({
|
|
128
|
+
name: 'connect' + contactTypeUName,
|
|
129
|
+
properties: {
|
|
130
|
+
...contactTypeProperties
|
|
131
|
+
},
|
|
132
|
+
access: (params, { client }) => {
|
|
133
|
+
return !!client.user
|
|
134
|
+
},
|
|
135
|
+
async execute({ [contactTypeName]: contact }, { client, service }, emit) {
|
|
136
|
+
await service.trigger({
|
|
137
|
+
type: 'checkNew' + contactTypeUName,
|
|
138
|
+
[contactTypeName]: contact,
|
|
139
|
+
})
|
|
140
|
+
return service.triggerService(definition.name, {
|
|
141
|
+
type: 'authenticateWithMessage',
|
|
142
|
+
contactType,
|
|
143
|
+
contact,
|
|
144
|
+
action: 'connectWithMessage',
|
|
145
|
+
actionProperties: {
|
|
146
|
+
user: client.user
|
|
147
|
+
},
|
|
148
|
+
messageData: {
|
|
149
|
+
user: client.user
|
|
150
|
+
},
|
|
151
|
+
targetPage: config.signUpTargetPage || { name: 'user:connectFinished' }
|
|
152
|
+
})
|
|
153
|
+
}
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
definition.action({
|
|
157
|
+
name: 'disconnect' + contactTypeUName,
|
|
158
|
+
properties: {
|
|
159
|
+
...contactTypeProperties
|
|
160
|
+
},
|
|
161
|
+
access: (params, { client }) => {
|
|
162
|
+
return !!client.user
|
|
163
|
+
},
|
|
164
|
+
async execute({ [contactTypeName]: contact }, { client, service }, emit) {
|
|
165
|
+
const contacts = (await service.trigger({
|
|
166
|
+
type: 'getConnectedContacts',
|
|
167
|
+
user: client.user
|
|
168
|
+
})).flat()
|
|
169
|
+
console.log("CONTACTS", contacts, contactTypeName, contact)
|
|
170
|
+
const contactData = contacts.find(c => c.type == contactTypeName && c.contact == contact)
|
|
171
|
+
if(!contactData) throw 'notFound'
|
|
172
|
+
if(contacts.length == 1) throw 'lastOne'
|
|
173
|
+
console.log("DISCONNECT", contact)
|
|
174
|
+
return await service.trigger({
|
|
175
|
+
type: 'disconnect' + contactTypeUName,
|
|
176
|
+
[contactTypeName]: contact,
|
|
177
|
+
user: client.user
|
|
178
|
+
})
|
|
179
|
+
}
|
|
180
|
+
})
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if(contactConfig.signUp || config.signUp) {
|
|
184
|
+
definition.action({
|
|
185
|
+
name: 'signInOrSignUp' + contactTypeUName,
|
|
186
|
+
waitForEvents: true,
|
|
187
|
+
properties: {
|
|
188
|
+
...contactTypeProperties
|
|
189
|
+
},
|
|
190
|
+
async execute({ [contactTypeName]: contact }, { client, service }, emit) {
|
|
191
|
+
const contactData = await service.trigger({
|
|
192
|
+
type: 'get' + contactTypeUName + 'OrNull',
|
|
193
|
+
[contactTypeName]: contact,
|
|
194
|
+
})
|
|
195
|
+
if(contactData) {
|
|
196
|
+
const messageData = {
|
|
197
|
+
user: contactData.user
|
|
198
|
+
}
|
|
199
|
+
return service.triggerService(definition.name, {
|
|
200
|
+
type: 'authenticateWithMessage',
|
|
201
|
+
contactType,
|
|
202
|
+
contact,
|
|
203
|
+
messageData,
|
|
204
|
+
action: 'signInWithMessage',
|
|
205
|
+
targetPage: config.signInTargetPage || { name: 'user:signInFinished' }
|
|
206
|
+
})
|
|
207
|
+
} else {
|
|
208
|
+
return service.triggerService(definition.name, {
|
|
209
|
+
type: 'authenticateWithMessage',
|
|
210
|
+
contactType,
|
|
211
|
+
contact,
|
|
212
|
+
action: 'signUpWithMessage',
|
|
213
|
+
targetPage: config.signUpTargetPage || { name: 'user:signUpFinished' }
|
|
214
|
+
})
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
})
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
}
|