@semapps/auth 0.4.0-alpha.1 → 0.4.0-alpha.13
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/mixins/auth.js +16 -5
- package/package.json +4 -4
- package/services/account.js +10 -8
- package/services/auth.local.js +5 -4
- package/services/jwt.js +3 -0
package/mixins/auth.js
CHANGED
@@ -1,7 +1,8 @@
|
|
1
|
+
const passport = require('passport');
|
2
|
+
const { Errors: E } = require('moleculer-web');
|
3
|
+
const { TripleStoreAdapter } = require('@semapps/triplestore');
|
1
4
|
const AuthAccountService = require('../services/account');
|
2
5
|
const AuthJWTService = require('../services/jwt');
|
3
|
-
const { Errors: E } = require('moleculer-web');
|
4
|
-
const passport = require('passport');
|
5
6
|
|
6
7
|
const AuthMixin = {
|
7
8
|
settings: {
|
@@ -9,18 +10,21 @@ const AuthMixin = {
|
|
9
10
|
jwtPath: null,
|
10
11
|
registrationAllowed: true,
|
11
12
|
reservedUsernames: [],
|
12
|
-
webIdSelection: []
|
13
|
+
webIdSelection: [],
|
14
|
+
accountSelection: [],
|
15
|
+
accountsDataset: 'settings'
|
13
16
|
},
|
14
17
|
dependencies: ['api', 'webid'],
|
15
18
|
async created() {
|
16
|
-
const { jwtPath, reservedUsernames } = this.settings;
|
19
|
+
const { jwtPath, reservedUsernames, accountsDataset } = this.settings;
|
17
20
|
|
18
21
|
await this.broker.createService(AuthJWTService, {
|
19
22
|
settings: { jwtPath }
|
20
23
|
});
|
21
24
|
|
22
25
|
await this.broker.createService(AuthAccountService, {
|
23
|
-
settings: { reservedUsernames }
|
26
|
+
settings: { reservedUsernames },
|
27
|
+
adapter: new TripleStoreAdapter({ type: 'AuthAccount', dataset: accountsDataset }),
|
24
28
|
});
|
25
29
|
},
|
26
30
|
async started() {
|
@@ -108,6 +112,13 @@ const AuthMixin = {
|
|
108
112
|
} else {
|
109
113
|
return data;
|
110
114
|
}
|
115
|
+
},
|
116
|
+
pickAccountData(data) {
|
117
|
+
if (this.settings.accountSelection.length > 0) {
|
118
|
+
return Object.fromEntries(this.settings.accountSelection.filter(key => key in data).map(key => [key, data[key]]));
|
119
|
+
} else {
|
120
|
+
return data || {};
|
121
|
+
}
|
111
122
|
}
|
112
123
|
}
|
113
124
|
};
|
package/package.json
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
{
|
2
2
|
"name": "@semapps/auth",
|
3
|
-
"version": "0.4.0-alpha.
|
3
|
+
"version": "0.4.0-alpha.13",
|
4
4
|
"description": "Authentification module for SemApps",
|
5
5
|
"license": "Apache-2.0",
|
6
6
|
"author": "Virtual Assembly",
|
7
7
|
"dependencies": {
|
8
|
-
"@semapps/mime-types": "0.4.0-alpha.
|
9
|
-
"@semapps/triplestore": "0.4.0-alpha.
|
8
|
+
"@semapps/mime-types": "0.4.0-alpha.13",
|
9
|
+
"@semapps/triplestore": "0.4.0-alpha.13",
|
10
10
|
"bcrypt": "^5.0.1",
|
11
11
|
"express-session": "^1.17.0",
|
12
12
|
"jsonwebtoken": "^8.5.1",
|
@@ -22,5 +22,5 @@
|
|
22
22
|
"publishConfig": {
|
23
23
|
"access": "public"
|
24
24
|
},
|
25
|
-
"gitHead": "
|
25
|
+
"gitHead": "8d9719359c3e791abe17a1c3e615fb138a0edf33"
|
26
26
|
}
|
package/services/account.js
CHANGED
@@ -13,9 +13,11 @@ module.exports = {
|
|
13
13
|
dependencies: ['triplestore'],
|
14
14
|
actions: {
|
15
15
|
async create(ctx) {
|
16
|
-
let { uuid, username, password, email, webId } = ctx.params;
|
16
|
+
let { uuid, username, password, email, webId, ...rest } = ctx.params;
|
17
17
|
const hashedPassword = password ? await this.hashPassword(password) : undefined;
|
18
18
|
|
19
|
+
email = email.toLowerCase();
|
20
|
+
|
19
21
|
const emailExists = await ctx.call('auth.account.emailExists', { email });
|
20
22
|
if (emailExists) {
|
21
23
|
throw new Error('email.already.exists');
|
@@ -36,6 +38,7 @@ module.exports = {
|
|
36
38
|
}
|
37
39
|
|
38
40
|
return await this._create(ctx, {
|
41
|
+
...rest,
|
39
42
|
uuid,
|
40
43
|
username,
|
41
44
|
email,
|
@@ -54,11 +57,10 @@ module.exports = {
|
|
54
57
|
async verify(ctx) {
|
55
58
|
const { username, password } = ctx.params;
|
56
59
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
});
|
60
|
+
// If the username includes a @, assume it is an email
|
61
|
+
const query = username.includes('@') ? { email: username } : { username };
|
62
|
+
|
63
|
+
const accounts = await this._find(ctx, { query });
|
62
64
|
|
63
65
|
if (accounts.length > 0) {
|
64
66
|
const passwordMatch = await this.comparePassword(password, accounts[0].hashedPassword);
|
@@ -92,7 +94,7 @@ module.exports = {
|
|
92
94
|
const account = await ctx.call('auth.account.findByWebId', { webId });
|
93
95
|
|
94
96
|
return await this._update(ctx, {
|
95
|
-
'@id': account
|
97
|
+
'@id': account['@id'],
|
96
98
|
hashedPassword
|
97
99
|
});
|
98
100
|
}
|
@@ -100,7 +102,7 @@ module.exports = {
|
|
100
102
|
methods: {
|
101
103
|
async isValidUsername(ctx, username) {
|
102
104
|
// Ensure the username has no space or special characters
|
103
|
-
if (!/^[a-
|
105
|
+
if (!/^[a-z0-9\-_.]+$/.exec(username)) {
|
104
106
|
throw new Error('username.invalid');
|
105
107
|
}
|
106
108
|
|
package/services/auth.local.js
CHANGED
@@ -11,18 +11,19 @@ const AuthLocalService = {
|
|
11
11
|
jwtPath: null,
|
12
12
|
registrationAllowed: true,
|
13
13
|
reservedUsernames: [],
|
14
|
-
webIdSelection: []
|
14
|
+
webIdSelection: [],
|
15
|
+
accountSelection: [],
|
15
16
|
},
|
16
17
|
created() {
|
17
18
|
this.passportId = 'local';
|
18
19
|
},
|
19
20
|
actions: {
|
20
21
|
async signup(ctx) {
|
21
|
-
const { username, email, password, ...
|
22
|
+
const { username, email, password, ...rest } = ctx.params;
|
22
23
|
|
23
|
-
let accountData = await ctx.call('auth.account.create', { username, email, password });
|
24
|
+
let accountData = await ctx.call('auth.account.create', { username, email, password, ...this.pickAccountData(rest) });
|
24
25
|
|
25
|
-
const profileData = { nick: username, email, ...
|
26
|
+
const profileData = { nick: username, email, ...rest };
|
26
27
|
const webId = await ctx.call('webid.create', this.pickWebIdData(profileData));
|
27
28
|
|
28
29
|
// Link the webId with the account
|
package/services/jwt.js
CHANGED
@@ -14,6 +14,9 @@ module.exports = {
|
|
14
14
|
|
15
15
|
if (!fs.existsSync(privateKeyPath) && !fs.existsSync(publicKeyPath)) {
|
16
16
|
console.log('JWT keypair not found, generating...');
|
17
|
+
if (!fs.existsSync(this.settings.jwtPath)) {
|
18
|
+
fs.mkdirSync(this.settings.jwtPath);
|
19
|
+
}
|
17
20
|
await this.actions.generateKeyPair({ privateKeyPath, publicKeyPath });
|
18
21
|
}
|
19
22
|
|