2023-04-07 00:27:18 +00:00
|
|
|
/*
|
|
|
|
* Vencord, a modification for Discord's desktop app
|
|
|
|
* Copyright (c) 2023 Vendicated and contributors
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import * as DataStore from "@api/DataStore";
|
|
|
|
import { showNotification } from "@api/Notifications";
|
2023-05-05 23:36:00 +00:00
|
|
|
import { Settings } from "@api/Settings";
|
2023-04-07 00:27:18 +00:00
|
|
|
import { findByProps } from "@webpack";
|
|
|
|
import { UserStore } from "@webpack/common";
|
|
|
|
|
2023-05-05 23:36:00 +00:00
|
|
|
import { Logger } from "./Logger";
|
2023-04-07 00:27:18 +00:00
|
|
|
import { openModal } from "./modal";
|
|
|
|
|
|
|
|
export const cloudLogger = new Logger("Cloud", "#39b7e0");
|
|
|
|
export const getCloudUrl = () => new URL(Settings.cloud.url);
|
|
|
|
|
2023-07-08 01:36:59 +00:00
|
|
|
const cloudUrlOrigin = () => getCloudUrl().origin;
|
|
|
|
const getUserId = () => {
|
|
|
|
const id = UserStore.getCurrentUser()?.id;
|
|
|
|
if (!id) throw new Error("User not yet logged in");
|
|
|
|
return id;
|
|
|
|
};
|
|
|
|
|
2023-04-07 00:27:18 +00:00
|
|
|
export async function getAuthorization() {
|
|
|
|
const secrets = await DataStore.get<Record<string, string>>("Vencord_cloudSecret") ?? {};
|
2023-07-08 01:36:59 +00:00
|
|
|
|
|
|
|
const origin = cloudUrlOrigin();
|
|
|
|
|
|
|
|
// we need to migrate from the old format here
|
|
|
|
if (secrets[origin]) {
|
|
|
|
await DataStore.update<Record<string, string>>("Vencord_cloudSecret", secrets => {
|
|
|
|
secrets ??= {};
|
|
|
|
// use the current user ID
|
|
|
|
secrets[`${origin}:${getUserId()}`] = secrets[origin];
|
|
|
|
delete secrets[origin];
|
|
|
|
return secrets;
|
|
|
|
});
|
|
|
|
|
|
|
|
// since this doesn't update the original object, we'll early return the existing authorization
|
|
|
|
return secrets[origin];
|
|
|
|
}
|
|
|
|
|
|
|
|
return secrets[`${origin}:${getUserId()}`];
|
2023-04-07 00:27:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function setAuthorization(secret: string) {
|
|
|
|
await DataStore.update<Record<string, string>>("Vencord_cloudSecret", secrets => {
|
|
|
|
secrets ??= {};
|
2023-07-08 01:36:59 +00:00
|
|
|
secrets[`${cloudUrlOrigin()}:${getUserId()}`] = secret;
|
2023-04-07 00:27:18 +00:00
|
|
|
return secrets;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function deauthorizeCloud() {
|
|
|
|
await DataStore.update<Record<string, string>>("Vencord_cloudSecret", secrets => {
|
|
|
|
secrets ??= {};
|
2023-07-08 01:36:59 +00:00
|
|
|
delete secrets[`${cloudUrlOrigin()}:${getUserId()}`];
|
2023-04-07 00:27:18 +00:00
|
|
|
return secrets;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function authorizeCloud() {
|
|
|
|
if (await getAuthorization() !== undefined) {
|
|
|
|
Settings.cloud.authenticated = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const oauthConfiguration = await fetch(new URL("/v1/oauth/settings", getCloudUrl()));
|
|
|
|
var { clientId, redirectUri } = await oauthConfiguration.json();
|
|
|
|
} catch {
|
|
|
|
showNotification({
|
|
|
|
title: "Cloud Integration",
|
|
|
|
body: "Setup failed (couldn't retrieve OAuth configuration)."
|
|
|
|
});
|
|
|
|
Settings.cloud.authenticated = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { OAuth2AuthorizeModal } = findByProps("OAuth2AuthorizeModal");
|
|
|
|
|
|
|
|
openModal((props: any) => <OAuth2AuthorizeModal
|
|
|
|
{...props}
|
|
|
|
scopes={["identify"]}
|
|
|
|
responseType="code"
|
|
|
|
redirectUri={redirectUri}
|
|
|
|
permissions={0n}
|
|
|
|
clientId={clientId}
|
|
|
|
cancelCompletesFlow={false}
|
2023-06-03 19:57:31 +00:00
|
|
|
callback={async ({ location }: any) => {
|
|
|
|
if (!location) {
|
2023-04-07 00:27:18 +00:00
|
|
|
Settings.cloud.authenticated = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-06-03 19:57:31 +00:00
|
|
|
const res = await fetch(location, {
|
2023-04-07 00:27:18 +00:00
|
|
|
headers: new Headers({ Accept: "application/json" })
|
|
|
|
});
|
|
|
|
const { secret } = await res.json();
|
|
|
|
if (secret) {
|
|
|
|
cloudLogger.info("Authorized with secret");
|
|
|
|
await setAuthorization(secret);
|
|
|
|
showNotification({
|
|
|
|
title: "Cloud Integration",
|
|
|
|
body: "Cloud integrations enabled!"
|
|
|
|
});
|
|
|
|
Settings.cloud.authenticated = true;
|
|
|
|
} else {
|
|
|
|
showNotification({
|
|
|
|
title: "Cloud Integration",
|
|
|
|
body: "Setup failed (no secret returned?)."
|
|
|
|
});
|
|
|
|
Settings.cloud.authenticated = false;
|
|
|
|
}
|
|
|
|
} catch (e: any) {
|
|
|
|
cloudLogger.error("Failed to authorize", e);
|
|
|
|
showNotification({
|
|
|
|
title: "Cloud Integration",
|
|
|
|
body: `Setup failed (${e.toString()}).`
|
|
|
|
});
|
|
|
|
Settings.cloud.authenticated = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/>);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getCloudAuth() {
|
|
|
|
const secret = await getAuthorization();
|
|
|
|
|
2023-07-08 01:36:59 +00:00
|
|
|
return window.btoa(`${secret}:${getUserId()}`);
|
2023-04-07 00:27:18 +00:00
|
|
|
}
|