mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
oauth: github: handle private email
This commit is contained in:
1 parent
f186278a5a
commit
5182031bca
5 files changed
+18
-5
No files matched your search
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "hydrooj",
|
"name": "hydrooj",
|
||||||
"version": "3.4.13",
|
"version": "3.4.14",
|
||||||
"bin": "bin/hydrooj.js",
|
"bin": "bin/hydrooj.js",
|
||||||
"main": "src/loader",
|
"main": "src/loader",
|
||||||
"module": "src/loader",
|
"module": "src/loader",
|
||||||
|
|||||||
@@ -381,6 +381,8 @@ class OauthCallbackHandler extends Handler {
|
|||||||
let username = '';
|
let username = '';
|
||||||
r.uname = r.uname || [];
|
r.uname = r.uname || [];
|
||||||
r.uname.push(String.random(16));
|
r.uname.push(String.random(16));
|
||||||
|
const mailDomain = r.email.split('@')[1];
|
||||||
|
if (await BlackListModel.get(`mail::${mailDomain}`)) throw new BlacklistedError(mailDomain);
|
||||||
for (const uname of r.uname) {
|
for (const uname of r.uname) {
|
||||||
// eslint-disable-next-line no-await-in-loop
|
// eslint-disable-next-line no-await-in-loop
|
||||||
const nudoc = await user.getByUname('system', uname);
|
const nudoc = await user.getByUname('system', uname);
|
||||||
|
|||||||
@@ -57,8 +57,8 @@ export interface Setting {
|
|||||||
|
|
||||||
export interface OAuthUserResponse {
|
export interface OAuthUserResponse {
|
||||||
_id: string;
|
_id: string;
|
||||||
|
email: string;
|
||||||
avatar?: string;
|
avatar?: string;
|
||||||
email?: string;
|
|
||||||
bio?: string;
|
bio?: string;
|
||||||
uname?: string[];
|
uname?: string[];
|
||||||
viewLang?: string;
|
viewLang?: string;
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import 'hydrooj';
|
import 'hydrooj';
|
||||||
|
|
||||||
import superagent from 'superagent';
|
import superagent from 'superagent';
|
||||||
|
import { ForbiddenError } from 'hydrooj/src/error';
|
||||||
|
|
||||||
declare module 'hydrooj' {
|
declare module 'hydrooj' {
|
||||||
interface SystemKeys {
|
interface SystemKeys {
|
||||||
@@ -19,7 +20,7 @@ async function get() {
|
|||||||
system.get('login-with-github.id'),
|
system.get('login-with-github.id'),
|
||||||
token.add(token.TYPE_OAUTH, 600, { redirect: this.request.referer }),
|
token.add(token.TYPE_OAUTH, 600, { redirect: this.request.referer }),
|
||||||
]);
|
]);
|
||||||
this.response.redirect = `https://github.com/login/oauth/authorize?client_id=${appid}&state=${state}`;
|
this.response.redirect = `https://github.com/login/oauth/authorize?client_id=${appid}&state=${state}&scope=read:user,user:email`;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function callback({ state, code }) {
|
async function callback({ state, code }) {
|
||||||
@@ -60,8 +61,18 @@ async function callback({ state, code }) {
|
|||||||
uname: [userInfo.body.name, userInfo.body.login],
|
uname: [userInfo.body.name, userInfo.body.login],
|
||||||
avatar: `github:${userInfo.body.login}`,
|
avatar: `github:${userInfo.body.login}`,
|
||||||
};
|
};
|
||||||
this.response.redirect = s.redirect;
|
if (!ret.email) {
|
||||||
|
const emailInfo = await superagent.get(`${endpoint ? `${endpoint}/api` : 'https://api.github.com'}/user/emails`)
|
||||||
|
.set('User-Agent', 'Hydro-OAuth')
|
||||||
|
.set('Accept', 'application/vnd.github.v3+json')
|
||||||
|
.set('Authorization', `token ${t}`);
|
||||||
|
if (emailInfo.body.length) {
|
||||||
|
ret.email = emailInfo.body.find((e) => e.primary && e.verified).email;
|
||||||
|
}
|
||||||
|
}
|
||||||
await token.del(s._id, token.TYPE_OAUTH);
|
await token.del(s._id, token.TYPE_OAUTH);
|
||||||
|
if (!ret.email) throw new ForbiddenError("You don't have a verified email.");
|
||||||
|
this.response.redirect = (s.redirect || '').endsWith('/login') ? '/' : s.redirect;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@hydrooj/login-with-github",
|
"name": "@hydrooj/login-with-github",
|
||||||
"version": "0.1.3",
|
"version": "0.1.4",
|
||||||
"main": "package.json",
|
"main": "package.json",
|
||||||
"repository": "git@github.com:hydro-dev/Hydro.git",
|
"repository": "git@github.com:hydro-dev/Hydro.git",
|
||||||
"author": "undefined <i@undefined.moe>",
|
"author": "undefined <i@undefined.moe>",
|
||||||
|
|||||||
Reference in new issue
Block a user