@webiny/api-core 6.5.0-beta.9 → 6.5.1-beta.0

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.
Files changed (60) hide show
  1. package/exports/api/security.d.ts +2 -0
  2. package/exports/api/security.js +2 -0
  3. package/features/ai/DEVELOPERS.md +14 -0
  4. package/features/ai/OpenRouterSdkFactory.d.ts +10 -0
  5. package/features/ai/OpenRouterSdkFactory.js +112 -0
  6. package/features/ai/OpenRouterSdkFactory.js.map +1 -0
  7. package/features/ai/feature.js +2 -0
  8. package/features/ai/feature.js.map +1 -1
  9. package/features/security/ResolveIdentityTenants/ResolveIdentityTenantsUseCase.d.ts +12 -0
  10. package/features/security/ResolveIdentityTenants/ResolveIdentityTenantsUseCase.js +46 -0
  11. package/features/security/ResolveIdentityTenants/ResolveIdentityTenantsUseCase.js.map +1 -0
  12. package/features/security/ResolveIdentityTenants/abstractions.d.ts +22 -0
  13. package/features/security/ResolveIdentityTenants/abstractions.js +5 -0
  14. package/features/security/ResolveIdentityTenants/abstractions.js.map +1 -0
  15. package/features/security/ResolveIdentityTenants/feature.d.ts +4 -0
  16. package/features/security/ResolveIdentityTenants/feature.js +11 -0
  17. package/features/security/ResolveIdentityTenants/feature.js.map +1 -0
  18. package/features/security/ResolveIdentityTenants/index.d.ts +1 -0
  19. package/features/security/ResolveIdentityTenants/index.js +1 -0
  20. package/features/security/SecurityFeature.js +4 -0
  21. package/features/security/SecurityFeature.js.map +1 -1
  22. package/features/security/login/LogInUseCase.d.ts +20 -0
  23. package/features/security/login/LogInUseCase.js +75 -0
  24. package/features/security/login/LogInUseCase.js.map +1 -0
  25. package/features/security/login/abstractions.d.ts +45 -0
  26. package/features/security/login/abstractions.js +2 -1
  27. package/features/security/login/abstractions.js.map +1 -1
  28. package/features/security/login/errors.d.ts +13 -0
  29. package/features/security/login/errors.js +25 -0
  30. package/features/security/login/errors.js.map +1 -0
  31. package/features/security/login/feature.d.ts +4 -0
  32. package/features/security/login/feature.js +11 -0
  33. package/features/security/login/feature.js.map +1 -0
  34. package/features/tenancy/TenancyFeature.js +1 -1
  35. package/features/tenancy/TenancyFeature.js.map +1 -1
  36. package/features/users/AdminUsersFeature.js +2 -0
  37. package/features/users/AdminUsersFeature.js.map +1 -1
  38. package/features/users/FindUserRecords/FindUserRecordsUseCase.d.ts +12 -0
  39. package/features/users/FindUserRecords/FindUserRecordsUseCase.js +54 -0
  40. package/features/users/FindUserRecords/FindUserRecordsUseCase.js.map +1 -0
  41. package/features/users/FindUserRecords/abstractions.d.ts +29 -0
  42. package/features/users/FindUserRecords/abstractions.js +5 -0
  43. package/features/users/FindUserRecords/abstractions.js.map +1 -0
  44. package/features/users/FindUserRecords/feature.d.ts +4 -0
  45. package/features/users/FindUserRecords/feature.js +11 -0
  46. package/features/users/FindUserRecords/feature.js.map +1 -0
  47. package/features/users/FindUserRecords/index.d.ts +1 -0
  48. package/features/users/FindUserRecords/index.js +1 -0
  49. package/features/users/shared/AdminUsersRepository.d.ts +3 -1
  50. package/features/users/shared/AdminUsersRepository.js +22 -7
  51. package/features/users/shared/AdminUsersRepository.js.map +1 -1
  52. package/features/users/shared/abstractions.d.ts +4 -1
  53. package/features/users/shared/abstractions.js.map +1 -1
  54. package/features/users/shared/types.d.ts +9 -0
  55. package/graphql/security/identity.gql.js +27 -22
  56. package/graphql/security/identity.gql.js.map +1 -1
  57. package/legacy/tenancy/createTenancyContext.js +8 -1
  58. package/legacy/tenancy/createTenancyContext.js.map +1 -1
  59. package/package.json +21 -20
  60. package/types/users.d.ts +17 -0
@@ -9,4 +9,6 @@ export { TeamFactory } from "../../features/security/teams/shared/abstractions.j
9
9
  export { ApiToken } from "../../domain/security/ApiToken.js";
10
10
  export { IdentityProvider, OidcIdentityProvider, JwtIdentityProvider } from "../../idp/index.js";
11
11
  export { Authenticator } from "../../features/security/authentication/Authenticator/abstractions.js";
12
+ export { ResolveIdentityTenantsUseCase } from "../../features/security/ResolveIdentityTenants/index.js";
13
+ export { LogInUseCase } from "../../features/security/login/index.js";
12
14
  export { Authorizer } from "../../features/security/authorization/Authorizer/index.js";
@@ -6,4 +6,6 @@ export { TeamFactory } from "../../features/security/teams/shared/abstractions.j
6
6
  export { ApiToken } from "../../domain/security/ApiToken.js";
7
7
  export { IdentityProvider, JwtIdentityProvider, OidcIdentityProvider } from "../../idp/index.js";
8
8
  export { Authenticator } from "../../features/security/authentication/Authenticator/abstractions.js";
9
+ export { ResolveIdentityTenantsUseCase } from "../../features/security/ResolveIdentityTenants/index.js";
10
+ export { LogInUseCase } from "../../features/security/login/index.js";
9
11
  export { Authorizer } from "../../features/security/authorization/Authorizer/index.js";
@@ -139,6 +139,20 @@ Env var fallback: `WEBINY_API_GOOGLE_API_KEY`.
139
139
 
140
140
  Source: https://ai.google.dev/gemini-api/docs/deprecations
141
141
 
142
+ ### OpenRouter (`OpenRouterSdkFactory.ts`)
143
+
144
+ OpenRouter is a gateway: one key reaches models from many vendors. Its model ids carry the vendor, so a stored model looks like `openrouter/anthropic/claude-sonnet-5.5`. `Ai` splits on the first slash, so the provider is `openrouter` and the rest goes to OpenRouter unchanged.
145
+
146
+ The factory list is a short pick of each vendor's current models that support tool calling, not OpenRouter's whole catalog (a few hundred models, many without tools). Pick from `https://openrouter.ai/api/v1/models`, keeping entries whose `supported_parameters` includes `tools` and skipping the `:free` and `:batch` variants. OpenRouter publishes retirement dates as `expiration_date`; leave out models that have one coming up.
147
+
148
+ `openrouter/auto` is OpenRouter's Auto Router: it picks the model per request, so the vendor that answers can change between calls. It's stored as `openrouter/openrouter/auto`. The catalog marks it as not accepting `temperature`, because some of the models it can pick reject it.
149
+
150
+ Uses `@openrouter/ai-sdk-provider` rather than `@ai-sdk/openai` with a different base URL, so non-OpenAI models routed through it don't get OpenAI-only request options.
151
+
152
+ Env var fallback: `WEBINY_API_OPENROUTER_API_KEY`.
153
+
154
+ Source: https://openrouter.ai/models
155
+
142
156
  ## Adding a new model
143
157
 
144
158
  1. Verify the model supports tool/function calling.
