alepha 0.14.4 → 0.15.1
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/README.md +44 -102
- package/dist/api/audits/index.d.ts +331 -443
- package/dist/api/audits/index.d.ts.map +1 -1
- package/dist/api/audits/index.js +2 -2
- package/dist/api/audits/index.js.map +1 -1
- package/dist/api/files/index.d.ts +0 -113
- package/dist/api/files/index.d.ts.map +1 -1
- package/dist/api/files/index.js +2 -3
- package/dist/api/files/index.js.map +1 -1
- package/dist/api/jobs/index.d.ts +151 -262
- package/dist/api/jobs/index.d.ts.map +1 -1
- package/dist/api/notifications/index.browser.js +4 -4
- package/dist/api/notifications/index.browser.js.map +1 -1
- package/dist/api/notifications/index.d.ts +164 -276
- package/dist/api/notifications/index.d.ts.map +1 -1
- package/dist/api/notifications/index.js +4 -4
- package/dist/api/notifications/index.js.map +1 -1
- package/dist/api/parameters/index.d.ts +265 -377
- package/dist/api/parameters/index.d.ts.map +1 -1
- package/dist/api/users/index.browser.js +1 -2
- package/dist/api/users/index.browser.js.map +1 -1
- package/dist/api/users/index.d.ts +195 -301
- package/dist/api/users/index.d.ts.map +1 -1
- package/dist/api/users/index.js +203 -184
- package/dist/api/users/index.js.map +1 -1
- package/dist/api/verifications/index.d.ts.map +1 -1
- package/dist/batch/index.d.ts.map +1 -1
- package/dist/batch/index.js +1 -2
- package/dist/batch/index.js.map +1 -1
- package/dist/bucket/index.d.ts.map +1 -1
- package/dist/cache/core/index.d.ts.map +1 -1
- package/dist/cache/redis/index.d.ts.map +1 -1
- package/dist/cache/redis/index.js +2 -2
- package/dist/cache/redis/index.js.map +1 -1
- package/dist/cli/index.d.ts +5900 -165
- package/dist/cli/index.d.ts.map +1 -1
- package/dist/cli/index.js +1481 -639
- package/dist/cli/index.js.map +1 -1
- package/dist/command/index.d.ts +8 -4
- package/dist/command/index.d.ts.map +1 -1
- package/dist/command/index.js +29 -25
- package/dist/command/index.js.map +1 -1
- package/dist/core/index.browser.js +563 -54
- package/dist/core/index.browser.js.map +1 -1
- package/dist/core/index.d.ts +175 -8
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +564 -54
- package/dist/core/index.js.map +1 -1
- package/dist/core/index.native.js +563 -54
- package/dist/core/index.native.js.map +1 -1
- package/dist/datetime/index.d.ts.map +1 -1
- package/dist/datetime/index.js +4 -4
- package/dist/datetime/index.js.map +1 -1
- package/dist/email/index.d.ts +89 -42
- package/dist/email/index.d.ts.map +1 -1
- package/dist/email/index.js +129 -33
- package/dist/email/index.js.map +1 -1
- package/dist/fake/index.d.ts +7969 -2
- package/dist/fake/index.d.ts.map +1 -1
- package/dist/fake/index.js +22 -22
- package/dist/fake/index.js.map +1 -1
- package/dist/file/index.d.ts +134 -1
- package/dist/file/index.d.ts.map +1 -1
- package/dist/file/index.js +253 -1
- package/dist/file/index.js.map +1 -1
- package/dist/lock/core/index.d.ts.map +1 -1
- package/dist/lock/redis/index.d.ts.map +1 -1
- package/dist/logger/index.d.ts +1 -2
- package/dist/logger/index.d.ts.map +1 -1
- package/dist/logger/index.js +1 -5
- package/dist/logger/index.js.map +1 -1
- package/dist/mcp/index.d.ts +19 -1
- package/dist/mcp/index.d.ts.map +1 -1
- package/dist/mcp/index.js +28 -4
- package/dist/mcp/index.js.map +1 -1
- package/dist/orm/chunk-DH6iiROE.js +38 -0
- package/dist/orm/index.browser.js +9 -9
- package/dist/orm/index.browser.js.map +1 -1
- package/dist/orm/index.bun.js +2821 -0
- package/dist/orm/index.bun.js.map +1 -0
- package/dist/orm/index.d.ts +318 -169
- package/dist/orm/index.d.ts.map +1 -1
- package/dist/orm/index.js +2086 -1776
- package/dist/orm/index.js.map +1 -1
- package/dist/queue/core/index.d.ts +4 -4
- package/dist/queue/core/index.d.ts.map +1 -1
- package/dist/queue/redis/index.d.ts.map +1 -1
- package/dist/redis/index.bun.js +285 -0
- package/dist/redis/index.bun.js.map +1 -0
- package/dist/redis/index.d.ts +13 -31
- package/dist/redis/index.d.ts.map +1 -1
- package/dist/redis/index.js +18 -38
- package/dist/redis/index.js.map +1 -1
- package/dist/retry/index.d.ts.map +1 -1
- package/dist/router/index.d.ts.map +1 -1
- package/dist/scheduler/index.d.ts +83 -1
- package/dist/scheduler/index.d.ts.map +1 -1
- package/dist/scheduler/index.js +393 -1
- package/dist/scheduler/index.js.map +1 -1
- package/dist/security/index.browser.js +5 -1
- package/dist/security/index.browser.js.map +1 -1
- package/dist/security/index.d.ts +598 -112
- package/dist/security/index.d.ts.map +1 -1
- package/dist/security/index.js +1808 -97
- package/dist/security/index.js.map +1 -1
- package/dist/server/auth/index.d.ts +1200 -175
- package/dist/server/auth/index.d.ts.map +1 -1
- package/dist/server/auth/index.js +1268 -37
- package/dist/server/auth/index.js.map +1 -1
- package/dist/server/cache/index.d.ts +6 -3
- package/dist/server/cache/index.d.ts.map +1 -1
- package/dist/server/cache/index.js +1 -1
- package/dist/server/cache/index.js.map +1 -1
- package/dist/server/compress/index.d.ts.map +1 -1
- package/dist/server/cookies/index.d.ts.map +1 -1
- package/dist/server/cookies/index.js +3 -3
- package/dist/server/cookies/index.js.map +1 -1
- package/dist/server/core/index.d.ts +115 -13
- package/dist/server/core/index.d.ts.map +1 -1
- package/dist/server/core/index.js +321 -139
- package/dist/server/core/index.js.map +1 -1
- package/dist/server/cors/index.d.ts +0 -1
- package/dist/server/cors/index.d.ts.map +1 -1
- package/dist/server/health/index.d.ts +0 -1
- package/dist/server/health/index.d.ts.map +1 -1
- package/dist/server/helmet/index.d.ts.map +1 -1
- package/dist/server/links/index.browser.js +9 -1
- package/dist/server/links/index.browser.js.map +1 -1
- package/dist/server/links/index.d.ts +1 -2
- package/dist/server/links/index.d.ts.map +1 -1
- package/dist/server/links/index.js +14 -7
- package/dist/server/links/index.js.map +1 -1
- package/dist/server/metrics/index.d.ts +514 -1
- package/dist/server/metrics/index.d.ts.map +1 -1
- package/dist/server/metrics/index.js +4462 -4
- package/dist/server/metrics/index.js.map +1 -1
- package/dist/server/multipart/index.d.ts.map +1 -1
- package/dist/server/proxy/index.d.ts +0 -1
- package/dist/server/proxy/index.d.ts.map +1 -1
- package/dist/server/rate-limit/index.d.ts.map +1 -1
- package/dist/server/static/index.d.ts.map +1 -1
- package/dist/server/swagger/index.d.ts +1 -2
- package/dist/server/swagger/index.d.ts.map +1 -1
- package/dist/server/swagger/index.js +1 -2
- package/dist/server/swagger/index.js.map +1 -1
- package/dist/sms/index.d.ts +3 -1
- package/dist/sms/index.d.ts.map +1 -1
- package/dist/sms/index.js +10 -10
- package/dist/sms/index.js.map +1 -1
- package/dist/thread/index.d.ts +0 -1
- package/dist/thread/index.d.ts.map +1 -1
- package/dist/thread/index.js +2 -2
- package/dist/thread/index.js.map +1 -1
- package/dist/topic/core/index.d.ts.map +1 -1
- package/dist/topic/redis/index.d.ts.map +1 -1
- package/dist/vite/index.d.ts +6315 -149
- package/dist/vite/index.d.ts.map +1 -1
- package/dist/vite/index.js +140 -469
- package/dist/vite/index.js.map +1 -1
- package/dist/websocket/index.browser.js +9 -9
- package/dist/websocket/index.browser.js.map +1 -1
- package/dist/websocket/index.d.ts +28 -28
- package/dist/websocket/index.d.ts.map +1 -1
- package/dist/websocket/index.js +9 -9
- package/dist/websocket/index.js.map +1 -1
- package/package.json +13 -18
- package/src/api/files/controllers/AdminFileStatsController.ts +0 -1
- package/src/api/users/atoms/realmAuthSettingsAtom.ts +5 -0
- package/src/api/users/controllers/{UserRealmController.ts → RealmController.ts} +11 -11
- package/src/api/users/entities/users.ts +1 -1
- package/src/api/users/index.ts +8 -8
- package/src/api/users/primitives/{$userRealm.ts → $realm.ts} +17 -19
- package/src/api/users/providers/{UserRealmProvider.ts → RealmProvider.ts} +26 -30
- package/src/api/users/schemas/{userRealmConfigSchema.ts → realmConfigSchema.ts} +2 -2
- package/src/api/users/services/CredentialService.ts +7 -7
- package/src/api/users/services/IdentityService.ts +4 -4
- package/src/api/users/services/RegistrationService.spec.ts +25 -27
- package/src/api/users/services/RegistrationService.ts +38 -27
- package/src/api/users/services/SessionCrudService.ts +3 -3
- package/src/api/users/services/SessionService.spec.ts +3 -3
- package/src/api/users/services/SessionService.ts +27 -18
- package/src/api/users/services/UserService.ts +7 -7
- package/src/batch/providers/BatchProvider.ts +1 -2
- package/src/cli/apps/AlephaCli.ts +2 -2
- package/src/cli/apps/AlephaPackageBuilderCli.ts +47 -20
- package/src/cli/assets/apiHelloControllerTs.ts +19 -0
- package/src/cli/assets/apiIndexTs.ts +16 -0
- package/src/cli/assets/biomeJson.ts +2 -1
- package/src/cli/assets/claudeMd.ts +308 -0
- package/src/cli/assets/dummySpecTs.ts +2 -1
- package/src/cli/assets/editorconfig.ts +2 -1
- package/src/cli/assets/mainBrowserTs.ts +4 -3
- package/src/cli/assets/mainCss.ts +24 -0
- package/src/cli/assets/mainServerTs.ts +24 -0
- package/src/cli/assets/tsconfigJson.ts +2 -1
- package/src/cli/assets/webAppRouterTs.ts +16 -0
- package/src/cli/assets/webHelloComponentTsx.ts +20 -0
- package/src/cli/assets/webIndexTs.ts +16 -0
- package/src/cli/atoms/appEntryOptions.ts +13 -0
- package/src/cli/atoms/buildOptions.ts +1 -1
- package/src/cli/atoms/changelogOptions.ts +1 -1
- package/src/cli/commands/build.ts +97 -61
- package/src/cli/commands/db.ts +21 -18
- package/src/cli/commands/deploy.ts +17 -5
- package/src/cli/commands/dev.ts +26 -47
- package/src/cli/commands/gen/env.ts +1 -1
- package/src/cli/commands/init.ts +79 -25
- package/src/cli/commands/lint.ts +9 -3
- package/src/cli/commands/test.ts +8 -2
- package/src/cli/commands/typecheck.ts +5 -1
- package/src/cli/commands/verify.ts +4 -2
- package/src/cli/defineConfig.ts +9 -0
- package/src/cli/index.ts +2 -1
- package/src/cli/providers/AppEntryProvider.ts +131 -0
- package/src/cli/providers/ViteBuildProvider.ts +82 -0
- package/src/cli/providers/ViteDevServerProvider.ts +350 -0
- package/src/cli/providers/ViteTemplateProvider.ts +27 -0
- package/src/cli/services/AlephaCliUtils.ts +72 -602
- package/src/cli/services/PackageManagerUtils.ts +308 -0
- package/src/cli/services/ProjectScaffolder.ts +329 -0
- package/src/command/helpers/Runner.ts +15 -3
- package/src/core/Alepha.ts +2 -8
- package/src/core/__tests__/Alepha-graph.spec.ts +4 -0
- package/src/core/index.shared.ts +1 -0
- package/src/core/index.ts +2 -0
- package/src/core/primitives/$hook.ts +6 -2
- package/src/core/primitives/$module.spec.ts +4 -0
- package/src/core/primitives/$module.ts +12 -0
- package/src/core/providers/AlsProvider.ts +1 -1
- package/src/core/providers/CodecManager.spec.ts +12 -6
- package/src/core/providers/CodecManager.ts +26 -6
- package/src/core/providers/EventManager.ts +169 -13
- package/src/core/providers/KeylessJsonSchemaCodec.spec.ts +878 -0
- package/src/core/providers/KeylessJsonSchemaCodec.ts +789 -0
- package/src/core/providers/SchemaValidator.spec.ts +236 -0
- package/src/core/providers/StateManager.spec.ts +27 -16
- package/src/email/providers/LocalEmailProvider.spec.ts +111 -87
- package/src/email/providers/LocalEmailProvider.ts +52 -15
- package/src/email/providers/NodemailerEmailProvider.ts +167 -56
- package/src/file/errors/FileError.ts +7 -0
- package/src/file/index.ts +9 -1
- package/src/file/providers/MemoryFileSystemProvider.ts +393 -0
- package/src/logger/providers/PrettyFormatterProvider.ts +0 -9
- package/src/mcp/errors/McpError.ts +30 -0
- package/src/mcp/index.ts +3 -0
- package/src/mcp/transports/SseMcpTransport.ts +16 -6
- package/src/orm/index.browser.ts +1 -19
- package/src/orm/index.bun.ts +77 -0
- package/src/orm/index.shared-server.ts +22 -0
- package/src/orm/index.shared.ts +15 -0
- package/src/orm/index.ts +19 -39
- package/src/orm/providers/DrizzleKitProvider.ts +3 -5
- package/src/orm/providers/drivers/BunPostgresProvider.ts +3 -5
- package/src/orm/providers/drivers/BunSqliteProvider.ts +1 -1
- package/src/orm/providers/drivers/CloudflareD1Provider.ts +4 -0
- package/src/orm/providers/drivers/DatabaseProvider.ts +4 -0
- package/src/orm/providers/drivers/PglitePostgresProvider.ts +4 -0
- package/src/orm/services/Repository.ts +19 -0
- package/src/redis/index.bun.ts +35 -0
- package/src/redis/providers/BunRedisProvider.ts +12 -43
- package/src/redis/providers/BunRedisSubscriberProvider.ts +2 -3
- package/src/redis/providers/NodeRedisProvider.ts +16 -34
- package/src/{server/security → security}/__tests__/BasicAuth.spec.ts +11 -11
- package/src/{server/security → security}/__tests__/ServerSecurityProvider-realm.spec.ts +21 -16
- package/src/{server/security/providers → security/__tests__}/ServerSecurityProvider.spec.ts +5 -5
- package/src/security/index.browser.ts +5 -0
- package/src/security/index.ts +90 -7
- package/src/security/primitives/{$realm.spec.ts → $issuer.spec.ts} +11 -11
- package/src/security/primitives/{$realm.ts → $issuer.ts} +20 -17
- package/src/security/primitives/$role.ts +5 -5
- package/src/security/primitives/$serviceAccount.spec.ts +5 -5
- package/src/security/primitives/$serviceAccount.ts +3 -3
- package/src/{server/security → security}/providers/ServerSecurityProvider.ts +5 -7
- package/src/server/auth/primitives/$auth.ts +10 -10
- package/src/server/auth/primitives/$authCredentials.ts +3 -3
- package/src/server/auth/primitives/$authGithub.ts +3 -3
- package/src/server/auth/primitives/$authGoogle.ts +3 -3
- package/src/server/auth/providers/ServerAuthProvider.ts +13 -13
- package/src/server/cache/providers/ServerCacheProvider.ts +1 -1
- package/src/server/cookies/providers/ServerCookiesProvider.ts +3 -3
- package/src/server/core/index.ts +1 -1
- package/src/server/core/providers/BunHttpServerProvider.ts +1 -1
- package/src/server/core/providers/NodeHttpServerProvider.spec.ts +125 -0
- package/src/server/core/providers/NodeHttpServerProvider.ts +92 -24
- package/src/server/core/providers/ServerBodyParserProvider.ts +19 -23
- package/src/server/core/providers/ServerLoggerProvider.ts +23 -19
- package/src/server/core/providers/ServerProvider.ts +144 -24
- package/src/server/core/providers/ServerRouterProvider.ts +259 -115
- package/src/server/core/providers/ServerTimingProvider.ts +2 -2
- package/src/server/links/atoms/apiLinksAtom.ts +7 -0
- package/src/server/links/index.browser.ts +2 -0
- package/src/server/links/index.ts +3 -1
- package/src/server/links/providers/LinkProvider.ts +1 -1
- package/src/server/swagger/index.ts +1 -1
- package/src/sms/providers/LocalSmsProvider.spec.ts +153 -111
- package/src/sms/providers/LocalSmsProvider.ts +8 -7
- package/src/vite/index.ts +3 -2
- package/src/vite/tasks/buildClient.ts +0 -1
- package/src/vite/tasks/buildServer.ts +80 -22
- package/src/vite/tasks/copyAssets.ts +5 -4
- package/src/vite/tasks/generateCloudflare.ts +7 -0
- package/src/vite/tasks/generateSitemap.ts +64 -23
- package/src/vite/tasks/index.ts +0 -2
- package/src/vite/tasks/prerenderPages.ts +49 -24
- package/dist/server/security/index.browser.js +0 -13
- package/dist/server/security/index.browser.js.map +0 -1
- package/dist/server/security/index.d.ts +0 -173
- package/dist/server/security/index.d.ts.map +0 -1
- package/dist/server/security/index.js +0 -311
- package/dist/server/security/index.js.map +0 -1
- package/src/cli/assets/appRouterTs.ts +0 -9
- package/src/cli/assets/indexHtml.ts +0 -15
- package/src/cli/assets/mainTs.ts +0 -13
- package/src/cli/commands/format.ts +0 -17
- package/src/server/security/index.browser.ts +0 -10
- package/src/server/security/index.ts +0 -94
- package/src/vite/helpers/boot.ts +0 -106
- package/src/vite/plugins/viteAlephaDev.ts +0 -177
- package/src/vite/tasks/devServer.ts +0 -69
- package/src/vite/tasks/runAlepha.ts +0 -270
- /package/src/{server/security → security}/primitives/$basicAuth.ts +0 -0
- /package/src/{server/security → security}/providers/ServerBasicAuthProvider.ts +0 -0
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
2
|
import { Alepha } from "alepha";
|
|
3
|
-
import {
|
|
4
|
-
|
|
3
|
+
import {
|
|
4
|
+
$action,
|
|
5
|
+
$route,
|
|
6
|
+
AlephaServer,
|
|
7
|
+
ForbiddenError,
|
|
8
|
+
ServerProvider,
|
|
9
|
+
} from "alepha/server";
|
|
5
10
|
import { describe, expect, it } from "vitest";
|
|
6
|
-
import {
|
|
11
|
+
import { $issuer, AlephaSecurity } from "../index.ts";
|
|
7
12
|
|
|
8
13
|
describe("ServerSecurityProvider - Realm Protection", () => {
|
|
9
14
|
it("should allow access when user belongs to the required realm", async () => {
|
|
10
15
|
class TestApp {
|
|
11
|
-
realmA = $
|
|
16
|
+
realmA = $issuer({
|
|
12
17
|
secret: "test-realm-a",
|
|
13
18
|
roles: [
|
|
14
19
|
{
|
|
@@ -18,7 +23,7 @@ describe("ServerSecurityProvider - Realm Protection", () => {
|
|
|
18
23
|
],
|
|
19
24
|
});
|
|
20
25
|
|
|
21
|
-
realmB = $
|
|
26
|
+
realmB = $issuer({
|
|
22
27
|
secret: "test-realm-b",
|
|
23
28
|
roles: [
|
|
24
29
|
{
|
|
@@ -43,7 +48,7 @@ describe("ServerSecurityProvider - Realm Protection", () => {
|
|
|
43
48
|
});
|
|
44
49
|
}
|
|
45
50
|
|
|
46
|
-
const alepha = Alepha.create().with(
|
|
51
|
+
const alepha = Alepha.create().with(AlephaServer).with(AlephaSecurity);
|
|
47
52
|
const app = alepha.inject(TestApp);
|
|
48
53
|
await alepha.start();
|
|
49
54
|
|
|
@@ -85,7 +90,7 @@ describe("ServerSecurityProvider - Realm Protection", () => {
|
|
|
85
90
|
|
|
86
91
|
it("should deny access when user does not belong to the required realm", async () => {
|
|
87
92
|
class TestApp {
|
|
88
|
-
realmA = $
|
|
93
|
+
realmA = $issuer({
|
|
89
94
|
secret: "test-realm-a",
|
|
90
95
|
roles: [
|
|
91
96
|
{
|
|
@@ -95,7 +100,7 @@ describe("ServerSecurityProvider - Realm Protection", () => {
|
|
|
95
100
|
],
|
|
96
101
|
});
|
|
97
102
|
|
|
98
|
-
realmB = $
|
|
103
|
+
realmB = $issuer({
|
|
99
104
|
secret: "test-realm-b",
|
|
100
105
|
roles: [
|
|
101
106
|
{
|
|
@@ -120,7 +125,7 @@ describe("ServerSecurityProvider - Realm Protection", () => {
|
|
|
120
125
|
});
|
|
121
126
|
}
|
|
122
127
|
|
|
123
|
-
const alepha = Alepha.create().with(
|
|
128
|
+
const alepha = Alepha.create().with(AlephaServer).with(AlephaSecurity);
|
|
124
129
|
const app = alepha.inject(TestApp);
|
|
125
130
|
await alepha.start();
|
|
126
131
|
|
|
@@ -172,7 +177,7 @@ describe("ServerSecurityProvider - Realm Protection", () => {
|
|
|
172
177
|
|
|
173
178
|
it("should work with actions when user has no realm attribute", async () => {
|
|
174
179
|
class TestApp {
|
|
175
|
-
realmA = $
|
|
180
|
+
realmA = $issuer({
|
|
176
181
|
secret: "test-realm-a",
|
|
177
182
|
roles: [
|
|
178
183
|
{
|
|
@@ -188,7 +193,7 @@ describe("ServerSecurityProvider - Realm Protection", () => {
|
|
|
188
193
|
});
|
|
189
194
|
}
|
|
190
195
|
|
|
191
|
-
const alepha = Alepha.create().with(
|
|
196
|
+
const alepha = Alepha.create().with(AlephaServer).with(AlephaSecurity);
|
|
192
197
|
const app = alepha.inject(TestApp);
|
|
193
198
|
await alepha.start();
|
|
194
199
|
|
|
@@ -215,7 +220,7 @@ describe("ServerSecurityProvider - Realm Protection", () => {
|
|
|
215
220
|
|
|
216
221
|
it("should combine realm and permission checks", async () => {
|
|
217
222
|
class TestApp {
|
|
218
|
-
realmA = $
|
|
223
|
+
realmA = $issuer({
|
|
219
224
|
secret: "test-realm-a",
|
|
220
225
|
roles: [
|
|
221
226
|
{
|
|
@@ -229,7 +234,7 @@ describe("ServerSecurityProvider - Realm Protection", () => {
|
|
|
229
234
|
],
|
|
230
235
|
});
|
|
231
236
|
|
|
232
|
-
realmB = $
|
|
237
|
+
realmB = $issuer({
|
|
233
238
|
secret: "test-realm-b",
|
|
234
239
|
});
|
|
235
240
|
|
|
@@ -248,7 +253,7 @@ describe("ServerSecurityProvider - Realm Protection", () => {
|
|
|
248
253
|
});
|
|
249
254
|
}
|
|
250
255
|
|
|
251
|
-
const alepha = Alepha.create().with(
|
|
256
|
+
const alepha = Alepha.create().with(AlephaServer).with(AlephaSecurity);
|
|
252
257
|
const app = alepha.inject(TestApp);
|
|
253
258
|
await alepha.start();
|
|
254
259
|
|
|
@@ -336,7 +341,7 @@ describe("ServerSecurityProvider - Realm Protection", () => {
|
|
|
336
341
|
|
|
337
342
|
it("should work with fetch requests when realm is valid", async () => {
|
|
338
343
|
class TestApp {
|
|
339
|
-
realmA = $
|
|
344
|
+
realmA = $issuer({
|
|
340
345
|
secret: "test-realm-a",
|
|
341
346
|
roles: [
|
|
342
347
|
{
|
|
@@ -352,7 +357,7 @@ describe("ServerSecurityProvider - Realm Protection", () => {
|
|
|
352
357
|
});
|
|
353
358
|
}
|
|
354
359
|
|
|
355
|
-
const alepha = Alepha.create().with(
|
|
360
|
+
const alepha = Alepha.create().with(AlephaServer).with(AlephaSecurity);
|
|
356
361
|
const app = alepha.inject(TestApp);
|
|
357
362
|
await alepha.start();
|
|
358
363
|
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
2
|
import { Alepha } from "alepha";
|
|
3
|
-
import { $realm } from "alepha/security";
|
|
4
3
|
import {
|
|
5
4
|
$action,
|
|
5
|
+
AlephaServer,
|
|
6
6
|
ForbiddenError,
|
|
7
7
|
HttpError,
|
|
8
8
|
ServerProvider,
|
|
9
9
|
UnauthorizedError,
|
|
10
10
|
} from "alepha/server";
|
|
11
11
|
import { describe, expect, it } from "vitest";
|
|
12
|
-
import {
|
|
12
|
+
import { $issuer, AlephaSecurity } from "../index.ts";
|
|
13
13
|
|
|
14
14
|
describe("ServerSecurityProvider", () => {
|
|
15
15
|
it("should protect action from unauthorized users", async () => {
|
|
@@ -19,7 +19,7 @@ describe("ServerSecurityProvider", () => {
|
|
|
19
19
|
});
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
const alepha = Alepha.create().with(
|
|
22
|
+
const alepha = Alepha.create().with(AlephaServer).with(AlephaSecurity);
|
|
23
23
|
const app = alepha.inject(TestApp);
|
|
24
24
|
await alepha.start();
|
|
25
25
|
|
|
@@ -62,7 +62,7 @@ describe("ServerSecurityProvider", () => {
|
|
|
62
62
|
group: "read",
|
|
63
63
|
handler: () => "USER",
|
|
64
64
|
});
|
|
65
|
-
|
|
65
|
+
issuer = $issuer({
|
|
66
66
|
secret: "test",
|
|
67
67
|
roles: [
|
|
68
68
|
{
|
|
@@ -77,7 +77,7 @@ describe("ServerSecurityProvider", () => {
|
|
|
77
77
|
});
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
-
const alepha = Alepha.create().with(
|
|
80
|
+
const alepha = Alepha.create().with(AlephaServer).with(AlephaSecurity);
|
|
81
81
|
const app = alepha.inject(TestApp);
|
|
82
82
|
await alepha.start();
|
|
83
83
|
|
|
@@ -11,3 +11,8 @@ export * from "./schemas/userAccountInfoSchema.ts";
|
|
|
11
11
|
export const AlephaSecurity = $module({
|
|
12
12
|
name: "alepha.security",
|
|
13
13
|
});
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @deprecated Use `AlephaSecurity` instead. Server security providers are automatically registered when `AlephaServer` is available.
|
|
17
|
+
*/
|
|
18
|
+
export const AlephaServerSecurity = AlephaSecurity;
|
package/src/security/index.ts
CHANGED
|
@@ -1,27 +1,37 @@
|
|
|
1
|
-
import { $module } from "alepha";
|
|
1
|
+
import { $module, type Alepha } from "alepha";
|
|
2
|
+
import { AlephaServer, type FetchOptions } from "alepha/server";
|
|
3
|
+
import type { UserAccountToken } from "./interfaces/UserAccountToken.ts";
|
|
4
|
+
import { $basicAuth } from "./primitives/$basicAuth.ts";
|
|
5
|
+
import { $issuer } from "./primitives/$issuer.ts";
|
|
2
6
|
import { $permission } from "./primitives/$permission.ts";
|
|
3
|
-
import { $realm } from "./primitives/$realm.ts";
|
|
4
7
|
import { $role } from "./primitives/$role.ts";
|
|
5
8
|
import { CryptoProvider } from "./providers/CryptoProvider.ts";
|
|
6
9
|
import { JwtProvider } from "./providers/JwtProvider.ts";
|
|
7
10
|
import { SecurityProvider } from "./providers/SecurityProvider.ts";
|
|
11
|
+
import { ServerBasicAuthProvider } from "./providers/ServerBasicAuthProvider.ts";
|
|
12
|
+
import { ServerSecurityProvider } from "./providers/ServerSecurityProvider.ts";
|
|
8
13
|
import type { UserAccount } from "./schemas/userAccountInfoSchema.ts";
|
|
9
14
|
|
|
10
15
|
export * from "./errors/InvalidCredentialsError.ts";
|
|
11
16
|
export * from "./errors/InvalidPermissionError.ts";
|
|
12
17
|
export * from "./errors/SecurityError.ts";
|
|
13
18
|
export * from "./interfaces/UserAccountToken.ts";
|
|
19
|
+
export * from "./primitives/$basicAuth.ts";
|
|
20
|
+
export * from "./primitives/$issuer.ts";
|
|
14
21
|
export * from "./primitives/$permission.ts";
|
|
15
|
-
export * from "./primitives/$realm.ts";
|
|
16
22
|
export * from "./primitives/$role.ts";
|
|
17
23
|
export * from "./primitives/$serviceAccount.ts";
|
|
18
24
|
export * from "./providers/CryptoProvider.ts";
|
|
19
25
|
export * from "./providers/JwtProvider.ts";
|
|
20
26
|
export * from "./providers/SecurityProvider.ts";
|
|
27
|
+
export * from "./providers/ServerBasicAuthProvider.ts";
|
|
28
|
+
export * from "./providers/ServerSecurityProvider.ts";
|
|
21
29
|
export * from "./schemas/permissionSchema.ts";
|
|
22
30
|
export * from "./schemas/roleSchema.ts";
|
|
23
31
|
export * from "./schemas/userAccountInfoSchema.ts";
|
|
24
32
|
|
|
33
|
+
import type { ServerRouteSecure } from "./providers/ServerSecurityProvider.ts";
|
|
34
|
+
|
|
25
35
|
declare module "alepha" {
|
|
26
36
|
interface Hooks {
|
|
27
37
|
"security:user:created": {
|
|
@@ -29,22 +39,95 @@ declare module "alepha" {
|
|
|
29
39
|
user: UserAccount;
|
|
30
40
|
};
|
|
31
41
|
}
|
|
42
|
+
|
|
43
|
+
interface State {
|
|
44
|
+
/**
|
|
45
|
+
* Real (or fake) user account, used for internal actions.
|
|
46
|
+
*
|
|
47
|
+
* If you define this, you assume that all actions are executed by this user by default.
|
|
48
|
+
* > To force a different user, you need to pass it explicitly in the options.
|
|
49
|
+
*/
|
|
50
|
+
"alepha.server.security.system.user"?: UserAccountToken;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The authenticated user account attached to the server request state.
|
|
54
|
+
*
|
|
55
|
+
* @internal
|
|
56
|
+
*/
|
|
57
|
+
"alepha.server.request.user"?: UserAccount;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
declare module "alepha/server" {
|
|
62
|
+
interface ServerRequest<TConfig> {
|
|
63
|
+
user?: UserAccountToken; // for all routes, user is maybe present
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
interface ServerActionRequest<TConfig> {
|
|
67
|
+
user: UserAccountToken; // for actions, user is always present
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
interface ServerRoute {
|
|
71
|
+
/**
|
|
72
|
+
* If true, the route will be protected by the security provider.
|
|
73
|
+
* All actions are secure by default, but you can disable it for specific actions.
|
|
74
|
+
*/
|
|
75
|
+
secure?: boolean | ServerRouteSecure;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
interface ClientRequestOptions extends FetchOptions {
|
|
79
|
+
/**
|
|
80
|
+
* Forward user from the previous request.
|
|
81
|
+
* If "system", use system user. @see {ServerSecurityProvider.localSystemUser}
|
|
82
|
+
* If "context", use the user from the current context (e.g. request).
|
|
83
|
+
*
|
|
84
|
+
* @default "system" if provided, else "context" if available.
|
|
85
|
+
*/
|
|
86
|
+
user?: UserAccountToken | "system" | "context";
|
|
87
|
+
}
|
|
32
88
|
}
|
|
33
89
|
|
|
34
90
|
/**
|
|
35
91
|
* Provides comprehensive authentication and authorization capabilities with JWT tokens, role-based access control, and user management.
|
|
36
92
|
*
|
|
37
|
-
* The security module enables building secure applications using primitives like `$
|
|
93
|
+
* The security module enables building secure applications using primitives like `$issuer`, `$role`, and `$permission`
|
|
38
94
|
* on class properties. It offers JWT-based authentication, fine-grained permissions, service accounts, and seamless
|
|
39
95
|
* integration with various authentication providers and user management systems.
|
|
40
96
|
*
|
|
41
|
-
*
|
|
97
|
+
* When used with `AlephaServer`, this module automatically registers `ServerSecurityProvider` and `ServerBasicAuthProvider`
|
|
98
|
+
* to protect HTTP routes and actions with JWT and Basic Auth.
|
|
99
|
+
*
|
|
100
|
+
* @see {@link $issuer}
|
|
42
101
|
* @see {@link $role}
|
|
43
102
|
* @see {@link $permission}
|
|
103
|
+
* @see {@link $basicAuth}
|
|
44
104
|
* @module alepha.security
|
|
45
105
|
*/
|
|
46
106
|
export const AlephaSecurity = $module({
|
|
47
107
|
name: "alepha.security",
|
|
48
|
-
primitives: [$
|
|
49
|
-
services: [
|
|
108
|
+
primitives: [$issuer, $role, $permission, $basicAuth],
|
|
109
|
+
services: [
|
|
110
|
+
SecurityProvider,
|
|
111
|
+
JwtProvider,
|
|
112
|
+
CryptoProvider,
|
|
113
|
+
ServerSecurityProvider,
|
|
114
|
+
ServerBasicAuthProvider,
|
|
115
|
+
],
|
|
116
|
+
register: (alepha: Alepha) => {
|
|
117
|
+
// Always register core security providers
|
|
118
|
+
alepha.with(SecurityProvider);
|
|
119
|
+
alepha.with(JwtProvider);
|
|
120
|
+
alepha.with(CryptoProvider);
|
|
121
|
+
|
|
122
|
+
// Register server security providers only if AlephaServer is available
|
|
123
|
+
if (alepha.has(AlephaServer)) {
|
|
124
|
+
alepha.with(ServerSecurityProvider);
|
|
125
|
+
alepha.with(ServerBasicAuthProvider);
|
|
126
|
+
}
|
|
127
|
+
},
|
|
50
128
|
});
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* @deprecated Use `AlephaSecurity` instead. Server security providers are automatically registered when `AlephaServer` is available.
|
|
132
|
+
*/
|
|
133
|
+
export const AlephaServerSecurity = AlephaSecurity;
|
|
@@ -2,9 +2,9 @@ import { randomUUID } from "node:crypto";
|
|
|
2
2
|
import { Alepha } from "alepha";
|
|
3
3
|
import { DateTimeProvider } from "alepha/datetime";
|
|
4
4
|
import { describe, test } from "vitest";
|
|
5
|
-
import { $
|
|
5
|
+
import { $issuer } from "../index.ts";
|
|
6
6
|
|
|
7
|
-
describe("$
|
|
7
|
+
describe("$issuer", () => {
|
|
8
8
|
const roles = [
|
|
9
9
|
{
|
|
10
10
|
name: "admin",
|
|
@@ -18,7 +18,7 @@ describe("$realm", () => {
|
|
|
18
18
|
|
|
19
19
|
test("should create token (access & refresh)", async ({ expect }) => {
|
|
20
20
|
class App {
|
|
21
|
-
|
|
21
|
+
issuer = $issuer({
|
|
22
22
|
secret: "test",
|
|
23
23
|
roles,
|
|
24
24
|
});
|
|
@@ -37,15 +37,15 @@ describe("$realm", () => {
|
|
|
37
37
|
|
|
38
38
|
const now = dt.pause();
|
|
39
39
|
|
|
40
|
-
const token = await app.
|
|
40
|
+
const token = await app.issuer.createToken(user);
|
|
41
41
|
|
|
42
42
|
expect(token).toEqual({
|
|
43
43
|
access_token: expect.any(String),
|
|
44
|
-
expires_in: app.
|
|
44
|
+
expires_in: app.issuer.accessTokenExpiration.asSeconds(),
|
|
45
45
|
refresh_token: expect.any(String),
|
|
46
46
|
token_type: "Bearer",
|
|
47
47
|
issued_at: now.unix(),
|
|
48
|
-
refresh_token_expires_in: app.
|
|
48
|
+
refresh_token_expires_in: app.issuer.refreshTokenExpiration.asSeconds(),
|
|
49
49
|
});
|
|
50
50
|
|
|
51
51
|
expect(
|
|
@@ -54,9 +54,9 @@ describe("$realm", () => {
|
|
|
54
54
|
),
|
|
55
55
|
).toEqual({
|
|
56
56
|
sub: user.id,
|
|
57
|
-
aud: app.
|
|
57
|
+
aud: app.issuer.name,
|
|
58
58
|
iat: now.unix(),
|
|
59
|
-
exp: now.unix() + app.
|
|
59
|
+
exp: now.unix() + app.issuer.accessTokenExpiration.asSeconds(),
|
|
60
60
|
name: user.name,
|
|
61
61
|
roles: ["admin", "user"],
|
|
62
62
|
sid: expect.any(String),
|
|
@@ -71,9 +71,9 @@ describe("$realm", () => {
|
|
|
71
71
|
),
|
|
72
72
|
).toEqual({
|
|
73
73
|
sub: user.id,
|
|
74
|
-
aud: app.
|
|
74
|
+
aud: app.issuer.name,
|
|
75
75
|
iat: now.unix(),
|
|
76
|
-
exp: now.unix() + app.
|
|
76
|
+
exp: now.unix() + app.issuer.refreshTokenExpiration.asSeconds(),
|
|
77
77
|
});
|
|
78
78
|
|
|
79
79
|
expect(
|
|
@@ -88,7 +88,7 @@ describe("$realm", () => {
|
|
|
88
88
|
typ: "refresh",
|
|
89
89
|
});
|
|
90
90
|
|
|
91
|
-
const newToken = await app.
|
|
91
|
+
const newToken = await app.issuer.createToken(user, token);
|
|
92
92
|
expect(newToken).toEqual({
|
|
93
93
|
access_token: expect.any(String),
|
|
94
94
|
issued_at: now.unix(),
|
|
@@ -13,43 +13,46 @@ import type { Role } from "../schemas/roleSchema.ts";
|
|
|
13
13
|
import type { UserAccount } from "../schemas/userAccountInfoSchema.ts";
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
|
-
* Create a new
|
|
16
|
+
* Create a new issuer.
|
|
17
|
+
*
|
|
18
|
+
* An issuer is responsible for creating and verifying JWT tokens.
|
|
19
|
+
* It can be internal (with a secret) or external (with a JWKS).
|
|
17
20
|
*/
|
|
18
|
-
export const $
|
|
19
|
-
return createPrimitive(
|
|
21
|
+
export const $issuer = (options: IssuerPrimitiveOptions): IssuerPrimitive => {
|
|
22
|
+
return createPrimitive(IssuerPrimitive, options);
|
|
20
23
|
};
|
|
21
24
|
|
|
22
25
|
// ---------------------------------------------------------------------------------------------------------------------
|
|
23
26
|
|
|
24
|
-
export type
|
|
27
|
+
export type IssuerPrimitiveOptions = {
|
|
25
28
|
/**
|
|
26
|
-
* Define the
|
|
29
|
+
* Define the issuer name.
|
|
27
30
|
* If not provided, it will use the property key.
|
|
28
31
|
*/
|
|
29
32
|
name?: string;
|
|
30
33
|
|
|
31
34
|
/**
|
|
32
|
-
* Short description about the
|
|
35
|
+
* Short description about the issuer.
|
|
33
36
|
*/
|
|
34
37
|
description?: string;
|
|
35
38
|
|
|
36
39
|
/**
|
|
37
|
-
* All roles available in the
|
|
40
|
+
* All roles available in the issuer. Role is a string (role name) or a Role object (embedded role).
|
|
38
41
|
*/
|
|
39
42
|
roles?: Array<string | Role>;
|
|
40
43
|
|
|
41
44
|
/**
|
|
42
|
-
*
|
|
45
|
+
* Issuer settings.
|
|
43
46
|
*/
|
|
44
|
-
settings?:
|
|
47
|
+
settings?: IssuerSettings;
|
|
45
48
|
|
|
46
49
|
/**
|
|
47
50
|
* Parse the JWT payload to create a user account info.
|
|
48
51
|
*/
|
|
49
52
|
profile?: (jwtPayload: Record<string, any>) => UserAccount;
|
|
50
|
-
} & (
|
|
53
|
+
} & (IssuerInternal | IssuerExternal);
|
|
51
54
|
|
|
52
|
-
export interface
|
|
55
|
+
export interface IssuerSettings {
|
|
53
56
|
accessToken?: {
|
|
54
57
|
/**
|
|
55
58
|
* Lifetime of the access token.
|
|
@@ -87,14 +90,14 @@ export interface RealmSettings {
|
|
|
87
90
|
onDeleteSession?: (refreshToken: string) => Promise<void>;
|
|
88
91
|
}
|
|
89
92
|
|
|
90
|
-
export type
|
|
93
|
+
export type IssuerInternal = {
|
|
91
94
|
/**
|
|
92
95
|
* Internal secret to sign JWT tokens and verify them.
|
|
93
96
|
*/
|
|
94
97
|
secret: string;
|
|
95
98
|
};
|
|
96
99
|
|
|
97
|
-
export interface
|
|
100
|
+
export interface IssuerExternal {
|
|
98
101
|
/**
|
|
99
102
|
* URL to the JWKS (JSON Web Key Set) to verify JWT tokens from external providers.
|
|
100
103
|
*/
|
|
@@ -103,7 +106,7 @@ export interface RealmExternal {
|
|
|
103
106
|
|
|
104
107
|
// ---------------------------------------------------------------------------------------------------------------------
|
|
105
108
|
|
|
106
|
-
export class
|
|
109
|
+
export class IssuerPrimitive extends Primitive<IssuerPrimitiveOptions> {
|
|
107
110
|
protected readonly securityProvider = $inject(SecurityProvider);
|
|
108
111
|
protected readonly dateTimeProvider = $inject(DateTimeProvider);
|
|
109
112
|
protected readonly jwt = $inject(JwtProvider);
|
|
@@ -148,14 +151,14 @@ export class RealmPrimitive extends Primitive<RealmPrimitiveOptions> {
|
|
|
148
151
|
}
|
|
149
152
|
|
|
150
153
|
/**
|
|
151
|
-
* Get all roles in the
|
|
154
|
+
* Get all roles in the issuer.
|
|
152
155
|
*/
|
|
153
156
|
public getRoles(): Role[] {
|
|
154
157
|
return this.securityProvider.getRoles(this.name);
|
|
155
158
|
}
|
|
156
159
|
|
|
157
160
|
/**
|
|
158
|
-
* Set all roles in the
|
|
161
|
+
* Set all roles in the issuer.
|
|
159
162
|
*/
|
|
160
163
|
public async setRoles(roles: Role[]): Promise<void> {
|
|
161
164
|
await this.securityProvider.updateRealm(this.name, roles);
|
|
@@ -336,7 +339,7 @@ export class RealmPrimitive extends Primitive<RealmPrimitiveOptions> {
|
|
|
336
339
|
}
|
|
337
340
|
}
|
|
338
341
|
|
|
339
|
-
$
|
|
342
|
+
$issuer[KIND] = IssuerPrimitive;
|
|
340
343
|
|
|
341
344
|
// ---------------------------------------------------------------------------------------------------------------------
|
|
342
345
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { $inject, createPrimitive, KIND, Primitive } from "alepha";
|
|
2
2
|
import { SecurityProvider } from "../providers/SecurityProvider.ts";
|
|
3
|
+
import type { IssuerPrimitive } from "./$issuer.ts";
|
|
3
4
|
import type { PermissionPrimitive } from "./$permission.ts";
|
|
4
|
-
import type { RealmPrimitive } from "./$realm.ts";
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Create a new role.
|
|
@@ -23,7 +23,7 @@ export interface RolePrimitiveOptions {
|
|
|
23
23
|
*/
|
|
24
24
|
description?: string;
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
issuer?: string | IssuerPrimitive;
|
|
27
27
|
|
|
28
28
|
permissions?: Array<
|
|
29
29
|
| string
|
|
@@ -60,10 +60,10 @@ export class RolePrimitive extends Primitive<RolePrimitiveOptions> {
|
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
/**
|
|
63
|
-
* Get the
|
|
63
|
+
* Get the issuer of the role.
|
|
64
64
|
*/
|
|
65
|
-
public get
|
|
66
|
-
return this.options.
|
|
65
|
+
public get issuer(): string | IssuerPrimitive | undefined {
|
|
66
|
+
return this.options.issuer;
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
public can(permission: string | PermissionPrimitive): boolean {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Alepha } from "alepha";
|
|
2
2
|
import { DateTimeProvider } from "alepha/datetime";
|
|
3
3
|
import { describe, expect, it } from "vitest";
|
|
4
|
-
import { $
|
|
4
|
+
import { $issuer, $serviceAccount } from "../index.ts";
|
|
5
5
|
|
|
6
6
|
class App {
|
|
7
7
|
oauth2 = $serviceAccount({
|
|
@@ -16,12 +16,12 @@ class App {
|
|
|
16
16
|
expiresIn: 300, // 5 minutes
|
|
17
17
|
};
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
secret: "your-
|
|
19
|
+
issuer = $issuer({
|
|
20
|
+
secret: "your-issuer-secret",
|
|
21
21
|
});
|
|
22
22
|
|
|
23
23
|
jwt = $serviceAccount({
|
|
24
|
-
|
|
24
|
+
issuer: this.issuer,
|
|
25
25
|
user: {
|
|
26
26
|
id: "service-account",
|
|
27
27
|
},
|
|
@@ -42,7 +42,7 @@ describe("$serviceAccount", () => {
|
|
|
42
42
|
);
|
|
43
43
|
expect(payload).toEqual({
|
|
44
44
|
sub: expect.any(String),
|
|
45
|
-
aud: "
|
|
45
|
+
aud: "issuer",
|
|
46
46
|
iat: expect.any(Number),
|
|
47
47
|
exp: expect.any(Number),
|
|
48
48
|
sid: expect.any(String),
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { $context } from "alepha";
|
|
2
2
|
import { DateTimeProvider } from "alepha/datetime";
|
|
3
3
|
import type { UserAccount } from "../schemas/userAccountInfoSchema.ts";
|
|
4
|
-
import type { AccessTokenResponse,
|
|
4
|
+
import type { AccessTokenResponse, IssuerPrimitive } from "./$issuer.ts";
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Allow to get an access token for a service account.
|
|
@@ -138,7 +138,7 @@ export const $serviceAccount = (
|
|
|
138
138
|
return tokenFromCache;
|
|
139
139
|
}
|
|
140
140
|
|
|
141
|
-
const token = await options.
|
|
141
|
+
const token = await options.issuer.createToken(options.user);
|
|
142
142
|
|
|
143
143
|
cacheToken({
|
|
144
144
|
...token,
|
|
@@ -157,7 +157,7 @@ export type ServiceAccountPrimitiveOptions = {
|
|
|
157
157
|
oauth2: Oauth2ServiceAccountPrimitiveOptions;
|
|
158
158
|
}
|
|
159
159
|
| {
|
|
160
|
-
|
|
160
|
+
issuer: IssuerPrimitive;
|
|
161
161
|
user: UserAccount;
|
|
162
162
|
}
|
|
163
163
|
);
|
|
@@ -1,19 +1,17 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
2
|
import { $hook, $inject, Alepha } from "alepha";
|
|
3
3
|
import { $logger } from "alepha/logger";
|
|
4
|
-
import {
|
|
5
|
-
JwtProvider,
|
|
6
|
-
type Permission,
|
|
7
|
-
SecurityProvider,
|
|
8
|
-
type UserAccountToken,
|
|
9
|
-
userAccountInfoSchema,
|
|
10
|
-
} from "alepha/security";
|
|
11
4
|
import {
|
|
12
5
|
$action,
|
|
13
6
|
ForbiddenError,
|
|
14
7
|
type ServerRequest,
|
|
15
8
|
UnauthorizedError,
|
|
16
9
|
} from "alepha/server";
|
|
10
|
+
import type { UserAccountToken } from "../interfaces/UserAccountToken.ts";
|
|
11
|
+
import type { Permission } from "../schemas/permissionSchema.ts";
|
|
12
|
+
import { userAccountInfoSchema } from "../schemas/userAccountInfoSchema.ts";
|
|
13
|
+
import { JwtProvider } from "./JwtProvider.ts";
|
|
14
|
+
import { SecurityProvider } from "./SecurityProvider.ts";
|
|
17
15
|
import {
|
|
18
16
|
type BasicAuthOptions,
|
|
19
17
|
isBasicAuth,
|
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
import { DateTimeProvider } from "alepha/datetime";
|
|
10
10
|
import {
|
|
11
11
|
type AccessTokenResponse,
|
|
12
|
-
type
|
|
12
|
+
type IssuerPrimitive,
|
|
13
13
|
SecurityError,
|
|
14
14
|
SecurityProvider,
|
|
15
15
|
type UserAccount,
|
|
@@ -105,10 +105,10 @@ export type AuthExternal = {
|
|
|
105
105
|
* When using your own authentication system, e.g. using a database to store user accounts.
|
|
106
106
|
* This is usually used with a custom login form.
|
|
107
107
|
*
|
|
108
|
-
* This relies on the `
|
|
108
|
+
* This relies on the `issuer`, which is used to create/verify the access token.
|
|
109
109
|
*/
|
|
110
110
|
export type AuthInternal = {
|
|
111
|
-
|
|
111
|
+
issuer: IssuerPrimitive;
|
|
112
112
|
} & (
|
|
113
113
|
| {
|
|
114
114
|
/**
|
|
@@ -261,9 +261,9 @@ export class AuthPrimitive extends Primitive<AuthPrimitiveOptions> {
|
|
|
261
261
|
return this.options.name ?? this.config.propertyKey;
|
|
262
262
|
}
|
|
263
263
|
|
|
264
|
-
public get
|
|
265
|
-
if ("
|
|
266
|
-
return this.options.
|
|
264
|
+
public get issuer(): IssuerPrimitive | undefined {
|
|
265
|
+
if ("issuer" in this.options) {
|
|
266
|
+
return this.options.issuer;
|
|
267
267
|
}
|
|
268
268
|
return undefined;
|
|
269
269
|
}
|
|
@@ -308,13 +308,13 @@ export class AuthPrimitive extends Primitive<AuthPrimitiveOptions> {
|
|
|
308
308
|
refreshToken: string,
|
|
309
309
|
accessToken?: string,
|
|
310
310
|
): Promise<AccessTokenResponse> {
|
|
311
|
-
if ("
|
|
312
|
-
return this.options.
|
|
311
|
+
if ("issuer" in this.options) {
|
|
312
|
+
return this.options.issuer
|
|
313
313
|
.refreshToken(refreshToken, accessToken)
|
|
314
314
|
.then((it) => it.tokens)
|
|
315
315
|
.catch((error) => {
|
|
316
316
|
throw new SecurityError(
|
|
317
|
-
"Failed to refresh access token using the refresh token (
|
|
317
|
+
"Failed to refresh access token using the refresh token (issuer)",
|
|
318
318
|
{
|
|
319
319
|
cause: error,
|
|
320
320
|
},
|
|
@@ -337,7 +337,7 @@ export class AuthPrimitive extends Primitive<AuthPrimitiveOptions> {
|
|
|
337
337
|
}
|
|
338
338
|
|
|
339
339
|
throw new AlephaError(
|
|
340
|
-
"No
|
|
340
|
+
"No issuer or OAuth2 configuration available for refreshing the access token",
|
|
341
341
|
);
|
|
342
342
|
}
|
|
343
343
|
|