alepha 0.14.4 → 0.15.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 (277) hide show
  1. package/README.md +1 -4
  2. package/dist/api/audits/index.d.ts +619 -731
  3. package/dist/api/audits/index.d.ts.map +1 -1
  4. package/dist/api/files/index.d.ts +185 -298
  5. package/dist/api/files/index.d.ts.map +1 -1
  6. package/dist/api/files/index.js +0 -1
  7. package/dist/api/files/index.js.map +1 -1
  8. package/dist/api/jobs/index.d.ts +245 -356
  9. package/dist/api/jobs/index.d.ts.map +1 -1
  10. package/dist/api/notifications/index.d.ts +238 -350
  11. package/dist/api/notifications/index.d.ts.map +1 -1
  12. package/dist/api/parameters/index.d.ts +499 -611
  13. package/dist/api/parameters/index.d.ts.map +1 -1
  14. package/dist/api/users/index.browser.js +1 -2
  15. package/dist/api/users/index.browser.js.map +1 -1
  16. package/dist/api/users/index.d.ts +1697 -1804
  17. package/dist/api/users/index.d.ts.map +1 -1
  18. package/dist/api/users/index.js +178 -151
  19. package/dist/api/users/index.js.map +1 -1
  20. package/dist/api/verifications/index.d.ts +132 -132
  21. package/dist/api/verifications/index.d.ts.map +1 -1
  22. package/dist/batch/index.d.ts +122 -122
  23. package/dist/batch/index.d.ts.map +1 -1
  24. package/dist/batch/index.js +1 -2
  25. package/dist/batch/index.js.map +1 -1
  26. package/dist/bucket/index.d.ts +163 -163
  27. package/dist/bucket/index.d.ts.map +1 -1
  28. package/dist/cache/core/index.d.ts +46 -46
  29. package/dist/cache/core/index.d.ts.map +1 -1
  30. package/dist/cache/redis/index.d.ts.map +1 -1
  31. package/dist/cli/index.d.ts +302 -299
  32. package/dist/cli/index.d.ts.map +1 -1
  33. package/dist/cli/index.js +966 -564
  34. package/dist/cli/index.js.map +1 -1
  35. package/dist/command/index.d.ts +303 -299
  36. package/dist/command/index.d.ts.map +1 -1
  37. package/dist/command/index.js +11 -7
  38. package/dist/command/index.js.map +1 -1
  39. package/dist/core/index.browser.js +419 -99
  40. package/dist/core/index.browser.js.map +1 -1
  41. package/dist/core/index.d.ts +718 -625
  42. package/dist/core/index.d.ts.map +1 -1
  43. package/dist/core/index.js +420 -99
  44. package/dist/core/index.js.map +1 -1
  45. package/dist/core/index.native.js +419 -99
  46. package/dist/core/index.native.js.map +1 -1
  47. package/dist/datetime/index.d.ts +44 -44
  48. package/dist/datetime/index.d.ts.map +1 -1
  49. package/dist/datetime/index.js +4 -4
  50. package/dist/datetime/index.js.map +1 -1
  51. package/dist/email/index.d.ts +97 -50
  52. package/dist/email/index.d.ts.map +1 -1
  53. package/dist/email/index.js +129 -33
  54. package/dist/email/index.js.map +1 -1
  55. package/dist/fake/index.d.ts +7981 -14
  56. package/dist/fake/index.d.ts.map +1 -1
  57. package/dist/file/index.d.ts +523 -390
  58. package/dist/file/index.d.ts.map +1 -1
  59. package/dist/file/index.js +253 -1
  60. package/dist/file/index.js.map +1 -1
  61. package/dist/lock/core/index.d.ts +208 -208
  62. package/dist/lock/core/index.d.ts.map +1 -1
  63. package/dist/lock/redis/index.d.ts.map +1 -1
  64. package/dist/logger/index.d.ts +25 -26
  65. package/dist/logger/index.d.ts.map +1 -1
  66. package/dist/mcp/index.d.ts +197 -197
  67. package/dist/mcp/index.d.ts.map +1 -1
  68. package/dist/orm/chunk-DtkW-qnP.js +38 -0
  69. package/dist/orm/index.browser.js.map +1 -1
  70. package/dist/orm/index.bun.js +2814 -0
  71. package/dist/orm/index.bun.js.map +1 -0
  72. package/dist/orm/index.d.ts +1205 -1057
  73. package/dist/orm/index.d.ts.map +1 -1
  74. package/dist/orm/index.js +2056 -1753
  75. package/dist/orm/index.js.map +1 -1
  76. package/dist/queue/core/index.d.ts +248 -248
  77. package/dist/queue/core/index.d.ts.map +1 -1
  78. package/dist/queue/redis/index.d.ts.map +1 -1
  79. package/dist/redis/index.bun.js +285 -0
  80. package/dist/redis/index.bun.js.map +1 -0
  81. package/dist/redis/index.d.ts +118 -136
  82. package/dist/redis/index.d.ts.map +1 -1
  83. package/dist/redis/index.js +18 -38
  84. package/dist/redis/index.js.map +1 -1
  85. package/dist/retry/index.d.ts +69 -69
  86. package/dist/retry/index.d.ts.map +1 -1
  87. package/dist/router/index.d.ts +6 -6
  88. package/dist/router/index.d.ts.map +1 -1
  89. package/dist/scheduler/index.d.ts +25 -25
  90. package/dist/scheduler/index.d.ts.map +1 -1
  91. package/dist/security/index.browser.js +5 -1
  92. package/dist/security/index.browser.js.map +1 -1
  93. package/dist/security/index.d.ts +417 -254
  94. package/dist/security/index.d.ts.map +1 -1
  95. package/dist/security/index.js +386 -86
  96. package/dist/security/index.js.map +1 -1
  97. package/dist/server/auth/index.d.ts +277 -277
  98. package/dist/server/auth/index.d.ts.map +1 -1
  99. package/dist/server/auth/index.js +20 -20
  100. package/dist/server/auth/index.js.map +1 -1
  101. package/dist/server/cache/index.d.ts +60 -57
  102. package/dist/server/cache/index.d.ts.map +1 -1
  103. package/dist/server/cache/index.js +1 -1
  104. package/dist/server/cache/index.js.map +1 -1
  105. package/dist/server/compress/index.d.ts +3 -3
  106. package/dist/server/compress/index.d.ts.map +1 -1
  107. package/dist/server/cookies/index.d.ts +6 -6
  108. package/dist/server/cookies/index.d.ts.map +1 -1
  109. package/dist/server/cookies/index.js +3 -3
  110. package/dist/server/cookies/index.js.map +1 -1
  111. package/dist/server/core/index.d.ts +242 -150
  112. package/dist/server/core/index.d.ts.map +1 -1
  113. package/dist/server/core/index.js +288 -122
  114. package/dist/server/core/index.js.map +1 -1
  115. package/dist/server/cors/index.d.ts +11 -12
  116. package/dist/server/cors/index.d.ts.map +1 -1
  117. package/dist/server/health/index.d.ts +0 -1
  118. package/dist/server/health/index.d.ts.map +1 -1
  119. package/dist/server/helmet/index.d.ts +2 -2
  120. package/dist/server/helmet/index.d.ts.map +1 -1
  121. package/dist/server/links/index.browser.js.map +1 -1
  122. package/dist/server/links/index.d.ts +84 -85
  123. package/dist/server/links/index.d.ts.map +1 -1
  124. package/dist/server/links/index.js +1 -2
  125. package/dist/server/links/index.js.map +1 -1
  126. package/dist/server/metrics/index.d.ts.map +1 -1
  127. package/dist/server/multipart/index.d.ts +6 -6
  128. package/dist/server/multipart/index.d.ts.map +1 -1
  129. package/dist/server/proxy/index.d.ts +102 -103
  130. package/dist/server/proxy/index.d.ts.map +1 -1
  131. package/dist/server/rate-limit/index.d.ts +16 -16
  132. package/dist/server/rate-limit/index.d.ts.map +1 -1
  133. package/dist/server/static/index.d.ts +44 -44
  134. package/dist/server/static/index.d.ts.map +1 -1
  135. package/dist/server/swagger/index.d.ts +48 -49
  136. package/dist/server/swagger/index.d.ts.map +1 -1
  137. package/dist/server/swagger/index.js +1 -2
  138. package/dist/server/swagger/index.js.map +1 -1
  139. package/dist/sms/index.d.ts +13 -11
  140. package/dist/sms/index.d.ts.map +1 -1
  141. package/dist/sms/index.js +7 -7
  142. package/dist/sms/index.js.map +1 -1
  143. package/dist/thread/index.d.ts +71 -72
  144. package/dist/thread/index.d.ts.map +1 -1
  145. package/dist/topic/core/index.d.ts +318 -318
  146. package/dist/topic/core/index.d.ts.map +1 -1
  147. package/dist/topic/redis/index.d.ts +6 -6
  148. package/dist/topic/redis/index.d.ts.map +1 -1
  149. package/dist/vite/index.d.ts +5720 -159
  150. package/dist/vite/index.d.ts.map +1 -1
  151. package/dist/vite/index.js +41 -18
  152. package/dist/vite/index.js.map +1 -1
  153. package/dist/websocket/index.browser.js +6 -6
  154. package/dist/websocket/index.browser.js.map +1 -1
  155. package/dist/websocket/index.d.ts +247 -247
  156. package/dist/websocket/index.d.ts.map +1 -1
  157. package/dist/websocket/index.js +6 -6
  158. package/dist/websocket/index.js.map +1 -1
  159. package/package.json +9 -14
  160. package/src/api/files/controllers/AdminFileStatsController.ts +0 -1
  161. package/src/api/users/atoms/realmAuthSettingsAtom.ts +5 -0
  162. package/src/api/users/controllers/{UserRealmController.ts → RealmController.ts} +11 -11
  163. package/src/api/users/entities/users.ts +1 -1
  164. package/src/api/users/index.ts +8 -8
  165. package/src/api/users/primitives/{$userRealm.ts → $realm.ts} +17 -19
  166. package/src/api/users/providers/{UserRealmProvider.ts → RealmProvider.ts} +26 -30
  167. package/src/api/users/schemas/{userRealmConfigSchema.ts → realmConfigSchema.ts} +2 -2
  168. package/src/api/users/services/CredentialService.ts +7 -7
  169. package/src/api/users/services/IdentityService.ts +4 -4
  170. package/src/api/users/services/RegistrationService.spec.ts +25 -27
  171. package/src/api/users/services/RegistrationService.ts +38 -27
  172. package/src/api/users/services/SessionCrudService.ts +3 -3
  173. package/src/api/users/services/SessionService.spec.ts +3 -3
  174. package/src/api/users/services/SessionService.ts +28 -9
  175. package/src/api/users/services/UserService.ts +7 -7
  176. package/src/batch/providers/BatchProvider.ts +1 -2
  177. package/src/cli/apps/AlephaPackageBuilderCli.ts +38 -19
  178. package/src/cli/assets/apiHelloControllerTs.ts +18 -0
  179. package/src/cli/assets/apiIndexTs.ts +16 -0
  180. package/src/cli/assets/claudeMd.ts +303 -0
  181. package/src/cli/assets/mainBrowserTs.ts +2 -2
  182. package/src/cli/assets/mainServerTs.ts +24 -0
  183. package/src/cli/assets/webAppRouterTs.ts +15 -0
  184. package/src/cli/assets/webHelloComponentTsx.ts +16 -0
  185. package/src/cli/assets/webIndexTs.ts +16 -0
  186. package/src/cli/commands/build.ts +41 -21
  187. package/src/cli/commands/db.ts +21 -18
  188. package/src/cli/commands/deploy.ts +17 -5
  189. package/src/cli/commands/dev.ts +13 -17
  190. package/src/cli/commands/format.ts +8 -2
  191. package/src/cli/commands/init.ts +74 -29
  192. package/src/cli/commands/lint.ts +8 -2
  193. package/src/cli/commands/test.ts +8 -2
  194. package/src/cli/commands/typecheck.ts +5 -1
  195. package/src/cli/commands/verify.ts +4 -2
  196. package/src/cli/services/AlephaCliUtils.ts +39 -600
  197. package/src/cli/services/PackageManagerUtils.ts +301 -0
  198. package/src/cli/services/ProjectScaffolder.ts +306 -0
  199. package/src/command/helpers/Runner.ts +15 -3
  200. package/src/core/__tests__/Alepha-graph.spec.ts +4 -0
  201. package/src/core/index.shared.ts +1 -0
  202. package/src/core/index.ts +2 -0
  203. package/src/core/primitives/$hook.ts +6 -2
  204. package/src/core/primitives/$module.spec.ts +4 -0
  205. package/src/core/providers/AlsProvider.ts +1 -1
  206. package/src/core/providers/CodecManager.spec.ts +12 -6
  207. package/src/core/providers/CodecManager.ts +26 -6
  208. package/src/core/providers/EventManager.ts +169 -13
  209. package/src/core/providers/KeylessJsonSchemaCodec.spec.ts +621 -0
  210. package/src/core/providers/KeylessJsonSchemaCodec.ts +407 -0
  211. package/src/core/providers/StateManager.spec.ts +27 -16
  212. package/src/email/providers/LocalEmailProvider.spec.ts +111 -87
  213. package/src/email/providers/LocalEmailProvider.ts +52 -15
  214. package/src/email/providers/NodemailerEmailProvider.ts +167 -56
  215. package/src/file/errors/FileError.ts +7 -0
  216. package/src/file/index.ts +9 -1
  217. package/src/file/providers/MemoryFileSystemProvider.ts +393 -0
  218. package/src/orm/index.browser.ts +1 -19
  219. package/src/orm/index.bun.ts +77 -0
  220. package/src/orm/index.shared-server.ts +22 -0
  221. package/src/orm/index.shared.ts +15 -0
  222. package/src/orm/index.ts +19 -39
  223. package/src/orm/providers/drivers/BunPostgresProvider.ts +3 -5
  224. package/src/orm/providers/drivers/BunSqliteProvider.ts +1 -1
  225. package/src/orm/providers/drivers/CloudflareD1Provider.ts +4 -0
  226. package/src/orm/providers/drivers/DatabaseProvider.ts +4 -0
  227. package/src/orm/providers/drivers/PglitePostgresProvider.ts +4 -0
  228. package/src/orm/services/Repository.ts +8 -0
  229. package/src/redis/index.bun.ts +35 -0
  230. package/src/redis/providers/BunRedisProvider.ts +12 -43
  231. package/src/redis/providers/BunRedisSubscriberProvider.ts +2 -3
  232. package/src/redis/providers/NodeRedisProvider.ts +16 -34
  233. package/src/{server/security → security}/__tests__/BasicAuth.spec.ts +11 -11
  234. package/src/{server/security → security}/__tests__/ServerSecurityProvider-realm.spec.ts +21 -16
  235. package/src/{server/security/providers → security/__tests__}/ServerSecurityProvider.spec.ts +5 -5
  236. package/src/security/index.browser.ts +5 -0
  237. package/src/security/index.ts +90 -7
  238. package/src/security/primitives/{$realm.spec.ts → $issuer.spec.ts} +11 -11
  239. package/src/security/primitives/{$realm.ts → $issuer.ts} +20 -17
  240. package/src/security/primitives/$role.ts +5 -5
  241. package/src/security/primitives/$serviceAccount.spec.ts +5 -5
  242. package/src/security/primitives/$serviceAccount.ts +3 -3
  243. package/src/{server/security → security}/providers/ServerSecurityProvider.ts +5 -7
  244. package/src/server/auth/primitives/$auth.ts +10 -10
  245. package/src/server/auth/primitives/$authCredentials.ts +3 -3
  246. package/src/server/auth/primitives/$authGithub.ts +3 -3
  247. package/src/server/auth/primitives/$authGoogle.ts +3 -3
  248. package/src/server/auth/providers/ServerAuthProvider.ts +13 -13
  249. package/src/server/cache/providers/ServerCacheProvider.ts +1 -1
  250. package/src/server/cookies/providers/ServerCookiesProvider.ts +3 -3
  251. package/src/server/core/providers/NodeHttpServerProvider.ts +25 -6
  252. package/src/server/core/providers/ServerBodyParserProvider.ts +19 -23
  253. package/src/server/core/providers/ServerLoggerProvider.ts +23 -19
  254. package/src/server/core/providers/ServerProvider.ts +144 -21
  255. package/src/server/core/providers/ServerRouterProvider.ts +259 -115
  256. package/src/server/core/providers/ServerTimingProvider.ts +2 -2
  257. package/src/server/links/index.ts +1 -1
  258. package/src/server/links/providers/LinkProvider.ts +1 -1
  259. package/src/server/swagger/index.ts +1 -1
  260. package/src/sms/providers/LocalSmsProvider.spec.ts +153 -111
  261. package/src/sms/providers/LocalSmsProvider.ts +8 -7
  262. package/src/vite/helpers/boot.ts +28 -17
  263. package/src/vite/tasks/buildServer.ts +12 -1
  264. package/src/vite/tasks/devServer.ts +3 -1
  265. package/src/vite/tasks/generateCloudflare.ts +7 -0
  266. package/dist/server/security/index.browser.js +0 -13
  267. package/dist/server/security/index.browser.js.map +0 -1
  268. package/dist/server/security/index.d.ts +0 -173
  269. package/dist/server/security/index.d.ts.map +0 -1
  270. package/dist/server/security/index.js +0 -311
  271. package/dist/server/security/index.js.map +0 -1
  272. package/src/cli/assets/appRouterTs.ts +0 -9
  273. package/src/cli/assets/mainTs.ts +0 -13
  274. package/src/server/security/index.browser.ts +0 -10
  275. package/src/server/security/index.ts +0 -94
  276. /package/src/{server/security → security}/primitives/$basicAuth.ts +0 -0
  277. /package/src/{server/security → security}/providers/ServerBasicAuthProvider.ts +0 -0
