@live-change/session-service 0.1.11 → 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/authenticator.js CHANGED
@@ -1,32 +1,35 @@
1
1
  const App = require('@live-change/framework')
2
2
  const app = App.app()
3
3
  const definition = require('./definition.js')
4
- const Session = require('./model.js')
4
+ const { Session } = require('./model.js')
5
5
  const { createHmac } = require('crypto')
6
+ const config = definition.config
6
7
 
7
- definition.authenticator(async function(credentials, config) {
8
- const sessionKey = credentials.sessionKey
9
- if(!sessionKey) throw new Error("sessionKey required!")
10
- const sessions = await app.dao.get(
11
- ['database', 'indexRange', app.databaseName, Session.tableName + '_byKey', {
12
- gt: `"${sessionKey}"_`,
13
- lt: `"${sessionKey}"_\xFF`
14
- }])
15
- //console.log("FOUND SESSIONS", sessions)
16
- let session = sessions[0]?.to
17
- if(!session) {
18
- if(config.createSessionOnUpdate) {
19
- session = createHmac('sha256', config.sessionHmacSecret || 'secret')
20
- .update(credentials.sessionKey)
21
- .digest('base64').slice(0, 32)
22
- } else {
23
- const createResult = await app.triggerService(definition.name, {
24
- type: "createSessionKeyIfNotExists",
25
- sessionKey
26
- })
27
- //console.log("CREATE SESSION RESULT", createResult)
28
- session = createResult.session
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
@@ -1,11 +1,7 @@
1
1
  const App = require("@live-change/framework")
2
2
  const app = App.app()
3
3
  const definition = require('./definition.js')
4
- const Session = require('./model.js')
5
-
6
-
7
- const User = definition.foreignModel('user', 'User')
8
-
4
+ const { Session } = require('./model.js')
9
5
 
10
6
  definition.view({
11
7
  name: 'currentSession',
@@ -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,115 +25,11 @@ 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
 
153
- module.exports = Session
35
+ module.exports = { Session }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/session-service",
3
- "version": "0.1.11",
3
+ "version": "0.2.2",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -8,13 +8,13 @@
8
8
  },
9
9
  "repository": {
10
10
  "type": "git",
11
- "url": "git+https://github.com/live-change/session-service.git"
11
+ "url": "git+https://github.com/live-change/live-change-services.git"
12
12
  },
13
13
  "license": "MIT",
14
14
  "bugs": {
15
- "url": "https://github.com/live-change/session-service/issues"
15
+ "url": "https://github.com/live-change/live-change-services/issues"
16
16
  },
17
- "homepage": "https://github.com/live-change/session-service",
17
+ "homepage": "https://github.com/live-change/live-change-services",
18
18
  "author": {
19
19
  "email": "michal@laszczewski.pl",
20
20
  "name": "Michał Łaszczewski",
@@ -23,5 +23,6 @@
23
23
  "dependencies": {
24
24
  "@live-change/framework": "^0.5.7",
25
25
  "@live-change/relations-plugin": "^0.1.10"
26
- }
26
+ },
27
+ "gitHead": "7691357c7569a18373668691eb6a81a9e161194d"
27
28
  }
package/sessionItem.js CHANGED
@@ -1,7 +1,7 @@
1
1
  const definition = require("./definition.js")
2
2
  const App = require("@live-change/framework")
3
3
  const { PropertyDefinition, ViewDefinition, IndexDefinition, ActionDefinition, EventDefinition } = App
4
- const Session = require("./model.js")
4
+ const { Session } = require("./model.js")
5
5
 
6
6
  definition.processor(function(service, app) {
7
7
 
@@ -1,13 +1,12 @@
1
1
  const definition = require("./definition.js")
2
2
  const App = require("@live-change/framework")
3
3
  const { PropertyDefinition, ViewDefinition, IndexDefinition, ActionDefinition, EventDefinition } = App
4
- const Session = require("./model.js")
4
+ const { Session } = require("./model.js")
5
5
 
6
6
  definition.processor(function(service, app) {
7
7
 
8
8
  for(let modelName in service.models) {
9
9
  const model = service.models[modelName]
10
- console.log("SP", modelName)
11
10
 
12
11
  if(model.sessionProperty) {
13
12
  console.log("MODEL " + modelName + " IS SESSION PROPERTY, CONFIG:", model.sessionProperty)
@@ -16,7 +15,6 @@ definition.processor(function(service, app) {
16
15
  const originalModelProperties = {...model.properties}
17
16
  const modelProperties = Object.keys(model.properties)
18
17
  const defaults = App.utils.generateDefault(model.properties)
19
- const modelPropertyName = modelName.slice(0, 1).toLowerCase() + modelName.slice(1)
20
18
 
21
19
  function modelRuntime() {
22
20
  return service._runtime.models[modelName]
@@ -30,14 +28,6 @@ definition.processor(function(service, app) {
30
28
  ...config
31
29
  }
32
30
 
33
- model.properties.session = new PropertyDefinition({
34
- type: Session,
35
- validation: ['nonEmpty']
36
- })
37
- if(!model.indexes) model.indexes = {}
38
- model.indexes.bySession = new IndexDefinition({
39
- property: 'session'
40
- })
41
31
  if(config.sessionReadAccess) {
42
32
  const viewName = 'mySession' + modelName
43
33
  service.views[viewName] = new ViewDefinition({