@miiajs/auth 0.1.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Ruslan Matiushev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,17 @@
1
+ # @miiajs/auth
2
+
3
+ Lightweight auth primitives for MiiaJS - `AuthProvider` interface, `AuthGuard` factory, HTTP token extractors, timing-safe equality.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @miiajs/auth
9
+ ```
10
+
11
+ ## Documentation
12
+
13
+ **[miiajs.com/docs/packages/auth](https://miiajs.com/docs/packages/auth)**
14
+
15
+ ## License
16
+
17
+ MIT
@@ -0,0 +1,18 @@
1
+ import type { Constructor, Guard } from '@miiajs/core';
2
+ import type { AuthProvider } from './provider.js';
3
+ /**
4
+ * Build a guard that authenticates the request using one or more
5
+ * `AuthProvider` classes. Providers are tried in order - the first one that
6
+ * resolves without throwing wins, and its result is assigned to `ctx.user`.
7
+ *
8
+ * **OR semantics:** if a provider throws an `HttpException`, the guard moves
9
+ * on to the next provider. Any other error (TypeError, ReferenceError, etc.)
10
+ * is treated as a programming bug and rethrown immediately so it is not
11
+ * silently masked by a later provider.
12
+ *
13
+ * If every provider throws an `HttpException`, the last error is rethrown.
14
+ *
15
+ * For AND semantics, stack guards instead: `@UseGuard(AuthGuard(JwtAuth), Roles('admin'))`.
16
+ */
17
+ export declare function AuthGuard(first: Constructor<AuthProvider>, ...rest: Constructor<AuthProvider>[]): Guard;
18
+ //# sourceMappingURL=auth.guard.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.guard.d.ts","sourceRoot":"","sources":["../src/auth.guard.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAe,WAAW,EAAE,KAAK,EAAkB,MAAM,cAAc,CAAA;AACnF,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAEjD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,WAAW,CAAC,YAAY,CAAC,EAAE,GAAG,IAAI,EAAE,WAAW,CAAC,YAAY,CAAC,EAAE,GAAG,KAAK,CA8BvG"}
@@ -0,0 +1,46 @@
1
+ import { GUARD_FACTORY, HttpException, UnauthorizedException, inject } from '@miiajs/core';
2
+ /**
3
+ * Build a guard that authenticates the request using one or more
4
+ * `AuthProvider` classes. Providers are tried in order - the first one that
5
+ * resolves without throwing wins, and its result is assigned to `ctx.user`.
6
+ *
7
+ * **OR semantics:** if a provider throws an `HttpException`, the guard moves
8
+ * on to the next provider. Any other error (TypeError, ReferenceError, etc.)
9
+ * is treated as a programming bug and rethrown immediately so it is not
10
+ * silently masked by a later provider.
11
+ *
12
+ * If every provider throws an `HttpException`, the last error is rethrown.
13
+ *
14
+ * For AND semantics, stack guards instead: `@UseGuard(AuthGuard(JwtAuth), Roles('admin'))`.
15
+ */
16
+ export function AuthGuard(first, ...rest) {
17
+ const providerCtors = [first, ...rest];
18
+ class AuthenticationGuard {
19
+ // Eagerly resolve providers in the field initializer - this runs during
20
+ // guard construction, which the framework performs inside an active
21
+ // container context. `canActivate()` runs later during request handling
22
+ // and would not have access to the active container.
23
+ providers = providerCtors.map((P) => inject(P));
24
+ async canActivate(ctx) {
25
+ let lastError;
26
+ for (const provider of this.providers) {
27
+ try {
28
+ ctx.user = await provider.authenticate(ctx);
29
+ return true;
30
+ }
31
+ catch (error) {
32
+ if (error instanceof HttpException) {
33
+ lastError = error;
34
+ continue;
35
+ }
36
+ throw error;
37
+ }
38
+ }
39
+ throw lastError ?? new UnauthorizedException('Unauthorized');
40
+ }
41
+ }
42
+ ;
43
+ AuthenticationGuard[GUARD_FACTORY] = AuthGuard;
44
+ return AuthenticationGuard;
45
+ }
46
+ //# sourceMappingURL=auth.guard.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.guard.js","sourceRoot":"","sources":["../src/auth.guard.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,qBAAqB,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AAI1F;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,SAAS,CAAC,KAAgC,EAAE,GAAG,IAAiC;IAC9F,MAAM,aAAa,GAAG,CAAC,KAAK,EAAE,GAAG,IAAI,CAAC,CAAA;IAEtC,MAAM,mBAAmB;QACvB,wEAAwE;QACxE,oEAAoE;QACpE,wEAAwE;QACxE,qDAAqD;QAC7C,SAAS,GAAG,aAAa,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAA;QAEvD,KAAK,CAAC,WAAW,CAAC,GAAwC;YACxD,IAAI,SAAoC,CAAA;YACxC,KAAK,MAAM,QAAQ,IAAI,IAAI,CAAC,SAAS,EAAE,CAAC;gBACtC,IAAI,CAAC;oBACH,GAAG,CAAC,IAAI,GAAG,MAAM,QAAQ,CAAC,YAAY,CAAC,GAAG,CAAC,CAAA;oBAC3C,OAAO,IAAI,CAAA;gBACb,CAAC;gBAAC,OAAO,KAAK,EAAE,CAAC;oBACf,IAAI,KAAK,YAAY,aAAa,EAAE,CAAC;wBACnC,SAAS,GAAG,KAAK,CAAA;wBACjB,SAAQ;oBACV,CAAC;oBACD,MAAM,KAAK,CAAA;gBACb,CAAC;YACH,CAAC;YACD,MAAM,SAAS,IAAI,IAAI,qBAAqB,CAAC,cAAc,CAAC,CAAA;QAC9D,CAAC;KACF;IAED,CAAC;IAAC,mBAA2B,CAAC,aAAa,CAAC,GAAG,SAAS,CAAA;IACxD,OAAO,mBAAmB,CAAA;AAC5B,CAAC"}
@@ -0,0 +1,6 @@
1
+ import type { RequestContext } from '@miiajs/core';
2
+ export type TokenExtractor = (ctx: RequestContext) => string | null;
3
+ export declare function fromHeader(header?: string, scheme?: string): TokenExtractor;
4
+ export declare function fromCookie(name: string): TokenExtractor;
5
+ export declare function fromQuery(param: string): TokenExtractor;
6
+ //# sourceMappingURL=extractors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"extractors.d.ts","sourceRoot":"","sources":["../src/extractors.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAA;AAElD,MAAM,MAAM,cAAc,GAAG,CAAC,GAAG,EAAE,cAAc,KAAK,MAAM,GAAG,IAAI,CAAA;AAEnE,wBAAgB,UAAU,CAAC,MAAM,SAAkB,EAAE,MAAM,SAAW,GAAG,cAAc,CAQtF;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,cAAc,CAOvD;AAED,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,cAAc,CAEvD"}
@@ -0,0 +1,25 @@
1
+ export function fromHeader(header = 'authorization', scheme = 'Bearer') {
2
+ return (ctx) => {
3
+ const value = ctx.req.headers.get(header);
4
+ if (!value)
5
+ return null;
6
+ if (!scheme)
7
+ return value;
8
+ if (!value.startsWith(scheme + ' '))
9
+ return null;
10
+ return value.slice(scheme.length + 1);
11
+ };
12
+ }
13
+ export function fromCookie(name) {
14
+ return (ctx) => {
15
+ const cookie = ctx.req.headers.get('cookie');
16
+ if (!cookie)
17
+ return null;
18
+ const match = cookie.match(new RegExp(`(?:^|;\\s*)${name}=([^;]+)`));
19
+ return match?.[1] ?? null;
20
+ };
21
+ }
22
+ export function fromQuery(param) {
23
+ return (ctx) => ctx.query[param] ?? null;
24
+ }
25
+ //# sourceMappingURL=extractors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"extractors.js","sourceRoot":"","sources":["../src/extractors.ts"],"names":[],"mappings":"AAIA,MAAM,UAAU,UAAU,CAAC,MAAM,GAAG,eAAe,EAAE,MAAM,GAAG,QAAQ;IACpE,OAAO,CAAC,GAAG,EAAE,EAAE;QACb,MAAM,KAAK,GAAG,GAAG,CAAC,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;QACzC,IAAI,CAAC,KAAK;YAAE,OAAO,IAAI,CAAA;QACvB,IAAI,CAAC,MAAM;YAAE,OAAO,KAAK,CAAA;QACzB,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,MAAM,GAAG,GAAG,CAAC;YAAE,OAAO,IAAI,CAAA;QAChD,OAAO,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAA;IACvC,CAAC,CAAA;AACH,CAAC;AAED,MAAM,UAAU,UAAU,CAAC,IAAY;IACrC,OAAO,CAAC,GAAG,EAAE,EAAE;QACb,MAAM,MAAM,GAAG,GAAG,CAAC,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAA;QAC5C,IAAI,CAAC,MAAM;YAAE,OAAO,IAAI,CAAA;QACxB,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,IAAI,MAAM,CAAC,cAAc,IAAI,UAAU,CAAC,CAAC,CAAA;QACpE,OAAO,KAAK,EAAE,CAAC,CAAC,CAAC,IAAI,IAAI,CAAA;IAC3B,CAAC,CAAA;AACH,CAAC;AAED,MAAM,UAAU,SAAS,CAAC,KAAa;IACrC,OAAO,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,IAAI,CAAA;AAC1C,CAAC"}
@@ -0,0 +1,6 @@
1
+ export { AuthGuard } from './auth.guard.js';
2
+ export type { AuthProvider } from './provider.js';
3
+ export { fromHeader, fromCookie, fromQuery } from './extractors.js';
4
+ export type { TokenExtractor } from './extractors.js';
5
+ export { timingSafeEqual } from './timing-safe.js';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAC3C,YAAY,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AACjD,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AACnE,YAAY,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AACrD,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export { AuthGuard } from './auth.guard.js';
2
+ export { fromHeader, fromCookie, fromQuery } from './extractors.js';
3
+ export { timingSafeEqual } from './timing-safe.js';
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAE3C,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAEnE,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAA"}
@@ -0,0 +1,27 @@
1
+ import type { RequestContext } from '@miiajs/core';
2
+ /**
3
+ * An authentication provider extracts credentials from a request and returns
4
+ * the authenticated subject (user, API client, service account, ...) or
5
+ * throws an `HttpException` on failure.
6
+ *
7
+ * Providers are regular `@Injectable()` classes - register them in your
8
+ * module's `providers` array and pass the class token to `AuthGuard(...)`.
9
+ *
10
+ * ```ts
11
+ * @Injectable()
12
+ * export class JwtAuth implements AuthProvider {
13
+ * private jwt = inject(JwtService)
14
+ * private extract = fromHeader()
15
+ *
16
+ * async authenticate(ctx: RequestContext) {
17
+ * const token = this.extract(ctx)
18
+ * if (!token) throw new UnauthorizedException('Missing token')
19
+ * return this.jwt.verify(token)
20
+ * }
21
+ * }
22
+ * ```
23
+ */
24
+ export interface AuthProvider {
25
+ authenticate(ctx: RequestContext): Promise<unknown> | unknown;
26
+ }
27
+ //# sourceMappingURL=provider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAA;AAElD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,WAAW,YAAY;IAC3B,YAAY,CAAC,GAAG,EAAE,cAAc,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAA;CAC9D"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=provider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"provider.js","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":""}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Constant-time equality check for two strings of the same expected length
3
+ * (e.g. pre-hashed tokens, HMAC digests, API keys).
4
+ *
5
+ * **Not suitable for plaintext password comparison** - use `bcrypt.compare`
6
+ * or `argon2.verify` instead, which handle salting and proper cost factors.
7
+ *
8
+ * Returns `false` immediately (non-constant-time) when lengths differ; callers
9
+ * should ensure inputs are the same expected length before calling.
10
+ */
11
+ export declare function timingSafeEqual(a: string, b: string): boolean;
12
+ //# sourceMappingURL=timing-safe.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"timing-safe.d.ts","sourceRoot":"","sources":["../src/timing-safe.ts"],"names":[],"mappings":"AAIA;;;;;;;;;GASG;AACH,wBAAgB,eAAe,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,OAAO,CAK7D"}
@@ -0,0 +1,20 @@
1
+ import { timingSafeEqual as nodeTimingSafeEqual } from 'node:crypto';
2
+ const encoder = new TextEncoder();
3
+ /**
4
+ * Constant-time equality check for two strings of the same expected length
5
+ * (e.g. pre-hashed tokens, HMAC digests, API keys).
6
+ *
7
+ * **Not suitable for plaintext password comparison** - use `bcrypt.compare`
8
+ * or `argon2.verify` instead, which handle salting and proper cost factors.
9
+ *
10
+ * Returns `false` immediately (non-constant-time) when lengths differ; callers
11
+ * should ensure inputs are the same expected length before calling.
12
+ */
13
+ export function timingSafeEqual(a, b) {
14
+ const bufA = encoder.encode(a);
15
+ const bufB = encoder.encode(b);
16
+ if (bufA.length !== bufB.length)
17
+ return false;
18
+ return nodeTimingSafeEqual(bufA, bufB);
19
+ }
20
+ //# sourceMappingURL=timing-safe.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"timing-safe.js","sourceRoot":"","sources":["../src/timing-safe.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,IAAI,mBAAmB,EAAE,MAAM,aAAa,CAAA;AAEpE,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAA;AAEjC;;;;;;;;;GASG;AACH,MAAM,UAAU,eAAe,CAAC,CAAS,EAAE,CAAS;IAClD,MAAM,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAA;IAC9B,MAAM,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAA;IAC9B,IAAI,IAAI,CAAC,MAAM,KAAK,IAAI,CAAC,MAAM;QAAE,OAAO,KAAK,CAAA;IAC7C,OAAO,mBAAmB,CAAC,IAAI,EAAE,IAAI,CAAC,CAAA;AACxC,CAAC"}
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@miiajs/auth",
3
+ "version": "0.1.0",
4
+ "author": "Ruslan Matiushev",
5
+ "license": "MIT",
6
+ "description": "Lightweight auth primitives for MiiaJS - AuthProvider interface, AuthGuard factory, HTTP token extractors, timing-safe equality.",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/miiajs/miia.git",
10
+ "directory": "packages/auth"
11
+ },
12
+ "homepage": "https://github.com/miiajs/miia/tree/main/packages/auth#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/miiajs/miia/issues"
15
+ },
16
+ "keywords": [
17
+ "miiajs",
18
+ "miia",
19
+ "auth",
20
+ "authentication",
21
+ "authorization",
22
+ "guard"
23
+ ],
24
+ "publishConfig": {
25
+ "access": "public"
26
+ },
27
+ "type": "module",
28
+ "exports": {
29
+ ".": {
30
+ "types": "./dist/index.d.ts",
31
+ "import": "./dist/index.js"
32
+ }
33
+ },
34
+ "main": "./dist/index.js",
35
+ "types": "./dist/index.d.ts",
36
+ "files": [
37
+ "dist",
38
+ "README.md",
39
+ "LICENSE"
40
+ ],
41
+ "sideEffects": false,
42
+ "engines": {
43
+ "node": ">=22.22.1",
44
+ "bun": ">=1.3.11",
45
+ "deno": ">=2.6.1"
46
+ },
47
+ "dependencies": {
48
+ "@miiajs/core": "workspace:*"
49
+ },
50
+ "scripts": {
51
+ "build": "tsc --build",
52
+ "clean": "tsc --build --clean"
53
+ }
54
+ }