@live-change/session-service 0.1.11 → 0.1.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/authenticator.js +26 -23
- package/index.js +0 -46
- package/model.js +1 -119
- package/package.json +1 -1
- package/sessionProperty.js +0 -1
package/authenticator.js
CHANGED
|
@@ -3,30 +3,33 @@ const app = App.app()
|
|
|
3
3
|
const definition = require('./definition.js')
|
|
4
4
|
const Session = require('./model.js')
|
|
5
5
|
const { createHmac } = require('crypto')
|
|
6
|
+
const config = definition.config
|
|
6
7
|
|
|
7
|
-
definition.authenticator(
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
if(
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
8
|
+
definition.authenticator({
|
|
9
|
+
async prepareCredentials(credentials) {
|
|
10
|
+
const sessionKey = credentials.sessionKey
|
|
11
|
+
if(!sessionKey) throw new Error("sessionKey required!")
|
|
12
|
+
const sessions = await app.dao.get(
|
|
13
|
+
['database', 'indexRange', app.databaseName, Session.tableName + '_byKey', {
|
|
14
|
+
gt: `"${sessionKey}"_`,
|
|
15
|
+
lt: `"${sessionKey}"_\xFF`
|
|
16
|
+
}])
|
|
17
|
+
//console.log("FOUND SESSIONS", sessions)
|
|
18
|
+
let session = sessions[0]?.to
|
|
19
|
+
if(!session) {
|
|
20
|
+
if(config.createSessionOnUpdate) {
|
|
21
|
+
session = createHmac('sha256', config.sessionHmacSecret || 'secret')
|
|
22
|
+
.update(credentials.sessionKey)
|
|
23
|
+
.digest('base64').slice(0, 32)
|
|
24
|
+
} else {
|
|
25
|
+
const createResult = await app.triggerService(definition.name, {
|
|
26
|
+
type: "createSessionKeyIfNotExists",
|
|
27
|
+
sessionKey
|
|
28
|
+
})
|
|
29
|
+
//console.log("CREATE SESSION RESULT", createResult)
|
|
30
|
+
session = createResult.session
|
|
31
|
+
}
|
|
29
32
|
}
|
|
33
|
+
credentials.session = session
|
|
30
34
|
}
|
|
31
|
-
credentials.session = session
|
|
32
35
|
})
|
package/index.js
CHANGED
|
@@ -3,10 +3,6 @@ const app = App.app()
|
|
|
3
3
|
const definition = require('./definition.js')
|
|
4
4
|
const Session = require('./model.js')
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
const User = definition.foreignModel('user', 'User')
|
|
8
|
-
|
|
9
|
-
|
|
10
6
|
definition.view({
|
|
11
7
|
name: 'currentSession',
|
|
12
8
|
properties: {},
|
|
@@ -20,7 +16,6 @@ definition.view({
|
|
|
20
16
|
async (input, output, { session, tableName }) => {
|
|
21
17
|
const mapper = (obj) => (obj || {
|
|
22
18
|
id: session,
|
|
23
|
-
user: null,
|
|
24
19
|
roles: []
|
|
25
20
|
})
|
|
26
21
|
let storedObj = undefined
|
|
@@ -81,50 +76,9 @@ definition.trigger({
|
|
|
81
76
|
}
|
|
82
77
|
})
|
|
83
78
|
|
|
84
|
-
definition.action({
|
|
85
|
-
name: "logout",
|
|
86
|
-
properties: {
|
|
87
|
-
},
|
|
88
|
-
async execute({ session }, { client, service }, emit) {
|
|
89
|
-
if(!session) session = client.session
|
|
90
|
-
if(session != client.session) throw new Error("Wrong session id")
|
|
91
|
-
const sessionRow = await Session.get(session)
|
|
92
|
-
if(!sessionRow) throw 'notFound'
|
|
93
|
-
if(!sessionRow.user) throw "loggedOut"
|
|
94
|
-
emit({
|
|
95
|
-
type: "loggedOut",
|
|
96
|
-
session
|
|
97
|
-
})
|
|
98
|
-
await service.trigger({
|
|
99
|
-
type: "OnLogout",
|
|
100
|
-
user: sessionRow.user,
|
|
101
|
-
session: client.session
|
|
102
|
-
})
|
|
103
|
-
return 'loggedOut'
|
|
104
|
-
}
|
|
105
|
-
})
|
|
106
|
-
|
|
107
|
-
definition.trigger({
|
|
108
|
-
name: "UserDeleted",
|
|
109
|
-
properties: {
|
|
110
|
-
user: {
|
|
111
|
-
type: User,
|
|
112
|
-
idOnly: true
|
|
113
|
-
}
|
|
114
|
-
},
|
|
115
|
-
async execute({ user }, context, emit) {
|
|
116
|
-
emit([{
|
|
117
|
-
type: "UserDeleted",
|
|
118
|
-
user
|
|
119
|
-
}])
|
|
120
|
-
}
|
|
121
|
-
})
|
|
122
|
-
|
|
123
|
-
|
|
124
79
|
require('./authenticator.js')
|
|
125
80
|
require('./localIdValidator.js')
|
|
126
81
|
require('./sessionProperty.js')
|
|
127
82
|
require('./sessionItem.js')
|
|
128
83
|
|
|
129
|
-
|
|
130
84
|
module.exports = definition
|
package/model.js
CHANGED
|
@@ -1,29 +1,15 @@
|
|
|
1
1
|
const definition = require("./definition.js")
|
|
2
2
|
|
|
3
|
-
const User = definition.foreignModel('user', 'User')
|
|
4
|
-
|
|
5
3
|
const Session = definition.model({
|
|
6
4
|
name: "Session",
|
|
7
5
|
properties: {
|
|
8
6
|
key: {
|
|
9
7
|
type: String
|
|
10
|
-
},
|
|
11
|
-
user: {
|
|
12
|
-
type: User
|
|
13
|
-
},
|
|
14
|
-
roles: {
|
|
15
|
-
type: Array,
|
|
16
|
-
of: {
|
|
17
|
-
type: String
|
|
18
|
-
}
|
|
19
8
|
}
|
|
20
9
|
},
|
|
21
10
|
indexes: {
|
|
22
11
|
byKey: {
|
|
23
12
|
property: 'key'
|
|
24
|
-
},
|
|
25
|
-
byUser: {
|
|
26
|
-
property: "user"
|
|
27
13
|
}
|
|
28
14
|
}
|
|
29
15
|
})
|
|
@@ -39,114 +25,10 @@ definition.event({
|
|
|
39
25
|
}
|
|
40
26
|
},
|
|
41
27
|
async execute({ session, key }) {
|
|
42
|
-
console.log("SESSION CREATING!", session, "AT", (new Date()).toISOString())
|
|
43
28
|
await Session.create({
|
|
44
29
|
id: session,
|
|
45
|
-
key: key
|
|
46
|
-
user: null,
|
|
47
|
-
roles: []
|
|
30
|
+
key: key
|
|
48
31
|
})
|
|
49
|
-
console.log("SESSION CREATED!", session, "AT", (new Date()).toISOString())
|
|
50
|
-
}
|
|
51
|
-
})
|
|
52
|
-
|
|
53
|
-
definition.event({
|
|
54
|
-
name: "loggedIn",
|
|
55
|
-
properties: {
|
|
56
|
-
session: {
|
|
57
|
-
type: Session
|
|
58
|
-
},
|
|
59
|
-
user: {
|
|
60
|
-
type: User
|
|
61
|
-
},
|
|
62
|
-
roles: {
|
|
63
|
-
type: Array,
|
|
64
|
-
of: {
|
|
65
|
-
type: String
|
|
66
|
-
}
|
|
67
|
-
},
|
|
68
|
-
expire: {
|
|
69
|
-
type: Date
|
|
70
|
-
}
|
|
71
|
-
},
|
|
72
|
-
async execute({ session, user, roles, expire, language, timezone }) {
|
|
73
|
-
console.log("SESSION UPDATE", session, { user, roles, expire, language, timezone })
|
|
74
|
-
await Session.update(session, { user, roles, expire, language, timezone })
|
|
75
|
-
}
|
|
76
|
-
})
|
|
77
|
-
|
|
78
|
-
definition.event({
|
|
79
|
-
name: "loggedOut",
|
|
80
|
-
properties: {
|
|
81
|
-
session: {
|
|
82
|
-
type: Session
|
|
83
|
-
}
|
|
84
|
-
},
|
|
85
|
-
async execute({ session }) {
|
|
86
|
-
await Session.update(session, [
|
|
87
|
-
{ op: 'reverseMerge', value: { id: session } },
|
|
88
|
-
{ op: 'merge', value: { user: null, roles: [] } }
|
|
89
|
-
])
|
|
90
|
-
}
|
|
91
|
-
})
|
|
92
|
-
|
|
93
|
-
definition.event({
|
|
94
|
-
name: "UserDeleted",
|
|
95
|
-
properties: {
|
|
96
|
-
user: {
|
|
97
|
-
type: User
|
|
98
|
-
}
|
|
99
|
-
},
|
|
100
|
-
async execute({ user }) {
|
|
101
|
-
await app.dao.request(['database', 'query'], app.databaseName, `(${
|
|
102
|
-
async (input, output, { table, index, user }) => {
|
|
103
|
-
const prefix = `"${user}"_`
|
|
104
|
-
await (await input.index(index)).range({
|
|
105
|
-
gte: prefix,
|
|
106
|
-
lte: prefix+"\xFF\xFF\xFF\xFF"
|
|
107
|
-
}).onChange((ind, oldInd) => {
|
|
108
|
-
if(ind && ind.to) {
|
|
109
|
-
output.table(table).update(ind.to, [
|
|
110
|
-
{ op: 'reverseMerge', value: { id: ind.to } },
|
|
111
|
-
{ op: 'merge', value: { user: null, roles: [], expire: null } }
|
|
112
|
-
])
|
|
113
|
-
}
|
|
114
|
-
})
|
|
115
|
-
}
|
|
116
|
-
})`, { table: Session.tableName, index: Session.tableName + '_byUser', user })
|
|
117
|
-
}
|
|
118
|
-
})
|
|
119
|
-
|
|
120
|
-
definition.event({
|
|
121
|
-
name: "rolesUpdated",
|
|
122
|
-
properties: {
|
|
123
|
-
user: {
|
|
124
|
-
type: User
|
|
125
|
-
},
|
|
126
|
-
roles: {
|
|
127
|
-
type: Array,
|
|
128
|
-
of: {
|
|
129
|
-
type: String
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
},
|
|
133
|
-
async execute({ user, roles }) {
|
|
134
|
-
await app.dao.request(['database', 'query'], app.databaseName, `(${
|
|
135
|
-
async (input, output, { table, index, user, roles }) => {
|
|
136
|
-
const prefix = `"${user}"_`
|
|
137
|
-
await (await input.index(index)).range({
|
|
138
|
-
gte: prefix,
|
|
139
|
-
lte: prefix+"\xFF\xFF\xFF\xFF"
|
|
140
|
-
}).onChange((ind, oldInd) => {
|
|
141
|
-
if(ind && ind.to) {
|
|
142
|
-
output.table(table).update(ind.to, [
|
|
143
|
-
{ op: 'reverseMerge', value: { id: session } },
|
|
144
|
-
{ op: 'merge', value: { roles } }
|
|
145
|
-
])
|
|
146
|
-
}
|
|
147
|
-
})
|
|
148
|
-
}
|
|
149
|
-
})`, { table: Session.tableName, index: Session.tableName + '_byUser', user, roles })
|
|
150
32
|
}
|
|
151
33
|
})
|
|
152
34
|
|
package/package.json
CHANGED
package/sessionProperty.js
CHANGED
|
@@ -16,7 +16,6 @@ definition.processor(function(service, app) {
|
|
|
16
16
|
const originalModelProperties = {...model.properties}
|
|
17
17
|
const modelProperties = Object.keys(model.properties)
|
|
18
18
|
const defaults = App.utils.generateDefault(model.properties)
|
|
19
|
-
const modelPropertyName = modelName.slice(0, 1).toLowerCase() + modelName.slice(1)
|
|
20
19
|
|
|
21
20
|
function modelRuntime() {
|
|
22
21
|
return service._runtime.models[modelName]
|