@@ -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 `$realm`, `$role`, and `$permission`
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
- * @see {@link $realm}
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: [$realm, $role, $permission],
49
- services: [SecurityProvider, JwtProvider, CryptoProvider],
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 { $realm } from "../index.ts";
5
+ import { $issuer } from "../index.ts";
6
6
 
7
- describe("$realm", () => {
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
- realm = $realm({
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.realm.createToken(user);
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.realm.accessTokenExpiration.asSeconds(),
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.realm.refreshTokenExpiration.asSeconds(),
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.realm.name,
57
+ aud: app.issuer.name,
58
58
  iat: now.unix(),
59
- exp: now.unix() + app.realm.accessTokenExpiration.asSeconds(),
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.realm.name,
74
+ aud: app.issuer.name,
75
75
  iat: now.unix(),
76
- exp: now.unix() + app.realm.refreshTokenExpiration.asSeconds(),
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.realm.createToken(user, token);
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 realm.
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 $realm = (options: RealmPrimitiveOptions): RealmPrimitive => {
19
- return createPrimitive(RealmPrimitive, options);
21
+ export const $issuer = (options: IssuerPrimitiveOptions): IssuerPrimitive => {
22
+ return createPrimitive(IssuerPrimitive, options);
20
23
  };
21
24
 
22
25
  // ---------------------------------------------------------------------------------------------------------------------
23
26
 
24
- export type RealmPrimitiveOptions = {
27
+ export type IssuerPrimitiveOptions = {
25
28
  /**
26
- * Define the realm name.
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 realm.
35
+ * Short description about the issuer.
33
36
  */
34
37
  description?: string;
35
38
 
36
39
  /**
37
- * All roles available in the realm. Role is a string (role name) or a Role object (embedded role).
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
- * Realm settings.
45
+ * Issuer settings.
43
46
  */
44
- settings?: RealmSettings;
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
- } & (RealmInternal | RealmExternal);
53
+ } & (IssuerInternal | IssuerExternal);
51
54
 
52
- export interface RealmSettings {
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 RealmInternal = {
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 RealmExternal {
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 RealmPrimitive extends Primitive<RealmPrimitiveOptions> {
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 realm.
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 realm.
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
- $realm[KIND] = RealmPrimitive;
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
- realm?: string | RealmPrimitive;
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 realm of the role.
63
+ * Get the issuer of the role.
64
64
  */
65
- public get realm(): string | RealmPrimitive | undefined {
66
- return this.options.realm;
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 { $realm, $serviceAccount } from "../index.ts";
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
- realm = $realm({
20
- secret: "your-realm-secret",
19
+ issuer = $issuer({
20
+ secret: "your-issuer-secret",
21
21
  });
22
22
 
23
23
  jwt = $serviceAccount({
24
- realm: this.realm,
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: "realm",
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, RealmPrimitive } from "./$realm.ts";
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.realm.createToken(options.user);
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
- realm: RealmPrimitive;
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 RealmPrimitive,
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 `realm`, which is used to create/verify the access token.
108
+ * This relies on the `issuer`, which is used to create/verify the access token.
109
109
  */
110
110
  export type AuthInternal = {
111
- realm: RealmPrimitive;
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 realm(): RealmPrimitive | undefined {
265
- if ("realm" in this.options) {
266
- return this.options.realm;
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 ("realm" in this.options) {
312
- return this.options.realm
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 (realm)",
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 realm or OAuth2 configuration available for refreshing the access token",
340
+ "No issuer or OAuth2 configuration available for refreshing the access token",
341
341
  );
342
342
  }
343
343
 
@@ -1,5 +1,5 @@
1
1
  import { AlephaError } from "alepha";
2
- import type { RealmPrimitive } from "alepha/security";
2
+ import type { IssuerPrimitive } from "alepha/security";
3
3
  import {
4
4
  $auth,
5
5
  type CredentialsFn,
@@ -13,7 +13,7 @@ import {
13
13
  * Uses username and password to authenticate users.
14
14
  */
15
15
  export const $authCredentials = (
16
- realm: RealmPrimitive & WithLoginFn,
16
+ realm: IssuerPrimitive & WithLoginFn,
17
17
  options: Partial<CredentialsOptions> = {},
18
18
  ) => {
19
19
  const name = "credentials";
@@ -29,7 +29,7 @@ export const $authCredentials = (
29
29
  }
30
30
 
31
31
  return $auth({
32
- realm,
32
+ issuer: realm,
33
33
  name,
34
34
  credentials: {
35
35
  account,
@@ -1,5 +1,5 @@
1
1
  import { $context, AlephaError, t } from "alepha";
2
- import type { RealmPrimitive } from "alepha/security";
2
+ import type { IssuerPrimitive } from "alepha/security";
3
3
  import type { OAuth2Profile } from "../providers/ServerAuthProvider.ts";
4
4
  import {
5
5
  $auth,
@@ -19,7 +19,7 @@ import {
19
19
  * - `GITHUB_CLIENT_SECRET`: The client secret obtained from the GitHub Developer Settings.
20
20
  */
21
21
  export const $authGithub = (
22
- realm: RealmPrimitive & WithLinkFn,
22
+ realm: IssuerPrimitive & WithLinkFn,
23
23
  options: Partial<OidcOptions> = {},
24
24
  ) => {
25
25
  const { alepha } = $context();
@@ -45,7 +45,7 @@ export const $authGithub = (
45
45
  }
46
46
 
47
47
  return $auth({
48
- realm,
48
+ issuer: realm,
49
49
  name,
50
50
  oauth: {
51
51
  clientId: env.GITHUB_CLIENT_ID!,
@@ -1,5 +1,5 @@
1
1
  import { $context, AlephaError, t } from "alepha";
2
- import type { RealmPrimitive } from "alepha/security";
2
+ import type { IssuerPrimitive } from "alepha/security";
3
3
  import {
4
4
  $auth,
5
5
  type LinkAccountFn,
@@ -18,7 +18,7 @@ import {
18
18
  * - `GOOGLE_CLIENT_SECRET`: The client secret obtained from the Google Developer Console.
19
19
  */
20
20
  export const $authGoogle = (
21
- realm: RealmPrimitive & WithLinkFn,
21
+ realm: IssuerPrimitive & WithLinkFn,
22
22
  options: Partial<OidcOptions> = {},
23
23
  ) => {
24
24
  const { alepha } = $context();
@@ -44,7 +44,7 @@ export const $authGoogle = (
44
44
  }
45
45
 
46
46
  return $auth({
47
- realm,
47
+ issuer: realm,
48
48
  name,
49
49
  oidc: {
50
50
  issuer: "https://accounts.google.com",
@@ -71,8 +71,8 @@ export class ServerAuthProvider {
71
71
 
72
72
  for (const identity of this.identities) {
73
73
  if (filters.realmName) {
74
- const realm = "realm" in identity.options && identity.options.realm;
75
- if (!realm || realm.name !== filters.realmName) {
74
+ const issuer = identity.issuer;
75
+ if (!issuer || issuer.name !== filters.realmName) {
76
76
  continue;
77
77
  }
78
78
  }
@@ -145,7 +145,7 @@ export class ServerAuthProvider {
145
145
  // [feature] support for auth providers with fallback
146
146
  if (!request.headers.authorization) {
147
147
  for (const provider of this.identities) {
148
- if (!("realm" in provider.options) && !!provider.options.fallback) {
148
+ if ("fallback" in provider.options && !!provider.options.fallback) {
149
149
  const token = await provider.options.fallback();
150
150
  if (token) {
151
151
  request.headers.authorization = `Bearer ${token}`;
@@ -340,8 +340,8 @@ export class ServerAuthProvider {
340
340
  realm: query.realm,
341
341
  });
342
342
 
343
- const realm = "realm" in provider.options && provider.options.realm;
344
- if (!realm) {
343
+ const issuer = provider.issuer;
344
+ if (!issuer) {
345
345
  throw new SecurityError(
346
346
  `Auth provider '${query.provider}' does not support password grant`,
347
347
  );
@@ -373,7 +373,7 @@ export class ServerAuthProvider {
373
373
 
374
374
  const tokens = {
375
375
  provider: query.provider,
376
- ...(await realm.createToken(user)),
376
+ ...(await issuer.createToken(user)),
377
377
  };
378
378
 
379
379
  // for web applications, we store tokens in cookies
@@ -519,10 +519,10 @@ export class ServerAuthProvider {
519
519
 
520
520
  this.authorizationCode.del({ cookies });
521
521
 
522
- const realm = "realm" in provider.options && provider.options.realm;
522
+ const issuer = provider.issuer;
523
523
 
524
524
  // external, full OIDC System (e.g. Keycloak, Auth0)
525
- if (!realm) {
525
+ if (!issuer) {
526
526
  this.setTokens(externalTokens, cookies);
527
527
  reply.redirect(redirectUri);
528
528
  return;
@@ -531,7 +531,7 @@ export class ServerAuthProvider {
531
531
  // internal, we need to create our own tokens
532
532
 
533
533
  const user = await provider.user(externalTokens);
534
- const tokens = await realm.createToken(user);
534
+ const tokens = await issuer.createToken(user);
535
535
 
536
536
  this.setTokens(
537
537
  {
@@ -570,9 +570,9 @@ export class ServerAuthProvider {
570
570
  this.tokens.del({ cookies });
571
571
 
572
572
  // for internal providers, we can delete the session - if available
573
- if ("realm" in provider.options && tokens.refresh_token) {
573
+ if (provider.issuer && tokens.refresh_token) {
574
574
  const onDeleteSession =
575
- provider.options.realm.options.settings?.onDeleteSession;
575
+ provider.issuer.options.settings?.onDeleteSession;
576
576
  if (onDeleteSession) {
577
577
  try {
578
578
  await onDeleteSession(tokens.refresh_token);
@@ -635,8 +635,8 @@ export class ServerAuthProvider {
635
635
  return false;
636
636
  }
637
637
 
638
- // If realm filter is specified, match against provider's realm
639
- if (realmName && identity.realm?.name !== realmName) {
638
+ // If realm filter is specified, match against provider's issuer
639
+ if (realmName && identity.issuer?.name !== realmName) {
640
640
  return false;
641
641
  }
642
642
 
@@ -204,7 +204,7 @@ export class ServerCacheProvider {
204
204
 
205
205
  protected readonly onSend = $hook({
206
206
  on: "server:onSend",
207
- handler: async ({ route, request }) => {
207
+ handler: ({ route, request }) => {
208
208
  // before sending the response, check if the ETag matches
209
209
  // and if so, return a 304 Not Modified response
210
210
  // -> this is only relevant for etag-only routes, not cached routes <-