mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
oauth map
This commit is contained in:
1 parent
6b92b96225
commit
71de49fc49
15 files changed
+65
-29
No files matched your search
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@hydrooj/hydrojudge",
|
"name": "@hydrooj/hydrojudge",
|
||||||
"version": "2.0.4",
|
"version": "2.0.5",
|
||||||
"main": "package.json",
|
"main": "package.json",
|
||||||
"author": "masnn",
|
"author": "masnn",
|
||||||
"os": [
|
"os": [
|
||||||
|
|||||||
@@ -11,13 +11,13 @@ import { homedir, tmpdir } from 'os';
|
|||||||
async function postInit() {
|
async function postInit() {
|
||||||
// Only start a single daemon
|
// Only start a single daemon
|
||||||
if (!cluster.isFirstWorker) return;
|
if (!cluster.isFirstWorker) return;
|
||||||
const { compilerText } = require('./src/utils');
|
const { compilerText } = require('./utils');
|
||||||
const tmpfs = require('./src/tmpfs');
|
const tmpfs = require('./tmpfs');
|
||||||
const { FormatError, CompileError, SystemError } = require('./src/error');
|
const { FormatError, CompileError, SystemError } = require('./error');
|
||||||
const { STATUS_COMPILE_ERROR, STATUS_SYSTEM_ERROR } = global.Hydro.model.builtin.STATUS;
|
const { STATUS_COMPILE_ERROR, STATUS_SYSTEM_ERROR } = global.Hydro.model.builtin.STATUS;
|
||||||
const readYamlCases = require('./src/cases');
|
const readYamlCases = require('./cases');
|
||||||
const judge = require('./src/judge');
|
const judge = require('./judge');
|
||||||
const sysinfo = require('./src/sysinfo');
|
const sysinfo = require('./sysinfo');
|
||||||
|
|
||||||
const { problem, file, task } = global.Hydro.model;
|
const { problem, file, task } = global.Hydro.model;
|
||||||
const _judge = global.Hydro.handler.judge as any;
|
const _judge = global.Hydro.handler.judge as any;
|
||||||
@@ -2,8 +2,8 @@
|
|||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"target": "es2018",
|
"target": "es2018",
|
||||||
"module": "commonjs",
|
"module": "commonjs",
|
||||||
"outDir": ".",
|
"outDir": "dist",
|
||||||
"rootDir": ".",
|
"rootDir": "src",
|
||||||
"moduleResolution": "node",
|
"moduleResolution": "node",
|
||||||
"declaration": true,
|
"declaration": true,
|
||||||
"sourceMap": true,
|
"sourceMap": true,
|
||||||
@@ -12,13 +12,7 @@
|
|||||||
"resolveJsonModule": true
|
"resolveJsonModule": true
|
||||||
},
|
},
|
||||||
"include": [
|
"include": [
|
||||||
"service.ts",
|
|
||||||
"src"
|
"src"
|
||||||
],
|
],
|
||||||
"paths": {
|
|
||||||
"src/*": [
|
|
||||||
"dist/*"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"exclude": []
|
"exclude": []
|
||||||
}
|
}
|
||||||
@@ -4,6 +4,4 @@ SETTINGS_PRIVACY.allowRegisteredUsers
|
|||||||
model.setting.ui.name
|
model.setting.ui.name
|
||||||
pdoc.secretConfig?
|
pdoc.secretConfig?
|
||||||
message.markAsRead
|
message.markAsRead
|
||||||
Handle OAuth Map !important
|
|
||||||
基于document的model难以lint,建议拆分
|
基于document的model难以lint,建议拆分
|
||||||
写test! !important
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "hydrooj",
|
"name": "hydrooj",
|
||||||
"version": "2.12.17",
|
"version": "2.12.18",
|
||||||
"bin": "bin/hydrooj.js",
|
"bin": "bin/hydrooj.js",
|
||||||
"main": "dist/loader.js",
|
"main": "dist/loader.js",
|
||||||
"typings": "dist/loader.d.ts",
|
"typings": "dist/loader.d.ts",
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import {
|
|||||||
Route, Handler, Types, param,
|
Route, Handler, Types, param,
|
||||||
} from '../service/server';
|
} from '../service/server';
|
||||||
import * as user from '../model/user';
|
import * as user from '../model/user';
|
||||||
|
import * as oauth from '../model/oauth';
|
||||||
import * as token from '../model/token';
|
import * as token from '../model/token';
|
||||||
import * as record from '../model/record';
|
import * as record from '../model/record';
|
||||||
import * as problem from '../model/problem';
|
import * as problem from '../model/problem';
|
||||||
@@ -227,10 +228,9 @@ class OauthCallbackHandler extends Handler {
|
|||||||
let r;
|
let r;
|
||||||
if (global.Hydro.lib[`oauth_${args.type}`]) r = await global.Hydro.lib[`oauth_${args.type}`].callback.call(this, args);
|
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');
|
else throw new UserFacingError('Oauth type');
|
||||||
const udoc = await user.getByEmail('system', r.email);
|
const uid = await oauth.get(r._id);
|
||||||
if (udoc) {
|
if (uid) this.session.uid = uid;
|
||||||
this.session.uid = udoc._id;
|
else {
|
||||||
} else {
|
|
||||||
this.checkPriv(PRIV.PRIV_REGISTER_USER);
|
this.checkPriv(PRIV.PRIV_REGISTER_USER);
|
||||||
let username = '';
|
let username = '';
|
||||||
r.uname = r.uname || [];
|
r.uname = r.uname || [];
|
||||||
@@ -243,7 +243,7 @@ class OauthCallbackHandler extends Handler {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const uid = await user.create(
|
const _id = await user.create(
|
||||||
r.email, username, String.random(32),
|
r.email, username, String.random(32),
|
||||||
undefined, this.request.ip,
|
undefined, this.request.ip,
|
||||||
);
|
);
|
||||||
@@ -252,8 +252,11 @@ class OauthCallbackHandler extends Handler {
|
|||||||
};
|
};
|
||||||
if (r.bio) $set.bio = r.bio;
|
if (r.bio) $set.bio = r.bio;
|
||||||
if (r.viewLang) $set.viewLang = r.viewLang;
|
if (r.viewLang) $set.viewLang = r.viewLang;
|
||||||
await user.setById(uid, $set);
|
await Promise.all([
|
||||||
this.session.uid = uid;
|
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('User-Agent', 'Hydro-OAuth')
|
||||||
.set('Authorization', `token ${t}`);
|
.set('Authorization', `token ${t}`);
|
||||||
const ret = {
|
const ret = {
|
||||||
|
_id: `${userInfo.body.id}@github.local`,
|
||||||
email: userInfo.body.email,
|
email: userInfo.body.email,
|
||||||
bio: userInfo.body.bio,
|
bio: userInfo.body.bio,
|
||||||
uname: [userInfo.body.name, userInfo.body.login],
|
uname: [userInfo.body.name, userInfo.body.login],
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@hydrooj/login-with-github",
|
"name": "@hydrooj/login-with-github",
|
||||||
"version": "0.0.4",
|
"version": "0.0.5",
|
||||||
"main": "package.json",
|
"main": "package.json",
|
||||||
"repository": "git@github.com:hydro-dev/Hydro.git",
|
"repository": "git@github.com:hydro-dev/Hydro.git",
|
||||||
"author": "undefined <masnn0@outlook.com>",
|
"author": "undefined <masnn0@outlook.com>",
|
||||||
|
|||||||
@@ -43,6 +43,8 @@ async function callback({
|
|||||||
await token.del(state, token.TYPE_OAUTH);
|
await token.del(state, token.TYPE_OAUTH);
|
||||||
this.response.redirect = s.redirect;
|
this.response.redirect = s.redirect;
|
||||||
return {
|
return {
|
||||||
|
// TODO use openid
|
||||||
|
_id: payload.email,
|
||||||
email: payload.email,
|
email: payload.email,
|
||||||
uname: [payload.given_name, payload.name, payload.family_name],
|
uname: [payload.given_name, payload.name, payload.family_name],
|
||||||
viewLang: payload.locale.replace('-', '_'),
|
viewLang: payload.locale.replace('-', '_'),
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@hydrooj/login-with-google",
|
"name": "@hydrooj/login-with-google",
|
||||||
"version": "0.0.4",
|
"version": "0.0.5",
|
||||||
"main": "package.json",
|
"main": "package.json",
|
||||||
"repository": "git@github.com:hydro-dev/Hydro.git",
|
"repository": "git@github.com:hydro-dev/Hydro.git",
|
||||||
"author": "undefined <masnn0@outlook.com>",
|
"author": "undefined <masnn0@outlook.com>",
|
||||||
|
|||||||
@@ -51,6 +51,7 @@ async function callback({ state, code }) {
|
|||||||
.set('User-Agent', 'Hydro-OAuth')
|
.set('User-Agent', 'Hydro-OAuth')
|
||||||
.set('Authorization', `Bearer ${t}`);
|
.set('Authorization', `Bearer ${t}`);
|
||||||
const ret = {
|
const ret = {
|
||||||
|
_id: `${userInfo.body.id}@osu.local`,
|
||||||
email: `${userInfo.body.id}@osu.local`,
|
email: `${userInfo.body.id}@osu.local`,
|
||||||
bio: '',
|
bio: '',
|
||||||
uname: [userInfo.body.username],
|
uname: [userInfo.body.username],
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@hydrooj/login-with-osu",
|
"name": "@hydrooj/login-with-osu",
|
||||||
"version": "0.0.4",
|
"version": "0.0.5",
|
||||||
"main": "package.json",
|
"main": "package.json",
|
||||||
"repository": "git@github.com:hydro-dev/Hydro.git",
|
"repository": "git@github.com:hydro-dev/Hydro.git",
|
||||||
"author": "undefined <masnn0@outlook.com>",
|
"author": "undefined <masnn0@outlook.com>",
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ async function callback({ state }) {
|
|||||||
const s = await token.get(state, token.TYPE_OAUTH);
|
const s = await token.get(state, token.TYPE_OAUTH);
|
||||||
if (!s || !s.email) throw new InvalidTokenError(state);
|
if (!s || !s.email) throw new InvalidTokenError(state);
|
||||||
const ret = {
|
const ret = {
|
||||||
|
_id: s.email,
|
||||||
email: s.email,
|
email: s.email,
|
||||||
bio: '',
|
bio: '',
|
||||||
uname: [s.username],
|
uname: [s.username],
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@hydrooj/login-with-qq",
|
"name": "@hydrooj/login-with-qq",
|
||||||
"version": "0.0.1",
|
"version": "0.0.2",
|
||||||
"main": "package.json",
|
"main": "package.json",
|
||||||
"repository": "git@github.com:hydro-dev/Hydro.git",
|
"repository": "git@github.com:hydro-dev/Hydro.git",
|
||||||
"author": "undefined <masnn0@outlook.com>",
|
"author": "undefined <masnn0@outlook.com>",
|
||||||
|
|||||||
Reference in new issue
Block a user