From 59739e3d869660ea4ca20f08e371d57de9d6cde4 Mon Sep 17 00:00:00 2001 From: undefined Date: Wed, 9 Feb 2022 19:24:20 +0800 Subject: [PATCH] oauth: github: /api --- packages/hydrooj/package.json | 2 +- packages/hydrooj/src/handler/user.ts | 2 +- packages/login-with-github/lib.ts | 9 +++++---- packages/login-with-github/package.json | 2 +- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/packages/hydrooj/package.json b/packages/hydrooj/package.json index 898d40915..79f8f546d 100644 --- a/packages/hydrooj/package.json +++ b/packages/hydrooj/package.json @@ -1,6 +1,6 @@ { "name": "hydrooj", - "version": "3.4.11", + "version": "3.4.12", "bin": "bin/hydrooj.js", "main": "src/loader", "module": "src/loader", diff --git a/packages/hydrooj/src/handler/user.ts b/packages/hydrooj/src/handler/user.ts index 698428a23..788258b25 100644 --- a/packages/hydrooj/src/handler/user.ts +++ b/packages/hydrooj/src/handler/user.ts @@ -352,7 +352,7 @@ class OauthHandler extends Handler { @param('type', Types.String) async get(domainId: string, type: string) { - if (global.Hydro.lib[`oauth_${type}`]) await global.Hydro.lib[`oauth_${type}`].get.call(this); + await global.Hydro.lib[`oauth_${type}`]?.get?.call(this); } } diff --git a/packages/login-with-github/lib.ts b/packages/login-with-github/lib.ts index 142c771c9..443caf753 100644 --- a/packages/login-with-github/lib.ts +++ b/packages/login-with-github/lib.ts @@ -15,11 +15,11 @@ declare module 'hydrooj' { async function get() { const { system, token } = global.Hydro.model; - const [[appid, endpoint = 'https://github.com'], [state]] = await Promise.all([ - system.getMany(['login-with-github.id', 'login-with-github.endpoint']), + const [appid, [state]] = await Promise.all([ + system.get('login-with-github.id'), token.add(token.TYPE_OAUTH, 600, { redirect: this.request.referer }), ]); - this.response.redirect = `${endpoint}/login/oauth/authorize?client_id=${appid}&state=${state}`; + this.response.redirect = `https://github.com/login/oauth/authorize?client_id=${appid}&state=${state}`; } async function callback({ state, code }) { @@ -49,8 +49,9 @@ async function callback({ state, code }) { ); } const t = res.body.access_token; - const userInfo = await superagent.get(`${endpoint || 'https://api.github.com'}/user`) + const userInfo = await superagent.get(`${`${endpoint}/api` || 'https://api.github.com'}/user`) .set('User-Agent', 'Hydro-OAuth') + .set('Accept', 'application/vnd.github.v3+json') .set('Authorization', `token ${t}`); const ret = { _id: `${userInfo.body.id}@github.local`, diff --git a/packages/login-with-github/package.json b/packages/login-with-github/package.json index 364f3c277..ecafa14fa 100644 --- a/packages/login-with-github/package.json +++ b/packages/login-with-github/package.json @@ -1,6 +1,6 @@ { "name": "@hydrooj/login-with-github", - "version": "0.1.1", + "version": "0.1.2", "main": "package.json", "repository": "git@github.com:hydro-dev/Hydro.git", "author": "undefined ",