@cocreate/authenticate 1.7.0 → 1.9.0
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/CHANGELOG.md +24 -0
- package/package.json +2 -3
- package/src/index.js +123 -106
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,27 @@
|
|
|
1
|
+
# [1.9.0](https://github.com/CoCreate-app/CoCreate-authenticate/compare/v1.8.0...v1.9.0) (2024-01-08)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* applied host to define environment/branch ([4f99d72](https://github.com/CoCreate-app/CoCreate-authenticate/commit/4f99d72aa822e5d7ea36beee81fe75153a0d2ca5))
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* bumped CoCreate dependencies to their latest versions ([d57103d](https://github.com/CoCreate-app/CoCreate-authenticate/commit/d57103dae51984d00d99032e45fc23263f955bc0))
|
|
12
|
+
|
|
13
|
+
# [1.8.0](https://github.com/CoCreate-app/CoCreate-authenticate/compare/v1.7.0...v1.8.0) (2023-12-05)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
### Bug Fixes
|
|
17
|
+
|
|
18
|
+
* removed "@cocreate/utlis" ([3726368](https://github.com/CoCreate-app/CoCreate-authenticate/commit/372636836f00c830c31baa6e85e138075ec510af))
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
### Features
|
|
22
|
+
|
|
23
|
+
* manage sessions using crud ([81a3d9b](https://github.com/CoCreate-app/CoCreate-authenticate/commit/81a3d9b15402b305f61ad23d6699e30277f1a0bd))
|
|
24
|
+
|
|
1
25
|
# [1.7.0](https://github.com/CoCreate-app/CoCreate-authenticate/compare/v1.6.0...v1.7.0) (2023-11-25)
|
|
2
26
|
|
|
3
27
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cocreate/authenticate",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.9.0",
|
|
4
4
|
"description": "A simple authenticate component in vanilla javascript. Easily configured using HTML5 data-attributes and/or JavaScript API.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"authenticate",
|
|
@@ -33,14 +33,13 @@
|
|
|
33
33
|
"type": "git",
|
|
34
34
|
"url": "git+https://github.com/CoCreate-app/CoCreate-authenticate.git"
|
|
35
35
|
},
|
|
36
|
-
"
|
|
36
|
+
"author": "CoCreate LLC",
|
|
37
37
|
"bugs": {
|
|
38
38
|
"url": "https://github.com/CoCreate-app/CoCreate-authenticate/issues"
|
|
39
39
|
},
|
|
40
40
|
"main": "./src/index.js",
|
|
41
41
|
"homepage": "https://cocreate.app/docs/authenticate",
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@cocreate/utils": "^1.29.0",
|
|
44
43
|
"jsonwebtoken": "^9.0.0"
|
|
45
44
|
}
|
|
46
45
|
}
|
package/src/index.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
const crypto = require('crypto');
|
|
2
2
|
const jwt = require('jsonwebtoken');
|
|
3
|
-
const { ObjectId } = require('@cocreate/utils');
|
|
4
3
|
|
|
5
4
|
// Configuration
|
|
6
5
|
const tokenExpiration = 60; // Token expiration in minutes
|
|
@@ -8,124 +7,142 @@ const tokenExpiration = 60; // Token expiration in minutes
|
|
|
8
7
|
// Array to store key pairs
|
|
9
8
|
const keyPairs = new Map();
|
|
10
9
|
|
|
11
|
-
// Map to store authenticated user
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
// TODO: user can have multiple sessions
|
|
15
|
-
const activeSessions = new Map();
|
|
16
|
-
|
|
17
|
-
// crud.listen('object.create', function (data) {
|
|
18
|
-
// if (data.object && data.object[0] && data.object[0].type === 'keyPair')
|
|
19
|
-
// keyPairs.set(data.object[0]._id, data.object[0]);
|
|
20
|
-
// });
|
|
21
|
-
|
|
22
|
-
// crud.listen('object.delete', function (data) {
|
|
23
|
-
// if (data.object && data.object[0] && data.object[0].type === 'keyPair')
|
|
24
|
-
// keyPairs.delete(data.object[0]._id);
|
|
25
|
-
// });
|
|
26
|
-
|
|
27
|
-
// Create new RSA key pair
|
|
28
|
-
function createKeyPair() {
|
|
29
|
-
const { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', {
|
|
30
|
-
modulusLength: 2048,
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
let created = new Date(new Date().toISOString()).getTime()
|
|
34
|
-
const keyPair = {
|
|
35
|
-
_id: ObjectId().toString(),
|
|
36
|
-
privateKey,
|
|
37
|
-
publicKey,
|
|
38
|
-
created,
|
|
39
|
-
expires: created + tokenExpiration * 60 * 1000, // Convert minutes to milliseconds
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
keyPairs.set(keyPair._id, keyPair);
|
|
43
|
-
|
|
44
|
-
// crud.send({
|
|
45
|
-
// method: 'object.create',
|
|
46
|
-
// array: 'keys',
|
|
47
|
-
// object: {
|
|
48
|
-
// ...keyPair,
|
|
49
|
-
// },
|
|
50
|
-
// organization_id: process.env.organization_id,
|
|
51
|
-
// });
|
|
52
|
-
|
|
53
|
-
return keyPair;
|
|
54
|
-
}
|
|
10
|
+
// Map to store authenticated user payload and tokens
|
|
11
|
+
const sessions = new Map();
|
|
55
12
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
}
|
|
67
|
-
},
|
|
68
|
-
organization_id: process.env.organization_id,
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
// Add retrieved key pairs to the keyPairs array
|
|
72
|
-
if (keys.object && keys.object.length) {
|
|
73
|
-
keys.object.forEach((keyPair) => {
|
|
74
|
-
keyPairs.set(keyPair._id, keyPair);
|
|
13
|
+
class CoCreateAuthenticate {
|
|
14
|
+
constructor(crud) {
|
|
15
|
+
this.wsManager = crud.wsManager
|
|
16
|
+
this.crud = crud
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Create new RSA key pair
|
|
20
|
+
createKeyPair() {
|
|
21
|
+
const { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', {
|
|
22
|
+
modulusLength: 2048,
|
|
75
23
|
});
|
|
24
|
+
|
|
25
|
+
let created = new Date(new Date().toISOString()).getTime()
|
|
26
|
+
const keyPair = {
|
|
27
|
+
privateKey,
|
|
28
|
+
publicKey,
|
|
29
|
+
created,
|
|
30
|
+
expires: created + tokenExpiration * 60 * 1000, // Convert minutes to milliseconds
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
keyPairs.set(keyPair._id, keyPair);
|
|
34
|
+
|
|
35
|
+
// crud.send({
|
|
36
|
+
// method: 'object.create',
|
|
37
|
+
// array: 'keys',
|
|
38
|
+
// object: {
|
|
39
|
+
// ...keyPair,
|
|
40
|
+
// },
|
|
41
|
+
// organization_id: process.env.organization_id,
|
|
42
|
+
// });
|
|
43
|
+
|
|
44
|
+
return keyPair;
|
|
76
45
|
}
|
|
77
|
-
}
|
|
78
46
|
|
|
79
|
-
// Delete new RSA key pair
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
}
|
|
47
|
+
// Delete new RSA key pair
|
|
48
|
+
deleteKeyPair(keyPair) {
|
|
49
|
+
keyPairs.delete(keyPair._id)
|
|
50
|
+
// crud.send({
|
|
51
|
+
// method: 'object.delete',
|
|
52
|
+
// array: 'keys',
|
|
53
|
+
// object: {
|
|
54
|
+
// _id: keyPair._id,
|
|
55
|
+
// type: 'RSA'
|
|
56
|
+
// },
|
|
57
|
+
// organization_id: process.env.organization_id,
|
|
58
|
+
// });
|
|
59
|
+
}
|
|
92
60
|
|
|
93
|
-
// Function to sign a new token using the newest keyPair
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
61
|
+
// Function to sign a new token using the newest keyPair
|
|
62
|
+
encodeToken(organization_id, user_id, clientId, host) {
|
|
63
|
+
let keyPair = null
|
|
64
|
+
const currentTime = new Date(new Date().toISOString()).getTime();
|
|
65
|
+
for (let [key, value] of keyPairs) {
|
|
66
|
+
if (currentTime > value.expires) {
|
|
67
|
+
this.deleteKeyPair(value);
|
|
68
|
+
} else {
|
|
69
|
+
keyPair = value
|
|
70
|
+
}
|
|
102
71
|
}
|
|
103
|
-
}
|
|
104
72
|
|
|
105
|
-
|
|
106
|
-
|
|
73
|
+
if (!keyPair) {
|
|
74
|
+
keyPair = this.createKeyPair();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const token = jwt.sign({ user_id, clientId }, keyPair.privateKey, { algorithm: 'RS256', expiresIn: tokenExpiration * 60 });
|
|
78
|
+
|
|
79
|
+
// TODO: session could have previous user ip and device information which we could use to comapre to current ip and device information
|
|
80
|
+
const session = { user_id, clientId, token, expires: currentTime + tokenExpiration * 60 * 1000 }
|
|
81
|
+
sessions.set(clientId, session)
|
|
82
|
+
|
|
83
|
+
this.crud.send({
|
|
84
|
+
method: 'object.update',
|
|
85
|
+
host,
|
|
86
|
+
array: 'clients',
|
|
87
|
+
object: {
|
|
88
|
+
_id: clientId,
|
|
89
|
+
session,
|
|
90
|
+
},
|
|
91
|
+
upsert: true,
|
|
92
|
+
organization_id
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
return token;
|
|
107
96
|
}
|
|
108
97
|
|
|
109
|
-
//
|
|
98
|
+
// Verify and decode a token using the available keys
|
|
99
|
+
async decodeToken(token, organization_id, clientId, host) {
|
|
100
|
+
if (!token)
|
|
101
|
+
return {}
|
|
102
|
+
const currentTime = new Date().getTime();
|
|
103
|
+
|
|
104
|
+
let session = sessions.get(clientId)
|
|
105
|
+
if (!session || session.token !== token)
|
|
106
|
+
session = await this.read(organization_id, clientId, host)
|
|
107
|
+
|
|
108
|
+
// TODO: request must be made from same clientId and user_id that created the token
|
|
109
|
+
if (session && currentTime < session.expires && session.token === token)
|
|
110
|
+
return { user_id: session.user_id, expires: session.expires };
|
|
111
|
+
|
|
112
|
+
sessions.delete(clientId)
|
|
113
|
+
this.crud.send({
|
|
114
|
+
method: 'object.update',
|
|
115
|
+
host,
|
|
116
|
+
array: 'clients',
|
|
117
|
+
object: {
|
|
118
|
+
_id: clientId,
|
|
119
|
+
session: undefined
|
|
120
|
+
},
|
|
121
|
+
organization_id,
|
|
122
|
+
});
|
|
110
123
|
|
|
111
|
-
|
|
112
|
-
users.set(token, { _id: payload.user_id, expires: currentTime + tokenExpiration * 60 * 1000 })
|
|
113
|
-
return token;
|
|
114
|
-
}
|
|
124
|
+
return {}
|
|
115
125
|
|
|
116
|
-
|
|
117
|
-
function decodeToken(token) {
|
|
118
|
-
const currentTime = new Date().getTime();
|
|
126
|
+
}
|
|
119
127
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
128
|
+
async read(organization_id, clientId, host) {
|
|
129
|
+
const client = await this.crud.send({
|
|
130
|
+
method: 'object.read',
|
|
131
|
+
host,
|
|
132
|
+
array: 'clients',
|
|
133
|
+
object: {
|
|
134
|
+
_id: clientId
|
|
135
|
+
},
|
|
136
|
+
organization_id,
|
|
137
|
+
});
|
|
123
138
|
|
|
124
|
-
|
|
125
|
-
|
|
139
|
+
if (client.object && client.object.length) {
|
|
140
|
+
sessions.set(clientId, client.object[0].session)
|
|
141
|
+
}
|
|
126
142
|
|
|
127
|
-
|
|
143
|
+
return client.object[0].session
|
|
144
|
+
}
|
|
128
145
|
|
|
129
|
-
|
|
146
|
+
}
|
|
130
147
|
|
|
131
|
-
module.exports =
|
|
148
|
+
module.exports = CoCreateAuthenticate;
|