@live-change/password-authentication-service 0.2.5 → 0.2.15

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/change.js ADDED
@@ -0,0 +1,47 @@
1
+ const definition = require('./definition.js')
2
+ const config = definition.config
3
+ const { PasswordAuthentication, secretProperties } = require('./model.js')
4
+
5
+ definition.action({
6
+ name: 'setPassword',
7
+ waitForEvents: true,
8
+ properties: {
9
+ ...secretProperties
10
+ },
11
+ access: (params, { client }) => {
12
+ return !!client.user
13
+ },
14
+ async execute({ passwordHash }, { client, service }, emit) {
15
+ const user = client.user
16
+ const passwordAuthenticationData = await PasswordAuthentication.get(user)
17
+ if(passwordAuthenticationData) throw 'exists'
18
+ emit({
19
+ type: 'passwordAuthenticationSet',
20
+ user, passwordHash
21
+ })
22
+ }
23
+ })
24
+
25
+ definition.action({
26
+ name: 'changePassword',
27
+ waitForEvents: true,
28
+ properties: {
29
+ currentPasswordHash: secretProperties.passwordHash,
30
+ ...secretProperties
31
+ },
32
+ access: (params, { client }) => {
33
+ return !!client.user
34
+ },
35
+ async execute({ currentPasswordHash, passwordHash }, { client, service }, emit) {
36
+ const user = client.user
37
+ const passwordAuthenticationData = await PasswordAuthentication.get(user)
38
+ if(!passwordAuthenticationData) throw 'notFound'
39
+ if(currentPasswordHash != passwordAuthenticationData.passwordHash) throw { properties: {
40
+ currentPasswordHash: 'wrongPassword'
41
+ } }
42
+ emit({
43
+ type: 'passwordAuthenticationSet',
44
+ user, passwordHash
45
+ })
46
+ }
47
+ })
package/definition.js ADDED
@@ -0,0 +1,9 @@
1
+ const app = require("@live-change/framework").app()
2
+ const user = require('@live-change/user-service')
3
+
4
+ const definition = app.createServiceDefinition({
5
+ name: "passwordAuthentication",
6
+ use: [ user ]
7
+ })
8
+
9
+ module.exports = definition
package/index.js CHANGED
@@ -1,74 +1,9 @@
1
- const autoValidation = require('@live-change/framework/lib/processors/autoValidation')
2
- const nodemailer = require('nodemailer')
3
- const app = require("@live-change/framework").app()
1
+ const definition = require('./definition.js')
4
2
 
5
- const definition = app.createServiceDefinition({
6
- name: "passwordAuthentication"
7
- })
8
- const config = definition.config
9
-
10
- const secretProperties = {
11
- passwordHash: {
12
- type: String,
13
- secret: true,
14
- preFilter: config.passwordHash ||
15
- (password => password && crypto.createHash('sha256').update(password).digest('hex')),
16
- validation: config.passwordValidation || ['password']
17
- },
18
- }
19
-
20
- for(const contactType of config.contactTypes) {
21
- const contactTypeUpperCaseName = contactType[0].toUpperCase() + contactType.slice(1)
22
-
23
- const contactConfig = (typeof contactType == "string") ? { name: contactType } : contactType
24
-
25
- const contactTypeName = contactConfig.name
26
- const contactTypeUName = contactTypeName[0].toUpperCase() + contactTypeName.slice(1)
27
-
28
- const contactTypeProperties = {
29
- [contactType]: {
30
- type: String,
31
- validation: ['nonEmpty', contactTypeName]
32
- }
33
- }
34
-
35
- definition.action({
36
- name: 'signIn' + contactTypeUpperCaseName,
37
- waitForEvents: true,
38
- properties: {
39
- ...contactTypeProperties,
40
- ...secretProperties
41
- },
42
- async execute({ [contactTypeName]: contact, passwordHash }, { client, service }, emit) {
43
- await service.trigger({
44
- type: 'checkExisting' + contactTypeUName,
45
- [contactType]: contact,
46
- })
47
- if(passwordHash) { // login with password
48
- throw Error('not implemented yet')
49
- } else { // login without password - with message
50
- if(!config.signInWithoutPassword) throw { properties: { passwordHash: 'empty' } }
51
- const contactData = (await service.trigger({
52
- type: 'get' + contactTypeUName,
53
- [contactType]: contact,
54
- }))[0]
55
- const messageData = {
56
- user: contactData.user
57
- }
58
- const results = await service.trigger({
59
- type: 'authenticateWithMessage',
60
- contactType,
61
- contact,
62
- messageData,
63
- action: 'signInWithMessage',
64
- targetPage: config.signInTargetPage || { name: 'user:signInFinished' }
65
- })
66
- return results[0]
67
- }
68
- }
69
- })
70
-
71
- }
3
+ require('./model.js')
4
+ require('./change.js')
5
+ require('./signIn.js')
6
+ require('./reset.js')
72
7
 
