@celljs/security 4.6.0 → 4.7.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/README.zh-cn.md +111 -0
- package/package.json +4 -4
package/README.zh-cn.md
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# @celljs/security
|
|
2
|
+
|
|
3
|
+
Cell 后端的身份验证和授权:表单登录、HTTP Basic、登出、`@Authenticated()` / `@Anonymous()` / `@PreAuthorize()` 在控制器上,以及密码哈希。安装后即可使用,无需注册。
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install @celljs/security
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## 用户
|
|
10
|
+
|
|
11
|
+
**没有内置用户。** 需要为登录提供用户来源,有两种方式:
|
|
12
|
+
|
|
13
|
+
1. 在 `cell.yml` 中配置一个用户。`password` 是由 `PasswordEncoder` 生成的哈希值,绝不是明文密码:
|
|
14
|
+
|
|
15
|
+
```yaml
|
|
16
|
+
security:
|
|
17
|
+
username: admin
|
|
18
|
+
password: pbkdf2-sha256$600000$<salt>$<key>
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
使用你部署的版本中的编码器生成哈希值,例如:
|
|
22
|
+
|
|
23
|
+
```js
|
|
24
|
+
// hash.mjs — node hash.mjs 'the password'
|
|
25
|
+
import 'reflect-metadata';
|
|
26
|
+
import { Pbkdf2PasswordEncoder } from '@celljs/security/lib/node/crypto/password/password-encoder';
|
|
27
|
+
console.log(await new Pbkdf2PasswordEncoder().encode(process.argv[2]));
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
如果设置了 `security.passwordEncoder.secret`(一个 pepper),新的哈希值会依赖于它:在应用程序内部生成它们(注入 `PasswordEncoder`),而不是用上面的脚本。
|
|
31
|
+
|
|
32
|
+
2. 你自己的 `UserService`,用于存储在数据库中的用户:
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import { Component } from '@celljs/core';
|
|
36
|
+
import { User, UserType } from '@celljs/security/lib/common';
|
|
37
|
+
import { UserService, UsernameNotFoundError } from '@celljs/security/lib/node';
|
|
38
|
+
|
|
39
|
+
@Component({ id: UserService, rebind: true })
|
|
40
|
+
export class DbUserService implements UserService<string, User> {
|
|
41
|
+
async load(username: string): Promise<User> {
|
|
42
|
+
const row = await findUser(username); // 你的数据访问
|
|
43
|
+
if (!row) {
|
|
44
|
+
throw new UsernameNotFoundError(`Could not find: ${username}`);
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
type: UserType.Database, username, password: row.passwordHash, policies: [],
|
|
48
|
+
accountNonExpired: true, accountNonLocked: true, credentialsNonExpired: true, enabled: true
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// 可选:让登录使用新的哈希值替换过时的哈希值(参见"密码哈希")。
|
|
53
|
+
async updatePassword(user: User, encodedPassword: string): Promise<void> {
|
|
54
|
+
await saveHash(user.username, encodedPassword);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
如果都没有配置,应用程序会启动并记录警告,说明没有用户能登录(应用程序可能仅使用 OAuth2 登录,或仅使用此包用于授权)。
|
|
60
|
+
|
|
61
|
+
## 登录
|
|
62
|
+
|
|
63
|
+
| 方式 | 请求 | 结果 |
|
|
64
|
+
| --- | --- | --- |
|
|
65
|
+
| 表单 | `POST /login`,包含 `username` 和 `password`,作为 JSON 或表单形式(`security.loginUrl`、`loginMethod`、`usernameKey`、`passwordKey`) | `302` 重定向到 `loginSuccessUrl`(或 `redirect` 查询参数),带有会话 cookie;错误的凭证返回 `401` |
|
|
66
|
+
| Basic | 任何请求上的 `Authorization: Basic …`,例如 `curl -u admin:secret http://localhost:3000/users` | 请求本身被验证并继续到资源;错误的凭证返回 `401` |
|
|
67
|
+
| 登出 | `POST /logout` | `302` 重定向到 `logoutSuccessUrl` |
|
|
68
|
+
|
|
69
|
+
Basic 默认**关闭**;使用 `security.basic.enabled: true` 启用。它在**每个请求**上检查密码,每次检查是一个 PBKDF2 衍生,有 600000 次迭代(约 45 ms CPU 时间)。仅对低频的机器对机器调用使用 Basic;对于频繁调用,请登录一次并使用会话,或使用 JWT(`@celljs/jwt`)。
|
|
70
|
+
|
|
71
|
+
## 保护端点
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
import { Controller, Get } from '@celljs/mvc/lib/node';
|
|
75
|
+
import { Anonymous, Authenticated } from '@celljs/security/lib/node';
|
|
76
|
+
|
|
77
|
+
@Controller('users')
|
|
78
|
+
@Authenticated() // 要求已登录的用户;匿名请求得到 401
|
|
79
|
+
export class UserController {
|
|
80
|
+
@Get()
|
|
81
|
+
list() { return []; }
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
@Controller()
|
|
85
|
+
@Anonymous() // 无需登录即可访问
|
|
86
|
+
export class HomeController {}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
`@PreAuthorize(el)` / `@PostAuthorize(el)` 在方法前或后检查表达式;`@Authorize`、`@Resource` 和 `@Action` 与策略一起使用。在 ACL 策略中,`resource` 和 `action` 是 glob 匹配名称的一部分,如 `micromatch.contains` 所做:`order:*` 匹配 `order:1` 和 `tenant:order:1`;`*` 停止在 `/`,`**` 则不停;`?`、`[0-9]` 和 `{a,b}` 如 glob 中一样工作。
|
|
90
|
+
|
|
91
|
+
在浏览器中,`UserManager.userInfoSubject` 是 Cell 自己的 `BehaviorSubject`(`value`、`next()`、`subscribe()` 返回 `{ unsubscribe() }`),不是 rxjs 的。
|
|
92
|
+
|
|
93
|
+
## 密码哈希
|
|
94
|
+
|
|
95
|
+
`PasswordEncoder`(`Pbkdf2PasswordEncoder`)存储 `pbkdf2-sha256$<iterations>$<salt>$<key>`:PBKDF2-HMAC-SHA256,600000 次迭代,一个随机 16 字节的盐,以恒定时间比较。
|
|
96
|
+
|
|
97
|
+
由 4.0.x 存储的哈希值仍可匹配。在成功登录后(表单或 Basic),当你的 `UserService` 实现了 `updatePassword()` 时,这样的哈希值会被新的哈希值替换。**升级是单向的:一旦用户登录,回滚到 4.0.x 的应用程序将无法再检查该用户的密码。** 不支持使用 crypto-js 4.1 或更旧版本制作的哈希值。
|
|
98
|
+
|
|
99
|
+
## 配置
|
|
100
|
+
|
|
101
|
+
此包的 `cell.yml` 中 `security` 下的键(默认值):
|
|
102
|
+
|
|
103
|
+
| 键 | 默认值 | |
|
|
104
|
+
| --- | --- | --- |
|
|
105
|
+
| `username`、`password` | — | 默认 `UserService` 的单个用户;`password` 是一个哈希值 |
|
|
106
|
+
| `loginUrl`、`loginMethod` | `/login`、`POST` | 表单登录端点 |
|
|
107
|
+
| `usernameKey`、`passwordKey` | `username`、`password` | 表单登录的字段名 |
|
|
108
|
+
| `loginSuccessUrl` | `/` | 表单登录重定向到的位置 |
|
|
109
|
+
| `logoutUrl`、`logoutMethod`、`logoutSuccessUrl` | `/logout`、`POST`、登录页面 | 登出 |
|
|
110
|
+
| `basic.enabled`、`basic.realm` | `false`、`realm` | HTTP Basic |
|
|
111
|
+
| `passwordEncoder.secret` | — | 可选的 pepper,混入新的哈希值中 |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@celljs/security",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.7.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cell-component"
|
|
@@ -33,9 +33,9 @@
|
|
|
33
33
|
"watch": "tsc -b -w --preserveWatchOutput"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@celljs/core": "4.
|
|
37
|
-
"@celljs/http": "4.
|
|
38
|
-
"@celljs/web": "4.
|
|
36
|
+
"@celljs/core": "4.7.0",
|
|
37
|
+
"@celljs/http": "4.7.0",
|
|
38
|
+
"@celljs/web": "4.7.0"
|
|
39
39
|
},
|
|
40
40
|
"publishConfig": {
|
|
41
41
|
"access": "public"
|