Skip to content

Commit

Permalink
Merge pull request #1250 from remotestorage/bugfix/1247-connect_via_url
Browse files Browse the repository at this point in the history
Allow to connect via URL
  • Loading branch information
galfert authored Oct 12, 2021
2 parents 553d11e + a8dd021 commit 5b580e8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/discover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ let cachedInfo = {};
* This function deals with the Webfinger lookup, discovering a connecting
* user's storage details.
*
* @param {string} userAddress - user@host
* @param {string} userAddress - user@host or URL
*
* @returns {Promise} A promise for an object with the following properties.
* href - Storage base URL,
Expand Down
11 changes: 8 additions & 3 deletions src/remotestorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,16 +327,21 @@ class RemoteStorage {
* tokens in all requests later on. This is useful for example when using
* Kerberos and similar protocols.
*
* @param {string} userAddress - The user address (user@host) to connect to.
* @param {string} userAddress - The user address (user@host) or URL to connect to.
* @param {string} token - (optional) A bearer token acquired beforehand
*/
connect (userAddress: string, token?: string): void {
this.setBackend('remotestorage');
if (userAddress.indexOf('@') < 0) {
this._emit('error', new RemoteStorage.DiscoveryError("User address doesn't contain an @."));
if (userAddress.indexOf('@') < 0 && !userAddress.match(/^(https?:\/\/)?[^\s\/$\.?#]+\.[^\s]*$/)) {
this._emit('error', new RemoteStorage.DiscoveryError("Not a valid user address or URL."));
return;
}

// Prefix URL with https:// if it's missing
if (userAddress.indexOf('@') < 0 && !userAddress.match(/^https?:\/\//)) {
userAddress = `https://${userAddress}`;
}

if (globalContext.cordova) {
if (typeof config.cordovaRedirectUri !== 'string') {
this._emit('error', new RemoteStorage.DiscoveryError("Please supply a custom HTTPS redirect URI for your Cordova app"));
Expand Down
32 changes: 31 additions & 1 deletion test/unit/remotestorage-suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ define(['require', 'tv4', './build/eventhandling', './build/util'],
},

{
desc: "#connect throws unauthorized when userAddress doesn't contain an @",
desc: "#connect throws unauthorized when userAddress doesn't contain an @ or URL",
run: function(env, test) {
env.rs.on('error', function(e) {
test.assert(e instanceof RemoteStorage.DiscoveryError, true);
Expand All @@ -255,6 +255,36 @@ define(['require', 'tv4', './build/eventhandling', './build/util'],
}
},

{
desc: "#connect accepts URLs for the userAddress",
run: function(env, test) {
env.rs.on('error', function(e) {
test.fail('URL userAddress was not accepted.');
});

env.rs.remote = new FakeRemote(false);
env.rs.remote.configure = function (options) {
test.assert(options.userAddress, 'https://personal.ho.st');
}
env.rs.connect('https://personal.ho.st');
}
},

{
desc: "#connect adds missing https:// to URLs",
run: function(env, test) {
env.rs.on('error', function(e) {
test.fail('URL userAddress was not accepted.');
});

env.rs.remote = new FakeRemote(false);
env.rs.remote.configure = function (options) {
test.assert(options.userAddress, 'https://personal.ho.st');
}
env.rs.connect('personal.ho.st');
}
},

{
desc: "#connect sets the backend to remotestorage",
run: function(env, test) {
Expand Down

0 comments on commit 5b580e8

Please sign in to comment.