73
8
  definition.processor(function(service, app) {
74
9
  service.validators.password = require('./passwordValidator.js')
package/model.js ADDED
@@ -0,0 +1,34 @@
1
+ const crypto = require('crypto')
2
+ const definition = require('./definition.js')
3
+ const config = definition.config
4
+
5
+ const secretProperties = {
6
+ passwordHash: {
7
+ type: String,
8
+ secret: true,
9
+ preFilter: config.passwordHash ||
10
+ (password => password && crypto.createHash('sha256').update(password).digest('hex')),
11
+ validation: config.passwordValidation || ['password']
12
+ },
13
+ }
14
+
15
+ const PasswordAuthentication = definition.model({
16
+ name: 'PasswordAuthentication',
17
+ userProperty: {
18
+ userViews: [
19
+ { suffix: 'Exists', fields: ['user'] }
20
+ ]
21
+ },
22
+ properties: {
23
+ ...secretProperties,
24
+ }
25
+ })
26
+
27
+ definition.event({
28
+ name: 'passwordAuthenticationSet',
29
+ async execute({ user, passwordHash }) {
30
+ await PasswordAuthentication.create({ id: user, user, passwordHash })
31
+ }
32
+ })
33
+
34
+ module.exports = { PasswordAuthentication, secretProperties }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/password-authentication-service",
3
- "version": "0.2.5",
3
+ "version": "0.2.15",
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": "7691357c7569a18373668691eb6a81a9e161194d"
27
+ "gitHead": "86bc53700aca24da682d1dd414428f03cdc8d2f5"
28
28
  }
package/reset.js ADDED
@@ -0,0 +1,158 @@
1
+ const app = require("@live-change/framework").app()
2
+ const { randomString } = require('@live-change/uid')
3
+ const definition = require('./definition.js')
4
+ const config = definition.config
5
+ const { PasswordAuthentication, secretProperties } = require('./model.js')
6
+
7
+ const User = definition.foreignModel('user', 'User')
8
+
9
+ const ResetPasswordAuthentication = definition.model({
10
+ name: 'ResetPasswordAuthentication',
11
+ userProperty: {
12
+ userReadAccess: () => true
13
+ },
14
+ properties: {
15
+ expire: {
16
+ type: Date,
17
+ validation: ['nonEmpty']
18
+ },
19
+ key: {
20
+ type: String,
21
+ validation: ['nonEmpty']
22
+ },
23
+ state: {
24
+ type: String,
25
+ validation: ['nonEmpty']
26
+ }
27
+ },
28
+ indexes: {
29
+ byKey: {
30
+ property: 'key'
31
+ }
32
+ }
33
+ })
34
+
35
+ definition.event({
36
+ name: 'resetPasswordAuthenticationCreated',
37
+ async execute({ resetPasswordAuthentication, user, key, expire }) {
38
+ await ResetPasswordAuthentication.create({ id: resetPasswordAuthentication, user, key, expire, state: 'created' })
39
+ }
40
+ })
41
+
42
+ definition.event({
43
+ name: 'resetPasswordAuthenticationUsed',
44
+ async execute({ resetPasswordAuthentication }) {
45
+ await ResetPasswordAuthentication.update(resetPasswordAuthentication, { state: 'used' })
46
+ }
47
+ })
48
+
49
+ definition.view({
50
+ name: "resetPasswordAuthentication",
51
+ properties: {
52
+ key: {
53
+ type: String,
54
+ validation: ['nonEmpty']
55
+ }
56
+ },
57
+ daoPath({ key }, { client, context }) {
58
+ return ResetPasswordAuthentication.indexObjectPath('byKey', key)
59
+ }
60
+ })
61
+
62
+ for(const contactType of config.contactTypes) {
63
+
64
+ const contactConfig = (typeof contactType == "string") ? { name: contactType } : contactType
65
+ const contactTypeName = contactConfig.name
66
+ const contactTypeUName = contactTypeName[0].toUpperCase() + contactTypeName.slice(1)
67
+
68
+ const contactTypeProperties = {
69
+ [contactTypeName]: {
70
+ type: String,
71
+ validation: ['nonEmpty', contactTypeName]
72
+ }
73
+ }
74
+
75
+ definition.action({
76
+ name: 'resetPassword' + contactTypeUName,
77
+ waitForEvents: true,
78
+ properties: {
79
+ ...contactTypeProperties
80
+ },
81
+ async execute({ [contactTypeName]: contact }, { client, service }, emit) {
82
+ const contactData = (await service.trigger({
83
+ type: 'get' + contactTypeUName,
84
+ [contactTypeName]: contact,
85
+ }))[0]
86
+ const { user } = contactData
87
+ const messageData = { user }
88
+ const actionProperties = { user }
89
+ return (await service.trigger({
90
+ type: 'authenticateWithMessage',
91
+ contactType,
92
+ contact,
93
+ messageData,
94
+ action: 'startResetPasswordWithMessage',
95
+ actionProperties,
96
+ targetPage: config.resetPasswordTargetPage || { name: 'user:resetPasswordForm' }
97
+ }))[0]
98
+ }
99
+ })
100
+
101
+ }
102
+
103
+ definition.trigger({
104
+ name: 'startResetPasswordWithMessageAuthenticated',
105
+ waitForEvents: true,
106
+ properties: {
107
+ user: {
108
+ type: User,
109
+ validation: ['nonEmpty']
110
+ }
111
+ },
112
+ async execute({ user, session }, { client, service }, emit) {
113
+ if(!user) throw new Error('user required')
114
+ const resetPasswordAuthentication = app.generateUid()
115
+ const key = randomString(config.resetKeyLength || 16)
116
+ const expire = new Date()
117
+ expire.setTime(Date.now() + (config.resetExpireTime || 1*60*60*1000))
118
+ service.trigger({
119
+ type: 'signIn',
120
+ user, session
121
+ })
122
+ emit({
123
+ type: 'resetPasswordAuthenticationCreated',
124
+ resetPasswordAuthentication,
125
+ user, key, expire
126
+ })
127
+ return {
128
+ resetKey: key
129
+ }
130
+ }
131
+ })
132
+
133
+ definition.action({
134
+ name: 'finishResetPassword',
135
+ waitForEvents: true,
136
+ properties: {
137
+ key: {
138
+ type: String,
139
+ validation: ['nonEmpty']
140
+ },
141
+ ...secretProperties
142
+ },
143
+ async execute({ key, passwordHash }, { client, service }, emit) {
144
+ const resetPasswordAuthenticationData = await ResetPasswordAuthentication.indexObjectGet('byKey', key)
145
+ console.log("RESET AUTH", resetPasswordAuthenticationData)
146
+ if(!resetPasswordAuthenticationData) throw 'authenticationNotFound'
147
+ if(resetPasswordAuthenticationData.state == 'used') throw 'authenticationUsed'
148
+ if(resetPasswordAuthenticationData.expire < (new Date().toISOString())) throw 'authenticationExpired'
149
+ const { user } = resetPasswordAuthenticationData
150
+ emit([{
151
+ type: 'passwordAuthenticationSet',
152
+ user, passwordHash
153
+ }, {
154
+ type: 'resetPasswordAuthenticationUsed',
155
+ resetPasswordAuthentication: resetPasswordAuthenticationData.id
156
+ }])
157
+ }
158
+ })
package/signIn.js ADDED
@@ -0,0 +1,62 @@
1
+ const definition = require('./definition.js')
2
+ const config = definition.config
3
+
4
+ const { PasswordAuthentication, secretProperties } = require('./model.js')
5
+
6
+ for(const contactType of config.contactTypes) {
7
+ const contactTypeUpperCaseName = contactType[0].toUpperCase() + contactType.slice(1)
8
+
9
+ const contactConfig = (typeof contactType == "string") ? { name: contactType } : contactType
10
+
11
+ const contactTypeName = contactConfig.name
12
+ const contactTypeUName = contactTypeName[0].toUpperCase() + contactTypeName.slice(1)
13
+
14
+ const contactTypeProperties = {
15
+ [contactType]: {
16
+ type: String,
17
+ validation: ['nonEmpty', contactTypeName]
18
+ }
19
+ }
20
+
21
+ definition.action({
22
+ name: 'signIn' + contactTypeUpperCaseName,
23
+ waitForEvents: true,
24
+ properties: {
25
+ ...contactTypeProperties,
26
+ ...secretProperties
27
+ },
28
+ async execute({ [contactTypeName]: contact, passwordHash }, { client, service }, emit) {
29
+ const contactData = (await service.trigger({
30
+ type: 'get' + contactTypeUName,
31
+ [contactType]: contact,
32
+ }))[0]
33
+ const { user } = contactData
34
+ if(passwordHash) { // login with password
35
+ console.log("USER!", user)
36
+ const passwordAuthenticationData = await PasswordAuthentication.get(user)
37
+ console.log("PASSWORD AUTH!", passwordAuthenticationData)
38
+ if(!passwordAuthenticationData || passwordAuthenticationData.passwordHash != passwordHash)
39
+ throw { properties: { passwordHash: 'wrongPassword' } }
40
+ await service.trigger({
41
+ type: 'signIn', user, session
42
+ })
43
+ return user
44
+ } else { // login without password - with message
45
+ if(!config.signInWithoutPassword) throw { properties: { passwordHash: 'empty' } }
46
+ const messageData = {
47
+ user
48
+ }
49
+ const results = await service.trigger({
50
+ type: 'authenticateWithMessage',
51
+ contactType,
52
+ contact,
53
+ messageData,
54
+ action: 'signInWithMessage',
55
+ targetPage: config.signInTargetPage || { name: 'user:signInFinished' }
56
+ })
57
+ return results[0]
58
+ }
59
+ }
60
+ })
61
+
62
+ }