@@ -0,0 +1,10 @@
1
+ import { AiSdkFactory as AiSdkFactoryAbstraction } from "./abstractions.js";
2
+ import type { IAiSdk, IAiSdkModel } from "./abstractions.js";
3
+ declare class OpenRouterSdkFactoryImpl implements AiSdkFactoryAbstraction.Interface {
4
+ readonly id = "openrouter";
5
+ readonly name = "OpenRouter";
6
+ readonly models: IAiSdkModel[];
7
+ execute(apiKey?: string): Promise<IAiSdk>;
8
+ }
9
+ export declare const OpenRouterSdkFactory: import("@webiny/di").Implementation<typeof OpenRouterSdkFactoryImpl>;
10
+ export {};
@@ -0,0 +1,112 @@
1
+ import { createImplementation } from "@webiny/feature/api";
2
+ import { AiSdkFactory } from "./abstractions.js";
3
+ const OPENROUTER_MODELS = [
4
+ {
5
+ id: "openrouter/auto",
6
+ name: "OpenRouter: Auto Router"
7
+ },
8
+ {
9
+ id: "mistralai/mistral-large-4-0",
10
+ name: "Mistral: Mistral Large 4"
11
+ },
12
+ {
13
+ id: "openai/gpt-6.1-sol",
14
+ name: "OpenAI: GPT-6.1 Sol"
15
+ },
16
+ {
17
+ id: "anthropic/claude-sonnet-5.5",
18
+ name: "Anthropic: Claude Sonnet 5.5"
19
+ },
20
+ {
21
+ id: "qwen/qwen3.8-max-prime",
22
+ name: "Qwen: Qwen3.8 Max Prime"
23
+ },
24
+ {
25
+ id: "z-ai/glm-5.3-prime",
26
+ name: "Z.ai: GLM 5.3 Prime"
27
+ },
28
+ {
29
+ id: "openai/gpt-6-sol",
30
+ name: "OpenAI: GPT-6 Sol"
31
+ },
32
+ {
33
+ id: "openai/gpt-6-luna",
34
+ name: "OpenAI: GPT-6 Luna"
35
+ },
36
+ {
37
+ id: "anthropic/claude-opus-5.5",
38
+ name: "Anthropic: Claude Opus 5.5"
39
+ },
40
+ {
41
+ id: "x-ai/grok-4.7",
42
+ name: "SpaceXAI: Grok 4.7"
43
+ },
44
+ {
45
+ id: "deepseek/deepseek-v4.1-flash",
46
+ name: "DeepSeek: DeepSeek V4.1 Flash"
47
+ },
48
+ {
49
+ id: "openai/gpt-6-astra",
50
+ name: "OpenAI: GPT-6 Astra"
51
+ },
52
+ {
53
+ id: "google/gemini-3.8-flash",
54
+ name: "Google: Gemini 3.8 Flash"
55
+ },
56
+ {
57
+ id: "anthropic/claude-fable-5.1",
58
+ name: "Anthropic: Claude Fable 5.1"
59
+ },
60
+ {
61
+ id: "anthropic/claude-opus-5",
62
+ name: "Anthropic: Claude Opus 5"
63
+ },
64
+ {
65
+ id: "google/gemini-3.5-flash-lite",
66
+ name: "Google: Gemini 3.5 Flash Lite"
67
+ },
68
+ {
69
+ id: "moonshotai/kimi-k3",
70
+ name: "MoonshotAI: Kimi K3"
71
+ },
72
+ {
73
+ id: "openai/gpt-5.6-terra",
74
+ name: "OpenAI: GPT-5.6 Terra"
75
+ },
76
+ {
77
+ id: "anthropic/claude-sonnet-5",
78
+ name: "Anthropic: Claude Sonnet 5"
79
+ },
80
+ {
81
+ id: "openai/gpt-5.4-mini",
82
+ name: "OpenAI: GPT-5.4 Mini"
83
+ },
84
+ {
85
+ id: "anthropic/claude-haiku-4.5",
86
+ name: "Anthropic: Claude Haiku 4.5"
87
+ }
88
+ ];
89
+ class OpenRouterSdkFactoryImpl {
90
+ async execute(apiKey) {
91
+ const { createOpenRouter } = await import("@openrouter/ai-sdk-provider");
92
+ const provider = createOpenRouter({
93
+ apiKey: apiKey ?? process.env.WEBINY_API_OPENROUTER_API_KEY
94
+ });
95
+ return {
96
+ languageModel: (modelId)=>provider.chat(modelId)
97
+ };
98
+ }
99
+ constructor(){
100
+ this.id = "openrouter";
101
+ this.name = "OpenRouter";
102
+ this.models = OPENROUTER_MODELS;
103
+ }
104
+ }
105
+ const OpenRouterSdkFactory = createImplementation({
106
+ abstraction: AiSdkFactory,
107
+ implementation: OpenRouterSdkFactoryImpl,
108
+ dependencies: []
109
+ });
110
+ export { OpenRouterSdkFactory };
111
+
112
+ //# sourceMappingURL=OpenRouterSdkFactory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"features/ai/OpenRouterSdkFactory.js","sources":["../../../src/features/ai/OpenRouterSdkFactory.ts"],"sourcesContent":["import { createImplementation } from \"@webiny/feature/api\";\nimport { AiSdkFactory as AiSdkFactoryAbstraction } from \"./abstractions.js\";\nimport type { IAiSdk, IAiSdkModel } from \"./abstractions.js\";\n\n/*\n * OpenRouter model ids carry their own vendor prefix, so the composite Webiny id has two slashes:\n * \"openrouter/anthropic/claude-sonnet-5.5\". `Ai` splits on the first slash only, which leaves the\n * OpenRouter id intact.\n */\nconst OPENROUTER_MODELS: IAiSdkModel[] = [\n // OpenRouter picks the model per request, so which vendor answers can change between calls.\n { id: \"openrouter/auto\", name: \"OpenRouter: Auto Router\" },\n { id: \"mistralai/mistral-large-4-0\", name: \"Mistral: Mistral Large 4\" },\n { id: \"openai/gpt-6.1-sol\", name: \"OpenAI: GPT-6.1 Sol\" },\n { id: \"anthropic/claude-sonnet-5.5\", name: \"Anthropic: Claude Sonnet 5.5\" },\n { id: \"qwen/qwen3.8-max-prime\", name: \"Qwen: Qwen3.8 Max Prime\" },\n { id: \"z-ai/glm-5.3-prime\", name: \"Z.ai: GLM 5.3 Prime\" },\n { id: \"openai/gpt-6-sol\", name: \"OpenAI: GPT-6 Sol\" },\n { id: \"openai/gpt-6-luna\", name: \"OpenAI: GPT-6 Luna\" },\n { id: \"anthropic/claude-opus-5.5\", name: \"Anthropic: Claude Opus 5.5\" },\n { id: \"x-ai/grok-4.7\", name: \"SpaceXAI: Grok 4.7\" },\n { id: \"deepseek/deepseek-v4.1-flash\", name: \"DeepSeek: DeepSeek V4.1 Flash\" },\n { id: \"openai/gpt-6-astra\", name: \"OpenAI: GPT-6 Astra\" },\n { id: \"google/gemini-3.8-flash\", name: \"Google: Gemini 3.8 Flash\" },\n { id: \"anthropic/claude-fable-5.1\", name: \"Anthropic: Claude Fable 5.1\" },\n { id: \"anthropic/claude-opus-5\", name: \"Anthropic: Claude Opus 5\" },\n { id: \"google/gemini-3.5-flash-lite\", name: \"Google: Gemini 3.5 Flash Lite\" },\n { id: \"moonshotai/kimi-k3\", name: \"MoonshotAI: Kimi K3\" },\n { id: \"openai/gpt-5.6-terra\", name: \"OpenAI: GPT-5.6 Terra\" },\n { id: \"anthropic/claude-sonnet-5\", name: \"Anthropic: Claude Sonnet 5\" },\n { id: \"openai/gpt-5.4-mini\", name: \"OpenAI: GPT-5.4 Mini\" },\n { id: \"anthropic/claude-haiku-4.5\", name: \"Anthropic: Claude Haiku 4.5\" }\n];\n\nclass OpenRouterSdkFactoryImpl implements AiSdkFactoryAbstraction.Interface {\n readonly id = \"openrouter\";\n readonly name = \"OpenRouter\";\n readonly models = OPENROUTER_MODELS;\n\n async execute(apiKey?: string): Promise<IAiSdk> {\n const { createOpenRouter } = await import(\"@openrouter/ai-sdk-provider\");\n const provider = createOpenRouter({\n apiKey: apiKey ?? process.env.WEBINY_API_OPENROUTER_API_KEY\n });\n return {\n languageModel: modelId => provider.chat(modelId)\n };\n }\n}\n\nexport const OpenRouterSdkFactory = createImplementation({\n abstraction: AiSdkFactoryAbstraction,\n implementation: OpenRouterSdkFactoryImpl,\n dependencies: []\n});\n"],"names":["OPENROUTER_MODELS","OpenRouterSdkFactoryImpl","apiKey","createOpenRouter","provider","process","modelId","OpenRouterSdkFactory","createImplementation","AiSdkFactoryAbstraction"],"mappings":";;AASA,MAAMA,oBAAmC;IAErC;QAAE,IAAI;QAAmB,MAAM;IAA0B;IACzD;QAAE,IAAI;QAA+B,MAAM;IAA2B;IACtE;QAAE,IAAI;QAAsB,MAAM;IAAsB;IACxD;QAAE,IAAI;QAA+B,MAAM;IAA+B;IAC1E;QAAE,IAAI;QAA0B,MAAM;IAA0B;IAChE;QAAE,IAAI;QAAsB,MAAM;IAAsB;IACxD;QAAE,IAAI;QAAoB,MAAM;IAAoB;IACpD;QAAE,IAAI;QAAqB,MAAM;IAAqB;IACtD;QAAE,IAAI;QAA6B,MAAM;IAA6B;IACtE;QAAE,IAAI;QAAiB,MAAM;IAAqB;IAClD;QAAE,IAAI;QAAgC,MAAM;IAAgC;IAC5E;QAAE,IAAI;QAAsB,MAAM;IAAsB;IACxD;QAAE,IAAI;QAA2B,MAAM;IAA2B;IAClE;QAAE,IAAI;QAA8B,MAAM;IAA8B;IACxE;QAAE,IAAI;QAA2B,MAAM;IAA2B;IAClE;QAAE,IAAI;QAAgC,MAAM;IAAgC;IAC5E;QAAE,IAAI;QAAsB,MAAM;IAAsB;IACxD;QAAE,IAAI;QAAwB,MAAM;IAAwB;IAC5D;QAAE,IAAI;QAA6B,MAAM;IAA6B;IACtE;QAAE,IAAI;QAAuB,MAAM;IAAuB;IAC1D;QAAE,IAAI;QAA8B,MAAM;IAA8B;CAC3E;AAED,MAAMC;IAKF,MAAM,QAAQC,MAAe,EAAmB;QAC5C,MAAM,EAAEC,gBAAgB,EAAE,GAAG,MAAM,MAAM,CAAC;QAC1C,MAAMC,WAAWD,iBAAiB;YAC9B,QAAQD,UAAUG,QAAQ,GAAG,CAAC,6BAA6B;QAC/D;QACA,OAAO;YACH,eAAeC,CAAAA,UAAWF,SAAS,IAAI,CAACE;QAC5C;IACJ;;aAZS,EAAE,GAAG;aACL,IAAI,GAAG;aACP,MAAM,GAAGN;;AAWtB;AAEO,MAAMO,uBAAuBC,qBAAqB;IACrD,aAAaC;IACb,gBAAgBR;IAChB,cAAc,EAAE;AACpB"}
@@ -2,6 +2,7 @@ import { createFeature } from "@webiny/feature/api";
2
2
  import { OpenAiSdkFactory } from "./OpenAiSdkFactory.js";
