@webiny/cognito 6.6.0-alpha.11 → 6.6.0-alpha.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/admin/Cognito.js +7 -1
- package/admin/Cognito.js.map +1 -1
- package/admin/presentation/Cognito/CognitoLoginScreen.js +9 -1
- package/admin/presentation/Cognito/CognitoLoginScreen.js.map +1 -1
- package/admin/presentation/Cognito/CognitoPresenter.d.ts +10 -1
- package/admin/presentation/Cognito/CognitoPresenter.js +50 -4
- package/admin/presentation/Cognito/CognitoPresenter.js.map +1 -1
- package/admin/presentation/Cognito/CognitoPresenter.test.d.ts +1 -0
- package/admin/presentation/Cognito/CognitoPresenter.test.js +180 -0
- package/admin/presentation/Cognito/CognitoPresenter.test.js.map +1 -0
- package/admin/presentation/Cognito/abstractions.d.ts +8 -1
- package/admin/presentation/Cognito/abstractions.js.map +1 -1
- package/admin/presentation/Cognito/components/FederatedProviders.js +1 -1
- package/admin/presentation/Cognito/components/FederatedProviders.js.map +1 -1
- package/admin/presentation/Cognito/components/NoTenantAccess.d.ts +23 -0
- package/admin/presentation/Cognito/components/NoTenantAccess.js +10 -0
- package/admin/presentation/Cognito/components/NoTenantAccess.js.map +1 -0
- package/admin/presentation/Cognito/components/SelectTenant.d.ts +26 -0
- package/admin/presentation/Cognito/components/SelectTenant.js +15 -0
- package/admin/presentation/Cognito/components/SelectTenant.js.map +1 -0
- package/admin/presentation/Cognito/components/SetNewPassword.js.map +1 -1
- package/admin/presentation/Cognito/components/SetupTotp.js +6 -5
- package/admin/presentation/Cognito/components/SetupTotp.js.map +1 -1
- package/admin/presentation/Cognito/components/View.js +5 -5
- package/admin/presentation/Cognito/components/View.js.map +1 -1
- package/admin/presentation/Cognito/components/index.d.ts +38 -0
- package/admin/presentation/Cognito/components/index.js +4 -0
- package/admin/presentation/Cognito/components/index.js.map +1 -1
- package/api/CognitoApiFeature.js +6 -0
- package/api/CognitoApiFeature.js.map +1 -1
- package/api/domain/errors.d.ts +68 -0
- package/api/domain/errors.js +73 -1
- package/api/domain/errors.js.map +1 -1
- package/api/features/CheckEmail/CheckEmailUseCase.d.ts +16 -0
- package/api/features/CheckEmail/CheckEmailUseCase.js +47 -0
- package/api/features/CheckEmail/CheckEmailUseCase.js.map +1 -0
- package/api/features/CheckEmail/abstractions.d.ts +29 -0
- package/api/features/CheckEmail/abstractions.js +5 -0
- package/api/features/CheckEmail/abstractions.js.map +1 -0
- package/api/features/CheckEmail/feature.d.ts +4 -0
- package/api/features/CheckEmail/feature.js +11 -0
- package/api/features/CheckEmail/feature.js.map +1 -0
- package/api/features/CheckEmail/index.d.ts +2 -0
- package/api/features/CheckEmail/index.js +2 -0
- package/api/features/CognitoIdp/CognitoIdentityProvider.d.ts +2 -0
- package/api/features/CognitoIdp/CognitoIdentityProvider.js +20 -0
- package/api/features/CognitoIdp/CognitoIdentityProvider.js.map +1 -1
- package/api/features/CognitoIdp/abstractions.d.ts +6 -0
- package/api/features/CognitoIdp/abstractions.js.map +1 -1
- package/api/features/CognitoService/CognitoService.d.ts +2 -1
- package/api/features/CognitoService/CognitoService.js +20 -5
- package/api/features/CognitoService/CognitoService.js.map +1 -1
- package/api/features/CognitoService/abstractions.d.ts +13 -2
- package/api/features/CognitoService/abstractions.js.map +1 -1
- package/api/features/CognitoService/adminDeleteSoftwareToken.d.ts +16 -0
- package/api/features/CognitoService/adminDeleteSoftwareToken.js +69 -0
- package/api/features/CognitoService/adminDeleteSoftwareToken.js.map +1 -0
- package/api/features/CognitoService/adminDeleteSoftwareToken.test.d.ts +1 -0
- package/api/features/CognitoService/adminDeleteSoftwareToken.test.js +91 -0
- package/api/features/CognitoService/adminDeleteSoftwareToken.test.js.map +1 -0
- package/api/features/CreateUser/CreateUserUseCase.d.ts +1 -2
- package/api/features/CreateUser/CreateUserUseCase.js +3 -3
- package/api/features/CreateUser/CreateUserUseCase.js.map +1 -1
- package/api/features/CreateUser/abstractions.d.ts +3 -1
- package/api/features/CreateUser/abstractions.js.map +1 -1
- package/api/features/DeleteUser/DeleteUserUseCase.d.ts +3 -1
- package/api/features/DeleteUser/DeleteUserUseCase.js +11 -2
- package/api/features/DeleteUser/DeleteUserUseCase.js.map +1 -1
- package/api/features/DeleteUser/abstractions.d.ts +2 -0
- package/api/features/DeleteUser/abstractions.js.map +1 -1
- package/api/features/LinkUser/LinkUserUseCase.d.ts +22 -0
- package/api/features/LinkUser/LinkUserUseCase.js +83 -0
- package/api/features/LinkUser/LinkUserUseCase.js.map +1 -0
- package/api/features/LinkUser/abstractions.d.ts +36 -0
- package/api/features/LinkUser/abstractions.js +5 -0
- package/api/features/LinkUser/abstractions.js.map +1 -0
- package/api/features/LinkUser/feature.d.ts +4 -0
- package/api/features/LinkUser/feature.js +11 -0
- package/api/features/LinkUser/feature.js.map +1 -0
- package/api/features/LinkUser/index.d.ts +2 -0
- package/api/features/LinkUser/index.js +2 -0
- package/api/features/ResetUserMfa/ResetUserMfaUseCase.d.ts +18 -0
- package/api/features/ResetUserMfa/ResetUserMfaUseCase.js +59 -0
- package/api/features/ResetUserMfa/ResetUserMfaUseCase.js.map +1 -0
- package/api/features/ResetUserMfa/abstractions.d.ts +24 -0
- package/api/features/ResetUserMfa/abstractions.js +5 -0
- package/api/features/ResetUserMfa/abstractions.js.map +1 -0
- package/api/features/ResetUserMfa/feature.d.ts +4 -0
- package/api/features/ResetUserMfa/feature.js +11 -0
- package/api/features/ResetUserMfa/feature.js.map +1 -0
- package/api/features/ResetUserMfa/index.d.ts +2 -0
- package/api/features/ResetUserMfa/index.js +2 -0
- package/api/features/UpdateUser/UpdateUserUseCase.d.ts +24 -1
- package/api/features/UpdateUser/UpdateUserUseCase.js +97 -4
- package/api/features/UpdateUser/UpdateUserUseCase.js.map +1 -1
- package/api/features/UpdateUser/abstractions.d.ts +7 -1
- package/api/features/UpdateUser/abstractions.js.map +1 -1
- package/api/features/UserInstaller/UserInstaller.d.ts +3 -1
- package/api/features/UserInstaller/UserInstaller.js +18 -3
- package/api/features/UserInstaller/UserInstaller.js.map +1 -1
- package/api/graphql/user.gql.js +87 -0
- package/api/graphql/user.gql.js.map +1 -1
- package/package.json +24 -19
|
@@ -6,7 +6,7 @@ import { CreateUserUseCase } from "@webiny/api-core/features/users/CreateUser/in
|
|
|
6
6
|
import { ListUsersUseCase } from "@webiny/api-core/features/users/ListUsers/index.js";
|
|
7
7
|
import { CreateUserUseCase as external_abstractions_js_CreateUserUseCase } from "./abstractions.js";
|
|
8
8
|
import { Username } from "../../domain/Username.js";
|
|
9
|
-
import { CognitoAccountExistsError, CognitoCreateUserError } from "../../domain/errors.js";
|
|
9
|
+
import { CognitoAccountExistsError, CognitoAccountInOtherTenantError, CognitoCreateUserError } from "../../domain/errors.js";
|
|
10
10
|
import { createAdminUserValidation } from "./schema.js";
|
|
11
11
|
import { CognitoService } from "../CognitoService/index.js";
|
|
12
12
|
class CreateUserUseCaseImpl {
|
|
@@ -31,8 +31,8 @@ class CreateUserUseCaseImpl {
|
|
|
31
31
|
if (existingUserResult.isOk()) return Result.fail(new CognitoAccountExistsError(data.email));
|
|
32
32
|
let user;
|
|
33
33
|
try {
|
|
34
|
-
const
|
|
35
|
-
if (
|
|
34
|
+
const cognitoUser = await this.cognitoService.getUser(username);
|
|
35
|
+
if (cognitoUser) return Result.fail(cognitoUser.attributes["custom:id"] ? new CognitoAccountInOtherTenantError(data.email) : new CognitoAccountExistsError(data.email));
|
|
36
36
|
const createUserResult = await this.createUserUseCase.execute(userDataWithoutPassword);
|
|
37
37
|
if (createUserResult.isFail()) return Result.fail(createUserResult.error);
|
|
38
38
|
user = createUserResult.value;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api/features/CreateUser/CreateUserUseCase.js","sources":["../../../../src/api/features/CreateUser/CreateUserUseCase.ts"],"sourcesContent":["import { Result } from \"@webiny/feature/api\";\nimport { AdminUsersRepository } from \"@webiny/api-core/features/users/shared/abstractions.js\";\nimport {\n NotAuthorizedError,\n UserValidationError\n} from \"@webiny/api-core/features/users/shared/errors.js\";\nimport type { AdminUser } from \"@webiny/api-core/types/users.js\";\nimport { IdentityContext } from \"@webiny/api-core/features/security/IdentityContext/index.js\";\nimport { CreateUserUseCase as CoreCreateUser } from \"@webiny/api-core/features/users/CreateUser/index.js\";\nimport { ListUsersUseCase } from \"@webiny/api-core/features/users/ListUsers/index.js\";\n\nimport { CreateUserUseCase as UseCaseAbstraction } from \"./abstractions.js\";\nimport { Username } from \"~/api/domain/Username.js\";\nimport {
|
|
1
|
+
{"version":3,"file":"api/features/CreateUser/CreateUserUseCase.js","sources":["../../../../src/api/features/CreateUser/CreateUserUseCase.ts"],"sourcesContent":["import { Result } from \"@webiny/feature/api\";\nimport { AdminUsersRepository } from \"@webiny/api-core/features/users/shared/abstractions.js\";\nimport {\n NotAuthorizedError,\n UserValidationError\n} from \"@webiny/api-core/features/users/shared/errors.js\";\nimport type { AdminUser } from \"@webiny/api-core/types/users.js\";\nimport { IdentityContext } from \"@webiny/api-core/features/security/IdentityContext/index.js\";\nimport { CreateUserUseCase as CoreCreateUser } from \"@webiny/api-core/features/users/CreateUser/index.js\";\nimport { ListUsersUseCase } from \"@webiny/api-core/features/users/ListUsers/index.js\";\n\nimport { CreateUserUseCase as UseCaseAbstraction } from \"./abstractions.js\";\nimport { Username } from \"~/api/domain/Username.js\";\nimport {\n CognitoAccountExistsError,\n CognitoAccountInOtherTenantError,\n CognitoCreateUserError\n} from \"~/api/domain/errors.js\";\nimport { createAdminUserValidation } from \"./schema.js\";\nimport type { CreateAdminUserInput } from \"./abstractions.js\";\nimport { CognitoService } from \"~/api/features/CognitoService/index.js\";\n\nclass CreateUserUseCaseImpl implements UseCaseAbstraction.Interface {\n constructor(\n private identityContext: IdentityContext.Interface,\n private cognitoService: CognitoService.Interface,\n private createUserUseCase: CoreCreateUser.Interface,\n private listUsersUseCase: ListUsersUseCase.Interface,\n private repository: AdminUsersRepository.Interface\n ) {}\n\n async execute(\n input: CreateAdminUserInput\n ): Promise<Result<UseCaseAbstraction.Output, UseCaseAbstraction.Error>> {\n const permission = await this.identityContext.getPermission(\"adminUsers.user\");\n if (!permission) {\n return Result.fail(new NotAuthorizedError());\n }\n\n // Validate input (including password)\n const validation = createAdminUserValidation.safeParse(input);\n if (!validation.success) {\n return Result.fail(new UserValidationError(validation.error.issues[0].message));\n }\n\n const data = validation.data;\n const { password, ...userDataWithoutPassword } = data;\n\n const username = Username.fromUser(data);\n\n // Check if user exists in database (faster than Cognito check)\n const existingUserResult = await this.repository.get({ email: data.email });\n if (existingUserResult.isOk()) {\n return Result.fail(new CognitoAccountExistsError(data.email));\n }\n\n let user: AdminUser | undefined = undefined;\n\n try {\n // An account from another tenant is never added silently: the caller links it.\n const cognitoUser = await this.cognitoService.getUser(username);\n if (cognitoUser) {\n return Result.fail(\n cognitoUser.attributes[\"custom:id\"]\n ? new CognitoAccountInOtherTenantError(data.email)\n : new CognitoAccountExistsError(data.email)\n );\n }\n\n // Create user in api-core\n const createUserResult = await this.createUserUseCase.execute(userDataWithoutPassword);\n if (createUserResult.isFail()) {\n return Result.fail(createUserResult.error);\n }\n\n user = createUserResult.value;\n\n // Create user in Cognito\n await this.cognitoService.createUser({\n username,\n temporaryPassword: password,\n attributes: {\n givenName: data.firstName,\n familyName: data.lastName,\n preferredUsername: username,\n email: username,\n customId: user.id\n }\n });\n\n // Set email as verified\n await this.cognitoService.setEmailVerified(username);\n\n // Check if this is the first user in the system, and if so, set permanent password\n const usersResult = await this.listUsersUseCase.execute();\n if (usersResult.isFail()) {\n // Log error but don't fail the operation\n console.error(`Failed to list users: ${usersResult.error.message}`);\n } else {\n const users = usersResult.value;\n if (users.length <= 1) {\n // This is the first user, set permanent password\n await this.cognitoService.setPermanentPassword(username, password);\n }\n }\n\n return Result.ok(user);\n } catch (cognitoError) {\n if (user) {\n // Rollback: Delete user from api-core if Cognito creation failed\n try {\n await this.repository.delete(user);\n } catch (rollbackError) {\n console.error(\"Failed to rollback user creation:\", rollbackError);\n }\n }\n return Result.fail(new CognitoCreateUserError(cognitoError as Error));\n }\n }\n}\n\nexport const CreateUserUseCase = UseCaseAbstraction.createImplementation({\n implementation: CreateUserUseCaseImpl,\n dependencies: [\n IdentityContext,\n CognitoService,\n CoreCreateUser,\n ListUsersUseCase,\n AdminUsersRepository\n ]\n});\n"],"names":["CreateUserUseCaseImpl","identityContext","cognitoService","createUserUseCase","listUsersUseCase","repository","input","permission","Result","NotAuthorizedError","validation","createAdminUserValidation","UserValidationError","data","password","userDataWithoutPassword","username","Username","existingUserResult","CognitoAccountExistsError","user","cognitoUser","CognitoAccountInOtherTenantError","createUserResult","usersResult","console","users","cognitoError","rollbackError","CognitoCreateUserError","CreateUserUseCase","UseCaseAbstraction","IdentityContext","CognitoService","CoreCreateUser","ListUsersUseCase","AdminUsersRepository"],"mappings":";;;;;;;;;;;AAsBA,MAAMA;IACF,YACYC,eAA0C,EAC1CC,cAAwC,EACxCC,iBAA2C,EAC3CC,gBAA4C,EAC5CC,UAA0C,CACpD;aALUJ,eAAe,GAAfA;aACAC,cAAc,GAAdA;aACAC,iBAAiB,GAAjBA;aACAC,gBAAgB,GAAhBA;aACAC,UAAU,GAAVA;IACT;IAEH,MAAM,QACFC,KAA2B,EACyC;QACpE,MAAMC,aAAa,MAAM,IAAI,CAAC,eAAe,CAAC,aAAa,CAAC;QAC5D,IAAI,CAACA,YACD,OAAOC,OAAO,IAAI,CAAC,IAAIC;QAI3B,MAAMC,aAAaC,0BAA0B,SAAS,CAACL;QACvD,IAAI,CAACI,WAAW,OAAO,EACnB,OAAOF,OAAO,IAAI,CAAC,IAAII,oBAAoBF,WAAW,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC,OAAO;QAGjF,MAAMG,OAAOH,WAAW,IAAI;QAC5B,MAAM,EAAEI,QAAQ,EAAE,GAAGC,yBAAyB,GAAGF;QAEjD,MAAMG,WAAWC,SAAS,QAAQ,CAACJ;QAGnC,MAAMK,qBAAqB,MAAM,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC;YAAE,OAAOL,KAAK,KAAK;QAAC;QACzE,IAAIK,mBAAmB,IAAI,IACvB,OAAOV,OAAO,IAAI,CAAC,IAAIW,0BAA0BN,KAAK,KAAK;QAG/D,IAAIO;QAEJ,IAAI;YAEA,MAAMC,cAAc,MAAM,IAAI,CAAC,cAAc,CAAC,OAAO,CAACL;YACtD,IAAIK,aACA,OAAOb,OAAO,IAAI,CACda,YAAY,UAAU,CAAC,YAAY,GAC7B,IAAIC,iCAAiCT,KAAK,KAAK,IAC/C,IAAIM,0BAA0BN,KAAK,KAAK;YAKtD,MAAMU,mBAAmB,MAAM,IAAI,CAAC,iBAAiB,CAAC,OAAO,CAACR;YAC9D,IAAIQ,iBAAiB,MAAM,IACvB,OAAOf,OAAO,IAAI,CAACe,iBAAiB,KAAK;YAG7CH,OAAOG,iBAAiB,KAAK;YAG7B,MAAM,IAAI,CAAC,cAAc,CAAC,UAAU,CAAC;gBACjCP;gBACA,mBAAmBF;gBACnB,YAAY;oBACR,WAAWD,KAAK,SAAS;oBACzB,YAAYA,KAAK,QAAQ;oBACzB,mBAAmBG;oBACnB,OAAOA;oBACP,UAAUI,KAAK,EAAE;gBACrB;YACJ;YAGA,MAAM,IAAI,CAAC,cAAc,CAAC,gBAAgB,CAACJ;YAG3C,MAAMQ,cAAc,MAAM,IAAI,CAAC,gBAAgB,CAAC,OAAO;YACvD,IAAIA,YAAY,MAAM,IAElBC,QAAQ,KAAK,CAAC,CAAC,sBAAsB,EAAED,YAAY,KAAK,CAAC,OAAO,EAAE;iBAC/D;gBACH,MAAME,QAAQF,YAAY,KAAK;gBAC/B,IAAIE,MAAM,MAAM,IAAI,GAEhB,MAAM,IAAI,CAAC,cAAc,CAAC,oBAAoB,CAACV,UAAUF;YAEjE;YAEA,OAAON,OAAO,EAAE,CAACY;QACrB,EAAE,OAAOO,cAAc;YACnB,IAAIP,MAEA,IAAI;gBACA,MAAM,IAAI,CAAC,UAAU,CAAC,MAAM,CAACA;YACjC,EAAE,OAAOQ,eAAe;gBACpBH,QAAQ,KAAK,CAAC,qCAAqCG;YACvD;YAEJ,OAAOpB,OAAO,IAAI,CAAC,IAAIqB,uBAAuBF;QAClD;IACJ;AACJ;AAEO,MAAMG,sCAAoBC,2CAAAA,oBAAuC,CAAC;IACrE,gBAAgB/B;IAChB,cAAc;QACVgC;QACAC;QACAC;QACAC;QACAC;KACH;AACL"}
|
|
@@ -2,7 +2,7 @@ import type { Result } from "@webiny/feature/api";
|
|
|
2
2
|
import type { AdminUser } from "@webiny/api-core/types/users.js";
|
|
3
3
|
import type { NotAuthorizedError, EmailTakenError, UserValidationError } from "@webiny/api-core/features/users/shared/errors.js";
|
|
4
4
|
import type { RepositoryError } from "@webiny/api-core/features/users/shared/abstractions.js";
|
|
5
|
-
import type { CognitoAccountExistsError, CognitoCreateUserError } from "../../../api/domain/errors.js";
|
|
5
|
+
import type { CognitoAccountExistsError, CognitoAccountInOtherTenantError, CognitoCreateUserError } from "../../../api/domain/errors.js";
|
|
6
6
|
export interface CreateAdminUserInput {
|
|
7
7
|
id?: string;
|
|
8
8
|
displayName?: string;
|
|
@@ -20,6 +20,7 @@ export interface ICreateUserUseCaseErrors {
|
|
|
20
20
|
emailTaken: EmailTakenError;
|
|
21
21
|
repository: RepositoryError;
|
|
22
22
|
cognitoAccountExists: CognitoAccountExistsError;
|
|
23
|
+
cognitoAccountInOtherTenant: CognitoAccountInOtherTenantError;
|
|
23
24
|
cognitoCreateUser: CognitoCreateUserError;
|
|
24
25
|
}
|
|
25
26
|
type CreateAdminUserError = ICreateUserUseCaseErrors[keyof ICreateUserUseCaseErrors];
|
|
@@ -30,6 +31,7 @@ export declare const CreateUserUseCase: import("@webiny/di").Abstraction<ICreate
|
|
|
30
31
|
export declare namespace CreateUserUseCase {
|
|
31
32
|
type Interface = ICreateUserUseCase;
|
|
32
33
|
type Input = CreateAdminUserInput;
|
|
34
|
+
type Output = AdminUser;
|
|
33
35
|
type Error = CreateAdminUserError;
|
|
34
36
|
}
|
|
35
37
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api/features/CreateUser/abstractions.js","sources":["../../../../src/api/features/CreateUser/abstractions.ts"],"sourcesContent":["import { createAbstraction } from \"@webiny/feature/api\";\nimport type { Result } from \"@webiny/feature/api\";\nimport type { AdminUser } from \"@webiny/api-core/types/users.js\";\nimport type {\n NotAuthorizedError,\n EmailTakenError,\n UserValidationError\n} from \"@webiny/api-core/features/users/shared/errors.js\";\nimport type { RepositoryError } from \"@webiny/api-core/features/users/shared/abstractions.js\";\nimport type {
|
|
1
|
+
{"version":3,"file":"api/features/CreateUser/abstractions.js","sources":["../../../../src/api/features/CreateUser/abstractions.ts"],"sourcesContent":["import { createAbstraction } from \"@webiny/feature/api\";\nimport type { Result } from \"@webiny/feature/api\";\nimport type { AdminUser } from \"@webiny/api-core/types/users.js\";\nimport type {\n NotAuthorizedError,\n EmailTakenError,\n UserValidationError\n} from \"@webiny/api-core/features/users/shared/errors.js\";\nimport type { RepositoryError } from \"@webiny/api-core/features/users/shared/abstractions.js\";\nimport type {\n CognitoAccountExistsError,\n CognitoAccountInOtherTenantError,\n CognitoCreateUserError\n} from \"~/api/domain/errors.js\";\n\nexport interface CreateAdminUserInput {\n id?: string;\n displayName?: string;\n email: string;\n firstName?: string;\n lastName?: string;\n password: string;\n avatar?: Record<string, any> | null;\n roles?: string[];\n teams?: string[];\n}\n\nexport interface ICreateUserUseCaseErrors {\n authorization: NotAuthorizedError;\n validation: UserValidationError;\n emailTaken: EmailTakenError;\n repository: RepositoryError;\n cognitoAccountExists: CognitoAccountExistsError;\n cognitoAccountInOtherTenant: CognitoAccountInOtherTenantError;\n cognitoCreateUser: CognitoCreateUserError;\n}\n\ntype CreateAdminUserError = ICreateUserUseCaseErrors[keyof ICreateUserUseCaseErrors];\n\nexport interface ICreateUserUseCase {\n execute(input: CreateAdminUserInput): Promise<Result<AdminUser, CreateAdminUserError>>;\n}\n\nexport const CreateUserUseCase = createAbstraction<ICreateUserUseCase>(\"CreateUserUseCase\");\n\nexport namespace CreateUserUseCase {\n export type Interface = ICreateUserUseCase;\n export type Input = CreateAdminUserInput;\n export type Output = AdminUser;\n export type Error = CreateAdminUserError;\n}\n"],"names":["CreateUserUseCase","createAbstraction"],"mappings":";AA2CO,MAAMA,oBAAoBC,kBAAsC"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Result } from "@webiny/feature/api";
|
|
2
2
|
import { DeleteUserUseCase as CoreDeleteUser } from "@webiny/api-core/features/users/DeleteUser/index.js";
|
|
3
3
|
import { GetUserUseCase } from "@webiny/api-core/features/users/GetUser/index.js";
|
|
4
|
+
import { FindUserRecordsUseCase } from "@webiny/api-core/features/users/FindUserRecords/index.js";
|
|
4
5
|
import { IdentityContext } from "@webiny/api-core/features/security/IdentityContext/index.js";
|
|
5
6
|
import { DeleteUserUseCase as UseCaseAbstraction } from "./abstractions.js";
|
|
6
7
|
import { CognitoService } from "../CognitoService/abstractions.js";
|
|
@@ -9,7 +10,8 @@ declare class DeleteUserUseCaseImpl implements UseCaseAbstraction.Interface {
|
|
|
9
10
|
private cognitoService;
|
|
10
11
|
private deleteUserUseCase;
|
|
11
12
|
private getUserUseCase;
|
|
12
|
-
|
|
13
|
+
private findUserRecords;
|
|
14
|
+
constructor(identityContext: IdentityContext.Interface, cognitoService: CognitoService.Interface, deleteUserUseCase: CoreDeleteUser.Interface, getUserUseCase: GetUserUseCase.Interface, findUserRecords: FindUserRecordsUseCase.Interface);
|
|
13
15
|
execute(id: string): Promise<Result<void, UseCaseAbstraction.Error>>;
|
|
14
16
|
}
|
|
15
17
|
export declare const DeleteUserUseCase: import("@webiny/di").Implementation<typeof DeleteUserUseCaseImpl>;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Result, createImplementation } from "@webiny/feature/api";
|
|
2
2
|
import { DeleteUserUseCase } from "@webiny/api-core/features/users/DeleteUser/index.js";
|
|
3
3
|
import { GetUserUseCase } from "@webiny/api-core/features/users/GetUser/index.js";
|
|
4
|
+
import { FindUserRecordsUseCase } from "@webiny/api-core/features/users/FindUserRecords/index.js";
|
|
4
5
|
import { NotAuthorizedError } from "@webiny/api-core/features/users/shared/errors.js";
|
|
5
6
|
import { IdentityContext } from "@webiny/api-core/features/security/IdentityContext/index.js";
|
|
6
7
|
import { DeleteUserUseCase as external_abstractions_js_DeleteUserUseCase } from "./abstractions.js";
|
|
@@ -8,11 +9,12 @@ import { Username } from "../../domain/Username.js";
|
|
|
8
9
|
import { CognitoDeleteUserError } from "../../domain/errors.js";
|
|
9
10
|
import { CognitoService } from "../CognitoService/abstractions.js";
|
|
10
11
|
class DeleteUserUseCaseImpl {
|
|
11
|
-
constructor(identityContext, cognitoService, deleteUserUseCase, getUserUseCase){
|
|
12
|
+
constructor(identityContext, cognitoService, deleteUserUseCase, getUserUseCase, findUserRecords){
|
|
12
13
|
this.identityContext = identityContext;
|
|
13
14
|
this.cognitoService = cognitoService;
|
|
14
15
|
this.deleteUserUseCase = deleteUserUseCase;
|
|
15
16
|
this.getUserUseCase = getUserUseCase;
|
|
17
|
+
this.findUserRecords = findUserRecords;
|
|
16
18
|
}
|
|
17
19
|
async execute(id) {
|
|
18
20
|
const permission = await this.identityContext.getPermission("adminUsers.user");
|
|
@@ -22,8 +24,14 @@ class DeleteUserUseCaseImpl {
|
|
|
22
24
|
});
|
|
23
25
|
if (getUserResult.isFail()) return Result.fail(getUserResult.error);
|
|
24
26
|
const user = getUserResult.value;
|
|
27
|
+
const recordsResult = await this.findUserRecords.execute(id, {
|
|
28
|
+
exhaustive: true
|
|
29
|
+
});
|
|
30
|
+
if (recordsResult.isFail()) return Result.fail(recordsResult.error);
|
|
31
|
+
const inOtherTenants = recordsResult.value.some((record)=>record.tenant !== user.tenant);
|
|
25
32
|
const deleteUserResult = await this.deleteUserUseCase.execute(id);
|
|
26
33
|
if (deleteUserResult.isFail()) return Result.fail(deleteUserResult.error);
|
|
34
|
+
if (inOtherTenants) return Result.ok();
|
|
27
35
|
try {
|
|
28
36
|
await this.cognitoService.deleteUser(Username.fromUser(user));
|
|
29
37
|
return Result.ok();
|
|
@@ -39,7 +47,8 @@ const DeleteUserUseCase_DeleteUserUseCase = createImplementation({
|
|
|
39
47
|
IdentityContext,
|
|
40
48
|
CognitoService,
|
|
41
49
|
DeleteUserUseCase,
|
|
42
|
-
GetUserUseCase
|
|
50
|
+
GetUserUseCase,
|
|
51
|
+
FindUserRecordsUseCase
|
|
43
52
|
]
|
|
44
53
|
});
|
|
45
54
|
export { DeleteUserUseCase_DeleteUserUseCase as DeleteUserUseCase };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api/features/DeleteUser/DeleteUserUseCase.js","sources":["../../../../src/api/features/DeleteUser/DeleteUserUseCase.ts"],"sourcesContent":["import { createImplementation } from \"@webiny/feature/api\";\nimport { Result } from \"@webiny/feature/api\";\nimport { DeleteUserUseCase as CoreDeleteUser } from \"@webiny/api-core/features/users/DeleteUser/index.js\";\nimport { GetUserUseCase } from \"@webiny/api-core/features/users/GetUser/index.js\";\nimport { NotAuthorizedError } from \"@webiny/api-core/features/users/shared/errors.js\";\nimport { IdentityContext } from \"@webiny/api-core/features/security/IdentityContext/index.js\";\nimport { DeleteUserUseCase as UseCaseAbstraction } from \"./abstractions.js\";\nimport { Username } from \"~/api/domain/Username.js\";\nimport { CognitoDeleteUserError } from \"~/api/domain/errors.js\";\nimport { CognitoService } from \"../CognitoService/abstractions.js\";\n\nclass DeleteUserUseCaseImpl implements UseCaseAbstraction.Interface {\n constructor(\n private identityContext: IdentityContext.Interface,\n private cognitoService: CognitoService.Interface,\n private deleteUserUseCase: CoreDeleteUser.Interface,\n private getUserUseCase: GetUserUseCase.Interface\n ) {}\n\n async execute(id: string): Promise<Result<void, UseCaseAbstraction.Error>> {\n const permission = await this.identityContext.getPermission(\"adminUsers.user\");\n if (!permission) {\n return Result.fail(new NotAuthorizedError());\n }\n\n // Get user to have email for Cognito deletion\n const getUserResult = await this.getUserUseCase.execute({ id });\n if (getUserResult.isFail()) {\n return Result.fail(getUserResult.error);\n }\n\n const user = getUserResult.value;\n\n // Delete user from the database\n const deleteUserResult = await this.deleteUserUseCase.execute(id);\n if (deleteUserResult.isFail()) {\n return Result.fail(deleteUserResult.error);\n }\n\n // Delete user from Cognito\n try {\n await this.cognitoService.deleteUser(Username.fromUser(user));\n\n return Result.ok();\n } catch (cognitoError) {\n return Result.fail(new CognitoDeleteUserError(cognitoError as Error));\n }\n }\n}\n\nexport const DeleteUserUseCase = createImplementation({\n abstraction: UseCaseAbstraction,\n implementation: DeleteUserUseCaseImpl,\n dependencies: [IdentityContext
|
|
1
|
+
{"version":3,"file":"api/features/DeleteUser/DeleteUserUseCase.js","sources":["../../../../src/api/features/DeleteUser/DeleteUserUseCase.ts"],"sourcesContent":["import { createImplementation } from \"@webiny/feature/api\";\nimport { Result } from \"@webiny/feature/api\";\nimport { DeleteUserUseCase as CoreDeleteUser } from \"@webiny/api-core/features/users/DeleteUser/index.js\";\nimport { GetUserUseCase } from \"@webiny/api-core/features/users/GetUser/index.js\";\nimport { FindUserRecordsUseCase } from \"@webiny/api-core/features/users/FindUserRecords/index.js\";\nimport { NotAuthorizedError } from \"@webiny/api-core/features/users/shared/errors.js\";\nimport { IdentityContext } from \"@webiny/api-core/features/security/IdentityContext/index.js\";\nimport { DeleteUserUseCase as UseCaseAbstraction } from \"./abstractions.js\";\nimport { Username } from \"~/api/domain/Username.js\";\nimport { CognitoDeleteUserError } from \"~/api/domain/errors.js\";\nimport { CognitoService } from \"../CognitoService/abstractions.js\";\n\nclass DeleteUserUseCaseImpl implements UseCaseAbstraction.Interface {\n constructor(\n private identityContext: IdentityContext.Interface,\n private cognitoService: CognitoService.Interface,\n private deleteUserUseCase: CoreDeleteUser.Interface,\n private getUserUseCase: GetUserUseCase.Interface,\n private findUserRecords: FindUserRecordsUseCase.Interface\n ) {}\n\n async execute(id: string): Promise<Result<void, UseCaseAbstraction.Error>> {\n const permission = await this.identityContext.getPermission(\"adminUsers.user\");\n if (!permission) {\n return Result.fail(new NotAuthorizedError());\n }\n\n // Get user to have email for Cognito deletion\n const getUserResult = await this.getUserUseCase.execute({ id });\n if (getUserResult.isFail()) {\n return Result.fail(getUserResult.error);\n }\n\n const user = getUserResult.value;\n\n // The sign-in account is shared by all of the user's tenants. Look them up before\n // deleting anything, so a failed lookup leaves everything as it was.\n const recordsResult = await this.findUserRecords.execute(id, { exhaustive: true });\n if (recordsResult.isFail()) {\n return Result.fail(recordsResult.error);\n }\n\n const inOtherTenants = recordsResult.value.some(record => record.tenant !== user.tenant);\n\n // Delete user from the database\n const deleteUserResult = await this.deleteUserUseCase.execute(id);\n if (deleteUserResult.isFail()) {\n return Result.fail(deleteUserResult.error);\n }\n\n if (inOtherTenants) {\n return Result.ok();\n }\n\n // Delete user from Cognito\n try {\n await this.cognitoService.deleteUser(Username.fromUser(user));\n\n return Result.ok();\n } catch (cognitoError) {\n return Result.fail(new CognitoDeleteUserError(cognitoError as Error));\n }\n }\n}\n\nexport const DeleteUserUseCase = createImplementation({\n abstraction: UseCaseAbstraction,\n implementation: DeleteUserUseCaseImpl,\n dependencies: [\n IdentityContext,\n CognitoService,\n CoreDeleteUser,\n GetUserUseCase,\n FindUserRecordsUseCase\n ]\n});\n"],"names":["DeleteUserUseCaseImpl","identityContext","cognitoService","deleteUserUseCase","getUserUseCase","findUserRecords","id","permission","Result","NotAuthorizedError","getUserResult","user","recordsResult","inOtherTenants","record","deleteUserResult","Username","cognitoError","CognitoDeleteUserError","DeleteUserUseCase","createImplementation","UseCaseAbstraction","IdentityContext","CognitoService","CoreDeleteUser","GetUserUseCase","FindUserRecordsUseCase"],"mappings":";;;;;;;;;;AAYA,MAAMA;IACF,YACYC,eAA0C,EAC1CC,cAAwC,EACxCC,iBAA2C,EAC3CC,cAAwC,EACxCC,eAAiD,CAC3D;aALUJ,eAAe,GAAfA;aACAC,cAAc,GAAdA;aACAC,iBAAiB,GAAjBA;aACAC,cAAc,GAAdA;aACAC,eAAe,GAAfA;IACT;IAEH,MAAM,QAAQC,EAAU,EAAmD;QACvE,MAAMC,aAAa,MAAM,IAAI,CAAC,eAAe,CAAC,aAAa,CAAC;QAC5D,IAAI,CAACA,YACD,OAAOC,OAAO,IAAI,CAAC,IAAIC;QAI3B,MAAMC,gBAAgB,MAAM,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC;YAAEJ;QAAG;QAC7D,IAAII,cAAc,MAAM,IACpB,OAAOF,OAAO,IAAI,CAACE,cAAc,KAAK;QAG1C,MAAMC,OAAOD,cAAc,KAAK;QAIhC,MAAME,gBAAgB,MAAM,IAAI,CAAC,eAAe,CAAC,OAAO,CAACN,IAAI;YAAE,YAAY;QAAK;QAChF,IAAIM,cAAc,MAAM,IACpB,OAAOJ,OAAO,IAAI,CAACI,cAAc,KAAK;QAG1C,MAAMC,iBAAiBD,cAAc,KAAK,CAAC,IAAI,CAACE,CAAAA,SAAUA,OAAO,MAAM,KAAKH,KAAK,MAAM;QAGvF,MAAMI,mBAAmB,MAAM,IAAI,CAAC,iBAAiB,CAAC,OAAO,CAACT;QAC9D,IAAIS,iBAAiB,MAAM,IACvB,OAAOP,OAAO,IAAI,CAACO,iBAAiB,KAAK;QAG7C,IAAIF,gBACA,OAAOL,OAAO,EAAE;QAIpB,IAAI;YACA,MAAM,IAAI,CAAC,cAAc,CAAC,UAAU,CAACQ,SAAS,QAAQ,CAACL;YAEvD,OAAOH,OAAO,EAAE;QACpB,EAAE,OAAOS,cAAc;YACnB,OAAOT,OAAO,IAAI,CAAC,IAAIU,uBAAuBD;QAClD;IACJ;AACJ;AAEO,MAAME,sCAAoBC,qBAAqB;IAClD,aAAaC;IACb,gBAAgBrB;IAChB,cAAc;QACVsB;QACAC;QACAC;QACAC;QACAC;KACH;AACL"}
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import type { Result } from "@webiny/feature/api";
|
|
2
2
|
import type { NotAuthorizedError } from "@webiny/api-core/features/users/shared/errors.js";
|
|
3
3
|
import type { DeleteUserUseCase as CoreDeleteUser } from "@webiny/api-core/features/users/DeleteUser/index.js";
|
|
4
|
+
import type { FindUserRecordsUseCase } from "@webiny/api-core/features/users/FindUserRecords/index.js";
|
|
4
5
|
import type { CognitoDeleteUserError } from "../../../api/domain/errors.js";
|
|
5
6
|
export interface IDeleteUserUseCaseErrors {
|
|
6
7
|
authorization: NotAuthorizedError;
|
|
7
8
|
deleteUser: CoreDeleteUser.Error;
|
|
8
9
|
cognitoDeleteUser: CognitoDeleteUserError;
|
|
10
|
+
findUserRecords: FindUserRecordsUseCase.Error;
|
|
9
11
|
}
|
|
10
12
|
type IDeleteAdminUserError = IDeleteUserUseCaseErrors[keyof IDeleteUserUseCaseErrors];
|
|
11
13
|
export interface IDeleteUserUseCase {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api/features/DeleteUser/abstractions.js","sources":["../../../../src/api/features/DeleteUser/abstractions.ts"],"sourcesContent":["import { createAbstraction } from \"@webiny/feature/api\";\nimport type { Result } from \"@webiny/feature/api\";\nimport type { NotAuthorizedError } from \"@webiny/api-core/features/users/shared/errors.js\";\nimport type { DeleteUserUseCase as CoreDeleteUser } from \"@webiny/api-core/features/users/DeleteUser/index.js\";\nimport type { CognitoDeleteUserError } from \"~/api/domain/errors.js\";\n\nexport interface IDeleteUserUseCaseErrors {\n authorization: NotAuthorizedError;\n deleteUser: CoreDeleteUser.Error;\n cognitoDeleteUser: CognitoDeleteUserError;\n}\n\ntype IDeleteAdminUserError = IDeleteUserUseCaseErrors[keyof IDeleteUserUseCaseErrors];\n\nexport interface IDeleteUserUseCase {\n execute(id: string): Promise<Result<void, IDeleteAdminUserError>>;\n}\n\nexport const DeleteUserUseCase = createAbstraction<IDeleteUserUseCase>(\"DeleteUserUseCase\");\n\nexport namespace DeleteUserUseCase {\n export type Interface = IDeleteUserUseCase;\n export type Error = IDeleteAdminUserError;\n}\n"],"names":["DeleteUserUseCase","createAbstraction"],"mappings":";
|
|
1
|
+
{"version":3,"file":"api/features/DeleteUser/abstractions.js","sources":["../../../../src/api/features/DeleteUser/abstractions.ts"],"sourcesContent":["import { createAbstraction } from \"@webiny/feature/api\";\nimport type { Result } from \"@webiny/feature/api\";\nimport type { NotAuthorizedError } from \"@webiny/api-core/features/users/shared/errors.js\";\nimport type { DeleteUserUseCase as CoreDeleteUser } from \"@webiny/api-core/features/users/DeleteUser/index.js\";\nimport type { FindUserRecordsUseCase } from \"@webiny/api-core/features/users/FindUserRecords/index.js\";\nimport type { CognitoDeleteUserError } from \"~/api/domain/errors.js\";\n\nexport interface IDeleteUserUseCaseErrors {\n authorization: NotAuthorizedError;\n deleteUser: CoreDeleteUser.Error;\n cognitoDeleteUser: CognitoDeleteUserError;\n findUserRecords: FindUserRecordsUseCase.Error;\n}\n\ntype IDeleteAdminUserError = IDeleteUserUseCaseErrors[keyof IDeleteUserUseCaseErrors];\n\nexport interface IDeleteUserUseCase {\n execute(id: string): Promise<Result<void, IDeleteAdminUserError>>;\n}\n\nexport const DeleteUserUseCase = createAbstraction<IDeleteUserUseCase>(\"DeleteUserUseCase\");\n\nexport namespace DeleteUserUseCase {\n export type Interface = IDeleteUserUseCase;\n export type Error = IDeleteAdminUserError;\n}\n"],"names":["DeleteUserUseCase","createAbstraction"],"mappings":";AAoBO,MAAMA,oBAAoBC,kBAAsC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Result } from "@webiny/feature/api";
|
|
2
|
+
import { AdminUsersRepository } from "@webiny/api-core/features/users/shared/abstractions.js";
|
|
3
|
+
import { IdentityContext } from "@webiny/api-core/features/security/IdentityContext/index.js";
|
|
4
|
+
import { TenantContext } from "@webiny/api-core/features/tenancy/TenantContext/index.js";
|
|
5
|
+
import { CreateUserUseCase as CoreCreateUser } from "@webiny/api-core/features/users/CreateUser/index.js";
|
|
6
|
+
import { FindUserRecordsUseCase } from "@webiny/api-core/features/users/FindUserRecords/index.js";
|
|
7
|
+
import { LinkUserUseCase as UseCaseAbstraction } from "./abstractions.js";
|
|
8
|
+
import { CognitoService } from "../../../api/features/CognitoService/index.js";
|
|
9
|
+
declare class LinkUserUseCaseImpl implements UseCaseAbstraction.Interface {
|
|
10
|
+
private identityContext;
|
|
11
|
+
private tenantContext;
|
|
12
|
+
private cognitoService;
|
|
13
|
+
private createUserUseCase;
|
|
14
|
+
private findUserRecords;
|
|
15
|
+
private repository;
|
|
16
|
+
constructor(identityContext: IdentityContext.Interface, tenantContext: TenantContext.Interface, cognitoService: CognitoService.Interface, createUserUseCase: CoreCreateUser.Interface, findUserRecords: FindUserRecordsUseCase.Interface, repository: AdminUsersRepository.Interface);
|
|
17
|
+
execute(input: UseCaseAbstraction.Input): Promise<Result<UseCaseAbstraction.Output, UseCaseAbstraction.Error>>;
|
|
18
|
+
}
|
|
19
|
+
export declare const LinkUserUseCase: typeof LinkUserUseCaseImpl & {
|
|
20
|
+
__abstraction: import("@webiny/di").Abstraction<import("./abstractions.js").ILinkUserUseCase>;
|
|
21
|
+
};
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { Result } from "@webiny/feature/api";
|
|
3
|
+
import { AdminUsersRepository } from "@webiny/api-core/features/users/shared/abstractions.js";
|
|
4
|
+
import { NotAuthorizedError, UserValidationError } from "@webiny/api-core/features/users/shared/errors.js";
|
|
5
|
+
import { IdentityContext } from "@webiny/api-core/features/security/IdentityContext/index.js";
|
|
6
|
+
import { TenantContext } from "@webiny/api-core/features/tenancy/TenantContext/index.js";
|
|
7
|
+
import { CreateUserUseCase } from "@webiny/api-core/features/users/CreateUser/index.js";
|
|
8
|
+
import { FindUserRecordsUseCase } from "@webiny/api-core/features/users/FindUserRecords/index.js";
|
|
9
|
+
import { LinkUserUseCase } from "./abstractions.js";
|
|
10
|
+
import { Username } from "../../domain/Username.js";
|
|
11
|
+
import { CognitoAccountExistsError, CognitoAccountNotFoundError, CognitoGetUserError } from "../../domain/errors.js";
|
|
12
|
+
import { CognitoService } from "../CognitoService/index.js";
|
|
13
|
+
const linkAdminUserValidation = z.object({
|
|
14
|
+
email: z.string().email(),
|
|
15
|
+
roles: z.array(z.string()).optional().default([]),
|
|
16
|
+
teams: z.array(z.string()).optional().default([])
|
|
17
|
+
});
|
|
18
|
+
class LinkUserUseCaseImpl {
|
|
19
|
+
constructor(identityContext, tenantContext, cognitoService, createUserUseCase, findUserRecords, repository){
|
|
20
|
+
this.identityContext = identityContext;
|
|
21
|
+
this.tenantContext = tenantContext;
|
|
22
|
+
this.cognitoService = cognitoService;
|
|
23
|
+
this.createUserUseCase = createUserUseCase;
|
|
24
|
+
this.findUserRecords = findUserRecords;
|
|
25
|
+
this.repository = repository;
|
|
26
|
+
}
|
|
27
|
+
async execute(input) {
|
|
28
|
+
const permission = await this.identityContext.getPermission("adminUsers.user");
|
|
29
|
+
if (!permission) return Result.fail(new NotAuthorizedError());
|
|
30
|
+
const validation = linkAdminUserValidation.safeParse(input);
|
|
31
|
+
if (!validation.success) return Result.fail(new UserValidationError(validation.error.issues[0].message));
|
|
32
|
+
const { email, roles, teams } = validation.data;
|
|
33
|
+
const existingUserResult = await this.repository.get({
|
|
34
|
+
email
|
|
35
|
+
});
|
|
36
|
+
if (existingUserResult.isOk()) return Result.fail(new CognitoAccountExistsError(email));
|
|
37
|
+
let cognitoUser;
|
|
38
|
+
try {
|
|
39
|
+
cognitoUser = await this.cognitoService.getUser(Username.fromUser({
|
|
40
|
+
email
|
|
41
|
+
}));
|
|
42
|
+
} catch (error) {
|
|
43
|
+
return Result.fail(new CognitoGetUserError(error));
|
|
44
|
+
}
|
|
45
|
+
if (!cognitoUser) return Result.fail(new CognitoAccountNotFoundError(email));
|
|
46
|
+
const id = cognitoUser.attributes["custom:id"];
|
|
47
|
+
if (!id) return Result.fail(new CognitoAccountExistsError(email));
|
|
48
|
+
const recordsResult = await this.findUserRecords.execute(id, {
|
|
49
|
+
exhaustive: true
|
|
50
|
+
});
|
|
51
|
+
if (recordsResult.isFail()) return Result.fail(recordsResult.error);
|
|
52
|
+
const records = recordsResult.value;
|
|
53
|
+
const currentTenant = this.tenantContext.getTenant().id;
|
|
54
|
+
if (records.some((record)=>record.tenant === currentTenant)) return Result.fail(new CognitoAccountExistsError(email));
|
|
55
|
+
const [existing] = records;
|
|
56
|
+
const firstName = existing?.firstName ?? cognitoUser.attributes["given_name"];
|
|
57
|
+
const lastName = existing?.lastName ?? cognitoUser.attributes["family_name"];
|
|
58
|
+
return this.createUserUseCase.execute({
|
|
59
|
+
id,
|
|
60
|
+
email: existing?.email ?? email,
|
|
61
|
+
firstName,
|
|
62
|
+
lastName,
|
|
63
|
+
displayName: existing?.displayName,
|
|
64
|
+
avatar: existing?.avatar,
|
|
65
|
+
roles,
|
|
66
|
+
teams
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const LinkUserUseCase_LinkUserUseCase = LinkUserUseCase.createImplementation({
|
|
71
|
+
implementation: LinkUserUseCaseImpl,
|
|
72
|
+
dependencies: [
|
|
73
|
+
IdentityContext,
|
|
74
|
+
TenantContext,
|
|
75
|
+
CognitoService,
|
|
76
|
+
CreateUserUseCase,
|
|
77
|
+
FindUserRecordsUseCase,
|
|
78
|
+
AdminUsersRepository
|
|
79
|
+
]
|
|
80
|
+
});
|
|
81
|
+
export { LinkUserUseCase_LinkUserUseCase as LinkUserUseCase };
|
|
82
|
+
|
|
83
|
+
//# sourceMappingURL=LinkUserUseCase.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api/features/LinkUser/LinkUserUseCase.js","sources":["../../../../src/api/features/LinkUser/LinkUserUseCase.ts"],"sourcesContent":["import { z } from \"zod\";\nimport { Result } from \"@webiny/feature/api\";\nimport { AdminUsersRepository } from \"@webiny/api-core/features/users/shared/abstractions.js\";\nimport {\n NotAuthorizedError,\n UserValidationError\n} from \"@webiny/api-core/features/users/shared/errors.js\";\nimport { IdentityContext } from \"@webiny/api-core/features/security/IdentityContext/index.js\";\nimport { TenantContext } from \"@webiny/api-core/features/tenancy/TenantContext/index.js\";\nimport { CreateUserUseCase as CoreCreateUser } from \"@webiny/api-core/features/users/CreateUser/index.js\";\nimport { FindUserRecordsUseCase } from \"@webiny/api-core/features/users/FindUserRecords/index.js\";\nimport { LinkUserUseCase as UseCaseAbstraction } from \"./abstractions.js\";\nimport { Username } from \"~/api/domain/Username.js\";\nimport {\n CognitoAccountExistsError,\n CognitoAccountNotFoundError,\n CognitoGetUserError\n} from \"~/api/domain/errors.js\";\nimport { CognitoService } from \"~/api/features/CognitoService/index.js\";\n\nconst linkAdminUserValidation = z.object({\n email: z.string().email(),\n roles: z.array(z.string()).optional().default([]),\n teams: z.array(z.string()).optional().default([])\n});\n\nclass LinkUserUseCaseImpl implements UseCaseAbstraction.Interface {\n constructor(\n private identityContext: IdentityContext.Interface,\n private tenantContext: TenantContext.Interface,\n private cognitoService: CognitoService.Interface,\n private createUserUseCase: CoreCreateUser.Interface,\n private findUserRecords: FindUserRecordsUseCase.Interface,\n private repository: AdminUsersRepository.Interface\n ) {}\n\n async execute(\n input: UseCaseAbstraction.Input\n ): Promise<Result<UseCaseAbstraction.Output, UseCaseAbstraction.Error>> {\n const permission = await this.identityContext.getPermission(\"adminUsers.user\");\n if (!permission) {\n return Result.fail(new NotAuthorizedError());\n }\n\n const validation = linkAdminUserValidation.safeParse(input);\n if (!validation.success) {\n return Result.fail(new UserValidationError(validation.error.issues[0].message));\n }\n\n const { email, roles, teams } = validation.data;\n\n const existingUserResult = await this.repository.get({ email });\n if (existingUserResult.isOk()) {\n return Result.fail(new CognitoAccountExistsError(email));\n }\n\n let cognitoUser: CognitoService.User | null;\n try {\n cognitoUser = await this.cognitoService.getUser(Username.fromUser({ email }));\n } catch (error) {\n return Result.fail(new CognitoGetUserError(error as Error));\n }\n\n if (!cognitoUser) {\n return Result.fail(new CognitoAccountNotFoundError(email));\n }\n\n const id = cognitoUser.attributes[\"custom:id\"];\n if (!id) {\n // Not an account this app created, so there's no user ID to share.\n return Result.fail(new CognitoAccountExistsError(email));\n }\n\n // Exhaustive, so records from before the user→tenant index get indexed now.\n const recordsResult = await this.findUserRecords.execute(id, { exhaustive: true });\n if (recordsResult.isFail()) {\n return Result.fail(recordsResult.error);\n }\n\n const records = recordsResult.value;\n const currentTenant = this.tenantContext.getTenant().id;\n if (records.some(record => record.tenant === currentTenant)) {\n return Result.fail(new CognitoAccountExistsError(email));\n }\n\n // Name, email and avatar are global per person, so they come from the person's other\n // records, or from the account.\n const [existing] = records;\n const firstName = existing?.firstName ?? cognitoUser.attributes[\"given_name\"];\n const lastName = existing?.lastName ?? cognitoUser.attributes[\"family_name\"];\n\n return this.createUserUseCase.execute({\n id,\n email: existing?.email ?? email,\n firstName,\n lastName,\n displayName: existing?.displayName,\n avatar: existing?.avatar,\n roles,\n teams\n });\n }\n}\n\nexport const LinkUserUseCase = UseCaseAbstraction.createImplementation({\n implementation: LinkUserUseCaseImpl,\n dependencies: [\n IdentityContext,\n TenantContext,\n CognitoService,\n CoreCreateUser,\n FindUserRecordsUseCase,\n AdminUsersRepository\n ]\n});\n"],"names":["linkAdminUserValidation","z","LinkUserUseCaseImpl","identityContext","tenantContext","cognitoService","createUserUseCase","findUserRecords","repository","input","permission","Result","NotAuthorizedError","validation","UserValidationError","email","roles","teams","existingUserResult","CognitoAccountExistsError","cognitoUser","Username","error","CognitoGetUserError","CognitoAccountNotFoundError","id","recordsResult","records","currentTenant","record","existing","firstName","lastName","LinkUserUseCase","UseCaseAbstraction","IdentityContext","TenantContext","CognitoService","CoreCreateUser","FindUserRecordsUseCase","AdminUsersRepository"],"mappings":";;;;;;;;;;;;AAoBA,MAAMA,0BAA0BC,EAAE,MAAM,CAAC;IACrC,OAAOA,EAAE,MAAM,GAAG,KAAK;IACvB,OAAOA,EAAE,KAAK,CAACA,EAAE,MAAM,IAAI,QAAQ,GAAG,OAAO,CAAC,EAAE;IAChD,OAAOA,EAAE,KAAK,CAACA,EAAE,MAAM,IAAI,QAAQ,GAAG,OAAO,CAAC,EAAE;AACpD;AAEA,MAAMC;IACF,YACYC,eAA0C,EAC1CC,aAAsC,EACtCC,cAAwC,EACxCC,iBAA2C,EAC3CC,eAAiD,EACjDC,UAA0C,CACpD;aANUL,eAAe,GAAfA;aACAC,aAAa,GAAbA;aACAC,cAAc,GAAdA;aACAC,iBAAiB,GAAjBA;aACAC,eAAe,GAAfA;aACAC,UAAU,GAAVA;IACT;IAEH,MAAM,QACFC,KAA+B,EACqC;QACpE,MAAMC,aAAa,MAAM,IAAI,CAAC,eAAe,CAAC,aAAa,CAAC;QAC5D,IAAI,CAACA,YACD,OAAOC,OAAO,IAAI,CAAC,IAAIC;QAG3B,MAAMC,aAAab,wBAAwB,SAAS,CAACS;QACrD,IAAI,CAACI,WAAW,OAAO,EACnB,OAAOF,OAAO,IAAI,CAAC,IAAIG,oBAAoBD,WAAW,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC,OAAO;QAGjF,MAAM,EAAEE,KAAK,EAAEC,KAAK,EAAEC,KAAK,EAAE,GAAGJ,WAAW,IAAI;QAE/C,MAAMK,qBAAqB,MAAM,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC;YAAEH;QAAM;QAC7D,IAAIG,mBAAmB,IAAI,IACvB,OAAOP,OAAO,IAAI,CAAC,IAAIQ,0BAA0BJ;QAGrD,IAAIK;QACJ,IAAI;YACAA,cAAc,MAAM,IAAI,CAAC,cAAc,CAAC,OAAO,CAACC,SAAS,QAAQ,CAAC;gBAAEN;YAAM;QAC9E,EAAE,OAAOO,OAAO;YACZ,OAAOX,OAAO,IAAI,CAAC,IAAIY,oBAAoBD;QAC/C;QAEA,IAAI,CAACF,aACD,OAAOT,OAAO,IAAI,CAAC,IAAIa,4BAA4BT;QAGvD,MAAMU,KAAKL,YAAY,UAAU,CAAC,YAAY;QAC9C,IAAI,CAACK,IAED,OAAOd,OAAO,IAAI,CAAC,IAAIQ,0BAA0BJ;QAIrD,MAAMW,gBAAgB,MAAM,IAAI,CAAC,eAAe,CAAC,OAAO,CAACD,IAAI;YAAE,YAAY;QAAK;QAChF,IAAIC,cAAc,MAAM,IACpB,OAAOf,OAAO,IAAI,CAACe,cAAc,KAAK;QAG1C,MAAMC,UAAUD,cAAc,KAAK;QACnC,MAAME,gBAAgB,IAAI,CAAC,aAAa,CAAC,SAAS,GAAG,EAAE;QACvD,IAAID,QAAQ,IAAI,CAACE,CAAAA,SAAUA,OAAO,MAAM,KAAKD,gBACzC,OAAOjB,OAAO,IAAI,CAAC,IAAIQ,0BAA0BJ;QAKrD,MAAM,CAACe,SAAS,GAAGH;QACnB,MAAMI,YAAYD,UAAU,aAAaV,YAAY,UAAU,CAAC,aAAa;QAC7E,MAAMY,WAAWF,UAAU,YAAYV,YAAY,UAAU,CAAC,cAAc;QAE5E,OAAO,IAAI,CAAC,iBAAiB,CAAC,OAAO,CAAC;YAClCK;YACA,OAAOK,UAAU,SAASf;YAC1BgB;YACAC;YACA,aAAaF,UAAU;YACvB,QAAQA,UAAU;YAClBd;YACAC;QACJ;IACJ;AACJ;AAEO,MAAMgB,kCAAkBC,gBAAAA,oBAAuC,CAAC;IACnE,gBAAgBhC;IAChB,cAAc;QACViC;QACAC;QACAC;QACAC;QACAC;QACAC;KACH;AACL"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { Result } from "@webiny/feature/api";
|
|
2
|
+
import type { AdminUser } from "@webiny/api-core/types/users.js";
|
|
3
|
+
import type { NotAuthorizedError, UserValidationError } from "@webiny/api-core/features/users/shared/errors.js";
|
|
4
|
+
import type { CreateUserUseCase as CoreCreateUser } from "@webiny/api-core/features/users/CreateUser/index.js";
|
|
5
|
+
import type { FindUserRecordsUseCase } from "@webiny/api-core/features/users/FindUserRecords/index.js";
|
|
6
|
+
import type { CognitoAccountExistsError, CognitoAccountNotFoundError, CognitoGetUserError } from "../../../api/domain/errors.js";
|
|
7
|
+
export interface LinkAdminUserInput {
|
|
8
|
+
email: string;
|
|
9
|
+
roles?: string[];
|
|
10
|
+
teams?: string[];
|
|
11
|
+
}
|
|
12
|
+
export interface ILinkUserUseCaseErrors {
|
|
13
|
+
authorization: NotAuthorizedError;
|
|
14
|
+
validation: UserValidationError;
|
|
15
|
+
createUser: CoreCreateUser.Error;
|
|
16
|
+
findUserRecords: FindUserRecordsUseCase.Error;
|
|
17
|
+
cognitoAccountExists: CognitoAccountExistsError;
|
|
18
|
+
cognitoAccountNotFound: CognitoAccountNotFoundError;
|
|
19
|
+
cognitoGetUser: CognitoGetUserError;
|
|
20
|
+
}
|
|
21
|
+
type LinkAdminUserError = ILinkUserUseCaseErrors[keyof ILinkUserUseCaseErrors];
|
|
22
|
+
/**
|
|
23
|
+
* Gives a person whose account already exists in another tenant access to the current tenant,
|
|
24
|
+
* with the given roles and teams. Their account (name, email, password) stays as it is.
|
|
25
|
+
*/
|
|
26
|
+
export interface ILinkUserUseCase {
|
|
27
|
+
execute(input: LinkAdminUserInput): Promise<Result<AdminUser, LinkAdminUserError>>;
|
|
28
|
+
}
|
|
29
|
+
export declare const LinkUserUseCase: import("@webiny/di").Abstraction<ILinkUserUseCase>;
|
|
30
|
+
export declare namespace LinkUserUseCase {
|
|
31
|
+
type Interface = ILinkUserUseCase;
|
|
32
|
+
type Input = LinkAdminUserInput;
|
|
33
|
+
type Output = AdminUser;
|
|
34
|
+
type Error = LinkAdminUserError;
|
|
35
|
+
}
|
|
36
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api/features/LinkUser/abstractions.js","sources":["../../../../src/api/features/LinkUser/abstractions.ts"],"sourcesContent":["import { createAbstraction } from \"@webiny/feature/api\";\nimport type { Result } from \"@webiny/feature/api\";\nimport type { AdminUser } from \"@webiny/api-core/types/users.js\";\nimport type {\n NotAuthorizedError,\n UserValidationError\n} from \"@webiny/api-core/features/users/shared/errors.js\";\nimport type { CreateUserUseCase as CoreCreateUser } from \"@webiny/api-core/features/users/CreateUser/index.js\";\nimport type { FindUserRecordsUseCase } from \"@webiny/api-core/features/users/FindUserRecords/index.js\";\nimport type {\n CognitoAccountExistsError,\n CognitoAccountNotFoundError,\n CognitoGetUserError\n} from \"~/api/domain/errors.js\";\n\nexport interface LinkAdminUserInput {\n email: string;\n roles?: string[];\n teams?: string[];\n}\n\nexport interface ILinkUserUseCaseErrors {\n authorization: NotAuthorizedError;\n validation: UserValidationError;\n createUser: CoreCreateUser.Error;\n findUserRecords: FindUserRecordsUseCase.Error;\n cognitoAccountExists: CognitoAccountExistsError;\n cognitoAccountNotFound: CognitoAccountNotFoundError;\n cognitoGetUser: CognitoGetUserError;\n}\n\ntype LinkAdminUserError = ILinkUserUseCaseErrors[keyof ILinkUserUseCaseErrors];\n\n/**\n * Gives a person whose account already exists in another tenant access to the current tenant,\n * with the given roles and teams. Their account (name, email, password) stays as it is.\n */\nexport interface ILinkUserUseCase {\n execute(input: LinkAdminUserInput): Promise<Result<AdminUser, LinkAdminUserError>>;\n}\n\nexport const LinkUserUseCase = createAbstraction<ILinkUserUseCase>(\"LinkUserUseCase\");\n\nexport namespace LinkUserUseCase {\n export type Interface = ILinkUserUseCase;\n export type Input = LinkAdminUserInput;\n export type Output = AdminUser;\n export type Error = LinkAdminUserError;\n}\n"],"names":["LinkUserUseCase","createAbstraction"],"mappings":";AAyCO,MAAMA,kBAAkBC,kBAAoC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { createFeature } from "@webiny/feature/api";
|
|
2
|
+
import { LinkUserUseCase } from "./LinkUserUseCase.js";
|
|
3
|
+
const LinkUserFeature = createFeature({
|
|
4
|
+
name: "LinkUserFeature",
|
|
5
|
+
register (container) {
|
|
6
|
+
container.register(LinkUserUseCase);
|
|
7
|
+
}
|
|
8
|
+
});
|
|
9
|
+
export { LinkUserFeature };
|
|
10
|
+
|
|
11
|
+
//# sourceMappingURL=feature.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api/features/LinkUser/feature.js","sources":["../../../../src/api/features/LinkUser/feature.ts"],"sourcesContent":["import { createFeature } from \"@webiny/feature/api\";\nimport { LinkUserUseCase } from \"./LinkUserUseCase.js\";\n\nexport const LinkUserFeature = createFeature({\n name: \"LinkUserFeature\",\n register(container) {\n container.register(LinkUserUseCase);\n }\n});\n"],"names":["LinkUserFeature","createFeature","container","LinkUserUseCase"],"mappings":";;AAGO,MAAMA,kBAAkBC,cAAc;IACzC,MAAM;IACN,UAASC,SAAS;QACdA,UAAU,QAAQ,CAACC;IACvB;AACJ"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Result } from "@webiny/feature/api";
|
|
2
|
+
import { GetUserUseCase } from "@webiny/api-core/features/users/GetUser/index.js";
|
|
3
|
+
import { IdentityContext } from "@webiny/api-core/features/security/IdentityContext/index.js";
|
|
4
|
+
import { RolesRepository } from "@webiny/api-core/features/security/roles/shared/abstractions.js";
|
|
5
|
+
import { TeamsRepository } from "@webiny/api-core/features/security/teams/shared/abstractions.js";
|
|
6
|
+
import { ResetUserMfaUseCase as UseCaseAbstraction } from "./abstractions.js";
|
|
7
|
+
import { CognitoService } from "../CognitoService/abstractions.js";
|
|
8
|
+
declare class ResetUserMfaUseCaseImpl implements UseCaseAbstraction.Interface {
|
|
9
|
+
private identityContext;
|
|
10
|
+
private cognitoService;
|
|
11
|
+
private getUserUseCase;
|
|
12
|
+
private rolesRepository;
|
|
13
|
+
private teamsRepository;
|
|
14
|
+
constructor(identityContext: IdentityContext.Interface, cognitoService: CognitoService.Interface, getUserUseCase: GetUserUseCase.Interface, rolesRepository: RolesRepository.Interface, teamsRepository: TeamsRepository.Interface);
|
|
15
|
+
execute(id: string): Promise<Result<void, UseCaseAbstraction.Error>>;
|
|
16
|
+
}
|
|
17
|
+
export declare const ResetUserMfaUseCase: import("@webiny/di").Implementation<typeof ResetUserMfaUseCaseImpl>;
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { Result, createImplementation } from "@webiny/feature/api";
|
|
2
|
+
import { GetUserUseCase } from "@webiny/api-core/features/users/GetUser/index.js";
|
|
3
|
+
import { NotAuthorizedError } from "@webiny/api-core/features/users/shared/errors.js";
|
|
4
|
+
import { IdentityContext } from "@webiny/api-core/features/security/IdentityContext/index.js";
|
|
5
|
+
import { RolesRepository } from "@webiny/api-core/features/security/roles/shared/abstractions.js";
|
|
6
|
+
import { TeamsRepository } from "@webiny/api-core/features/security/teams/shared/abstractions.js";
|
|
7
|
+
import { canAssignRolesAndTeams } from "@webiny/api-core/features/security/utils/canAssignRolesAndTeams.js";
|
|
8
|
+
import { CANNOT_GRANT_PERMISSIONS_MESSAGE } from "@webiny/api-core/features/security/utils/canGrantPermissions.js";
|
|
9
|
+
import { ResetUserMfaUseCase } from "./abstractions.js";
|
|
10
|
+
import { Username } from "../../domain/Username.js";
|
|
11
|
+
import { CognitoExternalUserError, CognitoResetMfaError } from "../../domain/errors.js";
|
|
12
|
+
import { CognitoService } from "../CognitoService/abstractions.js";
|
|
13
|
+
class ResetUserMfaUseCaseImpl {
|
|
14
|
+
constructor(identityContext, cognitoService, getUserUseCase, rolesRepository, teamsRepository){
|
|
15
|
+
this.identityContext = identityContext;
|
|
16
|
+
this.cognitoService = cognitoService;
|
|
17
|
+
this.getUserUseCase = getUserUseCase;
|
|
18
|
+
this.rolesRepository = rolesRepository;
|
|
19
|
+
this.teamsRepository = teamsRepository;
|
|
20
|
+
}
|
|
21
|
+
async execute(id) {
|
|
22
|
+
const permission = await this.identityContext.getPermission("adminUsers.user");
|
|
23
|
+
if (!permission) return Result.fail(new NotAuthorizedError());
|
|
24
|
+
const getUserResult = await this.getUserUseCase.execute({
|
|
25
|
+
id
|
|
26
|
+
});
|
|
27
|
+
if (getUserResult.isFail()) return Result.fail(getUserResult.error);
|
|
28
|
+
const user = getUserResult.value;
|
|
29
|
+
if (user.external) return Result.fail(new CognitoExternalUserError(id));
|
|
30
|
+
const canAssign = await canAssignRolesAndTeams({
|
|
31
|
+
identityContext: this.identityContext,
|
|
32
|
+
rolesRepository: this.rolesRepository,
|
|
33
|
+
teamsRepository: this.teamsRepository,
|
|
34
|
+
roles: user.roles ?? [],
|
|
35
|
+
teams: user.teams ?? []
|
|
36
|
+
});
|
|
37
|
+
if (!canAssign) return Result.fail(new NotAuthorizedError(CANNOT_GRANT_PERMISSIONS_MESSAGE));
|
|
38
|
+
try {
|
|
39
|
+
await this.cognitoService.resetMfa(Username.fromUser(user));
|
|
40
|
+
return Result.ok();
|
|
41
|
+
} catch (cognitoError) {
|
|
42
|
+
return Result.fail(new CognitoResetMfaError(cognitoError));
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const ResetUserMfaUseCase_ResetUserMfaUseCase = createImplementation({
|
|
47
|
+
abstraction: ResetUserMfaUseCase,
|
|
48
|
+
implementation: ResetUserMfaUseCaseImpl,
|
|
49
|
+
dependencies: [
|
|
50
|
+
IdentityContext,
|
|
51
|
+
CognitoService,
|
|
52
|
+
GetUserUseCase,
|
|
53
|
+
RolesRepository,
|
|
54
|
+
TeamsRepository
|
|
55
|
+
]
|
|
56
|
+
});
|
|
57
|
+
export { ResetUserMfaUseCase_ResetUserMfaUseCase as ResetUserMfaUseCase };
|
|
58
|
+
|
|
59
|
+
//# sourceMappingURL=ResetUserMfaUseCase.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api/features/ResetUserMfa/ResetUserMfaUseCase.js","sources":["../../../../src/api/features/ResetUserMfa/ResetUserMfaUseCase.ts"],"sourcesContent":["import { createImplementation } from \"@webiny/feature/api\";\nimport { Result } from \"@webiny/feature/api\";\nimport { GetUserUseCase } from \"@webiny/api-core/features/users/GetUser/index.js\";\nimport { NotAuthorizedError } from \"@webiny/api-core/features/users/shared/errors.js\";\nimport { IdentityContext } from \"@webiny/api-core/features/security/IdentityContext/index.js\";\nimport { RolesRepository } from \"@webiny/api-core/features/security/roles/shared/abstractions.js\";\nimport { TeamsRepository } from \"@webiny/api-core/features/security/teams/shared/abstractions.js\";\nimport { canAssignRolesAndTeams } from \"@webiny/api-core/features/security/utils/canAssignRolesAndTeams.js\";\nimport { CANNOT_GRANT_PERMISSIONS_MESSAGE } from \"@webiny/api-core/features/security/utils/canGrantPermissions.js\";\nimport { ResetUserMfaUseCase as UseCaseAbstraction } from \"./abstractions.js\";\nimport { Username } from \"~/api/domain/Username.js\";\nimport { CognitoExternalUserError, CognitoResetMfaError } from \"~/api/domain/errors.js\";\nimport { CognitoService } from \"../CognitoService/abstractions.js\";\n\nclass ResetUserMfaUseCaseImpl implements UseCaseAbstraction.Interface {\n constructor(\n private identityContext: IdentityContext.Interface,\n private cognitoService: CognitoService.Interface,\n private getUserUseCase: GetUserUseCase.Interface,\n private rolesRepository: RolesRepository.Interface,\n private teamsRepository: TeamsRepository.Interface\n ) {}\n\n async execute(id: string): Promise<Result<void, UseCaseAbstraction.Error>> {\n const permission = await this.identityContext.getPermission(\"adminUsers.user\");\n if (!permission) {\n return Result.fail(new NotAuthorizedError());\n }\n\n const getUserResult = await this.getUserUseCase.execute({ id });\n if (getUserResult.isFail()) {\n return Result.fail(getUserResult.error);\n }\n\n const user = getUserResult.value;\n\n // A federated user signs in through their own identity provider, which handles their MFA.\n if (user.external) {\n return Result.fail(new CognitoExternalUserError(id));\n }\n\n // Security: the caller may only manage a user whose roles and teams they could assign\n // (same rule as updating or deleting a user).\n const canAssign = await canAssignRolesAndTeams({\n identityContext: this.identityContext,\n rolesRepository: this.rolesRepository,\n teamsRepository: this.teamsRepository,\n roles: user.roles ?? [],\n teams: user.teams ?? []\n });\n if (!canAssign) {\n return Result.fail(new NotAuthorizedError(CANNOT_GRANT_PERMISSIONS_MESSAGE));\n }\n\n // Unlike changing a shared account's name, email or password, any administrator of one of\n // the person's tenants who may manage the user there may reset its MFA: the person can't\n // do it themselves when they can't sign in, and the reset doesn't give the administrator\n // access to the account.\n try {\n await this.cognitoService.resetMfa(Username.fromUser(user));\n\n return Result.ok();\n } catch (cognitoError) {\n return Result.fail(new CognitoResetMfaError(cognitoError as Error));\n }\n }\n}\n\nexport const ResetUserMfaUseCase = createImplementation({\n abstraction: UseCaseAbstraction,\n implementation: ResetUserMfaUseCaseImpl,\n dependencies: [\n IdentityContext,\n CognitoService,\n GetUserUseCase,\n RolesRepository,\n TeamsRepository\n ]\n});\n"],"names":["ResetUserMfaUseCaseImpl","identityContext","cognitoService","getUserUseCase","rolesRepository","teamsRepository","id","permission","Result","NotAuthorizedError","getUserResult","user","CognitoExternalUserError","canAssign","canAssignRolesAndTeams","CANNOT_GRANT_PERMISSIONS_MESSAGE","Username","cognitoError","CognitoResetMfaError","ResetUserMfaUseCase","createImplementation","UseCaseAbstraction","IdentityContext","CognitoService","GetUserUseCase","RolesRepository","TeamsRepository"],"mappings":";;;;;;;;;;;;AAcA,MAAMA;IACF,YACYC,eAA0C,EAC1CC,cAAwC,EACxCC,cAAwC,EACxCC,eAA0C,EAC1CC,eAA0C,CACpD;aALUJ,eAAe,GAAfA;aACAC,cAAc,GAAdA;aACAC,cAAc,GAAdA;aACAC,eAAe,GAAfA;aACAC,eAAe,GAAfA;IACT;IAEH,MAAM,QAAQC,EAAU,EAAmD;QACvE,MAAMC,aAAa,MAAM,IAAI,CAAC,eAAe,CAAC,aAAa,CAAC;QAC5D,IAAI,CAACA,YACD,OAAOC,OAAO,IAAI,CAAC,IAAIC;QAG3B,MAAMC,gBAAgB,MAAM,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC;YAAEJ;QAAG;QAC7D,IAAII,cAAc,MAAM,IACpB,OAAOF,OAAO,IAAI,CAACE,cAAc,KAAK;QAG1C,MAAMC,OAAOD,cAAc,KAAK;QAGhC,IAAIC,KAAK,QAAQ,EACb,OAAOH,OAAO,IAAI,CAAC,IAAII,yBAAyBN;QAKpD,MAAMO,YAAY,MAAMC,uBAAuB;YAC3C,iBAAiB,IAAI,CAAC,eAAe;YACrC,iBAAiB,IAAI,CAAC,eAAe;YACrC,iBAAiB,IAAI,CAAC,eAAe;YACrC,OAAOH,KAAK,KAAK,IAAI,EAAE;YACvB,OAAOA,KAAK,KAAK,IAAI,EAAE;QAC3B;QACA,IAAI,CAACE,WACD,OAAOL,OAAO,IAAI,CAAC,IAAIC,mBAAmBM;QAO9C,IAAI;YACA,MAAM,IAAI,CAAC,cAAc,CAAC,QAAQ,CAACC,SAAS,QAAQ,CAACL;YAErD,OAAOH,OAAO,EAAE;QACpB,EAAE,OAAOS,cAAc;YACnB,OAAOT,OAAO,IAAI,CAAC,IAAIU,qBAAqBD;QAChD;IACJ;AACJ;AAEO,MAAME,0CAAsBC,qBAAqB;IACpD,aAAaC;IACb,gBAAgBrB;IAChB,cAAc;QACVsB;QACAC;QACAC;QACAC;QACAC;KACH;AACL"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Result } from "@webiny/feature/api";
|
|
2
|
+
import type { NotAuthorizedError } from "@webiny/api-core/features/users/shared/errors.js";
|
|
3
|
+
import type { GetUserUseCase } from "@webiny/api-core/features/users/GetUser/index.js";
|
|
4
|
+
import type { CognitoExternalUserError, CognitoResetMfaError } from "../../../api/domain/errors.js";
|
|
5
|
+
export interface IResetUserMfaUseCaseErrors {
|
|
6
|
+
authorization: NotAuthorizedError;
|
|
7
|
+
getUser: GetUserUseCase.Error;
|
|
8
|
+
externalUser: CognitoExternalUserError;
|
|
9
|
+
cognitoResetMfa: CognitoResetMfaError;
|
|
10
|
+
}
|
|
11
|
+
type IResetUserMfaError = IResetUserMfaUseCaseErrors[keyof IResetUserMfaUseCaseErrors];
|
|
12
|
+
export interface IResetUserMfaUseCase {
|
|
13
|
+
/**
|
|
14
|
+
* Deletes the user's TOTP software token, so they set up MFA again on their next sign-in.
|
|
15
|
+
* The account is shared by all of the person's tenants, so this applies in all of them.
|
|
16
|
+
*/
|
|
17
|
+
execute(id: string): Promise<Result<void, IResetUserMfaError>>;
|
|
18
|
+
}
|
|
19
|
+
export declare const ResetUserMfaUseCase: import("@webiny/di").Abstraction<IResetUserMfaUseCase>;
|
|
20
|
+
export declare namespace ResetUserMfaUseCase {
|
|
21
|
+
type Interface = IResetUserMfaUseCase;
|
|
22
|
+
type Error = IResetUserMfaError;
|
|
23
|
+
}
|
|
24
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api/features/ResetUserMfa/abstractions.js","sources":["../../../../src/api/features/ResetUserMfa/abstractions.ts"],"sourcesContent":["import { createAbstraction } from \"@webiny/feature/api\";\nimport type { Result } from \"@webiny/feature/api\";\nimport type { NotAuthorizedError } from \"@webiny/api-core/features/users/shared/errors.js\";\nimport type { GetUserUseCase } from \"@webiny/api-core/features/users/GetUser/index.js\";\nimport type { CognitoExternalUserError, CognitoResetMfaError } from \"~/api/domain/errors.js\";\n\nexport interface IResetUserMfaUseCaseErrors {\n authorization: NotAuthorizedError;\n getUser: GetUserUseCase.Error;\n externalUser: CognitoExternalUserError;\n cognitoResetMfa: CognitoResetMfaError;\n}\n\ntype IResetUserMfaError = IResetUserMfaUseCaseErrors[keyof IResetUserMfaUseCaseErrors];\n\nexport interface IResetUserMfaUseCase {\n /**\n * Deletes the user's TOTP software token, so they set up MFA again on their next sign-in.\n * The account is shared by all of the person's tenants, so this applies in all of them.\n */\n execute(id: string): Promise<Result<void, IResetUserMfaError>>;\n}\n\nexport const ResetUserMfaUseCase = createAbstraction<IResetUserMfaUseCase>(\"ResetUserMfaUseCase\");\n\nexport namespace ResetUserMfaUseCase {\n export type Interface = IResetUserMfaUseCase;\n export type Error = IResetUserMfaError;\n}\n"],"names":["ResetUserMfaUseCase","createAbstraction"],"mappings":";AAuBO,MAAMA,sBAAsBC,kBAAwC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { createFeature } from "@webiny/feature/api";
|
|
2
|
+
import { ResetUserMfaUseCase } from "./ResetUserMfaUseCase.js";
|
|
3
|
+
const ResetUserMfaFeature = createFeature({
|
|
4
|
+
name: "ResetUserMfaFeature",
|
|
5
|
+
register (container) {
|
|
6
|
+
container.register(ResetUserMfaUseCase);
|
|
7
|
+
}
|
|
8
|
+
});
|
|
9
|
+
export { ResetUserMfaFeature };
|
|
10
|
+
|
|
11
|
+
//# sourceMappingURL=feature.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api/features/ResetUserMfa/feature.js","sources":["../../../../src/api/features/ResetUserMfa/feature.ts"],"sourcesContent":["import { createFeature } from \"@webiny/feature/api\";\nimport { ResetUserMfaUseCase } from \"./ResetUserMfaUseCase.js\";\n\nexport const ResetUserMfaFeature = createFeature({\n name: \"ResetUserMfaFeature\",\n register(container) {\n container.register(ResetUserMfaUseCase);\n }\n});\n"],"names":["ResetUserMfaFeature","createFeature","container","ResetUserMfaUseCase"],"mappings":";;AAGO,MAAMA,sBAAsBC,cAAc;IAC7C,MAAM;IACN,UAASC,SAAS;QACdA,UAAU,QAAQ,CAACC;IACvB;AACJ"}
|