@nocobase/auth 0.10.0-alpha.2
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 +201 -0
- package/lib/actions.d.ts +2 -0
- package/lib/actions.js +51 -0
- package/lib/auth-manager.d.ts +47 -0
- package/lib/auth-manager.js +104 -0
- package/lib/auth.d.ts +31 -0
- package/lib/auth.js +32 -0
- package/lib/base/auth.d.ts +22 -0
- package/lib/base/auth.js +81 -0
- package/lib/base/jwt-service.d.ts +12 -0
- package/lib/base/jwt-service.js +49 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.js +49 -0
- package/package.json +20 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/lib/actions.d.ts
ADDED
package/lib/actions.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.actions = void 0;
|
|
7
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
8
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
9
|
+
const actions = {
|
|
10
|
+
signIn: function () {
|
|
11
|
+
var _signIn = _asyncToGenerator(function* (ctx, next) {
|
|
12
|
+
ctx.body = yield ctx.auth.signIn();
|
|
13
|
+
yield next();
|
|
14
|
+
});
|
|
15
|
+
function signIn(_x, _x2) {
|
|
16
|
+
return _signIn.apply(this, arguments);
|
|
17
|
+
}
|
|
18
|
+
return signIn;
|
|
19
|
+
}(),
|
|
20
|
+
signOut: function () {
|
|
21
|
+
var _signOut = _asyncToGenerator(function* (ctx, next) {
|
|
22
|
+
yield ctx.auth.signOut();
|
|
23
|
+
yield next();
|
|
24
|
+
});
|
|
25
|
+
function signOut(_x3, _x4) {
|
|
26
|
+
return _signOut.apply(this, arguments);
|
|
27
|
+
}
|
|
28
|
+
return signOut;
|
|
29
|
+
}(),
|
|
30
|
+
signUp: function () {
|
|
31
|
+
var _signUp = _asyncToGenerator(function* (ctx, next) {
|
|
32
|
+
yield ctx.auth.signUp();
|
|
33
|
+
yield next();
|
|
34
|
+
});
|
|
35
|
+
function signUp(_x5, _x6) {
|
|
36
|
+
return _signUp.apply(this, arguments);
|
|
37
|
+
}
|
|
38
|
+
return signUp;
|
|
39
|
+
}(),
|
|
40
|
+
check: function () {
|
|
41
|
+
var _check = _asyncToGenerator(function* (ctx, next) {
|
|
42
|
+
ctx.body = ctx.auth.user || {};
|
|
43
|
+
yield next();
|
|
44
|
+
});
|
|
45
|
+
function check(_x7, _x8) {
|
|
46
|
+
return _check.apply(this, arguments);
|
|
47
|
+
}
|
|
48
|
+
return check;
|
|
49
|
+
}()
|
|
50
|
+
};
|
|
51
|
+
exports.actions = actions;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { Context, Next } from '@nocobase/actions';
|
|
2
|
+
import { Model } from '@nocobase/database';
|
|
3
|
+
import { Registry } from '@nocobase/utils';
|
|
4
|
+
import { Auth, AuthExtend } from './auth';
|
|
5
|
+
declare type Storer = {
|
|
6
|
+
get: (name: string) => Promise<Model>;
|
|
7
|
+
};
|
|
8
|
+
declare type AuthManagerOptions = {
|
|
9
|
+
authKey: string;
|
|
10
|
+
default?: string;
|
|
11
|
+
};
|
|
12
|
+
declare type AuthConfig = {
|
|
13
|
+
auth: AuthExtend<Auth>;
|
|
14
|
+
};
|
|
15
|
+
export declare class AuthManager {
|
|
16
|
+
protected options: AuthManagerOptions;
|
|
17
|
+
protected authTypes: Registry<AuthConfig>;
|
|
18
|
+
protected storer: Storer;
|
|
19
|
+
constructor(options: AuthManagerOptions);
|
|
20
|
+
setStorer(storer: Storer): void;
|
|
21
|
+
/**
|
|
22
|
+
* registerTypes
|
|
23
|
+
* @description Add a new authenticate type and the corresponding authenticator.
|
|
24
|
+
* The types will show in the authenticators list of the admin panel.
|
|
25
|
+
*
|
|
26
|
+
* @param {string} authType - The type of the authenticator. It is required to be unique.
|
|
27
|
+
* @param {AuthConfig} authConfig - Configurations of the kind of authenticator.
|
|
28
|
+
*/
|
|
29
|
+
registerTypes(authType: string, authConfig: AuthConfig): void;
|
|
30
|
+
listTypes(): string[];
|
|
31
|
+
getAuthConfig(authType: string): AuthConfig;
|
|
32
|
+
/**
|
|
33
|
+
* get
|
|
34
|
+
* @description Get authenticator instance by name.
|
|
35
|
+
* @param {string} name - The name of the authenticator.
|
|
36
|
+
* @return {Promise<Auth>} authenticator instance.
|
|
37
|
+
*/
|
|
38
|
+
get(name: string, ctx: Context): Promise<Auth>;
|
|
39
|
+
/**
|
|
40
|
+
* middleware
|
|
41
|
+
* @description Auth middleware, used to check the authentication status.
|
|
42
|
+
*/
|
|
43
|
+
middleware(): (ctx: Context & {
|
|
44
|
+
auth: Auth;
|
|
45
|
+
}, next: Next) => Promise<any>;
|
|
46
|
+
}
|
|
47
|
+
export {};
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.AuthManager = void 0;
|
|
7
|
+
function _utils() {
|
|
8
|
+
const data = require("@nocobase/utils");
|
|
9
|
+
_utils = function _utils() {
|
|
10
|
+
return data;
|
|
11
|
+
};
|
|
12
|
+
return data;
|
|
13
|
+
}
|
|
14
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
15
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
16
|
+
class AuthManager {
|
|
17
|
+
constructor(options) {
|
|
18
|
+
this.options = void 0;
|
|
19
|
+
this.authTypes = new (_utils().Registry)();
|
|
20
|
+
// authenticators collection manager.
|
|
21
|
+
this.storer = void 0;
|
|
22
|
+
this.options = options;
|
|
23
|
+
}
|
|
24
|
+
setStorer(storer) {
|
|
25
|
+
this.storer = storer;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* registerTypes
|
|
29
|
+
* @description Add a new authenticate type and the corresponding authenticator.
|
|
30
|
+
* The types will show in the authenticators list of the admin panel.
|
|
31
|
+
*
|
|
32
|
+
* @param {string} authType - The type of the authenticator. It is required to be unique.
|
|
33
|
+
* @param {AuthConfig} authConfig - Configurations of the kind of authenticator.
|
|
34
|
+
*/
|
|
35
|
+
registerTypes(authType, authConfig) {
|
|
36
|
+
this.authTypes.register(authType, authConfig);
|
|
37
|
+
}
|
|
38
|
+
listTypes() {
|
|
39
|
+
return Array.from(this.authTypes.getKeys());
|
|
40
|
+
}
|
|
41
|
+
getAuthConfig(authType) {
|
|
42
|
+
return this.authTypes.get(authType);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* get
|
|
46
|
+
* @description Get authenticator instance by name.
|
|
47
|
+
* @param {string} name - The name of the authenticator.
|
|
48
|
+
* @return {Promise<Auth>} authenticator instance.
|
|
49
|
+
*/
|
|
50
|
+
get(name, ctx) {
|
|
51
|
+
var _this = this;
|
|
52
|
+
return _asyncToGenerator(function* () {
|
|
53
|
+
if (!_this.storer) {
|
|
54
|
+
throw new Error('AuthManager.storer is not set.');
|
|
55
|
+
}
|
|
56
|
+
const authenticator = yield _this.storer.get(name);
|
|
57
|
+
if (!authenticator) {
|
|
58
|
+
throw new Error(`Authenticator [${name}] is not found.`);
|
|
59
|
+
}
|
|
60
|
+
const _this$authTypes$get = _this.authTypes.get(authenticator.authType),
|
|
61
|
+
auth = _this$authTypes$get.auth;
|
|
62
|
+
if (!auth) {
|
|
63
|
+
throw new Error(`AuthType [${name}] is not found.`);
|
|
64
|
+
}
|
|
65
|
+
return new auth({
|
|
66
|
+
authenticator,
|
|
67
|
+
options: authenticator.options,
|
|
68
|
+
ctx
|
|
69
|
+
});
|
|
70
|
+
})();
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* middleware
|
|
74
|
+
* @description Auth middleware, used to check the authentication status.
|
|
75
|
+
*/
|
|
76
|
+
middleware() {
|
|
77
|
+
var _this2 = this;
|
|
78
|
+
return /*#__PURE__*/function () {
|
|
79
|
+
var _ref = _asyncToGenerator(function* (ctx, next) {
|
|
80
|
+
const name = ctx.get(_this2.options.authKey) || _this2.options.default;
|
|
81
|
+
let authenticator;
|
|
82
|
+
try {
|
|
83
|
+
authenticator = yield ctx.app.authManager.get(name, ctx);
|
|
84
|
+
ctx.auth = authenticator;
|
|
85
|
+
} catch (err) {
|
|
86
|
+
ctx.auth = {};
|
|
87
|
+
ctx.app.logger.warn(`auth, ${err.message}, ${err.stack}`);
|
|
88
|
+
return next();
|
|
89
|
+
}
|
|
90
|
+
if (authenticator) {
|
|
91
|
+
const user = yield ctx.auth.check();
|
|
92
|
+
if (user) {
|
|
93
|
+
ctx.auth.user = user;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
yield next();
|
|
97
|
+
});
|
|
98
|
+
return function (_x, _x2) {
|
|
99
|
+
return _ref.apply(this, arguments);
|
|
100
|
+
};
|
|
101
|
+
}();
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
exports.AuthManager = AuthManager;
|
package/lib/auth.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Context } from '@nocobase/actions';
|
|
2
|
+
import { Model } from '@nocobase/database';
|
|
3
|
+
export declare type AuthConfig = {
|
|
4
|
+
authenticator: Model;
|
|
5
|
+
options: {
|
|
6
|
+
[key: string]: any;
|
|
7
|
+
};
|
|
8
|
+
ctx: Context;
|
|
9
|
+
};
|
|
10
|
+
export declare type AuthExtend<T extends Auth> = new (config: AuthConfig) => T;
|
|
11
|
+
interface IAuth {
|
|
12
|
+
user: Model;
|
|
13
|
+
check(): Promise<Model>;
|
|
14
|
+
signIn(): Promise<any>;
|
|
15
|
+
signUp(): Promise<any>;
|
|
16
|
+
signOut(): Promise<any>;
|
|
17
|
+
}
|
|
18
|
+
export declare abstract class Auth implements IAuth {
|
|
19
|
+
abstract user: Model;
|
|
20
|
+
protected authenticator: Model;
|
|
21
|
+
protected options: {
|
|
22
|
+
[key: string]: any;
|
|
23
|
+
};
|
|
24
|
+
protected ctx: Context;
|
|
25
|
+
constructor(config: AuthConfig);
|
|
26
|
+
abstract check(): any;
|
|
27
|
+
signIn(): Promise<any>;
|
|
28
|
+
signUp(): Promise<any>;
|
|
29
|
+
signOut(): Promise<any>;
|
|
30
|
+
}
|
|
31
|
+
export {};
|
package/lib/auth.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.Auth = void 0;
|
|
7
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
8
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
9
|
+
class Auth {
|
|
10
|
+
constructor(config) {
|
|
11
|
+
this.authenticator = void 0;
|
|
12
|
+
this.options = void 0;
|
|
13
|
+
this.ctx = void 0;
|
|
14
|
+
const authenticator = config.authenticator,
|
|
15
|
+
options = config.options,
|
|
16
|
+
ctx = config.ctx;
|
|
17
|
+
this.authenticator = authenticator;
|
|
18
|
+
this.options = options;
|
|
19
|
+
this.ctx = ctx;
|
|
20
|
+
}
|
|
21
|
+
// The following methods are mainly designed for user authentications.
|
|
22
|
+
signIn() {
|
|
23
|
+
return _asyncToGenerator(function* () {})();
|
|
24
|
+
}
|
|
25
|
+
signUp() {
|
|
26
|
+
return _asyncToGenerator(function* () {})();
|
|
27
|
+
}
|
|
28
|
+
signOut() {
|
|
29
|
+
return _asyncToGenerator(function* () {})();
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
exports.Auth = Auth;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Auth, AuthConfig } from '../auth';
|
|
2
|
+
import { JwtService } from './jwt-service';
|
|
3
|
+
import { Collection, Model } from '@nocobase/database';
|
|
4
|
+
/**
|
|
5
|
+
* BaseAuth
|
|
6
|
+
* @description A base class with jwt provide some common methods.
|
|
7
|
+
*/
|
|
8
|
+
export declare class BaseAuth extends Auth {
|
|
9
|
+
protected jwt: JwtService;
|
|
10
|
+
protected userCollection: Collection;
|
|
11
|
+
constructor(config: AuthConfig & {
|
|
12
|
+
userCollection: Collection;
|
|
13
|
+
});
|
|
14
|
+
set user(user: Model);
|
|
15
|
+
get user(): Model;
|
|
16
|
+
check(): Promise<any>;
|
|
17
|
+
validate(): Promise<Model>;
|
|
18
|
+
signIn(): Promise<{
|
|
19
|
+
user: Model<any, any>;
|
|
20
|
+
token: string;
|
|
21
|
+
}>;
|
|
22
|
+
}
|
package/lib/base/auth.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.BaseAuth = void 0;
|
|
7
|
+
var _auth = require("../auth");
|
|
8
|
+
var _jwtService = require("./jwt-service");
|
|
9
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
10
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
11
|
+
/**
|
|
12
|
+
* BaseAuth
|
|
13
|
+
* @description A base class with jwt provide some common methods.
|
|
14
|
+
*/
|
|
15
|
+
class BaseAuth extends _auth.Auth {
|
|
16
|
+
constructor(config) {
|
|
17
|
+
const options = config.options,
|
|
18
|
+
userCollection = config.userCollection;
|
|
19
|
+
super(config);
|
|
20
|
+
this.jwt = void 0;
|
|
21
|
+
this.userCollection = void 0;
|
|
22
|
+
this.userCollection = userCollection;
|
|
23
|
+
this.jwt = new _jwtService.JwtService(options.jwt);
|
|
24
|
+
}
|
|
25
|
+
set user(user) {
|
|
26
|
+
this.ctx.state.currentUser = user;
|
|
27
|
+
}
|
|
28
|
+
get user() {
|
|
29
|
+
return this.ctx.state.currentUser;
|
|
30
|
+
}
|
|
31
|
+
check() {
|
|
32
|
+
var _this = this;
|
|
33
|
+
return _asyncToGenerator(function* () {
|
|
34
|
+
const token = _this.ctx.getBearerToken();
|
|
35
|
+
if (!token) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
try {
|
|
39
|
+
const _yield$_this$jwt$deco = yield _this.jwt.decode(token),
|
|
40
|
+
userId = _yield$_this$jwt$deco.userId;
|
|
41
|
+
const user = yield _this.userCollection.repository.findOne({
|
|
42
|
+
filter: {
|
|
43
|
+
id: userId
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
return user;
|
|
47
|
+
} catch (err) {
|
|
48
|
+
_this.ctx.logger.error(err);
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
})();
|
|
52
|
+
}
|
|
53
|
+
validate() {
|
|
54
|
+
return _asyncToGenerator(function* () {
|
|
55
|
+
return null;
|
|
56
|
+
})();
|
|
57
|
+
}
|
|
58
|
+
signIn() {
|
|
59
|
+
var _this2 = this;
|
|
60
|
+
return _asyncToGenerator(function* () {
|
|
61
|
+
let user;
|
|
62
|
+
try {
|
|
63
|
+
user = yield _this2.validate();
|
|
64
|
+
} catch (err) {
|
|
65
|
+
console.log(err);
|
|
66
|
+
_this2.ctx.throw(401, err.message);
|
|
67
|
+
}
|
|
68
|
+
if (!user) {
|
|
69
|
+
_this2.ctx.throw(401, 'Unauthorized');
|
|
70
|
+
}
|
|
71
|
+
const token = _this2.jwt.sign({
|
|
72
|
+
userId: user.id
|
|
73
|
+
});
|
|
74
|
+
return {
|
|
75
|
+
user,
|
|
76
|
+
token
|
|
77
|
+
};
|
|
78
|
+
})();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
exports.BaseAuth = BaseAuth;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export interface JwtOptions {
|
|
2
|
+
secret: string;
|
|
3
|
+
expiresIn?: string;
|
|
4
|
+
}
|
|
5
|
+
export declare class JwtService {
|
|
6
|
+
protected options: JwtOptions;
|
|
7
|
+
constructor(options: JwtOptions);
|
|
8
|
+
private expiresIn;
|
|
9
|
+
private secret;
|
|
10
|
+
sign(payload: any): string;
|
|
11
|
+
decode(token: string): Promise<any>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.JwtService = void 0;
|
|
7
|
+
function _jsonwebtoken() {
|
|
8
|
+
const data = _interopRequireDefault(require("jsonwebtoken"));
|
|
9
|
+
_jsonwebtoken = function _jsonwebtoken() {
|
|
10
|
+
return data;
|
|
11
|
+
};
|
|
12
|
+
return data;
|
|
13
|
+
}
|
|
14
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
|
+
class JwtService {
|
|
16
|
+
constructor(options) {
|
|
17
|
+
this.options = void 0;
|
|
18
|
+
this.options = options;
|
|
19
|
+
const _ref = options || {},
|
|
20
|
+
secret = _ref.secret,
|
|
21
|
+
expiresIn = _ref.expiresIn;
|
|
22
|
+
this.options = {
|
|
23
|
+
secret: secret || process.env.APP_KEY,
|
|
24
|
+
expiresIn: expiresIn || process.env.JWT_EXPIRES_IN || '7d'
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
expiresIn() {
|
|
28
|
+
return this.options.expiresIn;
|
|
29
|
+
}
|
|
30
|
+
secret() {
|
|
31
|
+
return this.options.secret;
|
|
32
|
+
}
|
|
33
|
+
sign(payload) {
|
|
34
|
+
return _jsonwebtoken().default.sign(payload, this.secret(), {
|
|
35
|
+
expiresIn: this.expiresIn()
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
decode(token) {
|
|
39
|
+
return new Promise((resolve, reject) => {
|
|
40
|
+
_jsonwebtoken().default.verify(token, this.secret(), (err, decoded) => {
|
|
41
|
+
if (err) {
|
|
42
|
+
return reject(err);
|
|
43
|
+
}
|
|
44
|
+
resolve(decoded);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
exports.JwtService = JwtService;
|
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
var _authManager = require("./auth-manager");
|
|
7
|
+
Object.keys(_authManager).forEach(function (key) {
|
|
8
|
+
if (key === "default" || key === "__esModule") return;
|
|
9
|
+
if (key in exports && exports[key] === _authManager[key]) return;
|
|
10
|
+
Object.defineProperty(exports, key, {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
get: function get() {
|
|
13
|
+
return _authManager[key];
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
var _auth = require("./auth");
|
|
18
|
+
Object.keys(_auth).forEach(function (key) {
|
|
19
|
+
if (key === "default" || key === "__esModule") return;
|
|
20
|
+
if (key in exports && exports[key] === _auth[key]) return;
|
|
21
|
+
Object.defineProperty(exports, key, {
|
|
22
|
+
enumerable: true,
|
|
23
|
+
get: function get() {
|
|
24
|
+
return _auth[key];
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
var _auth2 = require("./base/auth");
|
|
29
|
+
Object.keys(_auth2).forEach(function (key) {
|
|
30
|
+
if (key === "default" || key === "__esModule") return;
|
|
31
|
+
if (key in exports && exports[key] === _auth2[key]) return;
|
|
32
|
+
Object.defineProperty(exports, key, {
|
|
33
|
+
enumerable: true,
|
|
34
|
+
get: function get() {
|
|
35
|
+
return _auth2[key];
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
var _actions = require("./actions");
|
|
40
|
+
Object.keys(_actions).forEach(function (key) {
|
|
41
|
+
if (key === "default" || key === "__esModule") return;
|
|
42
|
+
if (key in exports && exports[key] === _actions[key]) return;
|
|
43
|
+
Object.defineProperty(exports, key, {
|
|
44
|
+
enumerable: true,
|
|
45
|
+
get: function get() {
|
|
46
|
+
return _actions[key];
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nocobase/auth",
|
|
3
|
+
"version": "0.10.0-alpha.2",
|
|
4
|
+
"description": "",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"main": "./lib/index.js",
|
|
7
|
+
"types": "./lib/index.d.ts",
|
|
8
|
+
"dependencies": {
|
|
9
|
+
"@nocobase/actions": "0.10.0-alpha.2",
|
|
10
|
+
"@nocobase/database": "0.10.0-alpha.2",
|
|
11
|
+
"@nocobase/resourcer": "0.10.0-alpha.2",
|
|
12
|
+
"@nocobase/utils": "0.10.0-alpha.2"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/nocobase/nocobase.git",
|
|
17
|
+
"directory": "packages/auth"
|
|
18
|
+
},
|
|
19
|
+
"gitHead": "85028ae1733fcbd46ecd5d291dacbdc175f7f073"
|
|
20
|
+
}
|