oauth map

This commit is contained in:
undefined committed 2020-08-20 08:53:26 +08:00
1 parent 6b92b96225
commit 71de49fc49
15 files changed
+66 -30

No files matched your search

+1 -3
View File
@@ -4,6 +4,4 @@ SETTINGS_PRIVACY.allowRegisteredUsers
model.setting.ui.name
pdoc.secretConfig?
message.markAsRead
Handle OAuth Map !important
基于document的model难以lint,建议拆分
写test! !important
基于document的model难以lint,建议拆分
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "hydrooj",
"version": "2.12.17",
"version": "2.12.18",
"bin": "bin/hydrooj.js",
"main": "dist/loader.js",
"typings": "dist/loader.d.ts",
+10 -7
View File
@@ -8,6 +8,7 @@ import {
Route, Handler, Types, param,
} from '../service/server';
import * as user from '../model/user';
import * as oauth from '../model/oauth';
import * as token from '../model/token';
import * as record from '../model/record';
import * as problem from '../model/problem';
@@ -227,10 +228,9 @@ class OauthCallbackHandler extends Handler {
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');
const udoc = await user.getByEmail('system', r.email);
if (udoc) {
this.session.uid = udoc._id;
} else {
const uid = await oauth.get(r._id);
if (uid) this.session.uid = uid;
else {
this.checkPriv(PRIV.PRIV_REGISTER_USER);
let username = '';
r.uname = r.uname || [];
@@ -243,7 +243,7 @@ class OauthCallbackHandler extends Handler {
break;
}
}
const uid = await user.create(
const _id = await user.create(
r.email, username, String.random(32),
undefined, this.request.ip,
);
@@ -252,8 +252,11 @@ class OauthCallbackHandler extends Handler {
};
if (r.bio) $set.bio = r.bio;
if (r.viewLang) $set.viewLang = r.viewLang;
await user.setById(uid, $set);
this.session.uid = uid;
await Promise.all([
user.setById(_id, $set),
oauth.set(r.email, _id),
]);
this.session.uid = _id;
}
}
}
+36
View File
@@ -0,0 +1,36 @@
import Lru from 'lru-cache';
import { Collection } from 'mongodb';
import * as db from '../service/db';
interface OauthMap {
_id: string, // source openId
uid: number, // target uid
}
const coll: Collection<OauthMap> = db.collection('oauth');
const cache = new Lru({
maxAge: 5000,
});
export async function get(_id: string) {
const res = cache.get(_id);
if (res !== undefined) return res;
const doc = await coll.findOne({ _id });
if (doc) return doc.uid;
return null;
}
export async function set(_id: string, value: number) {
cache.set(_id, value);
const res = await coll.findOneAndUpdate(
{ _id },
{ $set: { value } },
{ upsert: true, returnOriginal: false },
);
return res.value.uid;
}
global.Hydro.model.oauth = {
get,
set,
};