mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 09:46:28 +08:00
文件结构更改
This commit is contained in:
1 parent
b3af28dc43
commit
1f1d12ad73
109 files changed
+544
-3296
No files matched your search
@@ -0,0 +1,52 @@
|
||||
import 'hydrooj';
|
||||
import superagent from 'superagent';
|
||||
|
||||
async function get() {
|
||||
const { system, token } = global.Hydro.model;
|
||||
const [appid, url, [state]] = await Promise.all([
|
||||
system.get('login-with-google.id'),
|
||||
system.get('server.url'),
|
||||
token.add(token.TYPE_OAUTH, 600, { redirect: this.request.referer }),
|
||||
]);
|
||||
this.response.redirect = `https://accounts.google.com/o/oauth2/v2/auth?client_id=${appid}&response_type=code&redirect_uri=${url}oauth/google/callback&scope=https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile&state=${state}`;
|
||||
}
|
||||
|
||||
async function callback({
|
||||
state, code, error,
|
||||
}) {
|
||||
const { system, token } = global.Hydro.model;
|
||||
const { UserFacingError } = global.Hydro.error;
|
||||
if (error) throw new UserFacingError(error);
|
||||
const [
|
||||
[appid, secret, url, proxy],
|
||||
s,
|
||||
] = await Promise.all([
|
||||
system.getMany([
|
||||
'login-with-google.id', 'oauth.googlesecret', 'server.url', 'proxy',
|
||||
]),
|
||||
token.get(state, token.TYPE_OAUTH),
|
||||
]);
|
||||
const res = await superagent.post('https://oauth2.googleapis.com/token')
|
||||
.proxy(proxy)
|
||||
.send({
|
||||
client_id: appid,
|
||||
client_secret: secret,
|
||||
code,
|
||||
grant_type: 'authorization_code',
|
||||
redirect_uri: `${url}oauth/google/callback`,
|
||||
});
|
||||
const payload = global.Hydro.lib.jwt.decode(res.body.id_token);
|
||||
await token.del(state, token.TYPE_OAUTH);
|
||||
this.response.redirect = s.redirect;
|
||||
return {
|
||||
email: payload.email,
|
||||
uname: [payload.given_name, payload.name, payload.family_name],
|
||||
viewLang: payload.locale.replace('-', '_'),
|
||||
};
|
||||
}
|
||||
|
||||
global.Hydro.lib.oauth_google = {
|
||||
text: 'Login with Google',
|
||||
callback,
|
||||
get,
|
||||
};
|
||||
Reference in new issue
Block a user