Google OAuth

This commit is contained in:
undefined committed 2020-06-22 13:29:14 +08:00
1 parent 8476eac3b4
commit d2173eeb38
12 files changed
+261 -117

No files matched your search

-23
View File
@@ -1,23 +0,0 @@
const axios = require('axios');
async function get(url, options) {
let res;
try {
res = await axios.get(url, options);
} catch (e) {
res = e;
}
return res;
}
async function post(url, data, options) {
let res;
try {
res = await axios.post(url, data, options);
} catch (e) {
res = e;
}
return res;
}
global.Hydro.lib.axios = module.exports = { get, post };
+10 -16
View File
@@ -1,23 +1,17 @@
const axios = require('axios');
const superagent = require('superagent');
const fs = require('fs');
async function download(url, path, retry = 3) {
let r;
try {
const res = await axios.get(url, { responseType: 'stream' });
if (path) {
const w = await fs.createWriteStream(path);
res.data.pipe(w);
await new Promise((resolve, reject) => {
w.on('finish', resolve);
w.on('error', reject);
});
} else r = res.data;
} catch (e) {
if (retry) r = await download(url, path, retry - 1);
else throw e;
if (path) {
const w = await fs.createWriteStream(path);
await superagent.get(url).retry(retry).pipe(w);
await new Promise((resolve, reject) => {
w.on('finish', resolve);
w.on('error', reject);
});
return path;
}
return r;
return superagent.get(url);
}
global.Hydro.lib.download = module.exports = download;
+35
View File
@@ -0,0 +1,35 @@
function decodeBase64WithUriEncoding(encodedText) {
return Buffer.from(encodedText, 'base64').toString('utf8');
}
function unescapedString(escapedString) {
escapedString += new Array(5 - escapedString.length % 4).join('=');
return escapedString.replace(/\-/g, '+').replace(/_/g, '/');
}
function decodeJWT(idToken) {
const token = idToken.split('.');
if (token.length !== 3) {
throw new Error('Invalid idToken');
}
try {
const headerSegment = JSON.parse(decodeBase64WithUriEncoding(token[0]));
const payloadSegment = JSON.parse(decodeBase64WithUriEncoding(token[1]));
const signature = unescapedString(token[2]);
return {
dataToSign: [token[0], token[1]].join('.'),
header: headerSegment,
payload: payloadSegment,
signature,
};
} catch (e) {
throw new Error('Invalid payload');
}
}
function decode(idToken) {
const decodedJWT = decodeJWT(idToken);
return decodedJWT.payload;
}
global.Hydro.lib.jwt = module.exports = { decode };