2021-12-17 15:57:11 +01:00
|
|
|
import { Component } from "@angular/core";
|
|
|
|
import { ActivatedRoute, Router } from "@angular/router";
|
|
|
|
import { first } from "rxjs/operators";
|
2021-10-15 00:59:43 +02:00
|
|
|
|
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";
|
[AC-1266] Enums filename conventions (#5140)
* refactor: update clientType enum
* refactor: update deviceType filename
* refactor: update encryptedExportType filename
* refactor: update encryptionType filename
* refactor: update eventType filename
* refactor: update fieldType filename
* refactor: update fileUploadType filename
* refactor: update hashPurpose filename
* refactor: update htmlStorageLocation filename
* refactor: update kdfType filename
* refactor: update keySuffixOptions filename
* refactor: update linkedIdType filename
* refactor: update logLevelType filename
* refactor: update nativeMessagingVersion filename
* refactor: update notificationType filename
* refactor: update productType filename
* refactor: update secureNoteType filename
* refactor: update stateVersion filename
* refactor: update storageLocation filename
* refactor: update themeType filename
* refactor: update uriMatchType filename
* fix: update kdfType classes missed in initial pass, refs AC-1266
* fix: missing import update for device-type
* refactor: add barrel file for enums and update pathed import statements, refs AC-1266
* fix: incorrect import statements for web, refs AC-1266
* fix: missed import statement updates (browser), refs AC-1266
* fix: missed import statement changes (cli), refs AC-1266
* fix: missed import statement changes (desktop), refs AC-1266
* fix: prettier, refs AC-1266
* refactor: (libs) update relative paths to use barrel file, refs AC-1266
* fix: missed find/replace import statements for SecureNoteType, refs AC-1266
* refactor: apply .enum suffix to enums folder and modify leftover relative paths, refs AC-1266
* fix: find/replace errors for native-messaging-version, refs AC-1266
2023-04-05 05:42:21 +02:00
|
|
|
import { HttpStatusCode } from "@bitwarden/common/enums";
|
2023-02-15 21:50:39 +01:00
|
|
|
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({
|
2021-12-17 15:57:11 +01:00
|
|
|
selector: "app-sso",
|
|
|
|
templateUrl: "sso.component.html",
|
2020-07-16 15:18:25 +02:00
|
|
|
})
|
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 {
|
2021-12-17 15:57:11 +01:00
|
|
|
constructor(
|
|
|
|
authService: AuthService,
|
|
|
|
router: Router,
|
|
|
|
i18nService: I18nService,
|
|
|
|
route: ActivatedRoute,
|
|
|
|
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-17 15:57:11 +01:00
|
|
|
) {
|
|
|
|
super(
|
|
|
|
authService,
|
|
|
|
router,
|
|
|
|
i18nService,
|
|
|
|
route,
|
|
|
|
stateService,
|
|
|
|
platformUtilsService,
|
|
|
|
apiService,
|
|
|
|
cryptoFunctionService,
|
|
|
|
environmentService,
|
|
|
|
passwordGenerationService,
|
|
|
|
logService
|
|
|
|
);
|
|
|
|
this.redirectUri = window.location.origin + "/sso-connector.html";
|
|
|
|
this.clientId = "web";
|
|
|
|
}
|
2020-08-20 22:39:05 +02:00
|
|
|
|
2021-12-17 15:57:11 +01:00
|
|
|
async ngOnInit() {
|
|
|
|
super.ngOnInit();
|
2023-02-15 21:50:39 +01:00
|
|
|
|
2023-04-26 14:47:35 +02:00
|
|
|
// if we have an emergency access invite, redirect to emergency access
|
|
|
|
const emergencyAccessInvite = await this.stateService.getEmergencyAccessInvitation();
|
|
|
|
if (emergencyAccessInvite != null) {
|
|
|
|
this.onSuccessfulLoginNavigate = async () => {
|
|
|
|
this.router.navigate(["/accept-emergency"], {
|
|
|
|
queryParams: {
|
|
|
|
id: emergencyAccessInvite.id,
|
|
|
|
name: emergencyAccessInvite.name,
|
|
|
|
email: emergencyAccessInvite.email,
|
|
|
|
token: emergencyAccessInvite.token,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:09:28 +02:00
|
|
|
// eslint-disable-next-line rxjs-angular/prefer-takeuntil, rxjs/no-async-subscribe
|
2021-12-17 15:57:11 +01: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-17 15:57:11 +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-17 15:57:11 +01:00
|
|
|
const storedIdentifier = await this.stateService.getSsoOrgIdentifier();
|
|
|
|
if (storedIdentifier != null) {
|
|
|
|
this.identifier = storedIdentifier;
|
2020-11-25 22:57:11 +01: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-17 15:57:11 +01:00
|
|
|
async submit() {
|
|
|
|
await this.stateService.setSsoOrganizationIdentifier(this.identifier);
|
|
|
|
if (this.clientId === "browser") {
|
|
|
|
document.cookie = `ssoHandOffMessage=${this.i18nService.t("ssoHandOff")};SameSite=strict`;
|
2020-08-20 22:39:05 +02:00
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
super.submit();
|
|
|
|
}
|
2020-07-16 15:18:25 +02:00
|
|
|
}
|