Skip to content

Commit

Permalink
Merge branch 'feature/PB-33597_As-a-user-running-an-instance-serving-…
Browse files Browse the repository at this point in the history
…an-invalid-certificate-I-cannot-install-passbolt-extension-using-an-API--v3' into 'release'

PB-33595 - As a user running an instance serving an invalid certificate I...

See merge request passbolt/passbolt-browser-extension!882
  • Loading branch information
Gamabunta57 committed May 17, 2024
2 parents 2eb85ca + 1800def commit ce79251
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 131 deletions.
39 changes: 10 additions & 29 deletions src/all/background_page/model/setup/setupModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class SetupModel {
* @throws {Error} if options are invalid or API error
*/
async startSetup(userId, authenticationTokenToken) {
let user, accountRecoveryOrganizationPolicy, userDto, accountRecoveryOrganizationPolicyDto, userPassphrasePoliciesDto, userPassphrasePolicies;
let user, accountRecoveryOrganizationPolicy, userPassphrasePolicies;

if (!Validator.isUUID(userId)) {
throw new TypeError("userId should be a valid uuid.");
Expand All @@ -50,20 +50,10 @@ class SetupModel {
throw new TypeError("authenticationTokenToken should be a valid uuid.");
}

try {
const result = await this.setupService.findSetupInfo(userId, authenticationTokenToken);
userDto = result?.user;
accountRecoveryOrganizationPolicyDto = result?.account_recovery_organization_policy;
userPassphrasePoliciesDto = result?.user_passphrase_policy;
} catch (error) {
// If the entry point doesn't exist or return a 500, the API version is <v3.
const code = error.data && error.data.code;
if (code === 404 || code === 500) {
userDto = await this.setupService.findLegacySetupInfo(userId, authenticationTokenToken);
} else {
throw error;
}
}
const result = await this.setupService.findSetupInfo(userId, authenticationTokenToken);
const userDto = result?.user;
const accountRecoveryOrganizationPolicyDto = result?.account_recovery_organization_policy;
const userPassphrasePoliciesDto = result?.user_passphrase_policy;

if (userDto) {
user = new UserEntity(userDto);
Expand All @@ -89,7 +79,7 @@ class SetupModel {
* @return {Promise<{user: UserEntity, userPassphrasePolicies: UserPassphrasePoliciesEntity}>}
*/
async startRecover(userId, authenticationTokenToken) {
let user, userDto, userPassphrasePoliciesDto, userPassphrasePolicies;
let user, userPassphrasePolicies;

if (!Validator.isUUID(userId)) {
throw new TypeError("userId should be a valid uuid.");
Expand All @@ -98,19 +88,10 @@ class SetupModel {
throw new TypeError("authenticationTokenToken should be a valid uuid.");
}

try {
const result = await this.setupService.findRecoverInfo(userId, authenticationTokenToken);
userDto = result?.user;
userPassphrasePoliciesDto = result?.user_passphrase_policy;
} catch (error) {
// If the entry point doesn't exist or return a 500, the API version is <v3.
const code = error.data && error.data.code;
if (code === 404 || code === 500) {
userDto = await this.setupService.findLegacyRecoverInfo(userId, authenticationTokenToken);
} else {
throw error;
}
}
const result = await this.setupService.findRecoverInfo(userId, authenticationTokenToken);
const userDto = result?.user;
const userPassphrasePoliciesDto = result?.user_passphrase_policy;

if (userDto) {
user = new UserEntity(userDto);
}
Expand Down
102 changes: 0 additions & 102 deletions src/all/background_page/service/api/setup/setupService.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
* @license https://opensource.org/licenses/AGPL-3.0 AGPL License
* @link https://www.passbolt.com Passbolt(tm)
*/
import PassboltBadResponseError from "../../../error/passboltBadResponseError";
import AbstractService from "../abstract/abstractService";
import PassboltServiceUnavailableError from "../../../error/passboltServiceUnavailableError";

const SETUP_SERVICE_RESOURCE_NAME = 'setup';

Expand Down Expand Up @@ -109,106 +107,6 @@ class SetupService extends AbstractService {
const response = await this.apiClient.fetchAndHandleResponse('POST', url, bodyString);
return response.body;
}

/**
* Find legacy setup info.
* @param {string} userId the user id
* @param {string} token the token
* @returns {Promise<*>} response body
* @throws {Error} if options are invalid or API error
* @deprecated will be removed with v4
*/
async findLegacySetupInfo(userId, token) {
this.assertValidId(userId);
this.assertValidId(token);

const url = new URL(`${this.apiClient.baseUrl}/install/${userId}/${token}`);
let response, responseHtml, username, firstName, lastName;
try {
response = await fetch(url.toString());
} catch (error) {
if (navigator.onLine) {
// Catch Network error such as bad certificate or server unreachable.
throw new PassboltServiceUnavailableError("Unable to reach the server, an unexpected error occurred");
} else {
// Network connection lost.
throw new PassboltServiceUnavailableError("Unable to reach the server, you are not connected to the network");
}
}

try {
responseHtml = await response.text();
const parser = new DOMParser();
const parsedHtml = parser.parseFromString(responseHtml, 'text/html');
username = parsedHtml.getElementById('js_setup_user_username').value;
firstName = parsedHtml.getElementById('js_setup_user_first_name').value;
lastName = parsedHtml.getElementById('js_setup_user_last_name').value;
} catch (error) {
/*
* If the response cannot be parsed, it's not a Passbolt API response.
* It can be a for example a proxy timeout error (504).
*/
throw new PassboltBadResponseError();
}

return {
username: username,
profile: {
first_name: firstName,
last_name: lastName
}
};
}

/**
* Find legacy recover info
* @param {string} userId the user id
* @param {string} token the token
* @returns {Promise<*>} response body
* @throws {Error} if options are invalid or API error
* @deprecated will be removed with v4
*/
async findLegacyRecoverInfo(userId, token) {
this.assertValidId(userId);
this.assertValidId(token);

const url = new URL(`${this.apiClient.baseUrl}/recover/${userId}/${token}`);
let response, responseHtml, username, firstName, lastName;
try {
response = await fetch(url.toString());
} catch (error) {
if (navigator.onLine) {
// Catch Network error such as bad certificate or server unreachable.
throw new PassboltServiceUnavailableError("Unable to reach the server, an unexpected error occurred");
} else {
// Network connection lost.
throw new PassboltServiceUnavailableError("Unable to reach the server, you are not connected to the network");
}
}

try {
responseHtml = await response.text();
const parser = new DOMParser();
const parsedHtml = parser.parseFromString(responseHtml, 'text/html');
username = parsedHtml.getElementById('js_setup_user_username').value;
firstName = parsedHtml.getElementById('js_setup_user_first_name').value;
lastName = parsedHtml.getElementById('js_setup_user_last_name').value;
} catch (error) {
/*
* If the response cannot be parsed, it's not a Passbolt API response.
* It can be a for example a proxy timeout error (504).
*/
throw new PassboltBadResponseError();
}

return {
username: username,
profile: {
first_name: firstName,
last_name: lastName
}
};
}
}

export default SetupService;

0 comments on commit ce79251

Please sign in to comment.