@maiyunnet/kebab 9.28.0 → 9.29.1
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/abox/session-login.d.ts +17 -0
- package/abox/session-login.js +228 -0
- package/doc/kebab-rag.md +871 -213
- package/index.d.ts +1 -1
- package/index.js +1 -2
- package/lib/core.js +4 -1
- package/lib/kv.d.ts +5 -0
- package/lib/kv.js +7 -0
- package/lib/session/login.d.ts +110 -0
- package/lib/session/login.js +83 -0
- package/lib/session.d.ts +2 -0
- package/lib/session.js +1 -0
- package/package.json +1 -1
- package/sys/master.js +30 -11
- package/www/example/ctr/test.d.ts +5 -0
- package/www/example/ctr/test.js +141 -1
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type * as lKv from '#kebab/lib/kv.js';
|
|
2
|
+
import type { ILoginCreateOptions, ILoginIdentity, ILoginRecord, IListLogin, ILoginOptions } from '#kebab/lib/session/login.js';
|
|
3
|
+
export declare class Store {
|
|
4
|
+
private readonly _kv;
|
|
5
|
+
private readonly _pre;
|
|
6
|
+
private readonly _ttl;
|
|
7
|
+
private readonly _valid;
|
|
8
|
+
constructor(_kv: lKv.Kv, options: ILoginOptions);
|
|
9
|
+
private _run;
|
|
10
|
+
create(token: string, identity: ILoginIdentity, options: ILoginCreateOptions): Promise<ILoginRecord | false>;
|
|
11
|
+
get(token: string): Promise<ILoginRecord | null | false>;
|
|
12
|
+
private _change;
|
|
13
|
+
touch(token: string, uid: string): Promise<true | null | false>;
|
|
14
|
+
remove(token: string, uid: string): Promise<true | null | false>;
|
|
15
|
+
list(token: string, uid: string): Promise<IListLogin[] | null | false>;
|
|
16
|
+
revoke(token: string, uid: string, target: string): Promise<true | null | false>;
|
|
17
|
+
}
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { createHash, randomBytes } from 'node:crypto';
|
|
2
|
+
const script = `
|
|
3
|
+
local base = KEYS[1]
|
|
4
|
+
local op = ARGV[1]
|
|
5
|
+
local function record(hash) return base .. 'record:' .. hash end
|
|
6
|
+
local function index(hash) return base .. 'index:' .. hash end
|
|
7
|
+
local function read(hash)
|
|
8
|
+
local data = redis.call('GET', record(hash))
|
|
9
|
+
if not data then return nil end
|
|
10
|
+
return cjson.decode(data)
|
|
11
|
+
end
|
|
12
|
+
local function indexExpiry(idx)
|
|
13
|
+
local last = redis.call('ZREVRANGE', idx, 0, 0, 'WITHSCORES')
|
|
14
|
+
if #last == 0 then return end
|
|
15
|
+
local score = tonumber(last[2])
|
|
16
|
+
if score == 9007199254740991 then redis.call('PERSIST', idx)
|
|
17
|
+
else redis.call('EXPIREAT', idx, score) end
|
|
18
|
+
end
|
|
19
|
+
local function erase(hash, r)
|
|
20
|
+
redis.call('DEL', record(hash))
|
|
21
|
+
redis.call('ZREM', index(r.index), hash)
|
|
22
|
+
end
|
|
23
|
+
local function expiry(hash, r, ttl)
|
|
24
|
+
local idx = index(r.index)
|
|
25
|
+
local score = 9007199254740991
|
|
26
|
+
if ttl > 0 then
|
|
27
|
+
score = tonumber(redis.call('TIME')[1]) + ttl + 1
|
|
28
|
+
redis.call('EXPIRE', record(hash), ttl)
|
|
29
|
+
else redis.call('PERSIST', record(hash)) end
|
|
30
|
+
redis.call('ZREMRANGEBYSCORE', idx, '-inf', redis.call('TIME')[1])
|
|
31
|
+
redis.call('ZADD', idx, score, hash)
|
|
32
|
+
indexExpiry(idx)
|
|
33
|
+
end
|
|
34
|
+
if op == 'create' then
|
|
35
|
+
local hash, uid, data, idx, ttl = ARGV[2], ARGV[3], ARGV[4], ARGV[5], tonumber(ARGV[6])
|
|
36
|
+
local old = read(hash)
|
|
37
|
+
if old and old.uid == uid then return nil end
|
|
38
|
+
local members = redis.call('ZRANGE', index(idx), 0, -1)
|
|
39
|
+
for _, member in ipairs(members) do
|
|
40
|
+
local r = read(member)
|
|
41
|
+
if r and r.index == idx and r.uid == uid then return nil end
|
|
42
|
+
end
|
|
43
|
+
if old then
|
|
44
|
+
erase(hash, old)
|
|
45
|
+
indexExpiry(index(old.index))
|
|
46
|
+
end
|
|
47
|
+
for _, member in ipairs(members) do
|
|
48
|
+
local r = read(member)
|
|
49
|
+
if not r or r.index ~= idx then redis.call('ZREM', index(idx), member)
|
|
50
|
+
elseif ARGV[7] == '1' then erase(member, r) end
|
|
51
|
+
end
|
|
52
|
+
local r = cjson.decode(data)
|
|
53
|
+
r.timeAdd = tonumber(redis.call('TIME')[1])
|
|
54
|
+
r.index = idx
|
|
55
|
+
redis.call('SET', record(hash), cjson.encode(r))
|
|
56
|
+
expiry(hash, r, ttl)
|
|
57
|
+
r.index = nil
|
|
58
|
+
return cjson.encode(r)
|
|
59
|
+
end
|
|
60
|
+
local hash = ARGV[2]
|
|
61
|
+
local me = read(hash)
|
|
62
|
+
if not me then return nil end
|
|
63
|
+
if op == 'get' then
|
|
64
|
+
me.index = nil
|
|
65
|
+
return cjson.encode(me)
|
|
66
|
+
end
|
|
67
|
+
if me.uid ~= ARGV[3] then return nil end
|
|
68
|
+
if op == 'touch' then
|
|
69
|
+
expiry(hash, me, tonumber(ARGV[4]))
|
|
70
|
+
return 1
|
|
71
|
+
end
|
|
72
|
+
if op == 'remove' then
|
|
73
|
+
erase(hash, me)
|
|
74
|
+
indexExpiry(index(me.index))
|
|
75
|
+
return 1
|
|
76
|
+
end
|
|
77
|
+
local idx = me.index
|
|
78
|
+
redis.call('ZREMRANGEBYSCORE', index(idx), '-inf', redis.call('TIME')[1])
|
|
79
|
+
local members = redis.call('ZRANGE', index(idx), 0, -1)
|
|
80
|
+
local result = {}
|
|
81
|
+
local removed = false
|
|
82
|
+
local target = ARGV[4]
|
|
83
|
+
for _, member in ipairs(members) do
|
|
84
|
+
local r = read(member)
|
|
85
|
+
if not r or r.index ~= idx then
|
|
86
|
+
redis.call('ZREM', index(idx), member)
|
|
87
|
+
elseif op == 'list' then
|
|
88
|
+
if r.version == me.version then
|
|
89
|
+
r.index = nil
|
|
90
|
+
r.current = member == hash
|
|
91
|
+
table.insert(result, r)
|
|
92
|
+
end
|
|
93
|
+
elseif (target == 'others' and member ~= hash) or r.uid == target then
|
|
94
|
+
erase(member, r)
|
|
95
|
+
removed = true
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
indexExpiry(index(idx))
|
|
99
|
+
if op == 'list' then
|
|
100
|
+
if #result == 0 then return '[]' end
|
|
101
|
+
return cjson.encode(result)
|
|
102
|
+
end
|
|
103
|
+
if target == 'others' or removed then return 1 end
|
|
104
|
+
return nil
|
|
105
|
+
`;
|
|
106
|
+
function hash(value) {
|
|
107
|
+
return createHash('sha256').update(value).digest('hex');
|
|
108
|
+
}
|
|
109
|
+
function tokenValid(token) {
|
|
110
|
+
return typeof token === 'string' && token.length > 0 && token.length <= 4096;
|
|
111
|
+
}
|
|
112
|
+
function uidValid(uid) {
|
|
113
|
+
return typeof uid === 'string' && /^[a-f0-9]{32}$/.test(uid);
|
|
114
|
+
}
|
|
115
|
+
function record(value) {
|
|
116
|
+
const row = value;
|
|
117
|
+
return { ...row, data: JSON.parse(row.data) };
|
|
118
|
+
}
|
|
119
|
+
function parse(value) {
|
|
120
|
+
if (value === null || value === false)
|
|
121
|
+
return value;
|
|
122
|
+
try {
|
|
123
|
+
if (Buffer.isBuffer(value))
|
|
124
|
+
value = value.toString('utf8');
|
|
125
|
+
if (typeof value !== 'string')
|
|
126
|
+
return false;
|
|
127
|
+
const data = JSON.parse(value);
|
|
128
|
+
return Array.isArray(data) ? data.map(row => record(row)) : record(data);
|
|
129
|
+
}
|
|
130
|
+
catch {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
export class Store {
|
|
135
|
+
_kv;
|
|
136
|
+
_pre;
|
|
137
|
+
_ttl;
|
|
138
|
+
_valid;
|
|
139
|
+
constructor(_kv, options) {
|
|
140
|
+
this._kv = _kv;
|
|
141
|
+
this._pre = this._kv.getPre() + options.name + ':' + (options.pre ?? 'login:');
|
|
142
|
+
this._ttl = options.ttl ?? 172800;
|
|
143
|
+
this._valid = typeof options.name === 'string' && options.name.length > 0 &&
|
|
144
|
+
Number.isInteger(this._ttl) && this._ttl >= 0 && this._ttl <= 2147483647 &&
|
|
145
|
+
(options.pre === undefined || typeof options.pre === 'string') && this._pre.length <= 1024;
|
|
146
|
+
}
|
|
147
|
+
async _run(args) {
|
|
148
|
+
if (!this._valid)
|
|
149
|
+
return false;
|
|
150
|
+
try {
|
|
151
|
+
const pipeline = await this._kv.pipeline();
|
|
152
|
+
if (pipeline === false)
|
|
153
|
+
return false;
|
|
154
|
+
pipeline.eval(script, [this._pre], args);
|
|
155
|
+
const result = await pipeline.exec();
|
|
156
|
+
if (result.length !== 1 || result[0] instanceof Error)
|
|
157
|
+
return false;
|
|
158
|
+
return result[0];
|
|
159
|
+
}
|
|
160
|
+
catch {
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
async create(token, identity, options) {
|
|
165
|
+
if (!this._valid || !tokenValid(token))
|
|
166
|
+
return false;
|
|
167
|
+
try {
|
|
168
|
+
if (!options || (options.single !== undefined && typeof options.single !== 'boolean'))
|
|
169
|
+
return false;
|
|
170
|
+
const { subject, scope = '', version = '', data = {} } = identity;
|
|
171
|
+
if (typeof subject !== 'string' || !subject || subject.length > 256 ||
|
|
172
|
+
typeof scope !== 'string' || scope.length > 256 || typeof version !== 'string' || version.length > 1024 ||
|
|
173
|
+
!data || typeof data !== 'object' || Array.isArray(data))
|
|
174
|
+
return false;
|
|
175
|
+
const encoded = JSON.stringify(data, (_key, value) => {
|
|
176
|
+
if (value === undefined || typeof value === 'function' || typeof value === 'symbol' ||
|
|
177
|
+
typeof value === 'bigint' || (typeof value === 'number' && !Number.isFinite(value))) {
|
|
178
|
+
throw new Error('Invalid login data');
|
|
179
|
+
}
|
|
180
|
+
return value;
|
|
181
|
+
});
|
|
182
|
+
if (Buffer.byteLength(encoded) > 16384)
|
|
183
|
+
return false;
|
|
184
|
+
const decoded = JSON.parse(encoded);
|
|
185
|
+
if (!decoded || typeof decoded !== 'object' || Array.isArray(decoded))
|
|
186
|
+
return false;
|
|
187
|
+
const index = hash(JSON.stringify([scope, subject]));
|
|
188
|
+
for (let attempt = 0; attempt < 3; ++attempt) {
|
|
189
|
+
const uid = randomBytes(16).toString('hex');
|
|
190
|
+
const input = JSON.stringify({ uid, subject, scope, version, data: encoded });
|
|
191
|
+
const result = parse(await this._run(['create', hash(token), uid, input, index, String(this._ttl), options.single === true ? '1' : '0']));
|
|
192
|
+
if (result !== null)
|
|
193
|
+
return result;
|
|
194
|
+
}
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
197
|
+
catch {
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
async get(token) {
|
|
202
|
+
if (!tokenValid(token))
|
|
203
|
+
return false;
|
|
204
|
+
return parse(await this._run(['get', hash(token)]));
|
|
205
|
+
}
|
|
206
|
+
async _change(op, token, uid, extra = '') {
|
|
207
|
+
if (!tokenValid(token) || !uidValid(uid))
|
|
208
|
+
return false;
|
|
209
|
+
const result = await this._run([op, hash(token), uid, extra]);
|
|
210
|
+
return result === 1 ? true : result === null ? null : false;
|
|
211
|
+
}
|
|
212
|
+
touch(token, uid) {
|
|
213
|
+
return this._change('touch', token, uid, String(this._ttl));
|
|
214
|
+
}
|
|
215
|
+
remove(token, uid) {
|
|
216
|
+
return this._change('remove', token, uid);
|
|
217
|
+
}
|
|
218
|
+
async list(token, uid) {
|
|
219
|
+
if (!tokenValid(token) || !uidValid(uid))
|
|
220
|
+
return false;
|
|
221
|
+
return parse(await this._run(['list', hash(token), uid]));
|
|
222
|
+
}
|
|
223
|
+
revoke(token, uid, target) {
|
|
224
|
+
if (target !== 'others' && !uidValid(target))
|
|
225
|
+
return Promise.resolve(false);
|
|
226
|
+
return this._change('revoke', token, uid, target);
|
|
227
|
+
}
|
|
228
|
+
}
|