koishi-plugin-new-auth 0.1.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.md +69 -0
- package/lib/index.d.ts +151 -0
- package/lib/index.js +637 -0
- package/newauth.md +1458 -0
- package/package.json +44 -0
package/README.md
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# koishi-plugin-new-auth
|
|
2
|
+
|
|
3
|
+
`koishi-plugin-new-auth` is a conservative role and scope based command permission layer for Koishi.
|
|
4
|
+
|
|
5
|
+
It implements the first usable version described in `newauth.md`:
|
|
6
|
+
|
|
7
|
+
- commands are registered into a permission table;
|
|
8
|
+
- newly discovered commands are `pending` by default;
|
|
9
|
+
- pending commands are executable only by Bot administrators;
|
|
10
|
+
- Koishi's legacy `authority` value is recorded as a suggestion, not used as the final grant;
|
|
11
|
+
- policies are evaluated by `scope + role + command`;
|
|
12
|
+
- guild owner/admin/member roles are separated from Bot administrator;
|
|
13
|
+
- custom roles and role members can be managed from Koishi commands.
|
|
14
|
+
|
|
15
|
+
## Configuration
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
export default {
|
|
19
|
+
plugins: {
|
|
20
|
+
'new-auth': {
|
|
21
|
+
botAdmins: ['onebot:10000'],
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
`botAdmins` are explicit Koishi instance administrators. Platform guild owners are not treated as Bot administrators.
|
|
28
|
+
|
|
29
|
+
The plugin also has a compatibility fallback for existing Koishi installations: users with `authority >= legacyAdminAuthority` are treated as Bot administrators when `trustLegacyAuthorityAsAdmin` is enabled.
|
|
30
|
+
|
|
31
|
+
## Commands
|
|
32
|
+
|
|
33
|
+
All management commands are under `newauth`.
|
|
34
|
+
|
|
35
|
+
```txt
|
|
36
|
+
newauth.commands --pending
|
|
37
|
+
newauth.roles
|
|
38
|
+
newauth.allow <roleId> <command> [scope]
|
|
39
|
+
newauth.deny <roleId> <command> [scope]
|
|
40
|
+
newauth.inherit <roleId> <command> [scope]
|
|
41
|
+
newauth.disable <command>
|
|
42
|
+
newauth.enable <command>
|
|
43
|
+
newauth.admin.add <uid>
|
|
44
|
+
newauth.admin.remove <uid>
|
|
45
|
+
newauth.role.create <id> <name> [scopeType]
|
|
46
|
+
newauth.member.add <roleId> <uid> [scope]
|
|
47
|
+
newauth.member.remove <roleId> <uid> [scope]
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
`uid` uses `platform:userId`, for example `onebot:10000`.
|
|
51
|
+
|
|
52
|
+
`scope` can be:
|
|
53
|
+
|
|
54
|
+
- `global`
|
|
55
|
+
- `guild:<platform>:<guildId>`
|
|
56
|
+
|
|
57
|
+
When `scope` is omitted, policy commands use `global`.
|
|
58
|
+
|
|
59
|
+
## Built-In Roles
|
|
60
|
+
|
|
61
|
+
```txt
|
|
62
|
+
bot-admin
|
|
63
|
+
guild-owner
|
|
64
|
+
guild-admin
|
|
65
|
+
guild-member
|
|
66
|
+
guest
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Custom roles do not inherit from other roles. To give a custom role access to a command, add an explicit policy with `newauth.allow`.
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { Command, Context, Schema, Session } from 'koishi';
|
|
2
|
+
import type { Argv } from 'koishi';
|
|
3
|
+
export declare const name = "new-auth";
|
|
4
|
+
export declare const inject: string[];
|
|
5
|
+
type RoleType = 'builtin' | 'custom';
|
|
6
|
+
type ScopeType = 'global' | 'guild';
|
|
7
|
+
type CommandStatus = 'pending' | 'configured' | 'disabled';
|
|
8
|
+
type PolicyState = 'inherit' | 'allow' | 'deny';
|
|
9
|
+
export interface NewAuthCommandRecord {
|
|
10
|
+
id: string;
|
|
11
|
+
name: string;
|
|
12
|
+
commandPath: string;
|
|
13
|
+
aliases: string[];
|
|
14
|
+
plugin: string;
|
|
15
|
+
description: string;
|
|
16
|
+
legacyAuthority: number;
|
|
17
|
+
status: CommandStatus;
|
|
18
|
+
allowGuildOverride: boolean;
|
|
19
|
+
createdAt: Date;
|
|
20
|
+
updatedAt: Date;
|
|
21
|
+
}
|
|
22
|
+
export interface NewAuthRoleRecord {
|
|
23
|
+
id: string;
|
|
24
|
+
name: string;
|
|
25
|
+
type: RoleType;
|
|
26
|
+
scopeType: ScopeType;
|
|
27
|
+
builtin: boolean;
|
|
28
|
+
createdAt: Date;
|
|
29
|
+
updatedAt: Date;
|
|
30
|
+
}
|
|
31
|
+
export interface NewAuthRoleMemberRecord {
|
|
32
|
+
roleId: string;
|
|
33
|
+
platform: string;
|
|
34
|
+
userId: string;
|
|
35
|
+
scope: string;
|
|
36
|
+
createdAt: Date;
|
|
37
|
+
}
|
|
38
|
+
export interface NewAuthPolicyRecord {
|
|
39
|
+
scope: string;
|
|
40
|
+
roleId: string;
|
|
41
|
+
commandId: string;
|
|
42
|
+
state: PolicyState;
|
|
43
|
+
updatedAt: Date;
|
|
44
|
+
}
|
|
45
|
+
declare module 'koishi' {
|
|
46
|
+
interface Tables {
|
|
47
|
+
new_auth_command: NewAuthCommandRecord;
|
|
48
|
+
new_auth_role: NewAuthRoleRecord;
|
|
49
|
+
new_auth_role_member: NewAuthRoleMemberRecord;
|
|
50
|
+
new_auth_policy: NewAuthPolicyRecord;
|
|
51
|
+
}
|
|
52
|
+
interface Context {
|
|
53
|
+
newauth: NewAuthService;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
export interface Config {
|
|
57
|
+
botAdmins: string[];
|
|
58
|
+
trustLegacyAuthorityAsAdmin: boolean;
|
|
59
|
+
legacyAdminAuthority: number;
|
|
60
|
+
ownerRoleNames: string[];
|
|
61
|
+
adminRoleNames: string[];
|
|
62
|
+
allowGuildOverrideAuthorityMax: number;
|
|
63
|
+
deniedMessage: string;
|
|
64
|
+
grantRuntimeCommandPermission: boolean;
|
|
65
|
+
raiseLegacyAuthority: boolean;
|
|
66
|
+
}
|
|
67
|
+
export declare const Config: Schema<Config>;
|
|
68
|
+
export declare function apply(ctx: Context, config: Config): void;
|
|
69
|
+
export declare class NewAuthService {
|
|
70
|
+
private ctx;
|
|
71
|
+
private config;
|
|
72
|
+
private commandCache;
|
|
73
|
+
private adminSet;
|
|
74
|
+
private ownerRoleNames;
|
|
75
|
+
private adminRoleNames;
|
|
76
|
+
constructor(ctx: Context, config: Config);
|
|
77
|
+
start(): Promise<void>;
|
|
78
|
+
registerCommand(command: Command): Promise<string | undefined>;
|
|
79
|
+
intercept(argv: Argv): Promise<string | undefined>;
|
|
80
|
+
canExecute(session: Session, commandId: string): Promise<{
|
|
81
|
+
allowed: boolean;
|
|
82
|
+
reason: "command_missing";
|
|
83
|
+
command?: undefined;
|
|
84
|
+
roles?: undefined;
|
|
85
|
+
matchedRole?: undefined;
|
|
86
|
+
matchedScope?: undefined;
|
|
87
|
+
} | {
|
|
88
|
+
allowed: boolean;
|
|
89
|
+
reason: "command_disabled";
|
|
90
|
+
command: NewAuthCommandRecord;
|
|
91
|
+
roles?: undefined;
|
|
92
|
+
matchedRole?: undefined;
|
|
93
|
+
matchedScope?: undefined;
|
|
94
|
+
} | {
|
|
95
|
+
allowed: boolean;
|
|
96
|
+
reason: "bot_admin";
|
|
97
|
+
command: NewAuthCommandRecord;
|
|
98
|
+
roles: string[];
|
|
99
|
+
matchedRole?: undefined;
|
|
100
|
+
matchedScope?: undefined;
|
|
101
|
+
} | {
|
|
102
|
+
allowed: boolean;
|
|
103
|
+
reason: "command_pending";
|
|
104
|
+
command: NewAuthCommandRecord;
|
|
105
|
+
roles: string[];
|
|
106
|
+
matchedRole?: undefined;
|
|
107
|
+
matchedScope?: undefined;
|
|
108
|
+
} | {
|
|
109
|
+
allowed: boolean;
|
|
110
|
+
reason: "role_policy";
|
|
111
|
+
command: NewAuthCommandRecord;
|
|
112
|
+
roles: string[];
|
|
113
|
+
matchedRole: string;
|
|
114
|
+
matchedScope: string;
|
|
115
|
+
} | {
|
|
116
|
+
allowed: boolean;
|
|
117
|
+
reason: "no_policy";
|
|
118
|
+
command: NewAuthCommandRecord;
|
|
119
|
+
roles: string[];
|
|
120
|
+
matchedRole?: undefined;
|
|
121
|
+
matchedScope?: undefined;
|
|
122
|
+
}>;
|
|
123
|
+
resolveRoles(session: Session): Promise<string[]>;
|
|
124
|
+
isBotAdmin(session: Session): Promise<boolean>;
|
|
125
|
+
listCommands(options?: {
|
|
126
|
+
pending?: boolean;
|
|
127
|
+
all?: boolean;
|
|
128
|
+
query?: string;
|
|
129
|
+
}): Promise<NewAuthCommandRecord[]>;
|
|
130
|
+
listRoles(): Promise<NewAuthRoleRecord[]>;
|
|
131
|
+
addBotAdmin(uid: string): Promise<void>;
|
|
132
|
+
removeBotAdmin(uid: string): Promise<void>;
|
|
133
|
+
createCustomRole(id: string, name: string, scopeType?: ScopeType): Promise<void>;
|
|
134
|
+
addRoleMember(roleId: string, uid: string, scope?: string): Promise<void>;
|
|
135
|
+
removeRoleMember(roleId: string, uid: string, scope?: string): Promise<void>;
|
|
136
|
+
setCommandStatus(input: string, status: CommandStatus): Promise<NewAuthCommandRecord>;
|
|
137
|
+
setCommandPolicy(scope: string, roleId: string, input: string, state: PolicyState): Promise<NewAuthCommandRecord>;
|
|
138
|
+
getCommand(input: string): Promise<NewAuthCommandRecord>;
|
|
139
|
+
private ensureBuiltinRoles;
|
|
140
|
+
private createCommandRecord;
|
|
141
|
+
private getDescription;
|
|
142
|
+
private resolveCommandInput;
|
|
143
|
+
private setPolicy;
|
|
144
|
+
private getEffectivePolicy;
|
|
145
|
+
private ensureRoleMember;
|
|
146
|
+
private hasPlatformRole;
|
|
147
|
+
private grantRuntimeCommandPermission;
|
|
148
|
+
private getCommandList;
|
|
149
|
+
private isSelfCommand;
|
|
150
|
+
}
|
|
151
|
+
export {};
|