From 0ad5455c8f4e98937fc50be1caebb3ca4903fda4 Mon Sep 17 00:00:00 2001 From: Zach Date: Fri, 10 May 2024 23:18:45 -0500 Subject: [PATCH 01/11] test: added accountTemplate for registering hostname at app creation time --- lib/create-app.js | 3 +++ lib/models/account-template.js | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/create-app.js b/lib/create-app.js index 0cc00b6bb..17e3b0fea 100644 --- a/lib/create-app.js +++ b/lib/create-app.js @@ -11,6 +11,7 @@ const corsProxy = require('./handlers/cors-proxy') const authProxy = require('./handlers/auth-proxy') const SolidHost = require('./models/solid-host') const AccountManager = require('./models/account-manager') +const AccountTemplate = require('./models/account-template') const vhost = require('vhost') const EmailService = require('./services/email-service') const TokenService = require('./services/token-service') @@ -52,6 +53,8 @@ function createApp (argv = {}) { defaultContentType: argv.defaultContentType }) + AccountTemplate.registerHostname(argv.serverUri) + const configPath = config.initConfigDir(argv) argv.templates = config.initTemplateDirs(configPath) diff --git a/lib/models/account-template.js b/lib/models/account-template.js index f03b572e4..3dcc24a0d 100644 --- a/lib/models/account-template.js +++ b/lib/models/account-template.js @@ -35,6 +35,19 @@ class AccountTemplate { this.templateFiles = options.templateFiles || TEMPLATE_FILES } + /** + * Registers the server hostname at compile time to check for webID stuff. + * @param {string} hostname + * @throws if hostname already registered + */ + static registerHostname (hostname) { + if (this.hostname) { + throw new Error('hostname already registered') + } else { + this.hostname = hostname + } + } + /** * Factory method, returns an AccountTemplate for a given user account. * From eee648882efafd0ea58402974b4ac8814c1e95e9 Mon Sep 17 00:00:00 2001 From: Zach Date: Fri, 10 May 2024 23:24:02 -0500 Subject: [PATCH 02/11] added logic for relative uri maybe --- lib/models/account-template.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/models/account-template.js b/lib/models/account-template.js index 3dcc24a0d..2ec0b5895 100644 --- a/lib/models/account-template.js +++ b/lib/models/account-template.js @@ -85,9 +85,17 @@ class AccountTemplate { * @return {Object} */ static templateSubstitutionsFor (userAccount) { + let realWebId + // this means the user's webId and server Uri are the same + // therefore, we use a relative address + if (userAccount.webId.indexOf(this.hostname) > -1) { + realWebId = userAccount.webId.substring(userAccount.webId.indexOf(this.hostname)) + } else { + realWebId = userAccount.webId + } const substitutions = { name: userAccount.displayName, - webId: userAccount.webId, + webId: realWebId, // userAccount.webId, email: userAccount.email, idp: userAccount.idp } From 2a1f7e4f315fae978cd6f8d1cfd291302984a7f9 Mon Sep 17 00:00:00 2001 From: Zach Date: Fri, 10 May 2024 23:25:37 -0500 Subject: [PATCH 03/11] removed training wheels to not break test cases --- lib/models/account-template.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/models/account-template.js b/lib/models/account-template.js index 2ec0b5895..3271f435a 100644 --- a/lib/models/account-template.js +++ b/lib/models/account-template.js @@ -41,11 +41,11 @@ class AccountTemplate { * @throws if hostname already registered */ static registerHostname (hostname) { - if (this.hostname) { - throw new Error('hostname already registered') - } else { - this.hostname = hostname - } + // if (this.hostname) { + // throw new Error('hostname already registered') + // } else { + this.hostname = hostname + // } } /** From b795602606ac0519afdf0977c9613bc4ce5b53d1 Mon Sep 17 00:00:00 2001 From: Zach Date: Fri, 10 May 2024 23:52:08 -0500 Subject: [PATCH 04/11] added test cases for relative webid change --- lib/models/account-template.js | 2 +- test/integration/account-template-test.js | 63 ++++++++++++++++++++++- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/lib/models/account-template.js b/lib/models/account-template.js index 3271f435a..04dd88095 100644 --- a/lib/models/account-template.js +++ b/lib/models/account-template.js @@ -89,7 +89,7 @@ class AccountTemplate { // this means the user's webId and server Uri are the same // therefore, we use a relative address if (userAccount.webId.indexOf(this.hostname) > -1) { - realWebId = userAccount.webId.substring(userAccount.webId.indexOf(this.hostname)) + realWebId = path.join('/', userAccount.webId.substring(userAccount.webId.indexOf(this.hostname) + this.hostname.length)) } else { realWebId = userAccount.webId } diff --git a/test/integration/account-template-test.js b/test/integration/account-template-test.js index 6fa388ebb..640054031 100644 --- a/test/integration/account-template-test.js +++ b/test/integration/account-template-test.js @@ -10,7 +10,7 @@ chai.use(sinonChai) chai.should() const AccountTemplate = require('../../lib/models/account-template') - +const UserAccount = require('../../lib/models/user-account') const templatePath = path.join(__dirname, '../../default-templates/new-account') const accountPath = path.join(__dirname, '../resources/new-account') @@ -62,4 +62,65 @@ describe('AccountTemplate', () => { }) }) }) + + describe('templateSubtitutionsFor()', () => { + it('should not update the webid', () => { + AccountTemplate.registerHostname('https://offexample.com/') + + const userAccount = new UserAccount({ + webId: 'https://alice.example.com/#me', + email: 'alice@example.com', + name: 'Alice Q.' + }) + + const substitutions = AccountTemplate.templateSubstitutionsFor(userAccount) + + expect(substitutions.webId).to.equal('https://alice.example.com/#me') + }) + + it('should update the webid', () => { + AccountTemplate.registerHostname('http://localhost:8443/') + + const userAccount = new UserAccount({ + webId: 'http://localhost:8443/alice/#me', + email: 'alice@example.com', + name: 'Alice Q.' + }) + + const substitutions = AccountTemplate.templateSubstitutionsFor(userAccount) + + expect(substitutions.webId).to.equal('/alice/#me') + }) + }) + + describe('creating account where webId does match server Uri?', () => { + it('should have a relative uri for the base path rather than a complete uri', () => { + AccountTemplate.registerHostname('http://localhost:8443/') + + const userAccount = new UserAccount({ + webId: 'http://localhost:8443/alice/#me', + email: 'alice@example.com', + name: 'Alice Q.' + }) + + const substitutions = AccountTemplate.templateSubstitutionsFor(userAccount) + const template = new AccountTemplate({ substitutions }) + return AccountTemplate.copyTemplateDir(templatePath, accountPath) + .then(() => { + return template.processAccount(accountPath) + }).then(() => { + const profile = fs.readFileSync(path.join(accountPath, '/profile/card$.ttl'), 'utf8') + expect(profile).to.include('"Alice Q."') + expect(profile).to.include('solid:oidcIssuer') + // why does this need to be included? + // with the current configuration, 'host' for + // ldp is not set, therefore solid:oidcIssuer is empty + // expect(profile).to.include('') + + const rootAcl = fs.readFileSync(path.join(accountPath, '.acl'), 'utf8') + expect(rootAcl).to.include('') + }) + }) + }) }) From 72b20b4351bab9d6865a43b84561ed2f1528d765 Mon Sep 17 00:00:00 2001 From: Zach Date: Mon, 13 May 2024 12:35:21 -0500 Subject: [PATCH 05/11] minor cleaning and variable clarity --- lib/models/account-template.js | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/lib/models/account-template.js b/lib/models/account-template.js index 04dd88095..227832e24 100644 --- a/lib/models/account-template.js +++ b/lib/models/account-template.js @@ -41,11 +41,7 @@ class AccountTemplate { * @throws if hostname already registered */ static registerHostname (hostname) { - // if (this.hostname) { - // throw new Error('hostname already registered') - // } else { this.hostname = hostname - // } } /** @@ -85,17 +81,17 @@ class AccountTemplate { * @return {Object} */ static templateSubstitutionsFor (userAccount) { - let realWebId + let podRelativeWebId // this means the user's webId and server Uri are the same - // therefore, we use a relative address + // therefore, we use a relative uri ref if (userAccount.webId.indexOf(this.hostname) > -1) { - realWebId = path.join('/', userAccount.webId.substring(userAccount.webId.indexOf(this.hostname) + this.hostname.length)) + podRelativeWebId = path.join('/', userAccount.webId.substring(userAccount.webId.indexOf(this.hostname) + this.hostname.length)) } else { - realWebId = userAccount.webId + podRelativeWebId = userAccount.webId } const substitutions = { name: userAccount.displayName, - webId: realWebId, // userAccount.webId, + webId: podRelativeWebId, email: userAccount.email, idp: userAccount.idp } From 18b7cb3c0b6506554365be66bcbd1771807d2946 Mon Sep 17 00:00:00 2001 From: Zach Date: Mon, 13 May 2024 13:09:08 -0500 Subject: [PATCH 06/11] renamed example var --- lib/models/account-manager.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/models/account-manager.js b/lib/models/account-manager.js index ecf73718b..8ff4ab13f 100644 --- a/lib/models/account-manager.js +++ b/lib/models/account-manager.js @@ -57,7 +57,7 @@ class AccountManager { * * ``` * let options = { host, multiuser, store } - * let accontManager = AccountManager.from(options) + * let accountManager = AccountManager.from(options) * ``` * * @param [options={}] {Object} See the `constructor()` docstring. From 1034123e268365230342894db825f8f419af1b32 Mon Sep 17 00:00:00 2001 From: bourgeoa Date: Fri, 17 May 2024 18:54:06 +0200 Subject: [PATCH 07/11] check against externalWebId --- lib/create-app.js | 3 --- lib/models/account-template.js | 19 +------------------ 2 files changed, 1 insertion(+), 21 deletions(-) diff --git a/lib/create-app.js b/lib/create-app.js index 17e3b0fea..0cc00b6bb 100644 --- a/lib/create-app.js +++ b/lib/create-app.js @@ -11,7 +11,6 @@ const corsProxy = require('./handlers/cors-proxy') const authProxy = require('./handlers/auth-proxy') const SolidHost = require('./models/solid-host') const AccountManager = require('./models/account-manager') -const AccountTemplate = require('./models/account-template') const vhost = require('vhost') const EmailService = require('./services/email-service') const TokenService = require('./services/token-service') @@ -53,8 +52,6 @@ function createApp (argv = {}) { defaultContentType: argv.defaultContentType }) - AccountTemplate.registerHostname(argv.serverUri) - const configPath = config.initConfigDir(argv) argv.templates = config.initTemplateDirs(configPath) diff --git a/lib/models/account-template.js b/lib/models/account-template.js index 227832e24..033906835 100644 --- a/lib/models/account-template.js +++ b/lib/models/account-template.js @@ -35,15 +35,6 @@ class AccountTemplate { this.templateFiles = options.templateFiles || TEMPLATE_FILES } - /** - * Registers the server hostname at compile time to check for webID stuff. - * @param {string} hostname - * @throws if hostname already registered - */ - static registerHostname (hostname) { - this.hostname = hostname - } - /** * Factory method, returns an AccountTemplate for a given user account. * @@ -81,17 +72,9 @@ class AccountTemplate { * @return {Object} */ static templateSubstitutionsFor (userAccount) { - let podRelativeWebId - // this means the user's webId and server Uri are the same - // therefore, we use a relative uri ref - if (userAccount.webId.indexOf(this.hostname) > -1) { - podRelativeWebId = path.join('/', userAccount.webId.substring(userAccount.webId.indexOf(this.hostname) + this.hostname.length)) - } else { - podRelativeWebId = userAccount.webId - } const substitutions = { name: userAccount.displayName, - webId: podRelativeWebId, + webId: userAccount.externalWebId ? userAccount.webId : '/profile/card#me', email: userAccount.email, idp: userAccount.idp } From 99b321937fe17fbaa6f36e0582665dc02ed4670c Mon Sep 17 00:00:00 2001 From: bourgeoa Date: Tue, 21 May 2024 19:44:40 +0200 Subject: [PATCH 08/11] webId relative to pod --- lib/models/account-template.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/models/account-template.js b/lib/models/account-template.js index 033906835..d7a300cc4 100644 --- a/lib/models/account-template.js +++ b/lib/models/account-template.js @@ -6,6 +6,7 @@ const recursiveRead = require('recursive-readdir') const fsUtils = require('../common/fs-utils') const templateUtils = require('../common/template-utils') const LDP = require('../ldp') +const { URL } = require('url') const TEMPLATE_EXTENSIONS = ['.acl', '.meta', '.json', '.hbs', '.handlebars'] const TEMPLATE_FILES = ['card'] @@ -72,9 +73,12 @@ class AccountTemplate { * @return {Object} */ static templateSubstitutionsFor (userAccount) { + const webUri = new URL(userAccount.webId) + const webIdOrigin = webUri.protocol + '//' + webUri.hostname + const podRelWebId = userAccount.webId.replace(webIdOrigin, '') const substitutions = { name: userAccount.displayName, - webId: userAccount.externalWebId ? userAccount.webId : '/profile/card#me', + webId: userAccount.externalWebId ? userAccount.webId : podRelWebId, email: userAccount.email, idp: userAccount.idp } From a5ab6564c45107002b3773c5bca315512565b0df Mon Sep 17 00:00:00 2001 From: Zach Date: Tue, 21 May 2024 18:07:45 -0500 Subject: [PATCH 09/11] removed hostname registration deleted fn --- test/integration/account-template-test.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/test/integration/account-template-test.js b/test/integration/account-template-test.js index 640054031..44632f954 100644 --- a/test/integration/account-template-test.js +++ b/test/integration/account-template-test.js @@ -65,8 +65,6 @@ describe('AccountTemplate', () => { describe('templateSubtitutionsFor()', () => { it('should not update the webid', () => { - AccountTemplate.registerHostname('https://offexample.com/') - const userAccount = new UserAccount({ webId: 'https://alice.example.com/#me', email: 'alice@example.com', @@ -79,8 +77,6 @@ describe('AccountTemplate', () => { }) it('should update the webid', () => { - AccountTemplate.registerHostname('http://localhost:8443/') - const userAccount = new UserAccount({ webId: 'http://localhost:8443/alice/#me', email: 'alice@example.com', @@ -95,8 +91,6 @@ describe('AccountTemplate', () => { describe('creating account where webId does match server Uri?', () => { it('should have a relative uri for the base path rather than a complete uri', () => { - AccountTemplate.registerHostname('http://localhost:8443/') - const userAccount = new UserAccount({ webId: 'http://localhost:8443/alice/#me', email: 'alice@example.com', From 3f2eb2706541f715e83f0543310a0d3cbe679ab4 Mon Sep 17 00:00:00 2001 From: Zach Date: Tue, 21 May 2024 18:15:14 -0500 Subject: [PATCH 10/11] added function to also strip port, updated test cases --- lib/models/account-template.js | 2 +- test/integration/account-template-test.js | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/models/account-template.js b/lib/models/account-template.js index d7a300cc4..7cc9c85ef 100644 --- a/lib/models/account-template.js +++ b/lib/models/account-template.js @@ -74,7 +74,7 @@ class AccountTemplate { */ static templateSubstitutionsFor (userAccount) { const webUri = new URL(userAccount.webId) - const webIdOrigin = webUri.protocol + '//' + webUri.hostname + const webIdOrigin = webUri.protocol + '//' + webUri.hostname + ':' + webUri.port const podRelWebId = userAccount.webId.replace(webIdOrigin, '') const substitutions = { name: userAccount.displayName, diff --git a/test/integration/account-template-test.js b/test/integration/account-template-test.js index 44632f954..c6803b4db 100644 --- a/test/integration/account-template-test.js +++ b/test/integration/account-template-test.js @@ -76,6 +76,18 @@ describe('AccountTemplate', () => { expect(substitutions.webId).to.equal('https://alice.example.com/#me') }) + it('should not update the nested webid', () => { + const userAccount = new UserAccount({ + webId: 'https://alice.example.com/alice/#me', + email: 'alice@example.com', + name: 'Alice Q.' + }) + + const substitutions = AccountTemplate.templateSubstitutionsFor(userAccount) + + expect(substitutions.webId).to.equal('https://alice.example.com/alice/#me') + }) + it('should update the webid', () => { const userAccount = new UserAccount({ webId: 'http://localhost:8443/alice/#me', From 6a44656881061f0fb72cfe3d5a393ee4a3f7d5a0 Mon Sep 17 00:00:00 2001 From: bourgeoa Date: Wed, 22 May 2024 17:49:11 +0200 Subject: [PATCH 11/11] use webUri.origin and update tests --- lib/models/account-template.js | 3 +-- test/integration/account-manager-test.js | 2 +- test/integration/account-template-test.js | 4 ++-- test/unit/account-template-test.js | 2 +- 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/models/account-template.js b/lib/models/account-template.js index 7cc9c85ef..ddb65c7f6 100644 --- a/lib/models/account-template.js +++ b/lib/models/account-template.js @@ -74,8 +74,7 @@ class AccountTemplate { */ static templateSubstitutionsFor (userAccount) { const webUri = new URL(userAccount.webId) - const webIdOrigin = webUri.protocol + '//' + webUri.hostname + ':' + webUri.port - const podRelWebId = userAccount.webId.replace(webIdOrigin, '') + const podRelWebId = userAccount.webId.replace(webUri.origin, '') const substitutions = { name: userAccount.displayName, webId: userAccount.externalWebId ? userAccount.webId : podRelWebId, diff --git a/test/integration/account-manager-test.js b/test/integration/account-manager-test.js index 6eaaa3b30..a939dfce3 100644 --- a/test/integration/account-manager-test.js +++ b/test/integration/account-manager-test.js @@ -139,7 +139,7 @@ describe('AccountManager', () => { const rootAcl = fs.readFileSync(path.join(accountDir, '.acl'), 'utf8') expect(rootAcl).to.include('') + expect(rootAcl).to.include('') }) }) }) diff --git a/test/integration/account-template-test.js b/test/integration/account-template-test.js index c6803b4db..02d195a57 100644 --- a/test/integration/account-template-test.js +++ b/test/integration/account-template-test.js @@ -73,7 +73,7 @@ describe('AccountTemplate', () => { const substitutions = AccountTemplate.templateSubstitutionsFor(userAccount) - expect(substitutions.webId).to.equal('https://alice.example.com/#me') + expect(substitutions.webId).to.equal('/#me') }) it('should not update the nested webid', () => { @@ -85,7 +85,7 @@ describe('AccountTemplate', () => { const substitutions = AccountTemplate.templateSubstitutionsFor(userAccount) - expect(substitutions.webId).to.equal('https://alice.example.com/alice/#me') + expect(substitutions.webId).to.equal('/alice/#me') }) it('should update the webid', () => { diff --git a/test/unit/account-template-test.js b/test/unit/account-template-test.js index 67bdf18e7..3a2bd43be 100644 --- a/test/unit/account-template-test.js +++ b/test/unit/account-template-test.js @@ -54,7 +54,7 @@ describe('AccountTemplate', () => { const substitutions = AccountTemplate.templateSubstitutionsFor(userAccount) expect(substitutions.name).to.equal('Alice Q.') expect(substitutions.email).to.equal('alice@example.com') - expect(substitutions.webId).to.equal('https://alice.example.com/profile/card#me') + expect(substitutions.webId).to.equal('/profile/card#me') }) }) })