2020-07-16 15:18:25 +02:00
|
|
|
import { Component } from "@angular/core";
|
|
|
|
import { ActivatedRoute, Router } from "@angular/router";
|
2021-10-15 00:59:43 +02:00
|
|
|
import { first } from "rxjs/operators";
|
|
|
|
|
2023-02-06 22:53:37 +01:00
|
|
|
import { SsoComponent as BaseSsoComponent } from "@bitwarden/angular/auth/components/sso.component";
|
2022-06-14 17:10:53 +02:00
|
|
|
import { ApiService } from "@bitwarden/common/abstractions/api.service";
|
|
|
|
import { CryptoFunctionService } from "@bitwarden/common/abstractions/cryptoFunction.service";
|
|
|
|
import { EnvironmentService } from "@bitwarden/common/abstractions/environment.service";
|
|
|
|
import { I18nService } from "@bitwarden/common/abstractions/i18n.service";
|
|
|
|
import { LogService } from "@bitwarden/common/abstractions/log.service";
|
2023-02-15 21:50:39 +01:00
|
|
|
import { OrgDomainApiServiceAbstraction } from "@bitwarden/common/abstractions/organization-domain/org-domain-api.service.abstraction";
|
|
|
|
import { OrganizationDomainSsoDetailsResponse } from "@bitwarden/common/abstractions/organization-domain/responses/organization-domain-sso-details.response";
|
2022-06-14 17:10:53 +02:00
|
|
|
import { PlatformUtilsService } from "@bitwarden/common/abstractions/platformUtils.service";
|
|
|
|
import { StateService } from "@bitwarden/common/abstractions/state.service";
|
2023-02-15 21:50:39 +01:00
|
|
|
import { ValidationService } from "@bitwarden/common/abstractions/validation.service";
|
2023-02-06 22:53:37 +01:00
|
|
|
import { AuthService } from "@bitwarden/common/auth/abstractions/auth.service";
|
2023-02-15 21:50:39 +01:00
|
|
|
import { LoginService } from "@bitwarden/common/auth/abstractions/login.service";
|
|
|
|
import { HttpStatusCode } from "@bitwarden/common/enums/http-status-code.enum";
|
|
|
|
import { ErrorResponse } from "@bitwarden/common/models/response/error.response";
|
2023-03-10 21:39:46 +01:00
|
|
|
import { PasswordGenerationServiceAbstraction } from "@bitwarden/common/tools/generator/password";
|
2020-07-16 15:18:25 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: "app-sso",
|
|
|
|
templateUrl: "sso.component.html",
|
|
|
|
})
|
2022-08-26 18:09:28 +02:00
|
|
|
// eslint-disable-next-line rxjs-angular/prefer-takeuntil
|
2020-08-13 20:32:07 +02:00
|
|
|
export class SsoComponent extends BaseSsoComponent {
|
|
|
|
constructor(
|
|
|
|
authService: AuthService,
|
|
|
|
router: Router,
|
|
|
|
i18nService: I18nService,
|
|
|
|
route: ActivatedRoute,
|
2021-12-14 17:10:26 +01:00
|
|
|
stateService: StateService,
|
|
|
|
platformUtilsService: PlatformUtilsService,
|
|
|
|
apiService: ApiService,
|
|
|
|
cryptoFunctionService: CryptoFunctionService,
|
|
|
|
environmentService: EnvironmentService,
|
2023-03-10 21:39:46 +01:00
|
|
|
passwordGenerationService: PasswordGenerationServiceAbstraction,
|
2023-02-15 21:50:39 +01:00
|
|
|
logService: LogService,
|
|
|
|
private orgDomainApiService: OrgDomainApiServiceAbstraction,
|
|
|
|
private loginService: LoginService,
|
|
|
|
private validationService: ValidationService
|
2021-12-14 17:10:26 +01:00
|
|
|
) {
|
|
|
|
super(
|
|
|
|
authService,
|
|
|
|
router,
|
|
|
|
i18nService,
|
|
|
|
route,
|
|
|
|
stateService,
|
|
|
|
platformUtilsService,
|
2021-10-20 18:30:04 +02:00
|
|
|
apiService,
|
2021-10-15 00:59:43 +02:00
|
|
|
cryptoFunctionService,
|
2021-12-14 17:10:26 +01:00
|
|
|
environmentService,
|
|
|
|
passwordGenerationService,
|
2021-10-20 18:30:04 +02:00
|
|
|
logService
|
2021-12-14 17:10:26 +01:00
|
|
|
);
|
|
|
|
this.redirectUri = window.location.origin + "/sso-connector.html";
|
2020-08-20 22:39:05 +02:00
|
|
|
this.clientId = "web";
|
|
|
|
}
|
|
|
|
|
|
|
|
async ngOnInit() {
|
|
|
|
super.ngOnInit();
|
2023-02-15 21:50:39 +01:00
|
|
|
|
2022-08-26 18:09:28 +02:00
|
|
|
// eslint-disable-next-line rxjs-angular/prefer-takeuntil, rxjs/no-async-subscribe
|
2020-08-20 22:39:05 +02:00
|
|
|
this.route.queryParams.pipe(first()).subscribe(async (qParams) => {
|
|
|
|
if (qParams.identifier != null) {
|
2023-02-15 21:50:39 +01:00
|
|
|
// SSO Org Identifier in query params takes precedence over claimed domains
|
2021-12-14 17:10:26 +01:00
|
|
|
this.identifier = qParams.identifier;
|
|
|
|
} else {
|
2023-02-15 21:50:39 +01:00
|
|
|
// Note: this flow is written for web but both browser and desktop
|
|
|
|
// redirect here on SSO button click.
|
|
|
|
|
|
|
|
// Check if email matches any claimed domains
|
|
|
|
if (qParams.email) {
|
|
|
|
// show loading spinner
|
|
|
|
this.loggingIn = true;
|
|
|
|
try {
|
|
|
|
const response: OrganizationDomainSsoDetailsResponse =
|
|
|
|
await this.orgDomainApiService.getClaimedOrgDomainByEmail(qParams.email);
|
|
|
|
|
|
|
|
if (response?.ssoAvailable) {
|
|
|
|
this.identifier = response.organizationIdentifier;
|
|
|
|
await this.submit();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
this.handleGetClaimedDomainByEmailError(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.loggingIn = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fallback to state svc if domain is unclaimed
|
2021-12-14 17:10:26 +01:00
|
|
|
const storedIdentifier = await this.stateService.getSsoOrgIdentifier();
|
2020-11-25 22:57:11 +01:00
|
|
|
if (storedIdentifier != null) {
|
2020-12-22 17:28:58 +01:00
|
|
|
this.identifier = storedIdentifier;
|
2020-11-25 22:57:11 +01:00
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2020-08-20 22:39:05 +02:00
|
|
|
});
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2023-02-15 21:50:39 +01:00
|
|
|
private handleGetClaimedDomainByEmailError(error: any): void {
|
|
|
|
if (error instanceof ErrorResponse) {
|
|
|
|
const errorResponse: ErrorResponse = error as ErrorResponse;
|
|
|
|
switch (errorResponse.statusCode) {
|
|
|
|
case HttpStatusCode.NotFound:
|
2023-04-03 17:45:22 +02:00
|
|
|
//this is a valid case for a domain not found
|
|
|
|
return;
|
2023-02-15 21:50:39 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
this.validationService.showError(errorResponse);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-20 22:39:05 +02:00
|
|
|
async submit() {
|
|
|
|
await this.stateService.setSsoOrganizationIdentifier(this.identifier);
|
2020-11-25 22:57:11 +01:00
|
|
|
if (this.clientId === "browser") {
|
2020-12-22 17:28:58 +01:00
|
|
|
document.cookie = `ssoHandOffMessage=${this.i18nService.t("ssoHandOff")};SameSite=strict`;
|
2020-08-20 22:39:05 +02:00
|
|
|
}
|
|
|
|
super.submit();
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2020-07-16 15:18:25 +02:00
|
|
|
}
|