kauth-sdk-node 0.0.17 → 0.0.1765774147-dev.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 (38) hide show
  1. package/dist/authenticator/authenticator.d.ts +14 -4
  2. package/dist/authenticator/authenticator.js +51 -5
  3. package/dist/authenticator/base.d.ts +35 -0
  4. package/dist/authenticator/base.js +20 -1
  5. package/dist/authenticator/google.js +14 -0
  6. package/dist/authenticator/index.d.ts +3 -3
  7. package/dist/authenticator/index.js +6 -0
  8. package/dist/authenticator/manager.d.ts +8 -0
  9. package/dist/authenticator/manager.js +64 -8
  10. package/dist/authenticator/miniprogram.d.ts +15 -0
  11. package/dist/authenticator/miniprogram.js +20 -0
  12. package/dist/authenticator/mozi.js +14 -0
  13. package/dist/authenticator/provider.d.ts +18 -5
  14. package/dist/authenticator/provider.js +55 -5
  15. package/dist/authorizer/index.d.ts +1 -0
  16. package/dist/authorizer/index.js +5 -0
  17. package/dist/authorizer/permission.d.ts +9 -0
  18. package/dist/authorizer/permission.js +51 -0
  19. package/dist/common/config/config.d.ts +31 -4
  20. package/dist/common/constant/http.d.ts +46 -0
  21. package/dist/common/constant/http.js +48 -1
  22. package/dist/common/http/kauth.d.ts +62 -14
  23. package/dist/common/http/kauth.js +199 -8
  24. package/dist/common/http/kflow.d.ts +14 -6
  25. package/dist/common/http/kflow.js +35 -4
  26. package/dist/common/http/request/kauth.d.ts +141 -0
  27. package/dist/common/http/request/kauth.js +5 -0
  28. package/dist/common/http/request/kflow.d.ts +22 -3
  29. package/dist/common/http/response/kauth.d.ts +56 -2
  30. package/dist/common/http/response/kauth.js +1 -1
  31. package/dist/common/http/tool/marmot.d.ts +26 -15
  32. package/dist/common/http/tool/marmot.js +92 -28
  33. package/dist/common/util/converter/permission.d.ts +12 -0
  34. package/dist/common/util/converter/permission.js +36 -0
  35. package/dist/index.d.ts +8 -3
  36. package/dist/index.js +31 -0
  37. package/package.json +30 -16
  38. package/CHANGELOG.md +0 -45
@@ -1,19 +1,29 @@
1
- import { AuthenticationInformation } from './base';
2
1
  import { KauthApi } from '../common/http/kauth';
2
+ import { AuthenticationInformation } from './base';
3
3
  interface Authenticator {
4
+ authorizeURL(callback: string, state: string, prompt?: string): Promise<string>;
4
5
  authenticate(token: string, redirectUri: string): Promise<AuthenticationInformation>;
5
- authorizeURL(callback: string): Promise<string>;
6
+ bind(userId: number, token: string, redirectUri: string): Promise<void>;
6
7
  }
7
8
  export declare class GoogleAuthenticator implements Authenticator {
8
9
  private kauthApi;
9
10
  constructor(kauthApi: KauthApi);
10
11
  authenticate(token: string, redirectUri: string): Promise<AuthenticationInformation>;
11
- authorizeURL(callback: string): Promise<string>;
12
+ bind(userId: number, token: string, redirectUri: string): Promise<void>;
13
+ authorizeURL(callback: string, state: string, prompt?: string): Promise<string>;
12
14
  }
13
15
  export declare class MoziAuthenticator implements Authenticator {
14
16
  private kauthApi;
15
17
  constructor(kauthApi: KauthApi);
16
18
  authenticate(token: string, redirectUri: string): Promise<AuthenticationInformation>;
17
- authorizeURL(callback: string): Promise<string>;
19
+ bind(userId: number, token: string, redirectUri: string): Promise<void>;
20
+ authorizeURL(callback: string, state: string, prompt?: string): Promise<string>;
21
+ }
22
+ export declare class MiniprogramAuthenticator implements Authenticator {
23
+ private kauthApi;
24
+ constructor(kauthApi: KauthApi);
25
+ authenticate(token: string, redirectUri: string): Promise<AuthenticationInformation>;
26
+ bind(userId: number, token: string, redirectUri: string): Promise<void>;
27
+ authorizeURL(callback: string, state: string, prompt?: string): Promise<string>;
18
28
  }
