1
This commit is contained in:
32
server-node/src/services/wechatAuthStateStore.ts
Normal file
32
server-node/src/services/wechatAuthStateStore.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import crypto from 'node:crypto';
|
||||
|
||||
export type WechatAuthStateRecord = {
|
||||
state: string;
|
||||
redirectPath: string;
|
||||
createdAt: string;
|
||||
};
|
||||
|
||||
export class WechatAuthStateStore {
|
||||
private readonly states = new Map<string, WechatAuthStateRecord>();
|
||||
|
||||
create(redirectPath: string) {
|
||||
const state = crypto.randomBytes(18).toString('hex');
|
||||
const record: WechatAuthStateRecord = {
|
||||
state,
|
||||
redirectPath,
|
||||
createdAt: new Date().toISOString(),
|
||||
};
|
||||
this.states.set(state, record);
|
||||
return record;
|
||||
}
|
||||
|
||||
consume(state: string) {
|
||||
const record = this.states.get(state) ?? null;
|
||||
if (!record) {
|
||||
return null;
|
||||
}
|
||||
|
||||
this.states.delete(state);
|
||||
return record;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user