1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-15 15:53:27 +00:00

[PM-4360] Move organization-domain and organization-user to admin console (#6630)

* Move organization-domain and organization-user to admin console
This commit is contained in:
Oscar Hinton
2023-10-30 22:32:57 +01:00
committed by GitHub
parent 97b91133a5
commit 485be21826
71 changed files with 129 additions and 129 deletions

View File

@@ -0,0 +1,19 @@
import { OrganizationDomainRequest } from "../../services/organization-domain/requests/organization-domain.request";
import { OrganizationDomainSsoDetailsResponse } from "./responses/organization-domain-sso-details.response";
import { OrganizationDomainResponse } from "./responses/organization-domain.response";
export abstract class OrgDomainApiServiceAbstraction {
getAllByOrgId: (orgId: string) => Promise<Array<OrganizationDomainResponse>>;
getByOrgIdAndOrgDomainId: (
orgId: string,
orgDomainId: string
) => Promise<OrganizationDomainResponse>;
post: (
orgId: string,
orgDomain: OrganizationDomainRequest
) => Promise<OrganizationDomainResponse>;
verify: (orgId: string, orgDomainId: string) => Promise<OrganizationDomainResponse>;
delete: (orgId: string, orgDomainId: string) => Promise<any>;
getClaimedOrgDomainByEmail: (email: string) => Promise<OrganizationDomainSsoDetailsResponse>;
}

View File

@@ -0,0 +1,20 @@
import { Observable } from "rxjs";
import { OrganizationDomainResponse } from "./responses/organization-domain.response";
export abstract class OrgDomainServiceAbstraction {
orgDomains$: Observable<OrganizationDomainResponse[]>;
get: (orgDomainId: string) => OrganizationDomainResponse;
copyDnsTxt: (dnsTxt: string) => void;
}
// Note: this separate class is designed to hold methods that are not
// meant to be used in components (e.g., data write methods)
export abstract class OrgDomainInternalServiceAbstraction extends OrgDomainServiceAbstraction {
upsert: (orgDomains: OrganizationDomainResponse[]) => void;
replace: (orgDomains: OrganizationDomainResponse[]) => void;
clearCache: () => void;
delete: (orgDomainIds: string[]) => void;
}

View File

@@ -0,0 +1,18 @@
import { BaseResponse } from "../../../../models/response/base.response";
export class OrganizationDomainSsoDetailsResponse extends BaseResponse {
id: string;
organizationIdentifier: string;
ssoAvailable: boolean;
domainName: string;
verifiedDate?: Date;
constructor(response: any) {
super(response);
this.id = this.getResponseProperty("id");
this.organizationIdentifier = this.getResponseProperty("organizationIdentifier");
this.ssoAvailable = this.getResponseProperty("ssoAvailable");
this.domainName = this.getResponseProperty("domainName");
this.verifiedDate = this.getResponseProperty("verifiedDate");
}
}

View File

@@ -0,0 +1,26 @@
import { BaseResponse } from "../../../../models/response/base.response";
export class OrganizationDomainResponse extends BaseResponse {
id: string;
organizationId: string;
txt: string;
domainName: string;
creationDate: string;
nextRunDate: string;
jobRunCount: number;
verifiedDate?: string;
lastCheckedDate?: string;
constructor(response: any) {
super(response);
this.id = this.getResponseProperty("id");
this.organizationId = this.getResponseProperty("organizationId");
this.txt = this.getResponseProperty("txt");
this.domainName = this.getResponseProperty("domainName");
this.creationDate = this.getResponseProperty("creationDate");
this.nextRunDate = this.getResponseProperty("nextRunDate");
this.jobRunCount = this.getResponseProperty("jobRunCount");
this.verifiedDate = this.getResponseProperty("verifiedDate");
this.lastCheckedDate = this.getResponseProperty("lastCheckedDate");
}
}

View File

@@ -0,0 +1,268 @@
import { ListResponse } from "../../../models/response/list.response";
import {
OrganizationUserAcceptInitRequest,
OrganizationUserAcceptRequest,
OrganizationUserBulkConfirmRequest,
OrganizationUserConfirmRequest,
OrganizationUserInviteRequest,
OrganizationUserResetPasswordEnrollmentRequest,
OrganizationUserResetPasswordRequest,
OrganizationUserUpdateGroupsRequest,
OrganizationUserUpdateRequest,
} from "./requests";
import {
OrganizationUserBulkPublicKeyResponse,
OrganizationUserBulkResponse,
OrganizationUserDetailsResponse,
OrganizationUserResetPasswordDetailsResponse,
OrganizationUserUserDetailsResponse,
} from "./responses";
/**
* Service for interacting with Organization Users via the API
*/
export abstract class OrganizationUserService {
/**
* Retrieve a single organization user by Id
* @param organizationId - Identifier for the user's organization
* @param id - Organization user identifier
* @param options - Options for the request
*/
abstract getOrganizationUser(
organizationId: string,
id: string,
options?: {
includeGroups?: boolean;
}
): Promise<OrganizationUserDetailsResponse>;
/**
* Retrieve a list of groups Ids the specified organization user belongs to
* @param organizationId - Identifier for the user's organization
* @param id - Organization user identifier
*/
abstract getOrganizationUserGroups(organizationId: string, id: string): Promise<string[]>;
/**
* Retrieve a list of all users that belong to the specified organization
* @param organizationId - Identifier for the organization
* @param options - Options for the request
*/
abstract getAllUsers(
organizationId: string,
options?: {
includeCollections?: boolean;
includeGroups?: boolean;
}
): Promise<ListResponse<OrganizationUserUserDetailsResponse>>;
/**
* Retrieve reset password details for the specified organization user
* @param organizationId - Identifier for the user's organization
* @param id - Organization user identifier
*/
abstract getOrganizationUserResetPasswordDetails(
organizationId: string,
id: string
): Promise<OrganizationUserResetPasswordDetailsResponse>;
/**
* Create new organization user invite(s) for the specified organization
* @param organizationId - Identifier for the organization
* @param request - New user invitation request details
*/
abstract postOrganizationUserInvite(
organizationId: string,
request: OrganizationUserInviteRequest
): Promise<void>;
/**
* Re-invite the specified organization user
* @param organizationId - Identifier for the user's organization
* @param id - Organization user identifier
*/
abstract postOrganizationUserReinvite(organizationId: string, id: string): Promise<any>;
/**
* Re-invite many organization users for the specified organization
* @param organizationId - Identifier for the organization
* @param ids - A list of organization user identifiers
* @return List of user ids, including both those that were successfully re-invited and those that had an error
*/
abstract postManyOrganizationUserReinvite(
organizationId: string,
ids: string[]
): Promise<ListResponse<OrganizationUserBulkResponse>>;
/**
* Accept an invitation to initialize and join an organization created via the Admin Portal **only**.
* This is only used once for the initial Owner, because it also creates the organization's encryption keys.
* This should not be used for organizations created via the Web client.
* @param organizationId - Identifier for the organization to accept
* @param id - Organization user identifier
* @param request - Request details for accepting the invitation
*/
abstract postOrganizationUserAcceptInit(
organizationId: string,
id: string,
request: OrganizationUserAcceptInitRequest
): Promise<void>;
/**
* Accept an organization user invitation
* @param organizationId - Identifier for the organization to accept
* @param id - Organization user identifier
* @param request - Request details for accepting the invitation
*/
abstract postOrganizationUserAccept(
organizationId: string,
id: string,
request: OrganizationUserAcceptRequest
): Promise<void>;
/**
* Confirm an organization user that has accepted their invitation
* @param organizationId - Identifier for the organization to confirm
* @param id - Organization user identifier
* @param request - Request details for confirming the user
*/
abstract postOrganizationUserConfirm(
organizationId: string,
id: string,
request: OrganizationUserConfirmRequest
): Promise<void>;
/**
* Retrieve a list of the specified users' public keys
* @param organizationId - Identifier for the organization to accept
* @param ids - A list of organization user identifiers to retrieve public keys for
*/
abstract postOrganizationUsersPublicKey(
organizationId: string,
ids: string[]
): Promise<ListResponse<OrganizationUserBulkPublicKeyResponse>>;
/**
* Confirm many organization users that have accepted their invitations
* @param organizationId - Identifier for the organization to confirm users
* @param request - Bulk request details for confirming the user
*/
abstract postOrganizationUserBulkConfirm(
organizationId: string,
request: OrganizationUserBulkConfirmRequest
): Promise<ListResponse<OrganizationUserBulkResponse>>;
/**
* Update an organization users
* @param organizationId - Identifier for the organization the user belongs to
* @param id - Organization user identifier
* @param request - Request details for updating the user
*/
abstract putOrganizationUser(
organizationId: string,
id: string,
request: OrganizationUserUpdateRequest
): Promise<void>;
/**
* Update an organization user's groups
* @param organizationId - Identifier for the organization the user belongs to
* @param id - Organization user identifier
* @param groupIds - List of group ids to associate the user with
*/
abstract putOrganizationUserGroups(
organizationId: string,
id: string,
groupIds: OrganizationUserUpdateGroupsRequest
): Promise<void>;
/**
* Update an organization user's reset password enrollment
* @param organizationId - Identifier for the organization the user belongs to
* @param userId - Organization user identifier
* @param request - Reset password enrollment details
*/
abstract putOrganizationUserResetPasswordEnrollment(
organizationId: string,
userId: string,
request: OrganizationUserResetPasswordEnrollmentRequest
): Promise<void>;
/**
* Reset an organization user's password
* @param organizationId - Identifier for the organization the user belongs to
* @param id - Organization user identifier
* @param request - Reset password details
*/
abstract putOrganizationUserResetPassword(
organizationId: string,
id: string,
request: OrganizationUserResetPasswordRequest
): Promise<void>;
/**
* Enable Secrets Manager for many users
* @param organizationId - Identifier for the organization the user belongs to
* @param ids - List of organization user identifiers to enable
* @return List of user ids, including both those that were successfully enabled and those that had an error
*/
abstract putOrganizationUserBulkEnableSecretsManager(
organizationId: string,
ids: string[]
): Promise<void>;
/**
* Delete an organization user
* @param organizationId - Identifier for the organization the user belongs to
* @param id - Organization user identifier
*/
abstract deleteOrganizationUser(organizationId: string, id: string): Promise<void>;
/**
* Delete many organization users
* @param organizationId - Identifier for the organization the users belongs to
* @param ids - List of organization user identifiers to delete
* @return List of user ids, including both those that were successfully deleted and those that had an error
*/
abstract deleteManyOrganizationUsers(
organizationId: string,
ids: string[]
): Promise<ListResponse<OrganizationUserBulkResponse>>;
/**
* Revoke an organization user's access to the organization
* @param organizationId - Identifier for the organization the user belongs to
* @param id - Organization user identifier
*/
abstract revokeOrganizationUser(organizationId: string, id: string): Promise<void>;
/**
* Revoke many organization users' access to the organization
* @param organizationId - Identifier for the organization the users belongs to
* @param ids - List of organization user identifiers to revoke
* @return List of user ids, including both those that were successfully revoked and those that had an error
*/
abstract revokeManyOrganizationUsers(
organizationId: string,
ids: string[]
): Promise<ListResponse<OrganizationUserBulkResponse>>;
/**
* Restore an organization user's access to the organization
* @param organizationId - Identifier for the organization the user belongs to
* @param id - Organization user identifier
*/
abstract restoreOrganizationUser(organizationId: string, id: string): Promise<void>;
/**
* Restore many organization users' access to the organization
* @param organizationId - Identifier for the organization the users belongs to
* @param ids - List of organization user identifiers to restore
* @return List of user ids, including both those that were successfully restored and those that had an error
*/
abstract restoreManyOrganizationUsers(
organizationId: string,
ids: string[]
): Promise<ListResponse<OrganizationUserBulkResponse>>;
}

View File

@@ -0,0 +1,9 @@
export * from "./organization-user-accept-init.request";
export * from "./organization-user-accept.request";
export * from "./organization-user-bulk-confirm.request";
export * from "./organization-user-confirm.request";
export * from "./organization-user-invite.request";
export * from "./organization-user-reset-password.request";
export * from "./organization-user-reset-password-enrollment.request";
export * from "./organization-user-update.request";
export * from "./organization-user-update-groups.request";

View File

@@ -0,0 +1,8 @@
import { OrganizationKeysRequest } from "../../../models/request/organization-keys.request";
export class OrganizationUserAcceptInitRequest {
token: string;
key: string;
keys: OrganizationKeysRequest;
collectionName: string;
}

View File

@@ -0,0 +1,5 @@
export class OrganizationUserAcceptRequest {
token: string;
// Used to auto-enroll in master password reset
resetPasswordKey: string;
}

View File

@@ -0,0 +1,12 @@
type OrganizationUserBulkRequestEntry = {
id: string;
key: string;
};
export class OrganizationUserBulkConfirmRequest {
keys: OrganizationUserBulkRequestEntry[];
constructor(keys: OrganizationUserBulkRequestEntry[]) {
this.keys = keys;
}
}

View File

@@ -0,0 +1,3 @@
export class OrganizationUserConfirmRequest {
key: string;
}

View File

@@ -0,0 +1,13 @@
import { OrganizationUserType } from "../../../enums";
import { PermissionsApi } from "../../../models/api/permissions.api";
import { SelectionReadOnlyRequest } from "../../../models/request/selection-read-only.request";
export class OrganizationUserInviteRequest {
emails: string[] = [];
type: OrganizationUserType;
accessAll: boolean;
accessSecretsManager: boolean;
collections: SelectionReadOnlyRequest[] = [];
groups: string[];
permissions: PermissionsApi;
}

View File

@@ -0,0 +1,5 @@
import { SecretVerificationRequest } from "../../../../auth/models/request/secret-verification.request";
export class OrganizationUserResetPasswordEnrollmentRequest extends SecretVerificationRequest {
resetPasswordKey: string;
}

View File

@@ -0,0 +1,4 @@
export class OrganizationUserResetPasswordRequest {
newMasterPasswordHash: string;
key: string;
}

View File

@@ -0,0 +1,3 @@
export class OrganizationUserUpdateGroupsRequest {
groupIds: string[] = [];
}

View File

@@ -0,0 +1,12 @@
import { OrganizationUserType } from "../../../enums";
import { PermissionsApi } from "../../../models/api/permissions.api";
import { SelectionReadOnlyRequest } from "../../../models/request/selection-read-only.request";
export class OrganizationUserUpdateRequest {
type: OrganizationUserType;
accessAll: boolean;
accessSecretsManager: boolean;
collections: SelectionReadOnlyRequest[] = [];
groups: string[] = [];
permissions: PermissionsApi;
}

View File

@@ -0,0 +1,3 @@
export * from "./organization-user.response";
export * from "./organization-user-bulk.response";
export * from "./organization-user-bulk-public-key.response";

View File

@@ -0,0 +1,14 @@
import { BaseResponse } from "../../../../models/response/base.response";
export class OrganizationUserBulkPublicKeyResponse extends BaseResponse {
id: string;
userId: string;
key: string;
constructor(response: any) {
super(response);
this.id = this.getResponseProperty("Id");
this.userId = this.getResponseProperty("UserId");
this.key = this.getResponseProperty("Key");
}
}

View File

@@ -0,0 +1,12 @@
import { BaseResponse } from "../../../../models/response/base.response";
export class OrganizationUserBulkResponse extends BaseResponse {
id: string;
error: string;
constructor(response: any) {
super(response);
this.id = this.getResponseProperty("Id");
this.error = this.getResponseProperty("Error");
}
}

View File

@@ -0,0 +1,85 @@
import { KdfType } from "../../../../enums";
import { BaseResponse } from "../../../../models/response/base.response";
import { OrganizationUserStatusType, OrganizationUserType } from "../../../enums";
import { PermissionsApi } from "../../../models/api/permissions.api";
import { SelectionReadOnlyResponse } from "../../../models/response/selection-read-only.response";
export class OrganizationUserResponse extends BaseResponse {
id: string;
userId: string;
type: OrganizationUserType;
status: OrganizationUserStatusType;
externalId: string;
accessAll: boolean;
accessSecretsManager: boolean;
permissions: PermissionsApi;
resetPasswordEnrolled: boolean;
hasMasterPassword: boolean;
collections: SelectionReadOnlyResponse[] = [];
groups: string[] = [];
constructor(response: any) {
super(response);
this.id = this.getResponseProperty("Id");
this.userId = this.getResponseProperty("UserId");
this.type = this.getResponseProperty("Type");
this.status = this.getResponseProperty("Status");
this.permissions = new PermissionsApi(this.getResponseProperty("Permissions"));
this.externalId = this.getResponseProperty("ExternalId");
this.accessAll = this.getResponseProperty("AccessAll");
this.accessSecretsManager = this.getResponseProperty("AccessSecretsManager");
this.resetPasswordEnrolled = this.getResponseProperty("ResetPasswordEnrolled");
this.hasMasterPassword = this.getResponseProperty("HasMasterPassword");
const collections = this.getResponseProperty("Collections");
if (collections != null) {
this.collections = collections.map((c: any) => new SelectionReadOnlyResponse(c));
}
const groups = this.getResponseProperty("Groups");
if (groups != null) {
this.groups = groups;
}
}
}
export class OrganizationUserUserDetailsResponse extends OrganizationUserResponse {
name: string;
email: string;
avatarColor: string;
twoFactorEnabled: boolean;
usesKeyConnector: boolean;
constructor(response: any) {
super(response);
this.name = this.getResponseProperty("Name");
this.email = this.getResponseProperty("Email");
this.avatarColor = this.getResponseProperty("AvatarColor");
this.twoFactorEnabled = this.getResponseProperty("TwoFactorEnabled");
this.usesKeyConnector = this.getResponseProperty("UsesKeyConnector") ?? false;
}
}
export class OrganizationUserDetailsResponse extends OrganizationUserResponse {
constructor(response: any) {
super(response);
}
}
export class OrganizationUserResetPasswordDetailsResponse extends BaseResponse {
kdf: KdfType;
kdfIterations: number;
kdfMemory?: number;
kdfParallelism?: number;
resetPasswordKey: string;
encryptedPrivateKey: string;
constructor(response: any) {
super(response);
this.kdf = this.getResponseProperty("Kdf");
this.kdfIterations = this.getResponseProperty("KdfIterations");
this.kdfMemory = this.getResponseProperty("KdfMemory");
this.kdfParallelism = this.getResponseProperty("KdfParallelism");
this.resetPasswordKey = this.getResponseProperty("ResetPasswordKey");
this.encryptedPrivateKey = this.getResponseProperty("EncryptedPrivateKey");
}
}

View File

@@ -1,3 +1,3 @@
import { OrganizationUserBulkPublicKeyResponse } from "../../../../abstractions/organization-user/responses";
import { OrganizationUserBulkPublicKeyResponse } from "../../../abstractions/organization-user/responses";
export class ProviderUserBulkPublicKeyResponse extends OrganizationUserBulkPublicKeyResponse {}

View File

@@ -0,0 +1,202 @@
import { mock } from "jest-mock-extended";
import { lastValueFrom } from "rxjs";
import { ApiService } from "../../../abstractions/api.service";
import { I18nService } from "../../../platform/abstractions/i18n.service";
import { PlatformUtilsService } from "../../../platform/abstractions/platform-utils.service";
import { OrganizationDomainSsoDetailsResponse } from "../../abstractions/organization-domain/responses/organization-domain-sso-details.response";
import { OrganizationDomainResponse } from "../../abstractions/organization-domain/responses/organization-domain.response";
import { OrgDomainApiService } from "./org-domain-api.service";
import { OrgDomainService } from "./org-domain.service";
import { OrganizationDomainSsoDetailsRequest } from "./requests/organization-domain-sso-details.request";
const mockedGetAllByOrgIdResponse: any = {
data: [
{
id: "ca01a674-7f2f-45f2-8245-af6d016416b7",
organizationId: "cb903acf-2361-4072-ae32-af6c014943b6",
txt: "bw=EUX6UKR8A68igAJkmodwkzMiqB00u7Iyq1QqALu6jFID",
domainName: "test.com",
creationDate: "2022-12-16T21:36:28.68Z",
nextRunDate: "2022-12-17T09:36:28.68Z",
jobRunCount: 0,
verifiedDate: null as any,
lastCheckedDate: "2022-12-16T21:36:28.7633333Z",
object: "organizationDomain",
},
{
id: "adbd44c5-90d5-4537-97e6-af6d01644870",
organizationId: "cb903acf-2361-4072-ae32-af6c014943b6",
txt: "bw=Ql4fCfDacmcjwyAP9BPmvhSMTCz4PkEDm4uQ3fH01pD4",
domainName: "test2.com",
creationDate: "2022-12-16T21:37:10.9566667Z",
nextRunDate: "2022-12-17T09:37:10.9566667Z",
jobRunCount: 0,
verifiedDate: "totally verified",
lastCheckedDate: "2022-12-16T21:37:11.1933333Z",
object: "organizationDomain",
},
{
id: "05cf3ab8-bcfe-4b95-92e8-af6d01680942",
organizationId: "cb903acf-2361-4072-ae32-af6c014943b6",
txt: "bw=EQNUs77BWQHbfSiyc/9nT3wCen9z2yMn/ABCz0cNKaTx",
domainName: "test3.com",
creationDate: "2022-12-16T21:50:50.96Z",
nextRunDate: "2022-12-17T09:50:50.96Z",
jobRunCount: 0,
verifiedDate: null,
lastCheckedDate: "2022-12-16T21:50:51.0933333Z",
object: "organizationDomain",
},
],
continuationToken: null as any,
object: "list",
};
const mockedOrgDomainServerResponse = {
id: "ca01a674-7f2f-45f2-8245-af6d016416b7",
organizationId: "cb903acf-2361-4072-ae32-af6c014943b6",
txt: "bw=EUX6UKR8A68igAJkmodwkzMiqB00u7Iyq1QqALu6jFID",
domainName: "test.com",
creationDate: "2022-12-16T21:36:28.68Z",
nextRunDate: "2022-12-17T09:36:28.68Z",
jobRunCount: 0,
verifiedDate: null as any,
lastCheckedDate: "2022-12-16T21:36:28.7633333Z",
object: "organizationDomain",
};
const mockedOrgDomainResponse = new OrganizationDomainResponse(mockedOrgDomainServerResponse);
const mockedOrganizationDomainSsoDetailsServerResponse = {
id: "fake-guid",
organizationIdentifier: "fake-org-identifier",
ssoAvailable: true,
domainName: "fake-domain-name",
verifiedDate: "2022-12-16T21:36:28.68Z",
};
const mockedOrganizationDomainSsoDetailsResponse = new OrganizationDomainSsoDetailsResponse(
mockedOrganizationDomainSsoDetailsServerResponse
);
describe("Org Domain API Service", () => {
let orgDomainApiService: OrgDomainApiService;
const apiService = mock<ApiService>();
let orgDomainService: OrgDomainService;
const platformUtilService = mock<PlatformUtilsService>();
const i18nService = mock<I18nService>();
beforeEach(() => {
orgDomainService = new OrgDomainService(platformUtilService, i18nService);
jest.resetAllMocks();
orgDomainApiService = new OrgDomainApiService(orgDomainService, apiService);
});
it("instantiates", () => {
expect(orgDomainApiService).not.toBeFalsy();
});
it("getAllByOrgId retrieves all org domains and calls orgDomainSvc replace", () => {
apiService.send.mockResolvedValue(mockedGetAllByOrgIdResponse);
expect(lastValueFrom(orgDomainService.orgDomains$)).resolves.toHaveLength(0);
const orgDomainSvcReplaceSpy = jest.spyOn(orgDomainService, "replace");
orgDomainApiService
.getAllByOrgId("fakeOrgId")
.then((orgDomainResponses: Array<OrganizationDomainResponse>) => {
expect(orgDomainResponses).toHaveLength(3);
expect(orgDomainSvcReplaceSpy).toHaveBeenCalled();
expect(lastValueFrom(orgDomainService.orgDomains$)).resolves.toHaveLength(3);
});
});
it("getByOrgIdAndOrgDomainId retrieves single org domain and calls orgDomainSvc upsert", () => {
apiService.send.mockResolvedValue(mockedOrgDomainServerResponse);
expect(lastValueFrom(orgDomainService.orgDomains$)).resolves.toHaveLength(0);
const orgDomainSvcUpsertSpy = jest.spyOn(orgDomainService, "upsert");
orgDomainApiService
.getByOrgIdAndOrgDomainId("fakeOrgId", "fakeDomainId")
.then((orgDomain: OrganizationDomainResponse) => {
expect(orgDomain.id).toEqual(mockedOrgDomainServerResponse.id);
expect(orgDomainSvcUpsertSpy).toHaveBeenCalled();
expect(lastValueFrom(orgDomainService.orgDomains$)).resolves.toHaveLength(1);
});
});
it("post success should call orgDomainSvc upsert", () => {
apiService.send.mockResolvedValue(mockedOrgDomainServerResponse);
expect(lastValueFrom(orgDomainService.orgDomains$)).resolves.toHaveLength(0);
const orgDomainSvcUpsertSpy = jest.spyOn(orgDomainService, "upsert");
orgDomainApiService
.post("fakeOrgId", mockedOrgDomainResponse)
.then((orgDomain: OrganizationDomainResponse) => {
expect(orgDomain.id).toEqual(mockedOrgDomainServerResponse.id);
expect(orgDomainSvcUpsertSpy).toHaveBeenCalled();
expect(lastValueFrom(orgDomainService.orgDomains$)).resolves.toHaveLength(1);
});
});
it("verify success should call orgDomainSvc upsert", () => {
apiService.send.mockResolvedValue(mockedOrgDomainServerResponse);
expect(lastValueFrom(orgDomainService.orgDomains$)).resolves.toHaveLength(0);
const orgDomainSvcUpsertSpy = jest.spyOn(orgDomainService, "upsert");
orgDomainApiService
.verify("fakeOrgId", "fakeOrgId")
.then((orgDomain: OrganizationDomainResponse) => {
expect(orgDomain.id).toEqual(mockedOrgDomainServerResponse.id);
expect(orgDomainSvcUpsertSpy).toHaveBeenCalled();
expect(lastValueFrom(orgDomainService.orgDomains$)).resolves.toHaveLength(1);
});
});
it("delete success should call orgDomainSvc delete", () => {
apiService.send.mockResolvedValue(true);
orgDomainService.upsert([mockedOrgDomainResponse]);
expect(lastValueFrom(orgDomainService.orgDomains$)).resolves.toHaveLength(1);
const orgDomainSvcDeleteSpy = jest.spyOn(orgDomainService, "delete");
orgDomainApiService.delete("fakeOrgId", "fakeOrgId").then(() => {
expect(orgDomainSvcDeleteSpy).toHaveBeenCalled();
expect(lastValueFrom(orgDomainService.orgDomains$)).resolves.toHaveLength(0);
});
});
it("getClaimedOrgDomainByEmail should call ApiService.send with correct parameters and return response", async () => {
const email = "test@example.com";
apiService.send.mockResolvedValue(mockedOrganizationDomainSsoDetailsServerResponse);
const result = await orgDomainApiService.getClaimedOrgDomainByEmail(email);
expect(apiService.send).toHaveBeenCalledWith(
"POST",
"/organizations/domain/sso/details",
new OrganizationDomainSsoDetailsRequest(email),
false, //anonymous
true
);
expect(result).toEqual(mockedOrganizationDomainSsoDetailsResponse);
});
});

View File

@@ -0,0 +1,112 @@
import { ApiService } from "../../../abstractions/api.service";
import { ListResponse } from "../../../models/response/list.response";
import { OrgDomainApiServiceAbstraction } from "../../abstractions/organization-domain/org-domain-api.service.abstraction";
import { OrgDomainInternalServiceAbstraction } from "../../abstractions/organization-domain/org-domain.service.abstraction";
import { OrganizationDomainSsoDetailsResponse } from "../../abstractions/organization-domain/responses/organization-domain-sso-details.response";
import { OrganizationDomainResponse } from "../../abstractions/organization-domain/responses/organization-domain.response";
import { OrganizationDomainSsoDetailsRequest } from "./requests/organization-domain-sso-details.request";
import { OrganizationDomainRequest } from "./requests/organization-domain.request";
export class OrgDomainApiService implements OrgDomainApiServiceAbstraction {
constructor(
private orgDomainService: OrgDomainInternalServiceAbstraction,
private apiService: ApiService
) {}
async getAllByOrgId(orgId: string): Promise<Array<OrganizationDomainResponse>> {
const listResponse: ListResponse<any> = await this.apiService.send(
"GET",
`/organizations/${orgId}/domain`,
null,
true,
true
);
const orgDomains = listResponse.data.map(
(resultOrgDomain: any) => new OrganizationDomainResponse(resultOrgDomain)
);
this.orgDomainService.replace(orgDomains);
return orgDomains;
}
async getByOrgIdAndOrgDomainId(
orgId: string,
orgDomainId: string
): Promise<OrganizationDomainResponse> {
const result = await this.apiService.send(
"GET",
`/organizations/${orgId}/domain/${orgDomainId}`,
null,
true,
true
);
const response = new OrganizationDomainResponse(result);
this.orgDomainService.upsert([response]);
return response;
}
async post(
orgId: string,
orgDomainReq: OrganizationDomainRequest
): Promise<OrganizationDomainResponse> {
const result = await this.apiService.send(
"POST",
`/organizations/${orgId}/domain`,
orgDomainReq,
true,
true
);
const response = new OrganizationDomainResponse(result);
this.orgDomainService.upsert([response]);
return response;
}
async verify(orgId: string, orgDomainId: string): Promise<OrganizationDomainResponse> {
const result = await this.apiService.send(
"POST",
`/organizations/${orgId}/domain/${orgDomainId}/verify`,
null,
true,
true
);
const response = new OrganizationDomainResponse(result);
this.orgDomainService.upsert([response]);
return response;
}
async delete(orgId: string, orgDomainId: string): Promise<any> {
await this.apiService.send(
"DELETE",
`/organizations/${orgId}/domain/${orgDomainId}`,
null,
true,
false
);
this.orgDomainService.delete([orgDomainId]);
}
async getClaimedOrgDomainByEmail(email: string): Promise<OrganizationDomainSsoDetailsResponse> {
const result = await this.apiService.send(
"POST",
`/organizations/domain/sso/details`,
new OrganizationDomainSsoDetailsRequest(email),
false, // anonymous
true
);
const response = new OrganizationDomainSsoDetailsResponse(result);
return response;
}
}

View File

@@ -0,0 +1,169 @@
import { mock, mockReset } from "jest-mock-extended";
import { lastValueFrom } from "rxjs";
import { I18nService } from "../../../platform/abstractions/i18n.service";
import { PlatformUtilsService } from "../../../platform/abstractions/platform-utils.service";
import { OrganizationDomainResponse } from "../../abstractions/organization-domain/responses/organization-domain.response";
import { OrgDomainService } from "./org-domain.service";
const mockedUnverifiedDomainServerResponse = {
creationDate: "2022-12-13T23:16:43.7066667Z",
domainName: "bacon.com",
id: "12eac4ea-9ed8-4dd4-85da-af6a017f9f97",
jobRunCount: 0,
lastCheckedDate: "2022-12-13T23:16:43.8033333Z",
nextRunDate: "2022-12-14T11:16:43.7066667Z",
object: "organizationDomain",
organizationId: "e4bffa5e-6602-4bc7-a83f-af55016566ef",
txt: "bw=eRBGgwJhZk0Kmpd8qPdSrrkSsTD006B+JgmMztk4XjDX",
verifiedDate: null as any,
};
const mockedVerifiedDomainServerResponse = {
creationDate: "2022-12-13T23:16:43.7066667Z",
domainName: "cat.com",
id: "58715f70-8650-4a42-9d4a-af6a0188151b",
jobRunCount: 0,
lastCheckedDate: "2022-12-13T23:16:43.8033333Z",
nextRunDate: "2022-12-14T11:16:43.7066667Z",
object: "organizationDomain",
organizationId: "e4bffa5e-6602-4bc7-a83f-af55016566ef",
txt: "bw=eRBGgwJhZk0Kmpd8qPdSrrkSsTD006B+JgmMztk4XjDX",
verifiedDate: "2022-12-13T23:16:43.7066667Z",
};
const mockedExtraDomainServerResponse = {
creationDate: "2022-12-13T23:16:43.7066667Z",
domainName: "dog.com",
id: "fac7cdb6-283e-4805-aa55-af6b016bf699",
jobRunCount: 0,
lastCheckedDate: "2022-12-13T23:16:43.8033333Z",
nextRunDate: "2022-12-14T11:16:43.7066667Z",
object: "organizationDomain",
organizationId: "e4bffa5e-6602-4bc7-a83f-af55016566ef",
txt: "bw=eRBGgwJhZk0Kmpd8qPdSrrkSsTD006B+JgmMztk4XjDX",
verifiedDate: null as any,
};
const mockedUnverifiedOrgDomainResponse = new OrganizationDomainResponse(
mockedUnverifiedDomainServerResponse
);
const mockedVerifiedOrgDomainResponse = new OrganizationDomainResponse(
mockedVerifiedDomainServerResponse
);
const mockedExtraOrgDomainResponse = new OrganizationDomainResponse(
mockedExtraDomainServerResponse
);
describe("Org Domain Service", () => {
let orgDomainService: OrgDomainService;
const platformUtilService = mock<PlatformUtilsService>();
const i18nService = mock<I18nService>();
beforeEach(() => {
mockReset(platformUtilService);
mockReset(i18nService);
orgDomainService = new OrgDomainService(platformUtilService, i18nService);
});
it("instantiates", () => {
expect(orgDomainService).not.toBeFalsy();
});
it("orgDomains$ public observable exists and instantiates w/ empty array", () => {
expect(orgDomainService.orgDomains$).toBeDefined();
expect(lastValueFrom(orgDomainService.orgDomains$)).resolves.toEqual([]);
});
it("replace and clear work", () => {
const newOrgDomains = [mockedUnverifiedOrgDomainResponse, mockedVerifiedOrgDomainResponse];
orgDomainService.replace(newOrgDomains);
expect(lastValueFrom(orgDomainService.orgDomains$)).resolves.toEqual(newOrgDomains);
orgDomainService.clearCache();
expect(lastValueFrom(orgDomainService.orgDomains$)).resolves.toEqual([]);
});
it("get successfully retrieves org domain by id", () => {
const orgDomains = [mockedUnverifiedOrgDomainResponse, mockedVerifiedOrgDomainResponse];
orgDomainService.replace(orgDomains);
expect(orgDomainService.get(mockedVerifiedOrgDomainResponse.id)).toEqual(
mockedVerifiedOrgDomainResponse
);
expect(orgDomainService.get(mockedUnverifiedOrgDomainResponse.id)).toEqual(
mockedUnverifiedOrgDomainResponse
);
});
it("upsert both updates an existing org domain and adds a new one", () => {
const orgDomains = [mockedUnverifiedOrgDomainResponse, mockedVerifiedOrgDomainResponse];
orgDomainService.replace(orgDomains);
const changedOrgDomain = new OrganizationDomainResponse(mockedVerifiedDomainServerResponse);
changedOrgDomain.domainName = "changed domain name";
expect(mockedVerifiedOrgDomainResponse.domainName).not.toEqual(changedOrgDomain.domainName);
orgDomainService.upsert([changedOrgDomain]);
expect(orgDomainService.get(mockedVerifiedOrgDomainResponse.id).domainName).toEqual(
changedOrgDomain.domainName
);
const newOrgDomain = new OrganizationDomainResponse({
creationDate: "2022-12-13T23:16:43.7066667Z",
domainName: "cat.com",
id: "magical-cat-id-number-999",
jobRunCount: 0,
lastCheckedDate: "2022-12-13T23:16:43.8033333Z",
nextRunDate: "2022-12-14T11:16:43.7066667Z",
object: "organizationDomain",
organizationId: "e4bffa5e-6602-4bc7-a83f-af55016566ef",
txt: "bw=eRBGgwJhZk0Kmpd8qPdSrrkSsTD006B+JgmMztk4XjDX",
verifiedDate: null as any,
});
expect(lastValueFrom(orgDomainService.orgDomains$)).resolves.toHaveLength(2);
orgDomainService.upsert([newOrgDomain]);
expect(lastValueFrom(orgDomainService.orgDomains$)).resolves.toHaveLength(3);
expect(orgDomainService.get(newOrgDomain.id)).toEqual(newOrgDomain);
});
it("delete successfully removes multiple org domains", () => {
const orgDomains = [
mockedUnverifiedOrgDomainResponse,
mockedVerifiedOrgDomainResponse,
mockedExtraOrgDomainResponse,
];
orgDomainService.replace(orgDomains);
expect(lastValueFrom(orgDomainService.orgDomains$)).resolves.toHaveLength(3);
orgDomainService.delete([mockedUnverifiedOrgDomainResponse.id]);
expect(lastValueFrom(orgDomainService.orgDomains$)).resolves.toHaveLength(2);
expect(orgDomainService.get(mockedUnverifiedOrgDomainResponse.id)).toEqual(undefined);
orgDomainService.delete([mockedVerifiedOrgDomainResponse.id, mockedExtraOrgDomainResponse.id]);
expect(lastValueFrom(orgDomainService.orgDomains$)).resolves.toHaveLength(0);
expect(orgDomainService.get(mockedVerifiedOrgDomainResponse.id)).toEqual(undefined);
expect(orgDomainService.get(mockedExtraOrgDomainResponse.id)).toEqual(undefined);
});
it("copyDnsTxt copies DNS TXT to clipboard and shows toast", () => {
orgDomainService.copyDnsTxt("fakeTxt");
expect(jest.spyOn(platformUtilService, "copyToClipboard")).toHaveBeenCalled();
expect(jest.spyOn(platformUtilService, "showToast")).toHaveBeenCalled();
});
});

View File

@@ -0,0 +1,73 @@
import { BehaviorSubject } from "rxjs";
import { I18nService } from "../../../platform/abstractions/i18n.service";
import { PlatformUtilsService } from "../../../platform/abstractions/platform-utils.service";
import { OrgDomainInternalServiceAbstraction } from "../../abstractions/organization-domain/org-domain.service.abstraction";
import { OrganizationDomainResponse } from "../../abstractions/organization-domain/responses/organization-domain.response";
export class OrgDomainService implements OrgDomainInternalServiceAbstraction {
protected _orgDomains$: BehaviorSubject<OrganizationDomainResponse[]> = new BehaviorSubject([]);
orgDomains$ = this._orgDomains$.asObservable();
constructor(
private platformUtilsService: PlatformUtilsService,
private i18nService: I18nService
) {}
get(orgDomainId: string): OrganizationDomainResponse {
const orgDomains: OrganizationDomainResponse[] = this._orgDomains$.getValue();
return orgDomains.find((orgDomain) => orgDomain.id === orgDomainId);
}
copyDnsTxt(dnsTxt: string): void {
this.platformUtilsService.copyToClipboard(dnsTxt);
this.platformUtilsService.showToast(
"success",
null,
this.i18nService.t("valueCopied", this.i18nService.t("dnsTxtRecord"))
);
}
upsert(orgDomains: OrganizationDomainResponse[]): void {
const existingOrgDomains: OrganizationDomainResponse[] = this._orgDomains$.getValue();
orgDomains.forEach((orgDomain: OrganizationDomainResponse) => {
// Determine if passed in orgDomain exists in existing array:
const index = existingOrgDomains.findIndex(
(existingOrgDomain) => existingOrgDomain.id === orgDomain.id
);
if (index !== -1) {
existingOrgDomains[index] = orgDomain;
} else {
existingOrgDomains.push(orgDomain);
}
});
this._orgDomains$.next(existingOrgDomains);
}
replace(orgDomains: OrganizationDomainResponse[]): void {
this._orgDomains$.next(orgDomains);
}
clearCache(): void {
this._orgDomains$.next([]);
}
delete(orgDomainIds: string[]): void {
const existingOrgDomains: OrganizationDomainResponse[] = this._orgDomains$.getValue();
orgDomainIds.forEach((orgDomainId: string) => {
const index = existingOrgDomains.findIndex(
(existingOrgDomain) => existingOrgDomain.id === orgDomainId
);
if (index !== -1) {
existingOrgDomains.splice(index, 1);
}
});
this._orgDomains$.next(existingOrgDomains);
}
}

View File

@@ -0,0 +1,3 @@
export class OrganizationDomainSsoDetailsRequest {
constructor(public email: string) {}
}

View File

@@ -0,0 +1,9 @@
export class OrganizationDomainRequest {
txt: string;
domainName: string;
constructor(txt: string, domainName: string) {
this.txt = txt;
this.domainName = domainName;
}
}

View File

@@ -0,0 +1,349 @@
import { ApiService } from "../../../abstractions/api.service";
import { ListResponse } from "../../../models/response/list.response";
import { OrganizationUserService } from "../../abstractions/organization-user/organization-user.service";
import {
OrganizationUserAcceptInitRequest,
OrganizationUserAcceptRequest,
OrganizationUserBulkConfirmRequest,
OrganizationUserConfirmRequest,
OrganizationUserInviteRequest,
OrganizationUserResetPasswordEnrollmentRequest,
OrganizationUserResetPasswordRequest,
OrganizationUserUpdateGroupsRequest,
OrganizationUserUpdateRequest,
} from "../../abstractions/organization-user/requests";
import {
OrganizationUserBulkPublicKeyResponse,
OrganizationUserBulkResponse,
OrganizationUserDetailsResponse,
OrganizationUserResetPasswordDetailsResponse,
OrganizationUserUserDetailsResponse,
} from "../../abstractions/organization-user/responses";
import { OrganizationUserBulkRequest } from "./requests";
export class OrganizationUserServiceImplementation implements OrganizationUserService {
constructor(private apiService: ApiService) {}
async getOrganizationUser(
organizationId: string,
id: string,
options?: {
includeGroups?: boolean;
}
): Promise<OrganizationUserDetailsResponse> {
const params = new URLSearchParams();
if (options?.includeGroups) {
params.set("includeGroups", "true");
}
const r = await this.apiService.send(
"GET",
`/organizations/${organizationId}/users/${id}?${params.toString()}`,
null,
true,
true
);
return new OrganizationUserDetailsResponse(r);
}
async getOrganizationUserGroups(organizationId: string, id: string): Promise<string[]> {
const r = await this.apiService.send(
"GET",
"/organizations/" + organizationId + "/users/" + id + "/groups",
null,
true,
true
);
return r;
}
async getAllUsers(
organizationId: string,
options?: {
includeCollections?: boolean;
includeGroups?: boolean;
}
): Promise<ListResponse<OrganizationUserUserDetailsResponse>> {
const params = new URLSearchParams();
if (options?.includeCollections) {
params.set("includeCollections", "true");
}
if (options?.includeGroups) {
params.set("includeGroups", "true");
}
const r = await this.apiService.send(
"GET",
`/organizations/${organizationId}/users?${params.toString()}`,
null,
true,
true
);
return new ListResponse(r, OrganizationUserUserDetailsResponse);
}
async getOrganizationUserResetPasswordDetails(
organizationId: string,
id: string
): Promise<OrganizationUserResetPasswordDetailsResponse> {
const r = await this.apiService.send(
"GET",
"/organizations/" + organizationId + "/users/" + id + "/reset-password-details",
null,
true,
true
);
return new OrganizationUserResetPasswordDetailsResponse(r);
}
postOrganizationUserInvite(
organizationId: string,
request: OrganizationUserInviteRequest
): Promise<void> {
return this.apiService.send(
"POST",
"/organizations/" + organizationId + "/users/invite",
request,
true,
false
);
}
postOrganizationUserReinvite(organizationId: string, id: string): Promise<any> {
return this.apiService.send(
"POST",
"/organizations/" + organizationId + "/users/" + id + "/reinvite",
null,
true,
false
);
}
async postManyOrganizationUserReinvite(
organizationId: string,
ids: string[]
): Promise<ListResponse<OrganizationUserBulkResponse>> {
const r = await this.apiService.send(
"POST",
"/organizations/" + organizationId + "/users/reinvite",
new OrganizationUserBulkRequest(ids),
true,
true
);
return new ListResponse(r, OrganizationUserBulkResponse);
}
postOrganizationUserAcceptInit(
organizationId: string,
id: string,
request: OrganizationUserAcceptInitRequest
): Promise<void> {
return this.apiService.send(
"POST",
"/organizations/" + organizationId + "/users/" + id + "/accept-init",
request,
true,
false
);
}
postOrganizationUserAccept(
organizationId: string,
id: string,
request: OrganizationUserAcceptRequest
): Promise<void> {
return this.apiService.send(
"POST",
"/organizations/" + organizationId + "/users/" + id + "/accept",
request,
true,
false
);
}
postOrganizationUserConfirm(
organizationId: string,
id: string,
request: OrganizationUserConfirmRequest
): Promise<void> {
return this.apiService.send(
"POST",
"/organizations/" + organizationId + "/users/" + id + "/confirm",
request,
true,
false
);
}
async postOrganizationUsersPublicKey(
organizationId: string,
ids: string[]
): Promise<ListResponse<OrganizationUserBulkPublicKeyResponse>> {
const r = await this.apiService.send(
"POST",
"/organizations/" + organizationId + "/users/public-keys",
new OrganizationUserBulkRequest(ids),
true,
true
);
return new ListResponse(r, OrganizationUserBulkPublicKeyResponse);
}
async postOrganizationUserBulkConfirm(
organizationId: string,
request: OrganizationUserBulkConfirmRequest
): Promise<ListResponse<OrganizationUserBulkResponse>> {
const r = await this.apiService.send(
"POST",
"/organizations/" + organizationId + "/users/confirm",
request,
true,
true
);
return new ListResponse(r, OrganizationUserBulkResponse);
}
async putOrganizationUserBulkEnableSecretsManager(
organizationId: string,
ids: string[]
): Promise<void> {
await this.apiService.send(
"PUT",
"/organizations/" + organizationId + "/users/enable-secrets-manager",
new OrganizationUserBulkRequest(ids),
true,
false
);
}
putOrganizationUser(
organizationId: string,
id: string,
request: OrganizationUserUpdateRequest
): Promise<void> {
return this.apiService.send(
"PUT",
"/organizations/" + organizationId + "/users/" + id,
request,
true,
false
);
}
putOrganizationUserGroups(
organizationId: string,
id: string,
request: OrganizationUserUpdateGroupsRequest
): Promise<void> {
return this.apiService.send(
"PUT",
"/organizations/" + organizationId + "/users/" + id + "/groups",
request,
true,
false
);
}
putOrganizationUserResetPasswordEnrollment(
organizationId: string,
userId: string,
request: OrganizationUserResetPasswordEnrollmentRequest
): Promise<void> {
return this.apiService.send(
"PUT",
"/organizations/" + organizationId + "/users/" + userId + "/reset-password-enrollment",
request,
true,
false
);
}
putOrganizationUserResetPassword(
organizationId: string,
id: string,
request: OrganizationUserResetPasswordRequest
): Promise<void> {
return this.apiService.send(
"PUT",
"/organizations/" + organizationId + "/users/" + id + "/reset-password",
request,
true,
false
);
}
deleteOrganizationUser(organizationId: string, id: string): Promise<any> {
return this.apiService.send(
"DELETE",
"/organizations/" + organizationId + "/users/" + id,
null,
true,
false
);
}
async deleteManyOrganizationUsers(
organizationId: string,
ids: string[]
): Promise<ListResponse<OrganizationUserBulkResponse>> {
const r = await this.apiService.send(
"DELETE",
"/organizations/" + organizationId + "/users",
new OrganizationUserBulkRequest(ids),
true,
true
);
return new ListResponse(r, OrganizationUserBulkResponse);
}
revokeOrganizationUser(organizationId: string, id: string): Promise<void> {
return this.apiService.send(
"PUT",
"/organizations/" + organizationId + "/users/" + id + "/revoke",
null,
true,
false
);
}
async revokeManyOrganizationUsers(
organizationId: string,
ids: string[]
): Promise<ListResponse<OrganizationUserBulkResponse>> {
const r = await this.apiService.send(
"PUT",
"/organizations/" + organizationId + "/users/revoke",
new OrganizationUserBulkRequest(ids),
true,
true
);
return new ListResponse(r, OrganizationUserBulkResponse);
}
restoreOrganizationUser(organizationId: string, id: string): Promise<void> {
return this.apiService.send(
"PUT",
"/organizations/" + organizationId + "/users/" + id + "/restore",
null,
true,
false
);
}
async restoreManyOrganizationUsers(
organizationId: string,
ids: string[]
): Promise<ListResponse<OrganizationUserBulkResponse>> {
const r = await this.apiService.send(
"PUT",
"/organizations/" + organizationId + "/users/restore",
new OrganizationUserBulkRequest(ids),
true,
true
);
return new ListResponse(r, OrganizationUserBulkResponse);
}
}

View File

@@ -0,0 +1 @@
export * from "./organization-user-bulk.request";

View File

@@ -0,0 +1,7 @@
export class OrganizationUserBulkRequest {
ids: string[];
constructor(ids: string[]) {
this.ids = ids == null ? [] : ids;
}
}