19
29
  export {};
@@ -1,9 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MoziAuthenticator = exports.GoogleAuthenticator = void 0;
3
+ exports.MiniprogramAuthenticator = exports.MoziAuthenticator = exports.GoogleAuthenticator = void 0;
4
4
  const mozi_1 = require("./mozi");
5
5
  const google_1 = require("./google");
6
+ const miniprogram_1 = require("./miniprogram");
6
7
  class GoogleAuthenticator {
8
+ kauthApi;
7
9
  constructor(kauthApi) {
8
10
  this.kauthApi = kauthApi;
9
11
  }
@@ -19,12 +21,20 @@ class GoogleAuthenticator {
19
21
  authenticationInformation.credential = result.credential;
20
22
  return authenticationInformation;
21
23
  }
22
- authorizeURL(callback) {
23
- return this.kauthApi.googleAuthorizeURL(callback);
24
+ async bind(userId, token, redirectUri) {
25
+ return this.kauthApi.googleBind({
26
+ user_id: userId,
27
+ code: token,
28
+ redirect_uri: redirectUri,
29
+ });
30
+ }
31
+ authorizeURL(callback, state, prompt = '') {
32
+ return this.kauthApi.googleAuthorizeURL(callback, state, prompt);
24
33
  }
25
34
  }
26
35
  exports.GoogleAuthenticator = GoogleAuthenticator;
27
36
  class MoziAuthenticator {
37
+ kauthApi;
28
38
  constructor(kauthApi) {
29
39
  this.kauthApi = kauthApi;
30
40
  }
@@ -40,8 +50,44 @@ class MoziAuthenticator {
40
50
  authenticationInformation.credential = result.credential;
41
51
  return authenticationInformation;
42
52
  }
43
- authorizeURL(callback) {
44
- return this.kauthApi.moziAuthorizeURL(callback);
53
+ async bind(userId, token, redirectUri) {
54
+ return this.kauthApi.moziBind({
55
+ user_id: userId,
56
+ code: token,
57
+ redirect_uri: redirectUri,
58
+ });
59
+ }
60
+ authorizeURL(callback, state, prompt = '') {
61
+ return this.kauthApi.moziAuthorizeURL(callback, state, prompt);
45
62
  }
46
63
  }
47
64
  exports.MoziAuthenticator = MoziAuthenticator;
65
+ class MiniprogramAuthenticator {
66
+ kauthApi;
67
+ constructor(kauthApi) {
68
+ this.kauthApi = kauthApi;
69
+ }
70
+ async authenticate(token, redirectUri) {
71
+ const request = {
72
+ code: token,
73
+ redirect_uri: redirectUri,
74
+ };
75
+ const result = await this.kauthApi.miniprogramAuthenticate(request);
76
+ const authenticationInformation = new miniprogram_1.MiniprogramAuthenticationInformation();
77
+ authenticationInformation.primaryPrincipal = result.primaryPrincipal;
78
+ authenticationInformation.principal = result.principal;
79
+ authenticationInformation.credential = result.credential;
80
+ return authenticationInformation;
81
+ }
82
+ async bind(userId, token, redirectUri) {
83
+ return this.kauthApi.miniprogramBind({
84
+ user_id: userId,
85
+ code: token,
86
+ redirect_uri: redirectUri,
87
+ });
88
+ }
89
+ authorizeURL(callback, state, prompt = '') {
90
+ return this.kauthApi.miniprogramAuthorizeURL(callback, state, prompt);
91
+ }
92
+ }
93
+ exports.MiniprogramAuthenticator = MiniprogramAuthenticator;
@@ -1,3 +1,38 @@
1
+ export declare enum UserAccountType {
2
+ Mozi = "mozi",
3
+ Google = "google",
4
+ Miniprogram = "miniprogram"
5
+ }
6
+ export declare enum PreAuthorizationAllowedErrorCode {
7
+ UserNotFound = "userNotFound",
8
+ UserNotInWhitelist = "userNotInWhitelist",
9
+ TokenExpired = "tokenExpired",
10
+ TokenNotExisted = "tokenNotExisted",
11
+ TokenUsedUp = "tokenUsedUp"
12
+ }
13
+ export type PreAuthorizationAllowed = ({
14
+ allowed: true;
15
+ } & {
16
+ id: number;
17
+ userId: number;
18
+ type: UserAccountType;
19
+ target: string;
20
+ token: string;
21
+ roleType: 'kauth' | string;
22
+ roleName: string;
23
+ roleExtendValue: string;
24
+ resolvedAuthorizeeTargets: Array<{
25
+ type: UserAccountType;
26
+ target: string;
27
+ id: number;
28
+ }>;
29
+ callback: string;
30
+ }) | ({
31
+ allowed: false;
32
+ } & {
33
+ code: PreAuthorizationAllowedErrorCode;
34
+ reason: string;
35
+ });
1
36
  export interface Principal {
2
37
  }
3
38
  export interface Credential {
@@ -1,6 +1,25 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PrimaryPrincipal = void 0;
3
+ exports.PrimaryPrincipal = exports.PreAuthorizationAllowedErrorCode = exports.UserAccountType = void 0;
4
+ var UserAccountType;
5
+ (function (UserAccountType) {
6
+ UserAccountType["Mozi"] = "mozi";
7
+ UserAccountType["Google"] = "google";
8
+ UserAccountType["Miniprogram"] = "miniprogram";
9
+ })(UserAccountType || (exports.UserAccountType = UserAccountType = {}));
10
+ var PreAuthorizationAllowedErrorCode;
11
+ (function (PreAuthorizationAllowedErrorCode) {
12
+ PreAuthorizationAllowedErrorCode["UserNotFound"] = "userNotFound";
13
+ PreAuthorizationAllowedErrorCode["UserNotInWhitelist"] = "userNotInWhitelist";
14
+ PreAuthorizationAllowedErrorCode["TokenExpired"] = "tokenExpired";
15
+ PreAuthorizationAllowedErrorCode["TokenNotExisted"] = "tokenNotExisted";
16
+ PreAuthorizationAllowedErrorCode["TokenUsedUp"] = "tokenUsedUp";
17
+ })(PreAuthorizationAllowedErrorCode || (exports.PreAuthorizationAllowedErrorCode = PreAuthorizationAllowedErrorCode = {}));
4
18
  class PrimaryPrincipal {
19
+ id;
20
+ name;
21
+ email;
22
+ avatar;
23
+ status;
5
24
  }
6
25
  exports.PrimaryPrincipal = PrimaryPrincipal;
@@ -2,11 +2,25 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.GoogleAuthenticationInformation = exports.GoogleCredential = exports.GooglePrincipal = void 0;
4
4
  class GooglePrincipal {
5
+ sub;
6
+ name;
7
+ givenName;
8
+ familyName;
9
+ picture;
10
+ email;
11
+ emailVerified;
12
+ locale;
5
13
  }
6
14
  exports.GooglePrincipal = GooglePrincipal;
7
15
  class GoogleCredential {
16
+ accessToken;
17
+ expiresIn;
18
+ refreshToken;
8
19
  }
9
20
  exports.GoogleCredential = GoogleCredential;
10
21
  class GoogleAuthenticationInformation {
22
+ primaryPrincipal;
23
+ principal;
24
+ credential;
11
25
  }
12
26
  exports.GoogleAuthenticationInformation = GoogleAuthenticationInformation;
@@ -1,3 +1,3 @@
1
- export { GoogleAuthenticator, MoziAuthenticator } from './authenticator';
2
- export { AuthenticationInformation } from './base';
3
- export { JsonWebTokenAuthenticationProvider } from './provider';
1
+ export { GoogleAuthenticator, MoziAuthenticator, MiniprogramAuthenticator } from './authenticator';
2
+ export { AuthenticationInformation, PrimaryPrincipal, UserAccountType, PreAuthorizationAllowed } from './base';
3
+ export { JsonWebTokenSubjectProvider, JsonWebTokenAuthenticationProvider } from './provider';
@@ -1,7 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.JsonWebTokenAuthenticationProvider = exports.JsonWebTokenSubjectProvider = exports.UserAccountType = exports.PrimaryPrincipal = exports.MiniprogramAuthenticator = exports.MoziAuthenticator = exports.GoogleAuthenticator = void 0;
3
4
  var authenticator_1 = require("./authenticator");
4
5
  Object.defineProperty(exports, "GoogleAuthenticator", { enumerable: true, get: function () { return authenticator_1.GoogleAuthenticator; } });
5
6
  Object.defineProperty(exports, "MoziAuthenticator", { enumerable: true, get: function () { return authenticator_1.MoziAuthenticator; } });
7
+ Object.defineProperty(exports, "MiniprogramAuthenticator", { enumerable: true, get: function () { return authenticator_1.MiniprogramAuthenticator; } });
8
+ var base_1 = require("./base");
9
+ Object.defineProperty(exports, "PrimaryPrincipal", { enumerable: true, get: function () { return base_1.PrimaryPrincipal; } });
10
+ Object.defineProperty(exports, "UserAccountType", { enumerable: true, get: function () { return base_1.UserAccountType; } });
6
11
  var provider_1 = require("./provider");
12
+ Object.defineProperty(exports, "JsonWebTokenSubjectProvider", { enumerable: true, get: function () { return provider_1.JsonWebTokenSubjectProvider; } });
7
13
  Object.defineProperty(exports, "JsonWebTokenAuthenticationProvider", { enumerable: true, get: function () { return provider_1.JsonWebTokenAuthenticationProvider; } });
@@ -2,6 +2,7 @@ import { AuthenticationInformation } from './base';
2
2
  import { GoogleAuthenticationInformation } from './google';
3
3
  import { MoziAuthenticationInformation } from './mozi';
4
4
  import { KauthApi } from '../common/http/kauth';
5
+ import { MiniprogramAuthenticationInformation } from './miniprogram';
5
6
  export interface Claims {
6
7
  [key: string]: any;
7
8
  }
@@ -12,6 +13,7 @@ export interface AuthenticationInfoManger {
12
13
  export declare class DelegatedAuthenticationInfoManager implements AuthenticationInfoManger {
13
14
  private googleAuthenticationInfoManager;
14
15
  private moziAuthenticationInfoManager;
16
+ private miniprogramAuthenticationInfoManager;
15
17
  constructor(kauthApi: KauthApi);
16
18
  append(information: AuthenticationInformation, claims: Claims): boolean;
17
19
  transform(claims: Claims): Promise<AuthenticationInformation>;
@@ -28,3 +30,9 @@ export declare class MoziAuthenticationInfoManager implements AuthenticationInfo
28
30
  append(information: AuthenticationInformation, claims: Claims): boolean;
29
31
  transform(claims: Claims): Promise<MoziAuthenticationInformation>;
30
32
  }
33
+ export declare class MiniprogramAuthenticationInfoManager implements AuthenticationInfoManger {
34
+ private kauthApi;
35
+ constructor(kauthApi: KauthApi);
36
+ append(information: AuthenticationInformation, claims: Claims): boolean;
37
+ transform(claims: Claims): Promise<MiniprogramAuthenticationInformation>;
38
+ }
@@ -1,15 +1,26 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MoziAuthenticationInfoManager = exports.GoogleAuthenticationInfoManger = exports.DelegatedAuthenticationInfoManager = void 0;
3
+ exports.MiniprogramAuthenticationInfoManager = exports.MoziAuthenticationInfoManager = exports.GoogleAuthenticationInfoManger = exports.DelegatedAuthenticationInfoManager = void 0;
4
4
  const base_1 = require("./base");
5
5
  const google_1 = require("./google");
6
6
  const mozi_1 = require("./mozi");
7
- const Sub = "sub";
8
- const Provider = "provider";
7
+ const miniprogram_1 = require("./miniprogram");
8
+ const Sub = 'sub';
9
+ const Provider = 'provider';
10
+ var ProviderType;
11
+ (function (ProviderType) {
12
+ ProviderType["Mozi"] = "mozi";
13
+ ProviderType["Google"] = "google";
14
+ ProviderType["Miniprogram"] = "miniprogram";
15
+ })(ProviderType || (ProviderType = {}));
9
16
  class DelegatedAuthenticationInfoManager {
17
+ googleAuthenticationInfoManager;
18
+ moziAuthenticationInfoManager;
19
+ miniprogramAuthenticationInfoManager;
10
20
  constructor(kauthApi) {
11
21
  this.googleAuthenticationInfoManager = new GoogleAuthenticationInfoManger(kauthApi);
12
22
  this.moziAuthenticationInfoManager = new MoziAuthenticationInfoManager(kauthApi);
23
+ this.miniprogramAuthenticationInfoManager = new MiniprogramAuthenticationInfoManager(kauthApi);
13
24
  }
14
25
  append(information, claims) {
15
26
  if (this.moziAuthenticationInfoManager.append(information, claims)) {
@@ -18,21 +29,27 @@ class DelegatedAuthenticationInfoManager {
18
29
  if (this.googleAuthenticationInfoManager.append(information, claims)) {
19
30
  return true;
20
31
  }
32
+ if (this.miniprogramAuthenticationInfoManager.append(information, claims)) {
33
+ return true;
34
+ }
21
35
  return false;
22
36
  }
23
37
  transform(claims) {
24
38
  const provider = claims[Provider];
25
39
  switch (provider) {
26
- case 'mozi':
40
+ case ProviderType.Mozi:
27
41
  return this.moziAuthenticationInfoManager.transform(claims);
28
- case 'google':
42
+ case ProviderType.Google:
29
43
  return this.googleAuthenticationInfoManager.transform(claims);
44
+ case ProviderType.Miniprogram:
45
+ return this.miniprogramAuthenticationInfoManager.transform(claims);
30
46
  }
31
- throw new Error("unkown provider for jwt " + claims[Sub]);
47
+ throw new Error('unkown provider for jwt ' + claims[Sub]);
32
48
  }
33
49
  }
34
50
  exports.DelegatedAuthenticationInfoManager = DelegatedAuthenticationInfoManager;
35
51
  class GoogleAuthenticationInfoManger {
52
+ kauthApi;
36
53
  constructor(kauthApi) {
37
54
  this.kauthApi = kauthApi;
38
55
  }
@@ -41,7 +58,7 @@ class GoogleAuthenticationInfoManger {
41
58
  const specifiedInformation = information;
42
59
  const id = specifiedInformation.primaryPrincipal.id;
43
60
  claims[Sub] = String(id);
44
- claims[Provider] = 'google';
61
+ claims[Provider] = ProviderType.Google;
45
62
  return true;
46
63
  }
47
64
  return false;
@@ -74,6 +91,7 @@ class GoogleAuthenticationInfoManger {
74
91
  }
75
92
  exports.GoogleAuthenticationInfoManger = GoogleAuthenticationInfoManger;
76
93
  class MoziAuthenticationInfoManager {
94
+ kauthApi;
77
95
  constructor(kauthApi) {
78
96
  this.kauthApi = kauthApi;
79
97
  }
@@ -82,7 +100,7 @@ class MoziAuthenticationInfoManager {
82
100
  const specifiedInformation = information;
83
101
  const id = specifiedInformation.primaryPrincipal.id;
84
102
  claims[Sub] = String(id);
85
- claims[Provider] = 'mozi';
103
+ claims[Provider] = ProviderType.Mozi;
86
104
  return true;
87
105
  }
88
106
  return false;
@@ -113,3 +131,41 @@ class MoziAuthenticationInfoManager {
113
131
  }
114
132
  }
115
133
  exports.MoziAuthenticationInfoManager = MoziAuthenticationInfoManager;
134
+ class MiniprogramAuthenticationInfoManager {
135
+ kauthApi;
136
+ constructor(kauthApi) {
137
+ this.kauthApi = kauthApi;
138
+ }
139
+ append(information, claims) {
140
+ if (information instanceof miniprogram_1.MiniprogramAuthenticationInformation) {
141
+ const specifiedInformation = information;
142
+ const id = specifiedInformation.primaryPrincipal.id;
143
+ claims[Sub] = String(id);
144
+ claims[Provider] = ProviderType.Miniprogram;
145
+ return true;
146
+ }
147
+ return false;
148
+ }
149
+ async transform(claims) {
150
+ const sub = parseInt(claims[Sub], 10);
151
+ const miniprogramAuthenticationInformation = await this.kauthApi.getMiniprogramAuthenticationInformation(sub);
152
+ const rawPrimaryPrincipal = miniprogramAuthenticationInformation.primaryPrincipal;
153
+ const primaryPrincipal = new base_1.PrimaryPrincipal();
154
+ primaryPrincipal.id = rawPrimaryPrincipal.id;
155
+ primaryPrincipal.name = rawPrimaryPrincipal.name;
156
+ primaryPrincipal.email = rawPrimaryPrincipal.email;
157
+ primaryPrincipal.avatar = rawPrimaryPrincipal.avatar;
158
+ primaryPrincipal.status = rawPrimaryPrincipal.status;
159
+ const rawPrincipal = miniprogramAuthenticationInformation.principal;
160
+ const principal = new miniprogram_1.MiniprogramPrincipal();
161
+ principal.customerId = rawPrincipal.customerId;
162
+ principal.email = rawPrincipal.email;
163
+ principal.nickName = rawPrincipal.nickName;
164
+ principal.trustLoginId = rawPrincipal.trustLoginId;
165
+ const authenticationInformation = new miniprogram_1.MiniprogramAuthenticationInformation();
166
+ authenticationInformation.primaryPrincipal = primaryPrincipal;
167
+ authenticationInformation.principal = principal;
168
+ return authenticationInformation;
169
+ }
170
+ }
171
+ exports.MiniprogramAuthenticationInfoManager = MiniprogramAuthenticationInfoManager;
@@ -0,0 +1,15 @@
1
+ import { Principal, PrimaryPrincipal, Credential, AuthenticationInformation } from './base';
2
+ export declare class MiniprogramPrincipal implements Principal {
3
+ trustLoginId: string;
4
+ customerId: string;
5
+ email: string;
6
+ nickName: string;
7
+ }
8
+ export declare class MiniprogramCredential implements Credential {
9
+ accessToken: string;
10
+ }
11
+ export declare class MiniprogramAuthenticationInformation implements AuthenticationInformation {
12
+ primaryPrincipal: PrimaryPrincipal;
13
+ principal: MiniprogramPrincipal;
14
+ credential: MiniprogramCredential;
15
+ }
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MiniprogramAuthenticationInformation = exports.MiniprogramCredential = exports.MiniprogramPrincipal = void 0;
4
+ class MiniprogramPrincipal {
5
+ trustLoginId;
6
+ customerId;
7
+ email;
8
+ nickName;
9
+ }
10
+ exports.MiniprogramPrincipal = MiniprogramPrincipal;
11
+ class MiniprogramCredential {
12
+ accessToken;
13
+ }
14
+ exports.MiniprogramCredential = MiniprogramCredential;
15
+ class MiniprogramAuthenticationInformation {
16
+ primaryPrincipal;
17
+ principal;
18
+ credential;
19
+ }
20
+ exports.MiniprogramAuthenticationInformation = MiniprogramAuthenticationInformation;
@@ -2,11 +2,25 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MoziAuthenticationInformation = exports.MoziCredential = exports.MoziPrincipal = void 0;
4
4
  class MoziPrincipal {
5
+ account;
6
+ employeeCode;
7
+ lastName;
8
+ namespace;
9
+ nickNameCn;
10
+ openid;
11
+ realmId;
12
+ realmName;
5
13
  }
6
14
  exports.MoziPrincipal = MoziPrincipal;
7
15
  class MoziCredential {
16
+ accessToken;
17
+ expiresIn;
18
+ refreshToken;
8
19
  }
9
20
  exports.MoziCredential = MoziCredential;
10
21
  class MoziAuthenticationInformation {
22
+ primaryPrincipal;
23
+ principal;
24
+ credential;
11
25
  }
12
26
  exports.MoziAuthenticationInformation = MoziAuthenticationInformation;
@@ -1,9 +1,21 @@
1
- import { AuthenticationInformation } from "./base";
2
- import { KauthApi } from "../common/http/kauth";
3
- import { KauthConfig } from "../common/config/config";
1
+ import { AuthenticationInformation } from './base';
2
+ import { KauthApi } from '../common/http/kauth';
3
+ import { KauthConfig } from '../common/config/config';
4
+ export interface SubjectProvider {
5
+ verify(token: string): Promise<number>;
6
+ refresh(token: string): string;
7
+ }
4
8
  export interface AuthenticationProvider {
5
9
  verify(token: string): Promise<AuthenticationInformation>;
6
- authenticate(authenticationInformation: AuthenticationInformation): string;
10
+ refresh(token: string): string;
11
+ authenticate(authenticationInformation: AuthenticationInformation, dynamicDuration?: number): string;
12
+ }
13
+ export declare class JsonWebTokenSubjectProvider implements SubjectProvider {
14
+ private secret;
15
+ private duration;
16
+ constructor(kauthConfig: KauthConfig);
17
+ verify(token: string): Promise<number>;
18
+ refresh(token: string): string;
7
19
  }
8
20
  export declare class JsonWebTokenAuthenticationProvider implements AuthenticationProvider {
9
21
  private authenticationInfoManager;
@@ -11,5 +23,6 @@ export declare class JsonWebTokenAuthenticationProvider implements Authenticatio
11
23
  private duration;
12
24
  constructor(kauthApi: KauthApi, kauthConfig: KauthConfig);
13
25
  verify(token: string): Promise<AuthenticationInformation>;
14
- authenticate(authenticationInformation: AuthenticationInformation): string;
26
+ refresh(token: string): string;
27
+ authenticate(authenticationInformation: AuthenticationInformation, dynamicDuration?: number): string;
15
28
  }
@@ -3,28 +3,78 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.JsonWebTokenAuthenticationProvider = void 0;
6
+ exports.JsonWebTokenAuthenticationProvider = exports.JsonWebTokenSubjectProvider = void 0;
7
7
  const dayjs_1 = __importDefault(require("dayjs"));
8
8
  const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
9
9
  const manager_1 = require("./manager");
10
10
  const Exp = 'exp';
11
+ const Sub = 'sub';
12
+ const Iat = 'iat';
13
+ class JsonWebTokenSubjectProvider {
14
+ secret;
15
+ duration;
16
+ constructor(kauthConfig) {
17
+ this.secret = kauthConfig.jwtSecret;
18
+ this.duration = kauthConfig.jwtDuration;
19
+ }
20
+ async verify(token) {
21
+ try {
22
+ const claims = jsonwebtoken_1.default.verify(token, this.secret);
23
+ return parseInt(claims[Sub], 10);
24
+ }
25
+ catch (error) {
26
+ return null;
27
+ }
28
+ }
29
+ refresh(token) {
30
+ const { [Iat]: iat, [Exp]: exp, ...claims } = jsonwebtoken_1.default.decode(token);
31
+ // jwt life duration from claims
32
+ const duration = exp && iat ? exp - iat : this.duration;
33
+ const elapsed = Math.floor(Date.now() / 1000) - iat;
34
+ if (elapsed > duration / 2 && elapsed < duration) {
35
+ const exp = (0, dayjs_1.default)().add(duration, 'second').unix();
36
+ claims[Exp] = exp;
37
+ return jsonwebtoken_1.default.sign(claims, this.secret);
38
+ }
39
+ return null;
40
+ }
41
+ }
42
+ exports.JsonWebTokenSubjectProvider = JsonWebTokenSubjectProvider;
11
43
  class JsonWebTokenAuthenticationProvider {
44
+ authenticationInfoManager;
45
+ secret;
46
+ duration;
12
47
  constructor(kauthApi, kauthConfig) {
13
48
  this.authenticationInfoManager = new manager_1.DelegatedAuthenticationInfoManager(kauthApi);
14
49
  this.secret = kauthConfig.jwtSecret;
15
50
  this.duration = kauthConfig.jwtDuration;
16
51
  }
17
- verify(token) {
52
+ async verify(token) {
18
53
  try {
19
54
  const claims = jsonwebtoken_1.default.verify(token, this.secret);
20
- return this.authenticationInfoManager.transform(claims);
55
+ const authenticationInformation = await this.authenticationInfoManager.transform(claims);
56
+ return authenticationInformation;
21
57
  }
22
58
  catch (error) {
23
59
  return null;
24
60
  }
25
61
  }
26
- authenticate(authenticationInformation) {
27
- const exp = dayjs_1.default().add(this.duration, 'second').unix();
62
+ refresh(token) {
63
+ const { [Iat]: iat, [Exp]: exp, ...claims } = jsonwebtoken_1.default.decode(token);
64
+ // jwt life duration from claims
65
+ const duration = exp && iat ? exp - iat : this.duration;
66
+ const elapsed = Math.floor(Date.now() / 1000) - iat;
67
+ if (elapsed > duration / 2 && elapsed < duration) {
68
+ const exp = (0, dayjs_1.default)().add(duration, 'second').unix();
69
+ claims[Exp] = exp;
70
+ return jsonwebtoken_1.default.sign(claims, this.secret);
71
+ }
72
+ return null;
73
+ }
74
+ authenticate(authenticationInformation, dynamicDuration) {
75
+ const exp = (0, dayjs_1.default)()
76
+ .add(dynamicDuration || this.duration, 'second')
77
+ .unix();
28
78
  const claims = {};
29
79
  claims[Exp] = exp;
30
80
  if (!this.authenticationInfoManager.append(authenticationInformation, claims)) {
@@ -0,0 +1 @@
1
+ export { PermissionCore } from './permission';
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PermissionCore = void 0;
4
+ var permission_1 = require("./permission");
5
+ Object.defineProperty(exports, "PermissionCore", { enumerable: true, get: function () { return permission_1.PermissionCore; } });
@@ -0,0 +1,9 @@
1
+ interface IPermissionPart {
2
+ [key: string]: true;
3
+ }
4
+ export declare class PermissionCore {
5
+ parts: IPermissionPart[];
6
+ setParts(str: string): void;
7
+ implies(target: PermissionCore): boolean;
8
+ }
9
+ export {};
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PermissionCore = void 0;
4
+ const WildcardToken = '*';
5
+ const PartDividerToken = ':';
6
+ const SubpartDividerToken = ',';
7
+ class PermissionCore {
8
+ // permission can:operation:a,b will be transformed to a certain structure,
9
+ // like [ { can: true }, { operation: true }, { a: true, b:true } ]
10
+ parts = [];
11
+ // A user's permission is stored by a string in the database.
12
+ //
13
+ // We should convert it to a certain structure, to make it comparable
14
+ setParts(str) {
15
+ const parts = [];
16
+ str.split(PartDividerToken).forEach((item) => {
17
+ const subParts = {};
18
+ item.split(SubpartDividerToken).forEach((subItem) => {
19
+ subParts[subItem] = true;
20
+ });
21
+ parts.push(subParts);
22
+ });
23
+ this.parts = parts;
24
+ }
25
+ // Implies this permissions to another
26
+ //
27
+ // For example, one of a user's permission is can:operation:*, and a certain permission
28
+ // is can:operation:machine. To check whether a user have this certain permission,
29
+ // we can make the user's permission imply the certain permission
30
+ implies(target) {
31
+ const originParts = this.parts;
32
+ const targetParts = target.parts;
33
+ for (let i = 0; i < targetParts.length; i++) {
34
+ const targetPart = targetParts[i];
35
+ if (i + 1 > originParts.length) {
36
+ return true;
37
+ }
38
+ const originPart = originParts[i];
39
+ if (originPart[WildcardToken]) {
40
+ continue;
41
+ }
42
+ for (const targetSubPart of Object.keys(targetPart)) {
43
+ if (!originPart[targetSubPart]) {
44
+ return false;
45
+ }
46
+ }
47
+ }
48
+ return true;
49
+ }
50
+ }
51
+ exports.PermissionCore = PermissionCore;