3
3
  import { AnthropicSdkFactory } from "./AnthropicSdkFactory.js";
4
4
  import { GoogleSdkFactory } from "./GoogleSdkFactory.js";
5
+ import { OpenRouterSdkFactory } from "./OpenRouterSdkFactory.js";
5
6
  import { Ai } from "./Ai.js";
6
7
  import { AiModelRegistry } from "./AiModelRegistry.js";
7
8
  import { RemoteAiModelCatalog } from "./RemoteAiModelCatalog.js";
@@ -15,6 +16,7 @@ const AiFeature = createFeature({
15
16
  container.register(OpenAiSdkFactory);
16
17
  container.register(AnthropicSdkFactory);
17
18
  container.register(GoogleSdkFactory);
19
+ container.register(OpenRouterSdkFactory);
18
20
  container.register(Ai);
19
21
  container.register(RemoteAiModelCatalog);
20
22
  container.register(AiModelRegistry);
@@ -1 +1 @@
1
- {"version":3,"file":"features/ai/feature.js","sources":["../../../src/features/ai/feature.ts"],"sourcesContent":["import { createFeature } from \"@webiny/feature/api\";\nimport { OpenAiSdkFactory } from \"./OpenAiSdkFactory.js\";\nimport { AnthropicSdkFactory } from \"./AnthropicSdkFactory.js\";\nimport { GoogleSdkFactory } from \"./GoogleSdkFactory.js\";\nimport { Ai } from \"./Ai.js\";\nimport { AiModelRegistry } from \"./AiModelRegistry.js\";\nimport { RemoteAiModelCatalog } from \"./RemoteAiModelCatalog.js\";\nimport { AiSdkTools } from \"./AiSdkTools.js\";\nimport { AiOutputToolRegistry } from \"./toolPipeline/AiOutputToolRegistry.js\";\nimport { AiToolPipelineRunner } from \"./toolPipeline/AiToolPipelineRunner.js\";\nimport { DefaultTextExtractor } from \"./TextExtractor/DefaultTextExtractor.js\";\n\nexport const AiFeature = createFeature({\n name: \"AiFeature\",\n register(container) {\n container.register(OpenAiSdkFactory);\n container.register(AnthropicSdkFactory);\n container.register(GoogleSdkFactory);\n container.register(Ai);\n container.register(RemoteAiModelCatalog);\n container.register(AiModelRegistry);\n container.register(AiSdkTools);\n container.register(AiOutputToolRegistry);\n container.register(AiToolPipelineRunner);\n container.register(DefaultTextExtractor);\n }\n});\n"],"names":["AiFeature","createFeature","container","OpenAiSdkFactory","AnthropicSdkFactory","GoogleSdkFactory","Ai","RemoteAiModelCatalog","AiModelRegistry","AiSdkTools","AiOutputToolRegistry","AiToolPipelineRunner","DefaultTextExtractor"],"mappings":";;;;;;;;;;;AAYO,MAAMA,YAAYC,cAAc;IACnC,MAAM;IACN,UAASC,SAAS;QACdA,UAAU,QAAQ,CAACC;QACnBD,UAAU,QAAQ,CAACE;QACnBF,UAAU,QAAQ,CAACG;QACnBH,UAAU,QAAQ,CAACI;QACnBJ,UAAU,QAAQ,CAACK;QACnBL,UAAU,QAAQ,CAACM;QACnBN,UAAU,QAAQ,CAACO;QACnBP,UAAU,QAAQ,CAACQ;QACnBR,UAAU,QAAQ,CAACS;QACnBT,UAAU,QAAQ,CAACU;IACvB;AACJ"}
1
+ {"version":3,"file":"features/ai/feature.js","sources":["../../../src/features/ai/feature.ts"],"sourcesContent":["import { createFeature } from \"@webiny/feature/api\";\nimport { OpenAiSdkFactory } from \"./OpenAiSdkFactory.js\";\nimport { AnthropicSdkFactory } from \"./AnthropicSdkFactory.js\";\nimport { GoogleSdkFactory } from \"./GoogleSdkFactory.js\";\nimport { OpenRouterSdkFactory } from \"./OpenRouterSdkFactory.js\";\nimport { Ai } from \"./Ai.js\";\nimport { AiModelRegistry } from \"./AiModelRegistry.js\";\nimport { RemoteAiModelCatalog } from \"./RemoteAiModelCatalog.js\";\nimport { AiSdkTools } from \"./AiSdkTools.js\";\nimport { AiOutputToolRegistry } from \"./toolPipeline/AiOutputToolRegistry.js\";\nimport { AiToolPipelineRunner } from \"./toolPipeline/AiToolPipelineRunner.js\";\nimport { DefaultTextExtractor } from \"./TextExtractor/DefaultTextExtractor.js\";\n\nexport const AiFeature = createFeature({\n name: \"AiFeature\",\n register(container) {\n container.register(OpenAiSdkFactory);\n container.register(AnthropicSdkFactory);\n container.register(GoogleSdkFactory);\n container.register(OpenRouterSdkFactory);\n container.register(Ai);\n container.register(RemoteAiModelCatalog);\n container.register(AiModelRegistry);\n container.register(AiSdkTools);\n container.register(AiOutputToolRegistry);\n container.register(AiToolPipelineRunner);\n container.register(DefaultTextExtractor);\n }\n});\n"],"names":["AiFeature","createFeature","container","OpenAiSdkFactory","AnthropicSdkFactory","GoogleSdkFactory","OpenRouterSdkFactory","Ai","RemoteAiModelCatalog","AiModelRegistry","AiSdkTools","AiOutputToolRegistry","AiToolPipelineRunner","DefaultTextExtractor"],"mappings":";;;;;;;;;;;;AAaO,MAAMA,YAAYC,cAAc;IACnC,MAAM;IACN,UAASC,SAAS;QACdA,UAAU,QAAQ,CAACC;QACnBD,UAAU,QAAQ,CAACE;QACnBF,UAAU,QAAQ,CAACG;QACnBH,UAAU,QAAQ,CAACI;QACnBJ,UAAU,QAAQ,CAACK;QACnBL,UAAU,QAAQ,CAACM;QACnBN,UAAU,QAAQ,CAACO;QACnBP,UAAU,QAAQ,CAACQ;QACnBR,UAAU,QAAQ,CAACS;QACnBT,UAAU,QAAQ,CAACU;QACnBV,UAAU,QAAQ,CAACW;IACvB;AACJ"}
@@ -0,0 +1,12 @@
1
+ import { ResolveIdentityTenantsUseCase as Abstraction } from "./abstractions.js";
2
+ import { GetTenantByIdUseCase } from "../../../features/tenancy/GetTenantById/index.js";
3
+ import { FindUserRecordsUseCase } from "../../../features/users/FindUserRecords/index.js";
4
+ declare class ResolveIdentityTenantsUseCaseImpl implements Abstraction.Interface {
5
+ private findUserRecords;
6
+ private getTenantById;
7
+ constructor(findUserRecords: FindUserRecordsUseCase.Interface, getTenantById: GetTenantByIdUseCase.Interface);
8
+ execute(identityId: string): Abstraction.Return;
9
+ private loadTenant;
10
+ }
11
+ export declare const ResolveIdentityTenantsUseCase: import("@webiny/di").Implementation<typeof ResolveIdentityTenantsUseCaseImpl>;
12
+ export {};
@@ -0,0 +1,46 @@
1
+ import { Result, createImplementation } from "@webiny/feature/api";
2
+ import { ResolveIdentityTenantsUseCase } from "./abstractions.js";
3
+ import { GetTenantByIdUseCase } from "../../tenancy/GetTenantById/index.js";
4
+ import { FindUserRecordsUseCase } from "../../users/FindUserRecords/index.js";
5
+ import { UserStorageError } from "../../users/shared/errors.js";
6
+ class ResolveIdentityTenantsUseCaseImpl {
7
+ constructor(findUserRecords, getTenantById){
8
+ this.findUserRecords = findUserRecords;
9
+ this.getTenantById = getTenantById;
10
+ }
11
+ async execute(identityId) {
12
+ const usersResult = await this.findUserRecords.execute(identityId);
13
+ if (usersResult.isFail()) return Result.fail(usersResult.error);
14
+ const tenantIds = [
15
+ ...new Set(usersResult.value.map((user)=>user.tenant))
16
+ ];
17
+ let tenants;
18
+ try {
19
+ tenants = await Promise.all(tenantIds.map((id)=>this.loadTenant(id)));
20
+ } catch (error) {
21
+ return Result.fail(new UserStorageError(error));
22
+ }
23
+ const identityTenants = tenants.filter((tenant)=>!!tenant && tenant.isInstalled && "disabled" !== tenant.status).map((tenant)=>({
24
+ id: tenant.id,
25
+ name: tenant.name
26
+ })).sort((a, b)=>a.name.localeCompare(b.name));
27
+ return Result.ok(identityTenants);
28
+ }
29
+ async loadTenant(id) {
30
+ const result = await this.getTenantById.execute(id);
31
+ if (result.isOk()) return result.value;
32
+ if ("NOT_FOUND" === result.error.type) return null;
33
+ throw result.error.cause;
34
+ }
35
+ }
36
+ const ResolveIdentityTenantsUseCase_ResolveIdentityTenantsUseCase = createImplementation({
37
+ abstraction: ResolveIdentityTenantsUseCase,
38
+ implementation: ResolveIdentityTenantsUseCaseImpl,
39
+ dependencies: [
40
+ FindUserRecordsUseCase,
41
+ GetTenantByIdUseCase
42
+ ]
43
+ });
44
+ export { ResolveIdentityTenantsUseCase_ResolveIdentityTenantsUseCase as ResolveIdentityTenantsUseCase };
45
+
46
+ //# sourceMappingURL=ResolveIdentityTenantsUseCase.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"features/security/ResolveIdentityTenants/ResolveIdentityTenantsUseCase.js","sources":["../../../../src/features/security/ResolveIdentityTenants/ResolveIdentityTenantsUseCase.ts"],"sourcesContent":["import { createImplementation } from \"@webiny/feature/api\";\nimport { Result } from \"@webiny/feature/api\";\nimport { ResolveIdentityTenantsUseCase as Abstraction } from \"./abstractions.js\";\nimport { GetTenantByIdUseCase } from \"~/features/tenancy/GetTenantById/index.js\";\nimport { FindUserRecordsUseCase } from \"~/features/users/FindUserRecords/index.js\";\nimport { UserStorageError } from \"~/features/users/shared/errors.js\";\nimport type { Tenant } from \"~/types/tenancy.js\";\n\nclass ResolveIdentityTenantsUseCaseImpl implements Abstraction.Interface {\n constructor(\n private findUserRecords: FindUserRecordsUseCase.Interface,\n private getTenantById: GetTenantByIdUseCase.Interface\n ) {}\n\n async execute(identityId: string): Abstraction.Return {\n const usersResult = await this.findUserRecords.execute(identityId);\n if (usersResult.isFail()) {\n return Result.fail(usersResult.error);\n }\n\n const tenantIds = [...new Set(usersResult.value.map(user => user.tenant))];\n\n let tenants: Array<Tenant | null>;\n try {\n tenants = await Promise.all(tenantIds.map(id => this.loadTenant(id)));\n } catch (error) {\n return Result.fail(new UserStorageError(error as Error));\n }\n\n const identityTenants = tenants\n // Disabled tenants can't be logged into, and uninstalled tenants have no users yet.\n .filter((tenant): tenant is Tenant => {\n return !!tenant && tenant.isInstalled && tenant.status !== \"disabled\";\n })\n .map(tenant => ({ id: tenant.id, name: tenant.name }))\n .sort((a, b) => a.name.localeCompare(b.name));\n\n return Result.ok(identityTenants);\n }\n\n private async loadTenant(id: string): Promise<Tenant | null> {\n const result = await this.getTenantById.execute(id);\n if (result.isOk()) {\n return result.value;\n }\n\n // A record left behind by a deleted tenant.\n if (result.error.type === \"NOT_FOUND\") {\n return null;\n }\n\n throw result.error.cause;\n }\n}\n\nexport const ResolveIdentityTenantsUseCase = createImplementation({\n abstraction: Abstraction,\n implementation: ResolveIdentityTenantsUseCaseImpl,\n dependencies: [FindUserRecordsUseCase, GetTenantByIdUseCase]\n});\n"],"names":["ResolveIdentityTenantsUseCaseImpl","findUserRecords","getTenantById","identityId","usersResult","Result","tenantIds","Set","user","tenants","Promise","id","error","UserStorageError","identityTenants","tenant","a","b","result","ResolveIdentityTenantsUseCase","createImplementation","Abstraction","FindUserRecordsUseCase","GetTenantByIdUseCase"],"mappings":";;;;;AAQA,MAAMA;IACF,YACYC,eAAiD,EACjDC,aAA6C,CACvD;aAFUD,eAAe,GAAfA;aACAC,aAAa,GAAbA;IACT;IAEH,MAAM,QAAQC,UAAkB,EAAsB;QAClD,MAAMC,cAAc,MAAM,IAAI,CAAC,eAAe,CAAC,OAAO,CAACD;QACvD,IAAIC,YAAY,MAAM,IAClB,OAAOC,OAAO,IAAI,CAACD,YAAY,KAAK;QAGxC,MAAME,YAAY;eAAI,IAAIC,IAAIH,YAAY,KAAK,CAAC,GAAG,CAACI,CAAAA,OAAQA,KAAK,MAAM;SAAG;QAE1E,IAAIC;QACJ,IAAI;YACAA,UAAU,MAAMC,QAAQ,GAAG,CAACJ,UAAU,GAAG,CAACK,CAAAA,KAAM,IAAI,CAAC,UAAU,CAACA;QACpE,EAAE,OAAOC,OAAO;YACZ,OAAOP,OAAO,IAAI,CAAC,IAAIQ,iBAAiBD;QAC5C;QAEA,MAAME,kBAAkBL,QAEnB,MAAM,CAAC,CAACM,SACE,CAAC,CAACA,UAAUA,OAAO,WAAW,IAAIA,AAAkB,eAAlBA,OAAO,MAAM,EAEzD,GAAG,CAACA,CAAAA,SAAW;gBAAE,IAAIA,OAAO,EAAE;gBAAE,MAAMA,OAAO,IAAI;YAAC,IAClD,IAAI,CAAC,CAACC,GAAGC,IAAMD,EAAE,IAAI,CAAC,aAAa,CAACC,EAAE,IAAI;QAE/C,OAAOZ,OAAO,EAAE,CAACS;IACrB;IAEA,MAAc,WAAWH,EAAU,EAA0B;QACzD,MAAMO,SAAS,MAAM,IAAI,CAAC,aAAa,CAAC,OAAO,CAACP;QAChD,IAAIO,OAAO,IAAI,IACX,OAAOA,OAAO,KAAK;QAIvB,IAAIA,AAAsB,gBAAtBA,OAAO,KAAK,CAAC,IAAI,EACjB,OAAO;QAGX,MAAMA,OAAO,KAAK,CAAC,KAAK;IAC5B;AACJ;AAEO,MAAMC,8DAAgCC,qBAAqB;IAC9D,aAAaC;IACb,gBAAgBrB;IAChB,cAAc;QAACsB;QAAwBC;KAAqB;AAChE"}
@@ -0,0 +1,22 @@
1
+ import { Result } from "@webiny/feature/api";
2
+ import { AdminUsersRepository } from "../../../features/users/shared/abstractions.js";
3
+ export interface IdentityTenant {
4
+ id: string;
5
+ name: string;
6
+ }
7
+ type ResolveIdentityTenantsError = AdminUsersRepository.Error;
8
+ export interface IResolveIdentityTenantsUseCase {
9
+ /**
10
+ * List the enabled, installed tenants in which the identity has a user record.
11
+ */
12
+ execute(identityId: string): Promise<Result<IdentityTenant[], ResolveIdentityTenantsError>>;
13
+ }
14
+ /** Find the tenants an identity can log into. */
15
+ export declare const ResolveIdentityTenantsUseCase: import("@webiny/di").Abstraction<IResolveIdentityTenantsUseCase>;
16
+ export declare namespace ResolveIdentityTenantsUseCase {
17
+ type Interface = IResolveIdentityTenantsUseCase;
18
+ type Tenant = IdentityTenant;
19
+ type Error = ResolveIdentityTenantsError;
20
+ type Return = Promise<Result<IdentityTenant[], ResolveIdentityTenantsError>>;
21
+ }
22
+ export {};
@@ -0,0 +1,5 @@
1
+ import { createAbstraction } from "@webiny/feature/api";
2
+ const ResolveIdentityTenantsUseCase = createAbstraction("ResolveIdentityTenantsUseCase");
3
+ export { ResolveIdentityTenantsUseCase };
4
+
5
+ //# sourceMappingURL=abstractions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"features/security/ResolveIdentityTenants/abstractions.js","sources":["../../../../src/features/security/ResolveIdentityTenants/abstractions.ts"],"sourcesContent":["import { createAbstraction } from \"@webiny/feature/api\";\nimport { Result } from \"@webiny/feature/api\";\nimport { AdminUsersRepository } from \"~/features/users/shared/abstractions.js\";\n\nexport interface IdentityTenant {\n id: string;\n name: string;\n}\n\ntype ResolveIdentityTenantsError = AdminUsersRepository.Error;\n\nexport interface IResolveIdentityTenantsUseCase {\n /**\n * List the enabled, installed tenants in which the identity has a user record.\n */\n execute(identityId: string): Promise<Result<IdentityTenant[], ResolveIdentityTenantsError>>;\n}\n\n/** Find the tenants an identity can log into. */\nexport const ResolveIdentityTenantsUseCase = createAbstraction<IResolveIdentityTenantsUseCase>(\n \"ResolveIdentityTenantsUseCase\"\n);\n\nexport namespace ResolveIdentityTenantsUseCase {\n export type Interface = IResolveIdentityTenantsUseCase;\n export type Tenant = IdentityTenant;\n export type Error = ResolveIdentityTenantsError;\n export type Return = Promise<Result<IdentityTenant[], ResolveIdentityTenantsError>>;\n}\n"],"names":["ResolveIdentityTenantsUseCase","createAbstraction"],"mappings":";AAmBO,MAAMA,gCAAgCC,kBACzC"}
@@ -0,0 +1,4 @@
1
+ export declare const ResolveIdentityTenantsFeature: {
2
+ name: string;
3
+ register(container: import("@webiny/di").Container): void;
4
+ };
@@ -0,0 +1,11 @@
1
+ import { createFeature } from "@webiny/feature/api";
2
+ import { ResolveIdentityTenantsUseCase } from "./ResolveIdentityTenantsUseCase.js";
3
+ const ResolveIdentityTenantsFeature = createFeature({
4
+ name: "ResolveIdentityTenants",
5
+ register (container) {
6
+ container.register(ResolveIdentityTenantsUseCase);
7
+ }
8
+ });
9
+ export { ResolveIdentityTenantsFeature };
10
+
11
+ //# sourceMappingURL=feature.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"features/security/ResolveIdentityTenants/feature.js","sources":["../../../../src/features/security/ResolveIdentityTenants/feature.ts"],"sourcesContent":["import { createFeature } from \"@webiny/feature/api\";\nimport { ResolveIdentityTenantsUseCase } from \"./ResolveIdentityTenantsUseCase.js\";\n\nexport const ResolveIdentityTenantsFeature = createFeature({\n name: \"ResolveIdentityTenants\",\n register(container) {\n container.register(ResolveIdentityTenantsUseCase);\n }\n});\n"],"names":["ResolveIdentityTenantsFeature","createFeature","container","ResolveIdentityTenantsUseCase"],"mappings":";;AAGO,MAAMA,gCAAgCC,cAAc;IACvD,MAAM;IACN,UAASC,SAAS;QACdA,UAAU,QAAQ,CAACC;IACvB;AACJ"}
@@ -0,0 +1 @@
1
+ export { ResolveIdentityTenantsUseCase } from "./abstractions.js";
@@ -0,0 +1 @@
1
+ export { ResolveIdentityTenantsUseCase } from "./abstractions.js";
@@ -7,6 +7,8 @@ import { IdentityContextFeature } from "./IdentityContext/index.js";
7
7
  import { ApiKeysFeature } from "./apiKeys/feature.js";
8
8
  import { TeamsFeature } from "./teams/feature.js";
9
9
  import { RolesFeature } from "./roles/feature.js";
10
+ import { ResolveIdentityTenantsFeature } from "./ResolveIdentityTenants/feature.js";
11
+ import { LogInFeature } from "./login/feature.js";
10
12
  const SecurityFeature = createFeature({
11
13
  name: "SecurityFeature",
12
14
  register (container, storageOperations) {
@@ -18,6 +20,8 @@ const SecurityFeature = createFeature({
18
20
  RolesFeature.register(container);
19
21
  TeamsFeature.register(container);
20
22
  GroupsTeamsAuthorizerFeature.register(container);
23
+ ResolveIdentityTenantsFeature.register(container);
24
+ LogInFeature.register(container);
21
25
  }
22
26
  });
23
27
  export { SecurityFeature };
@@ -1 +1 @@
1
- {"version":3,"file":"features/security/SecurityFeature.js","sources":["../../../src/features/security/SecurityFeature.ts"],"sourcesContent":["import { createFeature } from \"@webiny/feature/api\";\nimport type { Container } from \"@webiny/di\";\nimport type { SecurityStorageOperations as ISecurityStorageOperations } from \"~/types/security.js\";\nimport { SecurityStorageOperations } from \"./shared/abstractions.js\";\nimport { AuthenticationContextFeature } from \"./authentication/AuthenticationContext/index.js\";\nimport { AuthorizationContextFeature } from \"./authorization/AuthorizationContext/index.js\";\nimport { GroupsTeamsAuthorizerFeature } from \"./authorization/GroupsTeamsAuthorizer/feature.js\";\nimport { IdentityContextFeature } from \"./IdentityContext/index.js\";\nimport { ApiKeysFeature } from \"./apiKeys/feature.js\";\nimport { TeamsFeature } from \"./teams/feature.js\";\nimport { RolesFeature } from \"~/features/security/roles/feature.js\";\n\n/**\n * Setup all security features in the DI container.\n * This is the main entry point for registering all security-related features.\n *\n * @param container - DI container instance\n * @param storageOperations - Storage operations implementation\n */\nexport const SecurityFeature = createFeature({\n name: \"SecurityFeature\",\n register(container: Container, storageOperations: ISecurityStorageOperations) {\n // Register storage operations abstraction (singleton)\n container.registerInstance(SecurityStorageOperations, storageOperations);\n\n AuthorizationContextFeature.register(container);\n AuthenticationContextFeature.register(container);\n IdentityContextFeature.register(container);\n ApiKeysFeature.register(container);\n RolesFeature.register(container);\n TeamsFeature.register(container);\n GroupsTeamsAuthorizerFeature.register(container);\n }\n});\n"],"names":["SecurityFeature","createFeature","container","storageOperations","SecurityStorageOperations","AuthorizationContextFeature","AuthenticationContextFeature","IdentityContextFeature","ApiKeysFeature","RolesFeature","TeamsFeature","GroupsTeamsAuthorizerFeature"],"mappings":";;;;;;;;;AAmBO,MAAMA,kBAAkBC,cAAc;IACzC,MAAM;IACN,UAASC,SAAoB,EAAEC,iBAA6C;QAExED,UAAU,gBAAgB,CAACE,2BAA2BD;QAEtDE,4BAA4B,QAAQ,CAACH;QACrCI,6BAA6B,QAAQ,CAACJ;QACtCK,uBAAuB,QAAQ,CAACL;QAChCM,eAAe,QAAQ,CAACN;QACxBO,aAAa,QAAQ,CAACP;QACtBQ,aAAa,QAAQ,CAACR;QACtBS,6BAA6B,QAAQ,CAACT;IAC1C;AACJ"}
1
+ {"version":3,"file":"features/security/SecurityFeature.js","sources":["../../../src/features/security/SecurityFeature.ts"],"sourcesContent":["import { createFeature } from \"@webiny/feature/api\";\nimport type { Container } from \"@webiny/di\";\nimport type { SecurityStorageOperations as ISecurityStorageOperations } from \"~/types/security.js\";\nimport { SecurityStorageOperations } from \"./shared/abstractions.js\";\nimport { AuthenticationContextFeature } from \"./authentication/AuthenticationContext/index.js\";\nimport { AuthorizationContextFeature } from \"./authorization/AuthorizationContext/index.js\";\nimport { GroupsTeamsAuthorizerFeature } from \"./authorization/GroupsTeamsAuthorizer/feature.js\";\nimport { IdentityContextFeature } from \"./IdentityContext/index.js\";\nimport { ApiKeysFeature } from \"./apiKeys/feature.js\";\nimport { TeamsFeature } from \"./teams/feature.js\";\nimport { RolesFeature } from \"~/features/security/roles/feature.js\";\nimport { ResolveIdentityTenantsFeature } from \"./ResolveIdentityTenants/feature.js\";\nimport { LogInFeature } from \"./login/feature.js\";\n\n/**\n * Setup all security features in the DI container.\n * This is the main entry point for registering all security-related features.\n *\n * @param container - DI container instance\n * @param storageOperations - Storage operations implementation\n */\nexport const SecurityFeature = createFeature({\n name: \"SecurityFeature\",\n register(container: Container, storageOperations: ISecurityStorageOperations) {\n // Register storage operations abstraction (singleton)\n container.registerInstance(SecurityStorageOperations, storageOperations);\n\n AuthorizationContextFeature.register(container);\n AuthenticationContextFeature.register(container);\n IdentityContextFeature.register(container);\n ApiKeysFeature.register(container);\n RolesFeature.register(container);\n TeamsFeature.register(container);\n GroupsTeamsAuthorizerFeature.register(container);\n ResolveIdentityTenantsFeature.register(container);\n LogInFeature.register(container);\n }\n});\n"],"names":["SecurityFeature","createFeature","container","storageOperations","SecurityStorageOperations","AuthorizationContextFeature","AuthenticationContextFeature","IdentityContextFeature","ApiKeysFeature","RolesFeature","TeamsFeature","GroupsTeamsAuthorizerFeature","ResolveIdentityTenantsFeature","LogInFeature"],"mappings":";;;;;;;;;;;AAqBO,MAAMA,kBAAkBC,cAAc;IACzC,MAAM;IACN,UAASC,SAAoB,EAAEC,iBAA6C;QAExED,UAAU,gBAAgB,CAACE,2BAA2BD;QAEtDE,4BAA4B,QAAQ,CAACH;QACrCI,6BAA6B,QAAQ,CAACJ;QACtCK,uBAAuB,QAAQ,CAACL;QAChCM,eAAe,QAAQ,CAACN;QACxBO,aAAa,QAAQ,CAACP;QACtBQ,aAAa,QAAQ,CAACR;QACtBS,6BAA6B,QAAQ,CAACT;QACtCU,8BAA8B,QAAQ,CAACV;QACvCW,aAAa,QAAQ,CAACX;IAC1B;AACJ"}
@@ -0,0 +1,20 @@
1
+ import { EventPublisher } from "../../../features/eventPublisher/index.js";
2
+ import { IdentityContext } from "../../../features/security/IdentityContext/index.js";
3
+ import { TenantContext } from "../../../features/tenancy/TenantContext/index.js";
4
+ import { GetIdentityProfileUseCase } from "../../../features/users/GetIdentityProfile/index.js";
5
+ import { ResolveIdentityTenantsUseCase } from "../../../features/security/ResolveIdentityTenants/abstractions.js";
6
+ import { LogInUseCase as UseCaseAbstraction } from "./abstractions.js";
7
+ declare class LogInUseCaseImpl implements UseCaseAbstraction.Interface {
8
+ private identityContext;
9
+ private tenantContext;
10
+ private eventPublisher;
11
+ private getIdentityProfile;
12
+ private resolveIdentityTenants;
13
+ constructor(identityContext: IdentityContext.Interface, tenantContext: TenantContext.Interface, eventPublisher: EventPublisher.Interface, getIdentityProfile: GetIdentityProfileUseCase.Interface, resolveIdentityTenants: ResolveIdentityTenantsUseCase.Interface);
14
+ execute(): UseCaseAbstraction.Return;
15
+ private listIdentityTenants;
16
+ }
17
+ export declare const LogInUseCase: typeof LogInUseCaseImpl & {
18
+ __abstraction: import("@webiny/di").Abstraction<import("./abstractions.js").ILogInUseCase>;
19
+ };
20
+ export {};
@@ -0,0 +1,75 @@
1
+ import { Result } from "@webiny/feature/api";
2
+ import { EventPublisher } from "../../eventPublisher/index.js";
3
+ import { IdentityContext } from "../IdentityContext/index.js";
4
+ import { TenantContext } from "../../tenancy/TenantContext/index.js";
5
+ import { GetIdentityProfileUseCase } from "../../users/GetIdentityProfile/index.js";
6
+ import { UserNotFoundError, UserStorageError } from "../../users/shared/errors.js";
7
+ import { ResolveIdentityTenantsUseCase } from "../ResolveIdentityTenants/abstractions.js";
8
+ import { AfterLoginEvent, LogInUseCase } from "./abstractions.js";
9
+ import { LogInMissingProfileError, LogInNotAuthenticatedError, LogInTenantAccessDeniedError } from "./errors.js";
10
+ class LogInUseCaseImpl {
11
+ constructor(identityContext, tenantContext, eventPublisher, getIdentityProfile, resolveIdentityTenants){
12
+ this.identityContext = identityContext;
13
+ this.tenantContext = tenantContext;
14
+ this.eventPublisher = eventPublisher;
15
+ this.getIdentityProfile = getIdentityProfile;
16
+ this.resolveIdentityTenants = resolveIdentityTenants;
17
+ }
18
+ async execute() {
19
+ const identity = this.identityContext.getIdentity();
20
+ if (identity.isAnonymous()) return Result.fail(new LogInNotAuthenticatedError());
21
+ const isExternal = identity.profile.external;
22
+ if (isExternal) {
23
+ if (false === identity.context.canAccessTenant) return Result.fail(new LogInTenantAccessDeniedError());
24
+ await this.eventPublisher.publish(new AfterLoginEvent({
25
+ identity
26
+ }));
27
+ }
28
+ const profileResult = await this.identityContext.withoutAuthorization(async ()=>this.getIdentityProfile.execute(identity.id));
29
+ if (profileResult.isFail()) {
30
+ if (!isExternal) {
31
+ if (profileResult.error instanceof UserStorageError) return Result.fail(profileResult.error);
32
+ if (profileResult.error instanceof UserNotFoundError) {
33
+ const tenantsResult = await this.listIdentityTenants(identity.id);
34
+ if (tenantsResult.isFail()) return Result.fail(tenantsResult.error);
35
+ const tenants = tenantsResult.value;
36
+ if (tenants.length > 0) return Result.ok({
37
+ type: "tenantSelectionRequired",
38
+ tenants
39
+ });
40
+ }
41
+ }
42
+ return Result.fail(new LogInMissingProfileError());
43
+ }
44
+ if (!isExternal) await this.eventPublisher.publish(new AfterLoginEvent({
45
+ identity
46
+ }));
47
+ return Result.ok({
48
+ type: "loggedIn",
49
+ identity,
50
+ user: profileResult.value
51
+ });
52
+ }
53
+ async listIdentityTenants(identityId) {
54
+ const result = await this.identityContext.withoutAuthorization(async ()=>this.resolveIdentityTenants.execute(identityId));
55
+ if (result.isFail()) {
56
+ if (result.error instanceof UserStorageError) return Result.fail(result.error);
57
+ return Result.ok([]);
58
+ }
59
+ const currentTenantId = this.tenantContext.getTenant().id;
60
+ return Result.ok(result.value.filter((tenant)=>tenant.id !== currentTenantId));
61
+ }
62
+ }
63
+ const LogInUseCase_LogInUseCase = LogInUseCase.createImplementation({
64
+ implementation: LogInUseCaseImpl,
65
+ dependencies: [
66
+ IdentityContext,
67
+ TenantContext,
68
+ EventPublisher,
69
+ GetIdentityProfileUseCase,
70
+ ResolveIdentityTenantsUseCase
71
+ ]
72
+ });
73
+ export { LogInUseCase_LogInUseCase as LogInUseCase };
74
+
75
+ //# sourceMappingURL=LogInUseCase.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"features/security/login/LogInUseCase.js","sources":["../../../../src/features/security/login/LogInUseCase.ts"],"sourcesContent":["import { Result } from \"@webiny/feature/api\";\nimport { EventPublisher } from \"~/features/eventPublisher/index.js\";\nimport { IdentityContext } from \"~/features/security/IdentityContext/index.js\";\nimport { TenantContext } from \"~/features/tenancy/TenantContext/index.js\";\nimport { GetIdentityProfileUseCase } from \"~/features/users/GetIdentityProfile/index.js\";\nimport { UserNotFoundError, UserStorageError } from \"~/features/users/shared/errors.js\";\nimport { ResolveIdentityTenantsUseCase } from \"~/features/security/ResolveIdentityTenants/abstractions.js\";\nimport { AfterLoginEvent, LogInUseCase as UseCaseAbstraction } from \"./abstractions.js\";\nimport {\n LogInMissingProfileError,\n LogInNotAuthenticatedError,\n LogInTenantAccessDeniedError\n} from \"./errors.js\";\n\nclass LogInUseCaseImpl implements UseCaseAbstraction.Interface {\n constructor(\n private identityContext: IdentityContext.Interface,\n private tenantContext: TenantContext.Interface,\n private eventPublisher: EventPublisher.Interface,\n private getIdentityProfile: GetIdentityProfileUseCase.Interface,\n private resolveIdentityTenants: ResolveIdentityTenantsUseCase.Interface\n ) {}\n\n async execute(): UseCaseAbstraction.Return {\n const identity = this.identityContext.getIdentity();\n if (identity.isAnonymous()) {\n return Result.fail(new LogInNotAuthenticatedError());\n }\n\n /**\n * External identities are provisioned by the AfterLogin handlers, so the profile is\n * loaded after them. Native identities must have a profile before AfterLogin runs:\n * without one, they get the list of their tenants instead.\n */\n const isExternal = identity.profile.external;\n\n if (isExternal) {\n // The IdP decides tenant access for its identities. Without access, AfterLogin must\n // not run, or the user sync would create a record in a tenant they can't use.\n if (identity.context.canAccessTenant === false) {\n return Result.fail(new LogInTenantAccessDeniedError());\n }\n\n await this.eventPublisher.publish(new AfterLoginEvent({ identity }));\n }\n\n const profileResult = await this.identityContext.withoutAuthorization(async () => {\n return this.getIdentityProfile.execute(identity.id);\n });\n\n if (profileResult.isFail()) {\n if (!isExternal) {\n // A failed read says nothing about where the user belongs, so it fails the login.\n if (profileResult.error instanceof UserStorageError) {\n return Result.fail(profileResult.error);\n }\n\n if (profileResult.error instanceof UserNotFoundError) {\n const tenantsResult = await this.listIdentityTenants(identity.id);\n if (tenantsResult.isFail()) {\n return Result.fail(tenantsResult.error);\n }\n\n const tenants = tenantsResult.value;\n if (tenants.length > 0) {\n return Result.ok({ type: \"tenantSelectionRequired\", tenants });\n }\n }\n }\n\n return Result.fail(new LogInMissingProfileError());\n }\n\n if (!isExternal) {\n await this.eventPublisher.publish(new AfterLoginEvent({ identity }));\n }\n\n return Result.ok({ type: \"loggedIn\", identity, user: profileResult.value });\n }\n\n private async listIdentityTenants(\n identityId: string\n ): Promise<Result<ResolveIdentityTenantsUseCase.Tenant[], UserStorageError>> {\n const result = await this.identityContext.withoutAuthorization(async () => {\n return this.resolveIdentityTenants.execute(identityId);\n });\n\n if (result.isFail()) {\n // Like the profile read, a failed lookup fails the login instead of saying \"no access\".\n if (result.error instanceof UserStorageError) {\n return Result.fail(result.error);\n }\n return Result.ok([]);\n }\n\n // The profile lookup already covered the current tenant, so offering it would loop.\n const currentTenantId = this.tenantContext.getTenant().id;\n return Result.ok(result.value.filter(tenant => tenant.id !== currentTenantId));\n }\n}\n\nexport const LogInUseCase = UseCaseAbstraction.createImplementation({\n implementation: LogInUseCaseImpl,\n dependencies: [\n IdentityContext,\n TenantContext,\n EventPublisher,\n GetIdentityProfileUseCase,\n ResolveIdentityTenantsUseCase\n ]\n});\n"],"names":["LogInUseCaseImpl","identityContext","tenantContext","eventPublisher","getIdentityProfile","resolveIdentityTenants","identity","Result","LogInNotAuthenticatedError","isExternal","LogInTenantAccessDeniedError","AfterLoginEvent","profileResult","UserStorageError","UserNotFoundError","tenantsResult","tenants","LogInMissingProfileError","identityId","result","currentTenantId","tenant","LogInUseCase","UseCaseAbstraction","IdentityContext","TenantContext","EventPublisher","GetIdentityProfileUseCase","ResolveIdentityTenantsUseCase"],"mappings":";;;;;;;;;AAcA,MAAMA;IACF,YACYC,eAA0C,EAC1CC,aAAsC,EACtCC,cAAwC,EACxCC,kBAAuD,EACvDC,sBAA+D,CACzE;aALUJ,eAAe,GAAfA;aACAC,aAAa,GAAbA;aACAC,cAAc,GAAdA;aACAC,kBAAkB,GAAlBA;aACAC,sBAAsB,GAAtBA;IACT;IAEH,MAAM,UAAqC;QACvC,MAAMC,WAAW,IAAI,CAAC,eAAe,CAAC,WAAW;QACjD,IAAIA,SAAS,WAAW,IACpB,OAAOC,OAAO,IAAI,CAAC,IAAIC;QAQ3B,MAAMC,aAAaH,SAAS,OAAO,CAAC,QAAQ;QAE5C,IAAIG,YAAY;YAGZ,IAAIH,AAAqC,UAArCA,SAAS,OAAO,CAAC,eAAe,EAChC,OAAOC,OAAO,IAAI,CAAC,IAAIG;YAG3B,MAAM,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,IAAIC,gBAAgB;gBAAEL;YAAS;QACrE;QAEA,MAAMM,gBAAgB,MAAM,IAAI,CAAC,eAAe,CAAC,oBAAoB,CAAC,UAC3D,IAAI,CAAC,kBAAkB,CAAC,OAAO,CAACN,SAAS,EAAE;QAGtD,IAAIM,cAAc,MAAM,IAAI;YACxB,IAAI,CAACH,YAAY;gBAEb,IAAIG,cAAc,KAAK,YAAYC,kBAC/B,OAAON,OAAO,IAAI,CAACK,cAAc,KAAK;gBAG1C,IAAIA,cAAc,KAAK,YAAYE,mBAAmB;oBAClD,MAAMC,gBAAgB,MAAM,IAAI,CAAC,mBAAmB,CAACT,SAAS,EAAE;oBAChE,IAAIS,cAAc,MAAM,IACpB,OAAOR,OAAO,IAAI,CAACQ,cAAc,KAAK;oBAG1C,MAAMC,UAAUD,cAAc,KAAK;oBACnC,IAAIC,QAAQ,MAAM,GAAG,GACjB,OAAOT,OAAO,EAAE,CAAC;wBAAE,MAAM;wBAA2BS;oBAAQ;gBAEpE;YACJ;YAEA,OAAOT,OAAO,IAAI,CAAC,IAAIU;QAC3B;QAEA,IAAI,CAACR,YACD,MAAM,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,IAAIE,gBAAgB;YAAEL;QAAS;QAGrE,OAAOC,OAAO,EAAE,CAAC;YAAE,MAAM;YAAYD;YAAU,MAAMM,cAAc,KAAK;QAAC;IAC7E;IAEA,MAAc,oBACVM,UAAkB,EACuD;QACzE,MAAMC,SAAS,MAAM,IAAI,CAAC,eAAe,CAAC,oBAAoB,CAAC,UACpD,IAAI,CAAC,sBAAsB,CAAC,OAAO,CAACD;QAG/C,IAAIC,OAAO,MAAM,IAAI;YAEjB,IAAIA,OAAO,KAAK,YAAYN,kBACxB,OAAON,OAAO,IAAI,CAACY,OAAO,KAAK;YAEnC,OAAOZ,OAAO,EAAE,CAAC,EAAE;QACvB;QAGA,MAAMa,kBAAkB,IAAI,CAAC,aAAa,CAAC,SAAS,GAAG,EAAE;QACzD,OAAOb,OAAO,EAAE,CAACY,OAAO,KAAK,CAAC,MAAM,CAACE,CAAAA,SAAUA,OAAO,EAAE,KAAKD;IACjE;AACJ;AAEO,MAAME,4BAAeC,aAAAA,oBAAuC,CAAC;IAChE,gBAAgBvB;IAChB,cAAc;QACVwB;QACAC;QACAC;QACAC;QACAC;KACH;AACL"}
@@ -1,6 +1,11 @@
1
1
  import { DomainEvent } from "../../../features/eventPublisher/index.js";
2
2
  import type { IEventHandler } from "../../../features/eventPublisher/index.js";
3
+ import { Result } from "@webiny/feature/api";
3
4
  import { Identity } from "../../../features/security/IdentityContext/index.js";
5
+ import type { AdminUser } from "../../../features/users/shared/types.js";
6
+ import type { UserStorageError } from "../../../features/users/shared/errors.js";
7
+ import type { IdentityTenant } from "../../../features/security/ResolveIdentityTenants/abstractions.js";
8
+ import type { LogInNotAuthenticatedError, LogInMissingProfileError, LogInTenantAccessDeniedError } from "./errors.js";
4
9
  export interface AfterLoginPayload {
5
10
  identity: Identity;
6
11
  }
@@ -14,3 +19,43 @@ export declare namespace AfterLoginEventHandler {
14
19
  type Interface = IEventHandler<AfterLoginEvent>;
15
20
  type Event = AfterLoginEvent;
16
21
  }
22
+ /** The identity has a profile in the current tenant (or its parent), and is logged in. */
23
+ export interface LogInLoggedIn {
24
+ type: "loggedIn";
25
+ identity: Identity;
26
+ user: AdminUser;
27
+ }
28
+ /**
29
+ * A native identity has no profile in the current tenant, but has one in these tenants.
30
+ * The client picks one and logs in there.
31
+ */
32
+ export interface LogInTenantSelectionRequired {
33
+ type: "tenantSelectionRequired";
34
+ tenants: IdentityTenant[];
35
+ }
36
+ export type LogInOutput = LogInLoggedIn | LogInTenantSelectionRequired;
37
+ export interface ILogInUseCaseErrors {
38
+ notAuthenticated: LogInNotAuthenticatedError;
39
+ tenantAccessDenied: LogInTenantAccessDeniedError;
40
+ missingProfile: LogInMissingProfileError;
41
+ storage: UserStorageError;
42
+ }
43
+ type LogInError = ILogInUseCaseErrors[keyof ILogInUseCaseErrors];
44
+ export interface ILogInUseCase {
45
+ /**
46
+ * Log in the current identity: load its user profile and publish `AfterLoginEvent`.
47
+ * A native identity without a profile in the current tenant gets the tenants it can log into.
48
+ */
49
+ execute(): Promise<Result<LogInOutput, LogInError>>;
50
+ }
51
+ /** Log in the current identity. */
52
+ export declare const LogInUseCase: import("@webiny/di").Abstraction<ILogInUseCase>;
53
+ export declare namespace LogInUseCase {
54
+ type Interface = ILogInUseCase;
55
+ type Output = LogInOutput;
56
+ type LoggedIn = LogInLoggedIn;
57
+ type TenantSelectionRequired = LogInTenantSelectionRequired;
58
+ type Error = LogInError;
59
+ type Return = Promise<Result<LogInOutput, LogInError>>;
60
+ }
61
+ export {};
@@ -9,6 +9,7 @@ class AfterLoginEvent extends DomainEvent {
9
9
  }
10
10
  }
11
11
  const AfterLoginEventHandler = createAbstraction("AfterLoginEventHandler");
12
- export { AfterLoginEvent, AfterLoginEventHandler };
12
+ const LogInUseCase = createAbstraction("LogInUseCase");
13
+ export { AfterLoginEvent, AfterLoginEventHandler, LogInUseCase };
13
14
 
14
15
  //# sourceMappingURL=abstractions.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"features/security/login/abstractions.js","sources":["../../../../src/features/security/login/abstractions.ts"],"sourcesContent":["import { DomainEvent } from \"~/features/eventPublisher/index.js\";\nimport type { IEventHandler } from \"~/features/eventPublisher/index.js\";\nimport { createAbstraction } from \"@webiny/feature/api\";\nimport { Identity } from \"~/features/security/IdentityContext/index.js\";\n\nexport interface AfterLoginPayload {\n identity: Identity;\n}\n\nexport class AfterLoginEvent extends DomainEvent<AfterLoginPayload> {\n eventType = \"login.afterLogin\" as const;\n\n getHandlerAbstraction() {\n return AfterLoginEventHandler;\n }\n}\n\n/** Hook into login lifecycle after a user logs in. */\nexport const AfterLoginEventHandler =\n createAbstraction<IEventHandler<AfterLoginEvent>>(\"AfterLoginEventHandler\");\n\nexport namespace AfterLoginEventHandler {\n export type Interface = IEventHandler<AfterLoginEvent>;\n export type Event = AfterLoginEvent;\n}\n"],"names":["AfterLoginEvent","DomainEvent","AfterLoginEventHandler","createAbstraction"],"mappings":";;AASO,MAAMA,wBAAwBC;IAGjC,wBAAwB;QACpB,OAAOC;IACX;;QALG,qBACH,SAAS,GAAG;;AAKhB;AAGO,MAAMA,yBACTC,kBAAkD"}
1
+ {"version":3,"file":"features/security/login/abstractions.js","sources":["../../../../src/features/security/login/abstractions.ts"],"sourcesContent":["import { DomainEvent } from \"~/features/eventPublisher/index.js\";\nimport type { IEventHandler } from \"~/features/eventPublisher/index.js\";\nimport { createAbstraction } from \"@webiny/feature/api\";\nimport { Result } from \"@webiny/feature/api\";\nimport { Identity } from \"~/features/security/IdentityContext/index.js\";\nimport type { AdminUser } from \"~/features/users/shared/types.js\";\nimport type { UserStorageError } from \"~/features/users/shared/errors.js\";\nimport type { IdentityTenant } from \"~/features/security/ResolveIdentityTenants/abstractions.js\";\nimport type {\n LogInNotAuthenticatedError,\n LogInMissingProfileError,\n LogInTenantAccessDeniedError\n} from \"./errors.js\";\n\nexport interface AfterLoginPayload {\n identity: Identity;\n}\n\nexport class AfterLoginEvent extends DomainEvent<AfterLoginPayload> {\n eventType = \"login.afterLogin\" as const;\n\n getHandlerAbstraction() {\n return AfterLoginEventHandler;\n }\n}\n\n/** Hook into login lifecycle after a user logs in. */\nexport const AfterLoginEventHandler =\n createAbstraction<IEventHandler<AfterLoginEvent>>(\"AfterLoginEventHandler\");\n\nexport namespace AfterLoginEventHandler {\n export type Interface = IEventHandler<AfterLoginEvent>;\n export type Event = AfterLoginEvent;\n}\n\n/** The identity has a profile in the current tenant (or its parent), and is logged in. */\nexport interface LogInLoggedIn {\n type: \"loggedIn\";\n identity: Identity;\n user: AdminUser;\n}\n\n/**\n * A native identity has no profile in the current tenant, but has one in these tenants.\n * The client picks one and logs in there.\n */\nexport interface LogInTenantSelectionRequired {\n type: \"tenantSelectionRequired\";\n tenants: IdentityTenant[];\n}\n\nexport type LogInOutput = LogInLoggedIn | LogInTenantSelectionRequired;\n\nexport interface ILogInUseCaseErrors {\n notAuthenticated: LogInNotAuthenticatedError;\n tenantAccessDenied: LogInTenantAccessDeniedError;\n missingProfile: LogInMissingProfileError;\n storage: UserStorageError;\n}\n\ntype LogInError = ILogInUseCaseErrors[keyof ILogInUseCaseErrors];\n\nexport interface ILogInUseCase {\n /**\n * Log in the current identity: load its user profile and publish `AfterLoginEvent`.\n * A native identity without a profile in the current tenant gets the tenants it can log into.\n */\n execute(): Promise<Result<LogInOutput, LogInError>>;\n}\n\n/** Log in the current identity. */\nexport const LogInUseCase = createAbstraction<ILogInUseCase>(\"LogInUseCase\");\n\nexport namespace LogInUseCase {\n export type Interface = ILogInUseCase;\n export type Output = LogInOutput;\n export type LoggedIn = LogInLoggedIn;\n export type TenantSelectionRequired = LogInTenantSelectionRequired;\n export type Error = LogInError;\n export type Return = Promise<Result<LogInOutput, LogInError>>;\n}\n"],"names":["AfterLoginEvent","DomainEvent","AfterLoginEventHandler","createAbstraction","LogInUseCase"],"mappings":";;AAkBO,MAAMA,wBAAwBC;IAGjC,wBAAwB;QACpB,OAAOC;IACX;;QALG,qBACH,SAAS,GAAG;;AAKhB;AAGO,MAAMA,yBACTC,kBAAkD;AA2C/C,MAAMC,eAAeD,kBAAiC"}
@@ -0,0 +1,13 @@
1
+ import { BaseError } from "@webiny/feature/api";
2
+ export declare class LogInNotAuthenticatedError extends BaseError {
3
+ readonly code: "Security/Identity/NotAuthenticated";
4
+ constructor();
5
+ }
6
+ export declare class LogInTenantAccessDeniedError extends BaseError {
7
+ readonly code: "Security/Identity/NotAuthorized";
8
+ constructor();
9
+ }
10
+ export declare class LogInMissingProfileError extends BaseError {
11
+ readonly code: "Security/Identity/NotAuthorized";
12
+ constructor();
13
+ }
@@ -0,0 +1,25 @@
1
+ import { BaseError } from "@webiny/feature/api";
2
+ class LogInNotAuthenticatedError extends BaseError {
3
+ constructor(){
4
+ super({
5
+ message: "Unauthenticated!"
6
+ }), this.code = "Security/Identity/NotAuthenticated";
7
+ }
8
+ }
9
+ class LogInTenantAccessDeniedError extends BaseError {
10
+ constructor(){
11
+ super({
12
+ message: "You can't access this tenant."
13
+ }), this.code = "Security/Identity/NotAuthorized";
14
+ }
15
+ }
16
+ class LogInMissingProfileError extends BaseError {
17
+ constructor(){
18
+ super({
19
+ message: "Missing user profile!"
20
+ }), this.code = "Security/Identity/NotAuthorized";
21
+ }
22
+ }
23
+ export { LogInMissingProfileError, LogInNotAuthenticatedError, LogInTenantAccessDeniedError };
24
+
25
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"features/security/login/errors.js","sources":["../../../../src/features/security/login/errors.ts"],"sourcesContent":["import { BaseError } from \"@webiny/feature/api\";\n\nexport class LogInNotAuthenticatedError extends BaseError {\n override readonly code = \"Security/Identity/NotAuthenticated\" as const;\n\n constructor() {\n super({\n message: \"Unauthenticated!\"\n });\n }\n}\n\nexport class LogInTenantAccessDeniedError extends BaseError {\n override readonly code = \"Security/Identity/NotAuthorized\" as const;\n\n constructor() {\n super({\n message: \"You can't access this tenant.\"\n });\n }\n}\n\nexport class LogInMissingProfileError extends BaseError {\n override readonly code = \"Security/Identity/NotAuthorized\" as const;\n\n constructor() {\n super({\n message: \"Missing user profile!\"\n });\n }\n}\n"],"names":["LogInNotAuthenticatedError","BaseError","LogInTenantAccessDeniedError","LogInMissingProfileError"],"mappings":";AAEO,MAAMA,mCAAmCC;IAG5C,aAAc;QACV,KAAK,CAAC;YACF,SAAS;QACb,SALc,IAAI,GAAG;IAMzB;AACJ;AAEO,MAAMC,qCAAqCD;IAG9C,aAAc;QACV,KAAK,CAAC;YACF,SAAS;QACb,SALc,IAAI,GAAG;IAMzB;AACJ;AAEO,MAAME,iCAAiCF;IAG1C,aAAc;QACV,KAAK,CAAC;YACF,SAAS;QACb,SALc,IAAI,GAAG;IAMzB;AACJ"}