@live-change/password-authentication-service 0.2.7 → 0.2.12
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 +70 -3
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
|
-
const
|
|
2
|
-
const nodemailer = require('nodemailer')
|
|
1
|
+
const crypto = require('crypto')
|
|
3
2
|
const app = require("@live-change/framework").app()
|
|
3
|
+
const user = require('@live-change/user-service')
|
|
4
|
+
const { AuthenticatedUser } = require("../user-service/model.js")
|
|
4
5
|
|
|
5
6
|
const definition = app.createServiceDefinition({
|
|
6
|
-
name: "passwordAuthentication"
|
|
7
|
+
name: "passwordAuthentication",
|
|
8
|
+
use: [ user ]
|
|
7
9
|
})
|
|
8
10
|
const config = definition.config
|
|
9
11
|
|
|
12
|
+
const User = definition.foreignModel('user', 'User')
|
|
13
|
+
|
|
10
14
|
const secretProperties = {
|
|
11
15
|
passwordHash: {
|
|
12
16
|
type: String,
|
|
@@ -17,6 +21,69 @@ const secretProperties = {
|
|
|
17
21
|
},
|
|
18
22
|
}
|
|
19
23
|
|
|
24
|
+
const PasswordAuthentication = definition.model({
|
|
25
|
+
name: 'PasswordAuthentication',
|
|
26
|
+
userProperty: {
|
|
27
|
+
userViews: [
|
|
28
|
+
{ suffix: 'Exists', fields: ['user'] }
|
|
29
|
+
]
|
|
30
|
+
},
|
|
31
|
+
properties: {
|
|
32
|
+
...secretProperties,
|
|
33
|
+
}
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
definition.event({
|
|
37
|
+
name: 'passwordAuthenticationSet',
|
|
38
|
+
async execute({ user, passwordHash }) {
|
|
39
|
+
await PasswordAuthentication.create({ id: user, user, passwordHash })
|
|
40
|
+
}
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
definition.action({
|
|
44
|
+
name: 'setPassword',
|
|
45
|
+
waitForEvents: true,
|
|
46
|
+
properties: {
|
|
47
|
+
...secretProperties
|
|
48
|
+
},
|
|
49
|
+
access: (params, { client }) => {
|
|
50
|
+
return !!client.user
|
|
51
|
+
},
|
|
52
|
+
async execute({ passwordHash }, { client, service }, emit) {
|
|
53
|
+
const user = client.user
|
|
54
|
+
const passwordAuthenticationData = await PasswordAuthentication.get(user)
|
|
55
|
+
if(passwordAuthenticationData) throw 'exists'
|
|
56
|
+
emit({
|
|
57
|
+
type: 'passwordAuthenticationSet',
|
|
58
|
+
user, passwordHash
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
definition.action({
|
|
64
|
+
name: 'changePassword',
|
|
65
|
+
waitForEvents: true,
|
|
66
|
+
properties: {
|
|
67
|
+
currentPasswordHash: secretProperties.passwordHash,
|
|
68
|
+
...secretProperties
|
|
69
|
+
},
|
|
70
|
+
access: (params, { client }) => {
|
|
71
|
+
return !!client.user
|
|
72
|
+
},
|
|
73
|
+
async execute({ currentPasswordHash, passwordHash }, { client, service }, emit) {
|
|
74
|
+
const user = client.user
|
|
75
|
+
const passwordAuthenticationData = await PasswordAuthentication.get(user)
|
|
76
|
+
if(!passwordAuthenticationData) throw 'notFound'
|
|
77
|
+
if(currentPasswordHash != passwordAuthenticationData.passwordHash) throw { properties: {
|
|
78
|
+
currentPasswordHash: 'wrongPassword'
|
|
79
|
+
} }
|
|
80
|
+
emit({
|
|
81
|
+
type: 'passwordAuthenticationSet',
|
|
82
|
+
user, passwordHash
|
|
83
|
+
})
|
|
84
|
+
}
|
|
85
|
+
})
|
|
86
|
+
|
|
20
87
|
for(const contactType of config.contactTypes) {
|
|
21
88
|
const contactTypeUpperCaseName = contactType[0].toUpperCase() + contactType.slice(1)
|
|
22
89
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@live-change/password-authentication-service",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.12",
|
|
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": "a0dce761d803684a74e4cf4d50c63a0dc80dd4cf"
|
|
28
28
|
}
|