core: oauth: enhancement

This commit is contained in:
undefined committed 2022-02-09 18:44:41 +08:00
1 parent 04ec44dab9
commit b1f90ab5a1
10 files changed
+55 -26

No files matched your search

+3 -3
View File
@@ -1,6 +1,6 @@
{
"name": "hydrooj",
"version": "3.4.9",
"version": "3.4.10",
"bin": "bin/hydrooj.js",
"main": "src/loader",
"module": "src/loader",
@@ -15,7 +15,7 @@
"@graphql-tools/schema": "^8.3.1",
"@hydrooj/utils": "workspace:*",
"adm-zip": "0.5.5",
"ajv": "^8.9.0",
"ajv": "^8.10.0",
"ansi_up": "^5.1.0",
"cac": "^6.7.12",
"cookies": "^0.8.0",
@@ -34,7 +34,7 @@
"koa-router": "^10.1.1",
"koa-static-cache": "^5.1.4",
"lodash": "^4.17.21",
"lru-cache": "^6.0.0",
"lru-cache": "^7.3.0",
"mime-types": "^2.1.34",
"minio": "7.0.25",
"moment-timezone": "^0.5.34",
+17 -5
View File
@@ -6,7 +6,7 @@ import {
SystemError, UserAlreadyExistError, UserFacingError,
UserNotFoundError, ValidationError, VerifyPasswordError,
} from '../error';
import { User } from '../interface';
import { OAuthUserResponse, Udoc, User } from '../interface';
import avatar from '../lib/avatar';
import { sendMail } from '../lib/mail';
import { isEmail, isPassword, isUname } from '../lib/validator';
@@ -360,14 +360,23 @@ class OauthCallbackHandler extends Handler {
noCheckPermView = true;
async get(args: any) {
let r;
if (global.Hydro.lib[`oauth_${args.type}`]) r = await global.Hydro.lib[`oauth_${args.type}`].callback.call(this, args);
else throw new UserFacingError('Oauth type');
if (!global.Hydro.lib[`oauth_${args.type}`]) throw new UserFacingError('Oauth type');
const r = await global.Hydro.lib[`oauth_${args.type}`].callback.call(this, args) as OAuthUserResponse;
const uid = await oauth.get(r._id);
if (uid) {
await user.setById(uid, { loginat: new Date(), loginip: this.request.ip });
this.session.uid = uid;
this.session.scope = PERM.PERM_ALL.toString();
} else {
if (r.email) {
const udoc = await user.getByEmail('system', r.email);
if (udoc) {
await user.setById(udoc._id, { loginat: new Date(), loginip: this.request.ip });
this.session.uid = udoc._id;
this.session.scpoe = PERM.PERM_ALL.toString();
return;
}
}
this.checkPriv(PRIV.PRIV_REGISTER_USER);
let username = '';
r.uname = r.uname || [];
@@ -384,11 +393,14 @@ class OauthCallbackHandler extends Handler {
r.email, username, String.random(32),
undefined, this.request.ip,
);
const $set: any = {
const $set: Partial<Udoc> = {
oauth: args.type,
loginat: new Date(),
loginip: this.request.ip,
};
if (r.bio) $set.bio = r.bio;
if (r.viewLang) $set.viewLang = r.viewLang;
if (r.avatar) $set.avatar = r.avatar;
await Promise.all([
user.setById(_id, $set),
oauth.set(r.email, _id),
+9
View File
@@ -55,6 +55,15 @@ export interface Setting {
flag: number,
}
export interface OAuthUserResponse {
_id: string;
avatar?: string;
email?: string;
bio?: string;
uname?: string[];
viewLang?: string;
}
export interface Udoc extends Dictionary<any> {
_id: number;
mail: string;