mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-17 09:36:08 +08:00
oauth map
This commit is contained in:
1 parent
6b92b96225
commit
71de49fc49
15 files changed
+66
-30
No files matched your search
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@hydrooj/hydrojudge",
|
||||
"version": "2.0.4",
|
||||
"version": "2.0.5",
|
||||
"main": "package.json",
|
||||
"author": "masnn",
|
||||
"os": [
|
||||
|
||||
@@ -11,13 +11,13 @@ import { homedir, tmpdir } from 'os';
|
||||
async function postInit() {
|
||||
// Only start a single daemon
|
||||
if (!cluster.isFirstWorker) return;
|
||||
const { compilerText } = require('./src/utils');
|
||||
const tmpfs = require('./src/tmpfs');
|
||||
const { FormatError, CompileError, SystemError } = require('./src/error');
|
||||
const { compilerText } = require('./utils');
|
||||
const tmpfs = require('./tmpfs');
|
||||
const { FormatError, CompileError, SystemError } = require('./error');
|
||||
const { STATUS_COMPILE_ERROR, STATUS_SYSTEM_ERROR } = global.Hydro.model.builtin.STATUS;
|
||||
const readYamlCases = require('./src/cases');
|
||||
const judge = require('./src/judge');
|
||||
const sysinfo = require('./src/sysinfo');
|
||||
const readYamlCases = require('./cases');
|
||||
const judge = require('./judge');
|
||||
const sysinfo = require('./sysinfo');
|
||||
|
||||
const { problem, file, task } = global.Hydro.model;
|
||||
const _judge = global.Hydro.handler.judge as any;
|
||||
@@ -2,8 +2,8 @@
|
||||
"compilerOptions": {
|
||||
"target": "es2018",
|
||||
"module": "commonjs",
|
||||
"outDir": ".",
|
||||
"rootDir": ".",
|
||||
"outDir": "dist",
|
||||
"rootDir": "src",
|
||||
"moduleResolution": "node",
|
||||
"declaration": true,
|
||||
"sourceMap": true,
|
||||
@@ -12,13 +12,7 @@
|
||||
"resolveJsonModule": true
|
||||
},
|
||||
"include": [
|
||||
"service.ts",
|
||||
"src"
|
||||
],
|
||||
"paths": {
|
||||
"src/*": [
|
||||
"dist/*"
|
||||
]
|
||||
},
|
||||
"exclude": []
|
||||
}
|
||||
@@ -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,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",
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
@@ -46,6 +46,7 @@ async function callback({ state, code }) {
|
||||
.set('User-Agent', 'Hydro-OAuth')
|
||||
.set('Authorization', `token ${t}`);
|
||||
const ret = {
|
||||
_id: `${userInfo.body.id}@github.local`,
|
||||
email: userInfo.body.email,
|
||||
bio: userInfo.body.bio,
|
||||
uname: [userInfo.body.name, userInfo.body.login],
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@hydrooj/login-with-github",
|
||||
"version": "0.0.4",
|
||||
"version": "0.0.5",
|
||||
"main": "package.json",
|
||||
"repository": "git@github.com:hydro-dev/Hydro.git",
|
||||
"author": "undefined <masnn0@outlook.com>",
|
||||
|
||||
@@ -43,6 +43,8 @@ async function callback({
|
||||
await token.del(state, token.TYPE_OAUTH);
|
||||
this.response.redirect = s.redirect;
|
||||
return {
|
||||
// TODO use openid
|
||||
_id: payload.email,
|
||||
email: payload.email,
|
||||
uname: [payload.given_name, payload.name, payload.family_name],
|
||||
viewLang: payload.locale.replace('-', '_'),
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@hydrooj/login-with-google",
|
||||
"version": "0.0.4",
|
||||
"version": "0.0.5",
|
||||
"main": "package.json",
|
||||
"repository": "git@github.com:hydro-dev/Hydro.git",
|
||||
"author": "undefined <masnn0@outlook.com>",
|
||||
|
||||
@@ -51,6 +51,7 @@ async function callback({ state, code }) {
|
||||
.set('User-Agent', 'Hydro-OAuth')
|
||||
.set('Authorization', `Bearer ${t}`);
|
||||
const ret = {
|
||||
_id: `${userInfo.body.id}@osu.local`,
|
||||
email: `${userInfo.body.id}@osu.local`,
|
||||
bio: '',
|
||||
uname: [userInfo.body.username],
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@hydrooj/login-with-osu",
|
||||
"version": "0.0.4",
|
||||
"version": "0.0.5",
|
||||
"main": "package.json",
|
||||
"repository": "git@github.com:hydro-dev/Hydro.git",
|
||||
"author": "undefined <masnn0@outlook.com>",
|
||||
|
||||
@@ -20,6 +20,7 @@ async function callback({ state }) {
|
||||
const s = await token.get(state, token.TYPE_OAUTH);
|
||||
if (!s || !s.email) throw new InvalidTokenError(state);
|
||||
const ret = {
|
||||
_id: s.email,
|
||||
email: s.email,
|
||||
bio: '',
|
||||
uname: [s.username],
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@hydrooj/login-with-qq",
|
||||
"version": "0.0.1",
|
||||
"version": "0.0.2",
|
||||
"main": "package.json",
|
||||
"repository": "git@github.com:hydro-dev/Hydro.git",
|
||||
"author": "undefined <masnn0@outlook.com>",
|
||||
|
||||
Reference in new issue
Block a user