@semapps/auth 0.4.0-alpha.0 → 0.4.0-alpha.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/mixins/auth.js +8 -5
- package/package.json +4 -4
- package/services/account.js +8 -7
- package/services/jwt.js +3 -0
- package/services/migration.js +9 -5
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,20 @@ const AuthMixin = {
|
|
9
10
|
jwtPath: null,
|
10
11
|
registrationAllowed: true,
|
11
12
|
reservedUsernames: [],
|
12
|
-
webIdSelection: []
|
13
|
+
webIdSelection: [],
|
14
|
+
accountsDataset: 'settings'
|
13
15
|
},
|
14
16
|
dependencies: ['api', 'webid'],
|
15
17
|
async created() {
|
16
|
-
const { jwtPath, reservedUsernames } = this.settings;
|
18
|
+
const { jwtPath, reservedUsernames, accountsDataset } = this.settings;
|
17
19
|
|
18
20
|
await this.broker.createService(AuthJWTService, {
|
19
21
|
settings: { jwtPath }
|
20
22
|
});
|
21
23
|
|
22
24
|
await this.broker.createService(AuthAccountService, {
|
23
|
-
settings: { reservedUsernames }
|
25
|
+
settings: { reservedUsernames },
|
26
|
+
adapter: new TripleStoreAdapter({ type: 'AuthAccount', dataset: accountsDataset }),
|
24
27
|
});
|
25
28
|
},
|
26
29
|
async started() {
|
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.12",
|
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.12",
|
9
|
+
"@semapps/triplestore": "0.4.0-alpha.12",
|
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": "69f22b8c24503034a70fcefe452cede38b09a2cd"
|
26
26
|
}
|
package/services/account.js
CHANGED
@@ -16,6 +16,8 @@ module.exports = {
|
|
16
16
|
let { uuid, username, password, email, webId } = 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');
|
@@ -54,11 +56,10 @@ module.exports = {
|
|
54
56
|
async verify(ctx) {
|
55
57
|
const { username, password } = ctx.params;
|
56
58
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
});
|
59
|
+
// If the username includes a @, assume it is an email
|
60
|
+
const query = username.includes('@') ? { email: username } : { username };
|
61
|
+
|
62
|
+
const accounts = await this._find(ctx, { query });
|
62
63
|
|
63
64
|
if (accounts.length > 0) {
|
64
65
|
const passwordMatch = await this.comparePassword(password, accounts[0].hashedPassword);
|
@@ -92,7 +93,7 @@ module.exports = {
|
|
92
93
|
const account = await ctx.call('auth.account.findByWebId', { webId });
|
93
94
|
|
94
95
|
return await this._update(ctx, {
|
95
|
-
'@id': account
|
96
|
+
'@id': account['@id'],
|
96
97
|
hashedPassword
|
97
98
|
});
|
98
99
|
}
|
@@ -100,7 +101,7 @@ module.exports = {
|
|
100
101
|
methods: {
|
101
102
|
async isValidUsername(ctx, username) {
|
102
103
|
// Ensure the username has no space or special characters
|
103
|
-
if (!/^[a-
|
104
|
+
if (!/^[a-z0-9\-_.]+$/.exec(username)) {
|
104
105
|
throw new Error('username.invalid');
|
105
106
|
}
|
106
107
|
|
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
|
|
package/services/migration.js
CHANGED
@@ -10,11 +10,15 @@ module.exports = {
|
|
10
10
|
|
11
11
|
for (let user of results['ldp:contains']) {
|
12
12
|
if (user[emailPredicate]) {
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
13
|
+
try {
|
14
|
+
await ctx.call('auth.account.create', {
|
15
|
+
email: user[emailPredicate],
|
16
|
+
username: user[usernamePredicate],
|
17
|
+
webId: user.id
|
18
|
+
});
|
19
|
+
} catch (e) {
|
20
|
+
console.log(`Unable to create account for user ${user.id}. Error message: ${e.message}`);
|
21
|
+
}
|
18
22
|
} else {
|
19
23
|
console.log('No email found for user ' + user.id);
|
20
24
|
}
|