@kurdel/auth 0.1.0-beta.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/LICENSE +21 -0
- package/README.md +253 -0
- package/lib/domain/auth-context.d.ts +8 -0
- package/lib/domain/auth-context.js +2 -0
- package/lib/domain/auth-context.js.map +1 -0
- package/lib/domain/auth-event-sink-provider.d.ts +7 -0
- package/lib/domain/auth-event-sink-provider.js +2 -0
- package/lib/domain/auth-event-sink-provider.js.map +1 -0
- package/lib/domain/auth-event.d.ts +30 -0
- package/lib/domain/auth-event.js +5 -0
- package/lib/domain/auth-event.js.map +1 -0
- package/lib/domain/auth-strategy-provider.d.ts +9 -0
- package/lib/domain/auth-strategy-provider.js +2 -0
- package/lib/domain/auth-strategy-provider.js.map +1 -0
- package/lib/domain/auth-strategy.d.ts +17 -0
- package/lib/domain/auth-strategy.js +2 -0
- package/lib/domain/auth-strategy.js.map +1 -0
- package/lib/domain/authorization-policy-provider.d.ts +9 -0
- package/lib/domain/authorization-policy-provider.js +2 -0
- package/lib/domain/authorization-policy-provider.js.map +1 -0
- package/lib/domain/authorization-policy.d.ts +6 -0
- package/lib/domain/authorization-policy.js +2 -0
- package/lib/domain/authorization-policy.js.map +1 -0
- package/lib/domain/index.d.ts +7 -0
- package/lib/domain/index.js +8 -0
- package/lib/domain/index.js.map +1 -0
- package/lib/index.d.ts +6 -0
- package/lib/index.js +7 -0
- package/lib/index.js.map +1 -0
- package/lib/infra/in-memory/in-memory-api-key-repository.d.ts +7 -0
- package/lib/infra/in-memory/in-memory-api-key-repository.js +10 -0
- package/lib/infra/in-memory/in-memory-api-key-repository.js.map +1 -0
- package/lib/infra/in-memory/in-memory-auth-user-repository.d.ts +8 -0
- package/lib/infra/in-memory/in-memory-auth-user-repository.js +10 -0
- package/lib/infra/in-memory/in-memory-auth-user-repository.js.map +1 -0
- package/lib/infra/in-memory/in-memory-jwt-repository.d.ts +7 -0
- package/lib/infra/in-memory/in-memory-jwt-repository.js +8 -0
- package/lib/infra/in-memory/in-memory-jwt-repository.js.map +1 -0
- package/lib/infra/in-memory/index.d.ts +3 -0
- package/lib/infra/in-memory/index.js +4 -0
- package/lib/infra/in-memory/index.js.map +1 -0
- package/lib/infra/index.d.ts +1 -0
- package/lib/infra/index.js +2 -0
- package/lib/infra/index.js.map +1 -0
- package/lib/repositories/api-key/api-key-repository.d.ts +30 -0
- package/lib/repositories/api-key/api-key-repository.js +2 -0
- package/lib/repositories/api-key/api-key-repository.js.map +1 -0
- package/lib/repositories/api-key/api-key-usage-recorder.d.ts +4 -0
- package/lib/repositories/api-key/api-key-usage-recorder.js +2 -0
- package/lib/repositories/api-key/api-key-usage-recorder.js.map +1 -0
- package/lib/repositories/api-key/index.d.ts +2 -0
- package/lib/repositories/api-key/index.js +3 -0
- package/lib/repositories/api-key/index.js.map +1 -0
- package/lib/repositories/index.d.ts +3 -0
- package/lib/repositories/index.js +4 -0
- package/lib/repositories/index.js.map +1 -0
- package/lib/repositories/jwt/index.d.ts +1 -0
- package/lib/repositories/jwt/index.js +2 -0
- package/lib/repositories/jwt/index.js.map +1 -0
- package/lib/repositories/jwt/jwt-repository.d.ts +3 -0
- package/lib/repositories/jwt/jwt-repository.js +2 -0
- package/lib/repositories/jwt/jwt-repository.js.map +1 -0
- package/lib/repositories/user/auth-user-repository.d.ts +11 -0
- package/lib/repositories/user/auth-user-repository.js +2 -0
- package/lib/repositories/user/auth-user-repository.js.map +1 -0
- package/lib/repositories/user/index.d.ts +1 -0
- package/lib/repositories/user/index.js +2 -0
- package/lib/repositories/user/index.js.map +1 -0
- package/lib/runtime/auth-module.d.ts +64 -0
- package/lib/runtime/auth-module.js +83 -0
- package/lib/runtime/auth-module.js.map +1 -0
- package/lib/runtime/auth-strategy-registry.d.ts +25 -0
- package/lib/runtime/auth-strategy-registry.js +33 -0
- package/lib/runtime/auth-strategy-registry.js.map +1 -0
- package/lib/runtime/authorization-policy-registry.d.ts +8 -0
- package/lib/runtime/authorization-policy-registry.js +16 -0
- package/lib/runtime/authorization-policy-registry.js.map +1 -0
- package/lib/runtime/create-auth-middleware.d.ts +5 -0
- package/lib/runtime/create-auth-middleware.js +89 -0
- package/lib/runtime/create-auth-middleware.js.map +1 -0
- package/lib/runtime/index.d.ts +4 -0
- package/lib/runtime/index.js +5 -0
- package/lib/runtime/index.js.map +1 -0
- package/lib/strategies/api-key/api-key-strategy.d.ts +22 -0
- package/lib/strategies/api-key/api-key-strategy.js +36 -0
- package/lib/strategies/api-key/api-key-strategy.js.map +1 -0
- package/lib/strategies/api-key/index.d.ts +1 -0
- package/lib/strategies/api-key/index.js +2 -0
- package/lib/strategies/api-key/index.js.map +1 -0
- package/lib/strategies/index.d.ts +2 -0
- package/lib/strategies/index.js +3 -0
- package/lib/strategies/index.js.map +1 -0
- package/lib/strategies/jwt/index.d.ts +2 -0
- package/lib/strategies/jwt/index.js +3 -0
- package/lib/strategies/jwt/index.js.map +1 -0
- package/lib/strategies/jwt/jwt-service.d.ts +50 -0
- package/lib/strategies/jwt/jwt-service.js +87 -0
- package/lib/strategies/jwt/jwt-service.js.map +1 -0
- package/lib/strategies/jwt/jwt-strategy.d.ts +27 -0
- package/lib/strategies/jwt/jwt-strategy.js +45 -0
- package/lib/strategies/jwt/jwt-strategy.js.map +1 -0
- package/lib/tokens.d.ts +11 -0
- package/lib/tokens.js +13 -0
- package/lib/tokens.js.map +1 -0
- package/package.json +56 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/repositories/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,iBAAiB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './jwt-repository.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/repositories/jwt/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jwt-repository.js","sourceRoot":"","sources":["../../../src/repositories/jwt/jwt-repository.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { AuthUser } from '@kurdel/common';
|
|
2
|
+
/**
|
|
3
|
+
* Resolves the current application identity used for authorization.
|
|
4
|
+
*
|
|
5
|
+
* Implementations exclude identities that must not authenticate, such as
|
|
6
|
+
* disabled or deleted users. Roles come from this source of truth rather than
|
|
7
|
+
* from the presented credential.
|
|
8
|
+
*/
|
|
9
|
+
export interface AuthUserRepository {
|
|
10
|
+
findById(id: string | number): Promise<AuthUser | null> | AuthUser | null;
|
|
11
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth-user-repository.js","sourceRoot":"","sources":["../../../src/repositories/user/auth-user-repository.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './auth-user-repository.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/repositories/user/index.ts"],"names":[],"mappings":"AAAA,cAAc,2BAA2B,CAAC"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { Container } from '@kurdel/ioc';
|
|
2
|
+
import { ModulePriority, type AppModule, type ProviderConfig } from '@kurdel/core/app';
|
|
3
|
+
import { type AuthEventSinkProvider, type AuthStrategyProvider, type AuthorizationPolicyProvider } from '../domain/index.js';
|
|
4
|
+
/**
|
|
5
|
+
* ## AuthModuleConfig
|
|
6
|
+
*
|
|
7
|
+
* Defines authentication strategies supplied by the application.
|
|
8
|
+
*
|
|
9
|
+
* Example:
|
|
10
|
+
* ```ts
|
|
11
|
+
* new AuthModule({
|
|
12
|
+
* strategies: [
|
|
13
|
+
* { name: 'api-key', use: new ApiKeyStrategy({...}) },
|
|
14
|
+
* { name: 'jwt', use: new JwtStrategy({...}) },
|
|
15
|
+
* ]
|
|
16
|
+
* })
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* Strategies are registered **once** during module initialization.
|
|
20
|
+
*/
|
|
21
|
+
export interface AuthModuleConfig {
|
|
22
|
+
/** List of user-provided authentication strategies. */
|
|
23
|
+
strategies?: AuthStrategyProvider[];
|
|
24
|
+
/** List of named application authorization policies. */
|
|
25
|
+
policies?: AuthorizationPolicyProvider[];
|
|
26
|
+
/** Optional destination for sanitized authentication lifecycle events. */
|
|
27
|
+
events?: AuthEventSinkProvider;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* ## AuthModule
|
|
31
|
+
*
|
|
32
|
+
* Provides:
|
|
33
|
+
* - singleton `AuthStrategyRegistry`
|
|
34
|
+
* - automatic registration of configured strategies
|
|
35
|
+
*
|
|
36
|
+
* Responsibilities:
|
|
37
|
+
* - expose a DI-managed registry storing all auth strategies
|
|
38
|
+
* - populate it during `register()`
|
|
39
|
+
*
|
|
40
|
+
* Non-responsibilities:
|
|
41
|
+
* - no transport logic
|
|
42
|
+
* - no middleware ordering
|
|
43
|
+
* - no policy evaluation
|
|
44
|
+
*
|
|
45
|
+
* Identity repositories, credentials and strategy-specific services are
|
|
46
|
+
* application concerns and must be supplied outside this module.
|
|
47
|
+
*/
|
|
48
|
+
export declare class AuthModule implements AppModule<AuthModuleConfig> {
|
|
49
|
+
private readonly config;
|
|
50
|
+
readonly priority = ModulePriority.Auth;
|
|
51
|
+
/**
|
|
52
|
+
* Providers exposed by the module.
|
|
53
|
+
*
|
|
54
|
+
* - `StrategyRegistry` → a singleton container of auth strategies
|
|
55
|
+
*/
|
|
56
|
+
readonly providers: ProviderConfig[];
|
|
57
|
+
constructor(config?: AuthModuleConfig);
|
|
58
|
+
/**
|
|
59
|
+
* Registers all configured authentication strategies.
|
|
60
|
+
*
|
|
61
|
+
* Called once during app startup.
|
|
62
|
+
*/
|
|
63
|
+
register(ioc: Container): Promise<void>;
|
|
64
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { ModulePriority } from '@kurdel/core/app';
|
|
2
|
+
import { TOKENS } from '@kurdel/core/tokens';
|
|
3
|
+
import { NoopAuthEventSink, } from '../domain/index.js';
|
|
4
|
+
import { AuthStrategyRegistry, AuthorizationPolicyRegistry, createAuthMiddleware, } from '../runtime/index.js';
|
|
5
|
+
import { AUTH_TOKENS } from '../tokens.js';
|
|
6
|
+
/**
|
|
7
|
+
* ## AuthModule
|
|
8
|
+
*
|
|
9
|
+
* Provides:
|
|
10
|
+
* - singleton `AuthStrategyRegistry`
|
|
11
|
+
* - automatic registration of configured strategies
|
|
12
|
+
*
|
|
13
|
+
* Responsibilities:
|
|
14
|
+
* - expose a DI-managed registry storing all auth strategies
|
|
15
|
+
* - populate it during `register()`
|
|
16
|
+
*
|
|
17
|
+
* Non-responsibilities:
|
|
18
|
+
* - no transport logic
|
|
19
|
+
* - no middleware ordering
|
|
20
|
+
* - no policy evaluation
|
|
21
|
+
*
|
|
22
|
+
* Identity repositories, credentials and strategy-specific services are
|
|
23
|
+
* application concerns and must be supplied outside this module.
|
|
24
|
+
*/
|
|
25
|
+
export class AuthModule {
|
|
26
|
+
constructor(config = {}) {
|
|
27
|
+
this.config = config;
|
|
28
|
+
this.priority = ModulePriority.Auth;
|
|
29
|
+
const eventSink = config.events;
|
|
30
|
+
this.providers = [
|
|
31
|
+
{
|
|
32
|
+
provide: AUTH_TOKENS.StrategyRegistry,
|
|
33
|
+
useClass: AuthStrategyRegistry,
|
|
34
|
+
singleton: true,
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
provide: AUTH_TOKENS.PolicyRegistry,
|
|
38
|
+
useClass: AuthorizationPolicyRegistry,
|
|
39
|
+
singleton: true,
|
|
40
|
+
},
|
|
41
|
+
eventSink && 'useFactory' in eventSink
|
|
42
|
+
? {
|
|
43
|
+
provide: AUTH_TOKENS.EventSink,
|
|
44
|
+
useFactory: eventSink.useFactory,
|
|
45
|
+
singleton: true,
|
|
46
|
+
}
|
|
47
|
+
: {
|
|
48
|
+
provide: AUTH_TOKENS.EventSink,
|
|
49
|
+
useInstance: eventSink?.use ?? new NoopAuthEventSink(),
|
|
50
|
+
},
|
|
51
|
+
];
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Registers all configured authentication strategies.
|
|
55
|
+
*
|
|
56
|
+
* Called once during app startup.
|
|
57
|
+
*/
|
|
58
|
+
async register(ioc) {
|
|
59
|
+
const registry = ioc.get(AUTH_TOKENS.StrategyRegistry);
|
|
60
|
+
const policies = ioc.get(AUTH_TOKENS.PolicyRegistry);
|
|
61
|
+
const events = ioc.get(AUTH_TOKENS.EventSink);
|
|
62
|
+
// register user strategies
|
|
63
|
+
for (const s of this.config.strategies ?? []) {
|
|
64
|
+
if ('use' in s) {
|
|
65
|
+
registry.register(s.name, s.use);
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
const strategy = s.useFactory(ioc);
|
|
69
|
+
registry.register(s.name, strategy);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
for (const policy of this.config.policies ?? []) {
|
|
73
|
+
policies.register(policy.name, 'use' in policy ? policy.use : policy.useFactory(ioc));
|
|
74
|
+
}
|
|
75
|
+
// register system auth middleware
|
|
76
|
+
const mwReg = ioc.get(TOKENS.MiddlewareRegistry);
|
|
77
|
+
mwReg.use(createAuthMiddleware(registry, policies, events), {
|
|
78
|
+
zone: 'auth',
|
|
79
|
+
priority: 0,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=auth-module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth-module.js","sourceRoot":"","sources":["../../src/runtime/auth-module.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAuC,MAAM,kBAAkB,CAAC;AAEvF,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAE7C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C;QAE7D,2BAA2B;QAC3B,KAAK,MAAM,CAAC,IAAI,IAAI,CAAC,MAAM,CAAC,UAAU,IAAI,EAAE,EAAE,CAAC;YAC7C,IAAI,KAAK,IAAI,CAAC,EAAE,CAAC;gBACf,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC;YACnC,CAAC;iBAAM,CAAC;gBACN,MAAM,QAAQ,GAAG,CAAC,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;gBACnC,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;YACtC,CAAC;QACH,CAAC;QAED,KAAK,MAAM,MAAM,IAAI,IAAI,CAAC,MAAM,CAAC,QAAQ,IAAI,EAAE,EAAE,CAAC;YAChD,QAAQ,CAAC,QAAQ,CACf,MAAM,CAAC,IAAI,EACX,KAAK,IAAI,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC,UAAU,CAAC,GAAG,CAAC,CACtD,CAAC;QACJ,CAAC;QAED,kCAAkC;QAClC,MAAM,KAAK,GAAG,GAAG,CAAC,GAAG,CAAqB,MAAM,CAAC,kBAAkB,CAAC,CAAC;QACrE,KAAK,CAAC,GAAG,CAAC,oBAAoB,CAAC,QAAQ,EAAE,QAAQ,EAAE,MAAM,CAAC,EAAE;YAC1D,IAAI,EAAE,MAAM;YACZ,QAAQ,EAAE,CAAC;SACZ,CAAC,CAAC;IACL,CAAC;CACF"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { AuthStrategy } from '../domain/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* ## AuthStrategyRegistry
|
|
4
|
+
*
|
|
5
|
+
* Holds named authentication strategies (e.g. `jwt`, `apikey`, `session`).
|
|
6
|
+
* Strategies are resolved by middleware per request.
|
|
7
|
+
*/
|
|
8
|
+
export declare class AuthStrategyRegistry {
|
|
9
|
+
private readonly strategies;
|
|
10
|
+
/**
|
|
11
|
+
* Registers a new authentication strategy.
|
|
12
|
+
*
|
|
13
|
+
* @param name - Strategy identifier (e.g. "jwt", "apikey").
|
|
14
|
+
* @param strategy - Implementation of {@link AuthStrategy}.
|
|
15
|
+
*/
|
|
16
|
+
register(name: string, strategy: AuthStrategy): void;
|
|
17
|
+
/**
|
|
18
|
+
* Returns a strategy by name, if registered.
|
|
19
|
+
*/
|
|
20
|
+
get(name: string): AuthStrategy | undefined;
|
|
21
|
+
/**
|
|
22
|
+
* Removes a strategy (rarely used).
|
|
23
|
+
*/
|
|
24
|
+
unregister(name: string): void;
|
|
25
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ## AuthStrategyRegistry
|
|
3
|
+
*
|
|
4
|
+
* Holds named authentication strategies (e.g. `jwt`, `apikey`, `session`).
|
|
5
|
+
* Strategies are resolved by middleware per request.
|
|
6
|
+
*/
|
|
7
|
+
export class AuthStrategyRegistry {
|
|
8
|
+
constructor() {
|
|
9
|
+
this.strategies = new Map();
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Registers a new authentication strategy.
|
|
13
|
+
*
|
|
14
|
+
* @param name - Strategy identifier (e.g. "jwt", "apikey").
|
|
15
|
+
* @param strategy - Implementation of {@link AuthStrategy}.
|
|
16
|
+
*/
|
|
17
|
+
register(name, strategy) {
|
|
18
|
+
this.strategies.set(name, strategy);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Returns a strategy by name, if registered.
|
|
22
|
+
*/
|
|
23
|
+
get(name) {
|
|
24
|
+
return this.strategies.get(name);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Removes a strategy (rarely used).
|
|
28
|
+
*/
|
|
29
|
+
unregister(name) {
|
|
30
|
+
this.strategies.delete(name);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=auth-strategy-registry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth-strategy-registry.js","sourceRoot":"","sources":["../../src/runtime/auth-strategy-registry.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,MAAM,OAAO,oBAAoB;IAAjC;QACmB,eAAU,GAAG,IAAI,GAAG,EAAwB,CAAC;IAyBhE,CAAC;IAvBC;;;;;OAKG;IACH,QAAQ,CAAC,IAAY,EAAE,QAAsB;QAC3C,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;IACtC,CAAC;IAED;;OAEG;IACH,GAAG,CAAC,IAAY;QACd,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IACnC,CAAC;IAED;;OAEG;IACH,UAAU,CAAC,IAAY;QACrB,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAC/B,CAAC;CACF"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { AuthorizationPolicy } from '../domain/index.js';
|
|
2
|
+
/** Stores the named authorization policies available to route metadata. */
|
|
3
|
+
export declare class AuthorizationPolicyRegistry {
|
|
4
|
+
private readonly policies;
|
|
5
|
+
register(name: string, policy: AuthorizationPolicy): void;
|
|
6
|
+
get(name: string): AuthorizationPolicy | undefined;
|
|
7
|
+
unregister(name: string): void;
|
|
8
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** Stores the named authorization policies available to route metadata. */
|
|
2
|
+
export class AuthorizationPolicyRegistry {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.policies = new Map();
|
|
5
|
+
}
|
|
6
|
+
register(name, policy) {
|
|
7
|
+
this.policies.set(name, policy);
|
|
8
|
+
}
|
|
9
|
+
get(name) {
|
|
10
|
+
return this.policies.get(name);
|
|
11
|
+
}
|
|
12
|
+
unregister(name) {
|
|
13
|
+
this.policies.delete(name);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=authorization-policy-registry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"authorization-policy-registry.js","sourceRoot":"","sources":["../../src/runtime/authorization-policy-registry.ts"],"names":[],"mappings":"AAEA,2EAA2E;AAC3E,MAAM,OAAO,2BAA2B;IAAxC;QACmB,aAAQ,GAAG,IAAI,GAAG,EAA+B,CAAC;IAarE,CAAC;IAXC,QAAQ,CAAC,IAAY,EAAE,MAA2B;QAChD,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IAClC,CAAC;IAED,GAAG,CAAC,IAAY;QACd,OAAO,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IACjC,CAAC;IAED,UAAU,CAAC,IAAY;QACrB,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAC7B,CAAC;CACF"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Middleware } from '@kurdel/core/http';
|
|
2
|
+
import { type AuthEventSink } from '../domain/index.js';
|
|
3
|
+
import type { AuthStrategyRegistry } from './auth-strategy-registry.js';
|
|
4
|
+
import { AuthorizationPolicyRegistry } from './authorization-policy-registry.js';
|
|
5
|
+
export declare function createAuthMiddleware(registry: AuthStrategyRegistry, policies?: AuthorizationPolicyRegistry, events?: AuthEventSink, now?: () => Date): Middleware;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { NoopAuthEventSink } from '../domain/index.js';
|
|
2
|
+
import { AuthorizationPolicyRegistry } from './authorization-policy-registry.js';
|
|
3
|
+
export function createAuthMiddleware(registry, policies = new AuthorizationPolicyRegistry(), events = new NoopAuthEventSink(), now = () => new Date()) {
|
|
4
|
+
return async (ctx, next) => {
|
|
5
|
+
const meta = ctx.route?.auth;
|
|
6
|
+
if (!meta || meta.public) {
|
|
7
|
+
// public route
|
|
8
|
+
return next();
|
|
9
|
+
}
|
|
10
|
+
const { strategy, roles, policies: requiredPolicies } = meta;
|
|
11
|
+
// --- 1️⃣ Authenticate ---
|
|
12
|
+
let auth;
|
|
13
|
+
if (strategy) {
|
|
14
|
+
const strat = registry.get(strategy);
|
|
15
|
+
if (!strat) {
|
|
16
|
+
return ctx.json(500, { error: `Unknown auth strategy '${strategy}'` });
|
|
17
|
+
}
|
|
18
|
+
const result = await strat.authenticate(ctx.req);
|
|
19
|
+
if (!result) {
|
|
20
|
+
await events.report({
|
|
21
|
+
type: 'authentication.failed',
|
|
22
|
+
occurredAt: now(),
|
|
23
|
+
strategy,
|
|
24
|
+
reason: 'invalid-credential',
|
|
25
|
+
});
|
|
26
|
+
return ctx.json(401, { error: 'Unauthorized' });
|
|
27
|
+
}
|
|
28
|
+
auth = {
|
|
29
|
+
...result,
|
|
30
|
+
strategy,
|
|
31
|
+
};
|
|
32
|
+
ctx.auth = auth;
|
|
33
|
+
ctx.user = result.user;
|
|
34
|
+
await events.report({
|
|
35
|
+
type: 'authentication.succeeded',
|
|
36
|
+
occurredAt: now(),
|
|
37
|
+
strategy,
|
|
38
|
+
userId: auth.user.id,
|
|
39
|
+
...(auth.credential ? { credential: auth.credential } : {}),
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
// --- 2️⃣ Authorize ---
|
|
43
|
+
if (roles && roles.length > 0) {
|
|
44
|
+
const ok = auth && Array.isArray(auth.user.roles)
|
|
45
|
+
? roles.some(r => auth.user.roles.includes(r))
|
|
46
|
+
: false;
|
|
47
|
+
if (!ok) {
|
|
48
|
+
await events.report({
|
|
49
|
+
type: 'authorization.denied',
|
|
50
|
+
occurredAt: now(),
|
|
51
|
+
...(auth ? { strategy: auth.strategy, userId: auth.user.id } : {}),
|
|
52
|
+
...(auth?.credential ? { credential: auth.credential } : {}),
|
|
53
|
+
reason: 'missing-role',
|
|
54
|
+
});
|
|
55
|
+
return ctx.json(403, { error: 'Forbidden' });
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (requiredPolicies && requiredPolicies.length > 0) {
|
|
59
|
+
if (!auth) {
|
|
60
|
+
await events.report({
|
|
61
|
+
type: 'authorization.denied',
|
|
62
|
+
occurredAt: now(),
|
|
63
|
+
reason: 'missing-authentication',
|
|
64
|
+
});
|
|
65
|
+
return ctx.json(403, { error: 'Forbidden' });
|
|
66
|
+
}
|
|
67
|
+
for (const name of requiredPolicies) {
|
|
68
|
+
const policy = policies.get(name);
|
|
69
|
+
if (!policy) {
|
|
70
|
+
return ctx.json(500, { error: `Unknown authorization policy '${name}'` });
|
|
71
|
+
}
|
|
72
|
+
if (!(await policy.authorize(auth, ctx))) {
|
|
73
|
+
await events.report({
|
|
74
|
+
type: 'authorization.denied',
|
|
75
|
+
occurredAt: now(),
|
|
76
|
+
strategy: auth.strategy,
|
|
77
|
+
userId: auth.user.id,
|
|
78
|
+
...(auth.credential ? { credential: auth.credential } : {}),
|
|
79
|
+
reason: 'policy-rejected',
|
|
80
|
+
policy: name,
|
|
81
|
+
});
|
|
82
|
+
return ctx.json(403, { error: 'Forbidden' });
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return next();
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
//# sourceMappingURL=create-auth-middleware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-auth-middleware.js","sourceRoot":"","sources":["../../src/runtime/create-auth-middleware.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAwC,MAAM,qBAAqB,CAAC;AAG9F,OAAO,EAAE,2BAA2B,EAAE,MAAM,oCAAoC,CAAC;AAEjF,MAAM,UAAU,oBAAoB,CAClC,QAA8B,EAC9B,WAAwC,IAAI,2BAA2B,EAAE,EACzE,SAAwB,IAAI,iBAAiB,EAAE,EAC/C,MAAkB,GAAG,EAAE,CAAC,IAAI,IAAI,EAAE;IAElC,OAAO,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE;QACzB,MAAM,I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runtime/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,oCAAoC,CAAC;AACnD,cAAc,6BAA6B,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { HttpRequest } from '@kurdel/common';
|
|
2
|
+
import type { AuthenticationResult, AuthStrategy } from '../../domain/index.js';
|
|
3
|
+
import type { ApiKeyRepository, ApiKeyUsageRecorder, AuthUserRepository } from '../../repositories/index.js';
|
|
4
|
+
export interface ApiKeyStrategyOptions {
|
|
5
|
+
/** Name of the request header containing the API key. */
|
|
6
|
+
header: string;
|
|
7
|
+
/** Credential metadata source. It does not provide authorization data. */
|
|
8
|
+
credentials: ApiKeyRepository;
|
|
9
|
+
/** Source of truth for the current user and their authorization roles. */
|
|
10
|
+
users: AuthUserRepository;
|
|
11
|
+
/** Optional sink for recording successful credential usage. */
|
|
12
|
+
usage?: ApiKeyUsageRecorder;
|
|
13
|
+
/** Injectable clock keeps expiration behavior deterministic in tests. */
|
|
14
|
+
now?: () => Date;
|
|
15
|
+
}
|
|
16
|
+
/** Authenticates an API key and resolves its current application identity. */
|
|
17
|
+
export declare class ApiKeyStrategy implements AuthStrategy {
|
|
18
|
+
private readonly options;
|
|
19
|
+
constructor(options: ApiKeyStrategyOptions);
|
|
20
|
+
authenticate(req: HttpRequest): Promise<AuthenticationResult | null>;
|
|
21
|
+
private now;
|
|
22
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/** Authenticates an API key and resolves its current application identity. */
|
|
2
|
+
export class ApiKeyStrategy {
|
|
3
|
+
constructor(options) {
|
|
4
|
+
this.options = options;
|
|
5
|
+
}
|
|
6
|
+
async authenticate(req) {
|
|
7
|
+
const raw = req.headers?.[this.options.header.toLowerCase()];
|
|
8
|
+
const key = Array.isArray(raw) ? raw[0] : raw;
|
|
9
|
+
if (!key || typeof key !== 'string')
|
|
10
|
+
return null;
|
|
11
|
+
const credential = await this.options.credentials.findByKey(key);
|
|
12
|
+
if (!credential || credential.revoked)
|
|
13
|
+
return null;
|
|
14
|
+
const authenticatedAt = this.now();
|
|
15
|
+
if (credential.expiresAt && credential.expiresAt.getTime() <= authenticatedAt.getTime()) {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
const user = await this.options.users.findById(credential.userId);
|
|
19
|
+
if (!user)
|
|
20
|
+
return null;
|
|
21
|
+
if (credential.id) {
|
|
22
|
+
await this.options.usage?.recordUsage(credential.id, authenticatedAt);
|
|
23
|
+
}
|
|
24
|
+
return {
|
|
25
|
+
user,
|
|
26
|
+
credential: {
|
|
27
|
+
type: 'api-key',
|
|
28
|
+
...(credential.id ? { id: credential.id } : {}),
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
now() {
|
|
33
|
+
return this.options.now?.() ?? new Date();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=api-key-strategy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api-key-strategy.js","sourceRoot":"","sources":["../../../src/strategies/api-key/api-key-strategy.ts"],"names":[],"mappings":"AAsBA,8EAA8E;AAC9E,MAAM,OAAO,cAAc;IACzB,YAA6B,OAA8B;QAA9B,YAAO,GAAP,OAAO,CAAuB;IAAG,CAAC;IAE/D,KAAK,CAAC,YAAY,CAAC,GAAgB;QACjC,MAAM,GAAG,GAAG,GAAG,CAAC,OAAO,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,WAAW,EAAE,CAAC,CAAC;QAC7D,MAAM,GAAG,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC;QAC9C,IAAI,CAAC,GAAG,IAAI,OAAO,GAAG,KAAK,QAAQ;YAAE,OAAO,IAAI,CAAC;QAEjD,MAAM,UAAU,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;QACjE,IAAI,CAAC,UAAU,IAAI,UAAU,CAAC,OAAO;YAAE,OAAO,IAAI,CAAC;QAEnD,MAAM,eAAe,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QACnC,IAAI,UAAU,CAAC,SAAS,IAAI,UAAU,CAAC,SAAS,CAAC,OAAO,EAAE,IAAI,eAAe,CAAC,OAAO,EAAE,EAAE,CAAC;YACxF,OAAO,IAAI,CAAC;QACd,CAAC;QAED,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,QAAQ,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;QAClE,IAAI,CAAC,IAAI;YAAE,OAAO,IAAI,CAAC;QAEvB,IAAI,UAAU,CAAC,EAAE,EAAE,CAAC;YAClB,MAAM,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,WAAW,CAAC,UAAU,CAAC,EAAE,EAAE,eAAe,CAAC,CAAC;QACxE,CAAC;QAED,OAAO;YACL,IAAI;YACJ,UAAU,EAAE;gBACV,IAAI,EAAE,SAAS;gBACf,GAAG,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,UAAU,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;aAChD;SACF,CAAC;IACJ,CAAC;IAEO,GAAG;QACT,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,IAAI,IAAI,IAAI,EAAE,CAAC;IAC5C,CAAC;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './api-key-strategy.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/strategies/api-key/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/strategies/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAA;AAClC,cAAc,gBAAgB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/strategies/jwt/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ## JwtPayload
|
|
3
|
+
* Standard JWT claims plus application-level fields.
|
|
4
|
+
*/
|
|
5
|
+
export interface JwtPayload {
|
|
6
|
+
sub: string | number;
|
|
7
|
+
roles: string[];
|
|
8
|
+
iat?: number;
|
|
9
|
+
exp?: number;
|
|
10
|
+
nbf?: number;
|
|
11
|
+
iss?: string;
|
|
12
|
+
aud?: string;
|
|
13
|
+
[key: string]: unknown;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* ## JwtServiceOptions
|
|
17
|
+
* The minimal configuration required to sign and verify JWT tokens.
|
|
18
|
+
*/
|
|
19
|
+
export interface JwtServiceOptions {
|
|
20
|
+
secret: string;
|
|
21
|
+
issuer?: string;
|
|
22
|
+
audience?: string;
|
|
23
|
+
expiresIn?: number;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* ## JwtService
|
|
27
|
+
*
|
|
28
|
+
* Lightweight JWT (HS256) implementation with zero dependencies.
|
|
29
|
+
* Provides signing, decoding and verification.
|
|
30
|
+
*/
|
|
31
|
+
export declare class JwtService {
|
|
32
|
+
private readonly opts;
|
|
33
|
+
constructor(opts: JwtServiceOptions);
|
|
34
|
+
/**
|
|
35
|
+
* Signs a new JWT token with the configured HS256 secret.
|
|
36
|
+
*/
|
|
37
|
+
sign(payload: Omit<JwtPayload, 'iat' | 'exp'>): string;
|
|
38
|
+
/**
|
|
39
|
+
* Decodes the payload **without validating the signature**.
|
|
40
|
+
* Useful for debugging or optional preview.
|
|
41
|
+
*/
|
|
42
|
+
decode(token: string): JwtPayload | null;
|
|
43
|
+
/**
|
|
44
|
+
* Verifies signature + exp/nbf/iss/aud constraints.
|
|
45
|
+
* Returns the decoded payload or throws an Error.
|
|
46
|
+
*/
|
|
47
|
+
verify(token: string): JwtPayload;
|
|
48
|
+
private b64;
|
|
49
|
+
private signSegment;
|
|
50
|
+
}
|