onsite-toolkit: add iplogin support
parent
01cb6944b7
commit
346bf7d085
@ -0,0 +1,36 @@
|
||||
import {
|
||||
Context, db, ForbiddenError, UserModel,
|
||||
} from 'hydrooj';
|
||||
|
||||
interface IpLoginInfo {
|
||||
_id: string;
|
||||
uid: number;
|
||||
}
|
||||
|
||||
declare module 'hydrooj' {
|
||||
interface Collections {
|
||||
iplogin: IpLoginInfo;
|
||||
}
|
||||
}
|
||||
const coll = db.collection('iplogin');
|
||||
|
||||
function normalizeIp(ip: string) {
|
||||
if (ip.startsWith('::ffff:')) return ip.slice(7);
|
||||
return ip;
|
||||
}
|
||||
|
||||
export function apply(ctx: Context) {
|
||||
ctx.on('handler/init', async (that) => {
|
||||
const iplogin = await coll.findOne({ _id: normalizeIp(that.request.ip) });
|
||||
if (iplogin) {
|
||||
that.user = await UserModel.getById(that.domain._id, iplogin.uid);
|
||||
if (!that.user) {
|
||||
that.user = await UserModel.getById(that.domain._id, 0);
|
||||
throw new ForbiddenError(`User ${iplogin.uid}not found`);
|
||||
}
|
||||
that.session.ipLoggedIn = true;
|
||||
that.session.uid = iplogin.uid;
|
||||
that.session.user = that.user;
|
||||
}
|
||||
});
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
{
|
||||
"name": "@hydrooj/onsite-toolkit",
|
||||
"version": "0.0.1"
|
||||
}
|
Loading…
Reference in New Issue