@aomex/auth 3.3.0 → 3.4.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/CHANGELOG.md CHANGED
@@ -3,6 +3,14 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [3.4.0](https://github.com/aomex/aomex/compare/v3.3.0...v3.4.0) (2024-10-04)
7
+
8
+ **Note:** Version bump only for package @aomex/auth
9
+
10
+
11
+
12
+
13
+
6
14
  # [3.3.0](https://github.com/aomex/aomex/compare/v3.2.4...v3.3.0) (2024-09-26)
7
15
 
8
16
  **Note:** Version bump only for package @aomex/auth
package/dist/index.d.ts CHANGED
@@ -1,15 +1,37 @@
1
1
  import { WebContext, OpenApiInjector, WebMiddleware } from '@aomex/web';
2
2
 
3
- declare abstract class AuthenticationAdapter<Payload extends object | string> {
3
+ declare abstract class Strategy<Payload extends object | string> {
4
4
  protected abstract authenticate(ctx: WebContext): Promise<Payload | false>;
5
5
  protected openapi(): OpenApiInjector;
6
6
  }
7
7
 
8
- /**
9
- * 授权认证中间件
10
- * @param adapter 授权具体实现方案
11
- * @param authKey 授权成功后挂载到ctx上的属性名称,默认值:`auth`
12
- */
13
- declare const authentication: <Payload extends object | string, Key extends string = "auth">(adapter: AuthenticationAdapter<Payload>, authKey?: Key) => WebMiddleware<{ readonly [K in Key]: Payload; }>;
8
+ declare namespace Authentication {
9
+ interface Options<S extends {
10
+ [K: string]: Strategy<object | string>;
11
+ }> {
12
+ strategies: S;
13
+ }
14
+ }
15
+ declare class Authentication<S extends {
16
+ [K: string]: Strategy<object | string>;
17
+ }> {
18
+ protected readonly options: Authentication.Options<S>;
19
+ constructor(options: Authentication.Options<S>);
20
+ /**
21
+ * 获取策略实例
22
+ */
23
+ strategy<StrategyName extends keyof S>(name: StrategyName): S[StrategyName];
24
+ /**
25
+ * 身份认证中间件
26
+ */
27
+ authenticate<StrategyName extends keyof S, CtxKey extends string = StrategyName extends string ? StrategyName : never, Payload extends object | string = S[StrategyName] extends Strategy<infer P> ? P : never>(name: StrategyName, opts?: {
28
+ /**
29
+ * ctx上使用的属性
30
+ */
31
+ contextKey?: CtxKey;
32
+ }): WebMiddleware<{
33
+ readonly [K in CtxKey]: Payload;
34
+ }>;
35
+ }
14
36
 
15
- export { AuthenticationAdapter, authentication };
37
+ export { Authentication, Strategy };
package/dist/index.js CHANGED
@@ -1,28 +1,6 @@
1
- // src/authentication.ts
2
- import { middleware } from "@aomex/core";
3
- var DEFAULT_KEY = "auth";
4
- var authentication = (adapter, authKey) => {
5
- return middleware.web({
6
- fn: async (ctx, next) => {
7
- let payload = false;
8
- try {
9
- payload = await adapter["authenticate"](ctx);
10
- } catch (e) {
11
- return void ctx.throw(401, e);
12
- }
13
- if (payload === false) return void ctx.throw(401);
14
- Object.defineProperty(ctx, authKey || DEFAULT_KEY, {
15
- get: () => payload
16
- });
17
- return next();
18
- },
19
- openapi: adapter["openapi"]()
20
- });
21
- };
22
-
23
- // src/authentication-adapter.ts
1
+ // src/strategy.ts
24
2
  import { statuses } from "@aomex/web";
25
- var AuthenticationAdapter = class {
3
+ var Strategy = class {
26
4
  openapi() {
27
5
  return {
28
6
  postMethod(methodItem) {
@@ -33,8 +11,45 @@ var AuthenticationAdapter = class {
33
11
  };
34
12
  }
35
13
  };
14
+
15
+ // src/authentication.ts
16
+ import { middleware } from "@aomex/core";
17
+ var Authentication = class {
18
+ constructor(options) {
19
+ this.options = options;
20
+ }
21
+ /**
22
+ * 获取策略实例
23
+ */
24
+ strategy(name) {
25
+ return this.options.strategies[name];
26
+ }
27
+ /**
28
+ * 身份认证中间件
29
+ */
30
+ authenticate(name, opts = {}) {
31
+ const { contextKey = name } = opts;
32
+ const strategy = this.strategy(name);
33
+ return middleware.web({
34
+ fn: async (ctx, next) => {
35
+ let payload = false;
36
+ try {
37
+ payload = await strategy["authenticate"](ctx);
38
+ } catch (e) {
39
+ return void ctx.throw(401, e);
40
+ }
41
+ if (payload === false) return ctx.throw(401);
42
+ Object.defineProperty(ctx, contextKey, {
43
+ get: () => payload
44
+ });
45
+ return next();
46
+ },
47
+ openapi: strategy["openapi"]()
48
+ });
49
+ }
50
+ };
36
51
  export {
37
- AuthenticationAdapter,
38
- authentication
52
+ Authentication,
53
+ Strategy
39
54
  };
40
55
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/authentication.ts","../src/authentication-adapter.ts"],"sourcesContent":["import type { WebMiddleware } from '@aomex/web';\nimport type { AuthenticationAdapter } from './authentication-adapter';\nimport { middleware } from '@aomex/core';\n\nconst DEFAULT_KEY = 'auth';\n\n/**\n * 授权认证中间件\n * @param adapter 授权具体实现方案\n * @param authKey 授权成功后挂载到ctx上的属性名称,默认值:`auth`\n */\nexport const authentication = <\n Payload extends object | string,\n Key extends string = typeof DEFAULT_KEY,\n>(\n adapter: AuthenticationAdapter<Payload>,\n authKey?: Key,\n): WebMiddleware<{ readonly [K in Key]: Payload }> => {\n return middleware.web({\n fn: async (ctx, next) => {\n let payload: Payload | false = false;\n\n try {\n payload = await adapter['authenticate'](ctx);\n } catch (e) {\n return void ctx.throw(401, e as Error);\n }\n\n if (payload === false) return void ctx.throw(401);\n\n Object.defineProperty(ctx, authKey || DEFAULT_KEY, {\n get: () => payload,\n });\n return next();\n },\n openapi: adapter['openapi'](),\n });\n};\n","import { statuses, type OpenApiInjector, type WebContext } from '@aomex/web';\n\nexport abstract class AuthenticationAdapter<Payload extends object | string> {\n protected abstract authenticate(ctx: WebContext): Promise<Payload | false>;\n\n protected openapi(): OpenApiInjector {\n return {\n postMethod(methodItem) {\n methodItem.responses[401] ||= {\n description: statuses.message[401]!,\n };\n },\n };\n }\n}\n"],"mappings":";AAEA,SAAS,kBAAkB;AAE3B,IAAM,cAAc;AAOb,IAAM,iBAAiB,CAI5B,SACA,YACoD;AACpD,SAAO,WAAW,IAAI;AAAA,IACpB,IAAI,OAAO,KAAK,SAAS;AACvB,UAAI,UAA2B;AAE/B,UAAI;AACF,kBAAU,MAAM,QAAQ,cAAc,EAAE,GAAG;AAAA,MAC7C,SAAS,GAAG;AACV,eAAO,KAAK,IAAI,MAAM,KAAK,CAAU;AAAA,MACvC;AAEA,UAAI,YAAY,MAAO,QAAO,KAAK,IAAI,MAAM,GAAG;AAEhD,aAAO,eAAe,KAAK,WAAW,aAAa;AAAA,QACjD,KAAK,MAAM;AAAA,MACb,CAAC;AACD,aAAO,KAAK;AAAA,IACd;AAAA,IACA,SAAS,QAAQ,SAAS,EAAE;AAAA,EAC9B,CAAC;AACH;;;ACrCA,SAAS,gBAAuD;AAEzD,IAAe,wBAAf,MAAsE;AAAA,EAGjE,UAA2B;AACnC,WAAO;AAAA,MACL,WAAW,YAAY;AACrB,mBAAW,UAAU,GAAG,MAAM;AAAA,UAC5B,aAAa,SAAS,QAAQ,GAAG;AAAA,QACnC;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/strategy.ts","../src/authentication.ts"],"sourcesContent":["import { statuses, type OpenApiInjector, type WebContext } from '@aomex/web';\n\nexport abstract class Strategy<Payload extends object | string> {\n protected abstract authenticate(ctx: WebContext): Promise<Payload | false>;\n\n protected openapi(): OpenApiInjector {\n return {\n postMethod(methodItem) {\n methodItem.responses[401] ||= {\n description: statuses.message[401]!,\n };\n },\n };\n }\n}\n","import type { WebMiddleware } from '@aomex/web';\nimport { Strategy } from './strategy';\nimport { middleware } from '@aomex/core';\n\nexport namespace Authentication {\n export interface Options<S extends { [K: string]: Strategy<object | string> }> {\n strategies: S;\n }\n}\n\nexport class Authentication<S extends { [K: string]: Strategy<object | string> }> {\n constructor(protected readonly options: Authentication.Options<S>) {}\n\n /**\n * 获取策略实例\n */\n public strategy<StrategyName extends keyof S>(name: StrategyName) {\n return this.options.strategies[name]! as S[StrategyName];\n }\n\n /**\n * 身份认证中间件\n */\n public authenticate<\n StrategyName extends keyof S,\n CtxKey extends string = StrategyName extends string ? StrategyName : never,\n Payload extends object | string = S[StrategyName] extends Strategy<infer P>\n ? P\n : never,\n >(\n name: StrategyName,\n opts: {\n /**\n * ctx上使用的属性\n */\n contextKey?: CtxKey;\n } = {},\n ): WebMiddleware<{ readonly [K in CtxKey]: Payload }> {\n const { contextKey = name } = opts;\n const strategy = this.strategy(name);\n\n return middleware.web({\n fn: async (ctx, next) => {\n let payload: Payload | false = false;\n try {\n payload = (await strategy['authenticate'](ctx)) as any;\n } catch (e) {\n return void ctx.throw(401, e as Error);\n }\n\n if (payload === false) return ctx.throw(401);\n\n Object.defineProperty(ctx, contextKey, {\n get: () => payload,\n });\n return next();\n },\n openapi: strategy['openapi'](),\n });\n }\n}\n"],"mappings":";AAAA,SAAS,gBAAuD;AAEzD,IAAe,WAAf,MAAyD;AAAA,EAGpD,UAA2B;AACnC,WAAO;AAAA,MACL,WAAW,YAAY;AACrB,mBAAW,UAAU,GAAG,MAAM;AAAA,UAC5B,aAAa,SAAS,QAAQ,GAAG;AAAA,QACnC;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;;;ACZA,SAAS,kBAAkB;AAQpB,IAAM,iBAAN,MAA2E;AAAA,EAChF,YAA+B,SAAoC;AAApC;AAAA,EAAqC;AAAA;AAAA;AAAA;AAAA,EAK7D,SAAuC,MAAoB;AAChE,WAAO,KAAK,QAAQ,WAAW,IAAI;AAAA,EACrC;AAAA;AAAA;AAAA;AAAA,EAKO,aAOL,MACA,OAKI,CAAC,GAC+C;AACpD,UAAM,EAAE,aAAa,KAAK,IAAI;AAC9B,UAAM,WAAW,KAAK,SAAS,IAAI;AAEnC,WAAO,WAAW,IAAI;AAAA,MACpB,IAAI,OAAO,KAAK,SAAS;AACvB,YAAI,UAA2B;AAC/B,YAAI;AACF,oBAAW,MAAM,SAAS,cAAc,EAAE,GAAG;AAAA,QAC/C,SAAS,GAAG;AACV,iBAAO,KAAK,IAAI,MAAM,KAAK,CAAU;AAAA,QACvC;AAEA,YAAI,YAAY,MAAO,QAAO,IAAI,MAAM,GAAG;AAE3C,eAAO,eAAe,KAAK,YAAY;AAAA,UACrC,KAAK,MAAM;AAAA,QACb,CAAC;AACD,eAAO,KAAK;AAAA,MACd;AAAA,MACA,SAAS,SAAS,SAAS,EAAE;AAAA,IAC/B,CAAC;AAAA,EACH;AACF;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aomex/auth",
3
- "version": "3.3.0",
3
+ "version": "3.4.0",
4
4
  "description": "身份认证统一入口",
5
5
  "type": "module",
6
6
  "types": "dist/index.d.ts",
@@ -32,8 +32,8 @@
32
32
  "@aomex/web": "^3.0.0"
33
33
  },
34
34
  "devDependencies": {
35
- "@aomex/web": "^3.3.0",
36
- "@aomex/core": "^3.3.0"
35
+ "@aomex/core": "^3.4.0",
36
+ "@aomex/web": "^3.4.0"
37
37
  },
38
38
  "scripts": {}
39
39
  }