core: recreate sessionId when logging in (#714)

This commit is contained in:
undefined committed 2024-01-20 13:48:34 +08:00
1 parent 6c727ab678
commit d23db46d0b
2 files changed
+6 -3

No files matched your search

+3
View File
@@ -125,6 +125,7 @@ class UserLoginHandler extends Handler {
this.session.sudo = null; this.session.sudo = null;
this.session.scope = PERM.PERM_ALL.toString(); this.session.scope = PERM.PERM_ALL.toString();
this.session.save = rememberme; this.session.save = rememberme;
this.session.recreate = true;
this.response.redirect = redirect || ((this.request.referer || '/login').endsWith('/login') this.response.redirect = redirect || ((this.request.referer || '/login').endsWith('/login')
? this.url('homepage') : this.request.referer); ? this.url('homepage') : this.request.referer);
} }
@@ -325,6 +326,7 @@ class UserRegisterWithCodeHandler extends Handler {
this.session.uid = uid; this.session.uid = uid;
this.session.sudoUid = null; this.session.sudoUid = null;
this.session.scope = PERM.PERM_ALL.toString(); this.session.scope = PERM.PERM_ALL.toString();
this.session.recreate = true;
this.response.redirect = tdoc.redirect || this.url('home_settings', { category: 'preference' }); this.response.redirect = tdoc.redirect || this.url('home_settings', { category: 'preference' });
} }
} }
@@ -484,6 +486,7 @@ class OauthCallbackHandler extends Handler {
await user.setById(udoc._id, { loginat: new Date(), loginip: this.request.ip }); await user.setById(udoc._id, { loginat: new Date(), loginip: this.request.ip });
this.session.uid = udoc._id; this.session.uid = udoc._id;
this.session.scope = PERM.PERM_ALL.toString(); this.session.scope = PERM.PERM_ALL.toString();
this.session.recreate = true;
this.response.redirect = '/'; this.response.redirect = '/';
return; return;
} }
+3 -3
View File
@@ -1,6 +1,6 @@
import { PassThrough } from 'stream'; import { PassThrough } from 'stream';
import type { Next } from 'koa'; import type { Next } from 'koa';
import { cloneDeep, pick } from 'lodash'; import { cloneDeep, omit, pick } from 'lodash';
import { PERM } from '../../model/builtin'; import { PERM } from '../../model/builtin';
import * as system from '../../model/system'; import * as system from '../../model/system';
import token from '../../model/token'; import token from '../../model/token';
@@ -80,8 +80,8 @@ export default async (ctx: KoaContext, next: Next) => {
: system.get('session.unsaved_expire_seconds'); : system.get('session.unsaved_expire_seconds');
if (!ctx.session._id && !Object.getOwnPropertyNames(ctx.session).length) return; if (!ctx.session._id && !Object.getOwnPropertyNames(ctx.session).length) return;
Object.assign(ctx.session, { updateIp: request.ip, updateUa: ua }); Object.assign(ctx.session, { updateIp: request.ip, updateUa: ua });
if (ctx.session._id) { if (ctx.session._id && !ctx.session.recreate) {
await token.update(ctx.session._id, token.TYPE_SESSION, expireSeconds, ctx.session); await token.update(ctx.session._id, token.TYPE_SESSION, expireSeconds, omit(ctx.session, ['_id', 'recreate']));
} else { } else {
Object.assign(ctx.session, { createIp: request.ip, createUa: ua, createHost: request.host }); Object.assign(ctx.session, { createIp: request.ip, createUa: ua, createHost: request.host });
[ctx.session._id] = await token.add(token.TYPE_SESSION, expireSeconds, ctx.session); [ctx.session._id] = await token.add(token.TYPE_SESSION, expireSeconds, ctx.session);