Skip to content

Commit

Permalink
Added token refresh ability to cloudflare
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasbenb committed Aug 3, 2021
1 parent 4687922 commit 399a248
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 25 deletions.
82 changes: 62 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,101 @@ addEventListener("fetch", (event) => {
});

async function handleRequest(request) {
const drive = new googleDrive();
let url = new URL(request.url);
let path = url.pathname;
const url = new URL(request.url);
const path = url.pathname;
if (!path.toLowerCase().startsWith("/api")) {
return new Response(
"libDrive for Cloudflare doesn't work on its own. It is only an extention to the backend."
);
} else if (path.toLowerCase().startsWith("/api/v1/download")) {
const session = JSON.parse(atob(url.searchParams.get("session")));
return drive.downloadAPI(
request.headers.get("Range") || "",
session.access_token,
session.transcoded,
session.cookie,
session.url
);
const drive = new googleDrive(session);
return drive.downloadAPI(request.headers.get("Range") || "", session);
}
}

class googleDrive {
async downloadAPI(range = "", access_token, transcoded, cookie, url) {
if (transcoded == true && cookie) {
constructor(session) {
let token_expiry = new Date(session.token_expiry);
this.config = {
access_token: null,
client_id: session.client_id,
client_secret: session.client_secret,
refresh_token: session.refresh_token,
token_expiry: token_expiry,
};
}
async downloadAPI(range, session) {
if (session.transcoded == true && session.cookie) {
let requestOption = {
method: "GET",
headers: {
Cookie: cookie,
Cookie: session.cookie,
Range: range,
},
};
let resp = await fetch(url, requestOption);
let resp = await fetch(session.url, requestOption);
let { headers } = (resp = new Response(resp.body, resp));
headers.append("Access-Control-Allow-Origin", "*");
headers.set("Content-Disposition", "inline");
headers.set("Access-Control-Allow-Headers", "*");
headers.set("Cache-Control", "no-cache, no-store, must-revalidate")
headers.set("pragma", "no-cache")
headers.set("Cache-Control", "no-cache, no-store, must-revalidate");
headers.set("pragma", "no-cache");
return resp;
} else {
await this.setAccessToken();
let requestOption = {
method: "GET",
headers: { Authorization: `Bearer ${access_token}`, Range: range },
headers: {
Authorization: `Bearer ${this.config.access_token}`,
Range: range,
},
};
let resp = await fetch(url, requestOption);
let resp = await fetch(session.url, requestOption);
let { headers } = (resp = new Response(resp.body, resp));
headers.append("Access-Control-Allow-Origin", "*");
headers.set("Content-Disposition", "inline");
headers.set("Access-Control-Allow-Headers", "*");
headers.set("Cache-Control", "no-cache, no-store, must-revalidate")
headers.set("pragma", "no-cache")
headers.set("Cache-Control", "no-cache, no-store, must-revalidate");
headers.set("pragma", "no-cache");
return resp;
}
}

async setAccessToken() {
if (
this.config.token_expiry == undefined ||
this.config.token_expiry < Date.now()
) {
const obj = await this.fetchAccessToken();
if (obj.access_token != undefined) {
console.log(obj);
this.config.access_token = obj.access_token;
this.config.token_expiry = obj.token_expiry;
}
}
}

async getAccessToken() {
const url = "https://www.googleapis.com/oauth2/v4/token";
const headers = {
"Content-Type": "application/x-www-form-urlencoded",
};
const post_data = {
client_id: this.config.client_id,
client_secret: this.config.client_secret,
refresh_token: this.config.refresh_token,
grant_type: "refresh_token",
};
let requestOption = {
method: "POST",
headers: headers,
body: this.enQuery(post_data),
};
const response = await fetch(url, requestOption);
return await response.json();
}

enQuery(data) {
const ret = [];
for (let d in data) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"dependencies": {},
"devDependencies": {
"@cloudflare/wrangler": "^1.16.1"
"@cloudflare/wrangler": "^1.19.0"
},
"main": "index.js",
"repository": {
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# yarn lockfile v1


"@cloudflare/wrangler@^1.16.1":
version "1.16.1"
resolved "https://registry.yarnpkg.com/@cloudflare/wrangler/-/wrangler-1.16.1.tgz#c7a4d827c703ba9ecc0c43d447a7666dbec4dd88"
integrity sha512-jDxtri9v6IRszObICQU+eB/eFGySOnBJJvVcp5tp+Q1niH2l51wbWoXOThLfgv0DeSu7o7rSpjn13oULeSj2vg==
"@cloudflare/wrangler@^1.19.0":
version "1.19.0"
resolved "https://registry.yarnpkg.com/@cloudflare/wrangler/-/wrangler-1.19.0.tgz#1cf2d7332b74e60a12730f9aa69b7e562022d57b"
integrity sha512-fbvJEzb8P/IulJus72pG/G3THDyNk+YGwl1PzuMGlJGF+yvCaNWj+NmyZERmQxfq2bDs8UTUSQTrIdYVGH1tXQ==
dependencies:
axios "^0.21.1"
rimraf "^3.0.2"
Expand Down

0 comments on commit 399a248

Please sign in to comment.