mirror of
https://github.com/bitwarden/browser
synced 2025-12-16 16:23:44 +00:00
Wrap sso login with pre-validation check (#160)
* Wrap sso login with pre-validation check * Add form promise for SSO preValidate * Removed boolean variable, .catch()
This commit is contained in:
@@ -306,4 +306,6 @@ export abstract class ApiService {
|
|||||||
getActiveBearerToken: () => Promise<string>;
|
getActiveBearerToken: () => Promise<string>;
|
||||||
fetch: (request: Request) => Promise<Response>;
|
fetch: (request: Request) => Promise<Response>;
|
||||||
nativeFetch: (request: Request) => Promise<Response>;
|
nativeFetch: (request: Request) => Promise<Response>;
|
||||||
|
|
||||||
|
preValidateSso: (identifier: string) => Promise<boolean>;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ export class SsoComponent {
|
|||||||
loggingIn = false;
|
loggingIn = false;
|
||||||
|
|
||||||
formPromise: Promise<AuthResult>;
|
formPromise: Promise<AuthResult>;
|
||||||
|
initiateSsoFormPromise: Promise<any>;
|
||||||
onSuccessfulLogin: () => Promise<any>;
|
onSuccessfulLogin: () => Promise<any>;
|
||||||
onSuccessfulLoginNavigate: () => Promise<any>;
|
onSuccessfulLoginNavigate: () => Promise<any>;
|
||||||
onSuccessfulLoginTwoFactorNavigate: () => Promise<any>;
|
onSuccessfulLoginTwoFactorNavigate: () => Promise<any>;
|
||||||
@@ -67,8 +68,20 @@ export class SsoComponent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async submit(returnUri?: string, includeUserIdentifier?: boolean) {
|
async submit(returnUri?: string, includeUserIdentifier?: boolean) {
|
||||||
const authorizeUrl = await this.buildAuthorizeUrl(returnUri, includeUserIdentifier);
|
this.initiateSsoFormPromise = this.preValidate();
|
||||||
this.platformUtilsService.launchUri(authorizeUrl, { sameWindow: true });
|
if (await this.initiateSsoFormPromise) {
|
||||||
|
const authorizeUrl = await this.buildAuthorizeUrl(returnUri, includeUserIdentifier);
|
||||||
|
this.platformUtilsService.launchUri(authorizeUrl, { sameWindow: true });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async preValidate(): Promise<boolean> {
|
||||||
|
if (this.identifier == null || this.identifier === '') {
|
||||||
|
this.platformUtilsService.showToast('error', this.i18nService.t('ssoValidationFailed'),
|
||||||
|
this.i18nService.t('ssoIdentifierRequired'));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return await this.apiService.preValidateSso(this.identifier);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async buildAuthorizeUrl(returnUri?: string, includeUserIdentifier?: boolean): Promise<string> {
|
protected async buildAuthorizeUrl(returnUri?: string, includeUserIdentifier?: boolean): Promise<string> {
|
||||||
|
|||||||
@@ -1040,6 +1040,35 @@ export class ApiService implements ApiServiceAbstraction {
|
|||||||
return fetch(request);
|
return fetch(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async preValidateSso(identifier: string): Promise<boolean> {
|
||||||
|
|
||||||
|
if (identifier == null || identifier === '') {
|
||||||
|
throw new Error('Organization Identifier was not provided.');
|
||||||
|
}
|
||||||
|
const headers = new Headers({
|
||||||
|
'Accept': 'application/json',
|
||||||
|
'Device-Type': this.deviceType,
|
||||||
|
});
|
||||||
|
if (this.customUserAgent != null) {
|
||||||
|
headers.set('User-Agent', this.customUserAgent);
|
||||||
|
}
|
||||||
|
|
||||||
|
const path = `/account/prevalidate?domainHint=${encodeURIComponent(identifier)}`;
|
||||||
|
const response = await this.fetch(new Request(this.identityBaseUrl + path, {
|
||||||
|
cache: 'no-store',
|
||||||
|
credentials: this.getCredentials(),
|
||||||
|
headers: headers,
|
||||||
|
method: 'GET',
|
||||||
|
}));
|
||||||
|
|
||||||
|
if (response.status === 200) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
const error = await this.handleError(response, false);
|
||||||
|
return Promise.reject(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async send(method: 'GET' | 'POST' | 'PUT' | 'DELETE', path: string, body: any,
|
private async send(method: 'GET' | 'POST' | 'PUT' | 'DELETE', path: string, body: any,
|
||||||
authed: boolean, hasResponse: boolean): Promise<any> {
|
authed: boolean, hasResponse: boolean): Promise<any> {
|
||||||
const headers = new Headers({
|
const headers = new Headers({
|
||||||
|
|||||||
Reference in New Issue
Block a user