Fedi.CrowdedGames.Group/JS/setting.js

116 lines
4.5 KiB
JavaScript

import * as MastodonAPI from "./MastodonAPI.js";
import * as BlueskyAPI from "./BlueskyAPI.js";
import * as TumblrAPI from "./TumblrAPI.js";
// Settings buttons
let LocalButton = document.getElementsByClassName("Local")[0];
let RemoteButton = document.getElementsByClassName("Remote")[0];
let MastodonLoginButton = document.getElementsByClassName("Login Mastodon")[0];
let MastodonWebInput = document.getElementsByClassName("WebInput Mastodon")[0];
let MastodonLogoutButton = document.getElementsByClassName("Logout Mastodon")[0];
let BlueskyLoginButton = document.getElementsByClassName("Login Bluesky")[0];
let BlueskyWebInput = document.getElementsByClassName("WebInput Bluesky")[0];
let BlueskyLogoutButton = document.getElementsByClassName("Logout Bluesky")[0];
// MAKE SURE THESE ARE SYNCED!
// Mastodon
let MastodonWebsite = "https://wetdry.world";
let MastodonClientID = "mastodon_client_id";
let MastodonClientSecret = "mastodon_client_secret";
let MastodonAccessToken = "mastodon_access_token";
let MastodonTokenType = "mastodon_token_type";
// Bluesky
let BlueskyApp = "https://bsky.app";
let BlueskyPDS = "https://bsky.social";
let BlueskyPKCEverifer = "bluesky_pkce_verifier";
let BlueskyPKCEchallenge = "bluesky_pkce_challenge";
let BlueskyPrivateKey = "bluesky_private_key";
let BlueskyPublicKey = "bluesky_public_key";
let BlueskyNonce = "bluesky_nonce";
// Tumblr
let TumblrWebsite = "https://www.tumblr.com";
// Change weather the timelines are public or remote
LocalButton.onclick = (event) => {
// Toggle the cookie
if (document.cookie.split(";").some((item) => item.trim().startsWith("Local="))) {
document.cookie = "Local=true;samesite=strict;path=/;expires=expires=0000-01-01;";
} else {
document.cookie = "Local=true;samesite=strict;path=/;";
}
}
RemoteButton.onclick = (event) => {
// Toggle the cookie
if (document.cookie.split(";").some((item) => item.trim().startsWith("Remote="))) {
document.cookie = "Remote=true;samesite=strict;path=/;expires=expires=0000-01-01;";
} else {
document.cookie = "Remote=true;samesite=strict;path=/;";
}
}
// Mastodon buttons
MastodonLoginButton.onclick = (event) => {
if (MastodonWebInput != "") {
MastodonAPI.HandleAuthentication(MastodonWebsite, MastodonClientID, MastodonClientSecret);
}
}
MastodonLogoutButton.onclick = (event) => {
document.cookie = MastodonClientID + "=nothing;" + ";samesite=strict;path=/;expires=Thu, 01 Jan 1970 00:00:00 GMT;";
document.cookie = MastodonClientSecret + "=nothing;" + ";samesite=strict;path=/;expires=Thu, 01 Jan 1970 00:00:00 GMT;";
document.cookie = MastodonAccessToken + "=nothing;" + ";samesite=strict;path=/;expires=Thu, 01 Jan 1970 00:00:00 GMT;";
document.cookie = MastodonTokenType + "=nothing;" + ";samesite=strict;path=/;expires=Thu, 01 Jan 1970 00:00:00 GMT;";
document.location.href = document.location.href;
}
// Bluesky Buttons
BlueskyLoginButton.onclick = (event) => {
if (BlueskyWebInput != "") {
BlueskyTestingAuthorization();
}
}
BlueskyLogoutButton.onclick = (event) => {
// Nothing at the moment
}
// if an access token is found, login.
function CheckLogin() {
// Check for a mastodon token.
if (document.cookie.split("; ").find((row) => row.startsWith(MastodonAccessToken + "="))?.split("=").length > 1 || document.location.href.split("code=").length == 1) {
// Swap the buttons
MastodonLoginButton.remove();
MastodonWebInput.remove();
MastodonLogoutButton.setAttribute("style", "");
} else {
MastodonAPI.GainToken(MastodonWebsite, MastodonClientID, MastodonClientSecret, MastodonAccessToken, MastodonTokenType);
}
// Check for a bluesky token.
}
// Runs on website start.
// Remove traces of "login".
CheckLogin();
// The next section is dedicated to testing.
// WARNING: I don't know what I am doing.
await BlueskyAPI.AssertionJWT();
async function BlueskyTestingAuthorization() {
// Declare Variables
let WellKnown = await BlueskyAPI.GetPDSWellKnown();
let PAREndpoint = WellKnown.pushed_authorization_request_endpoint;
let State = BlueskyAPI.GenerateToken(64);
let PKCEverifier = await BlueskyAPI.CreatePKCECodeVerifier();
let PKCEchallenge = await BlueskyAPI.CreatePKCECodeChallenge(PKCEverifier);
// PAR request (beginning)
let PAR = await BlueskyAPI.PARrequest(WellKnown.pushed_authorization_request_endpoint, State, PKCEchallenge);
// Now we need to authenticate. Make sure the State stays the same throughout this whole process :]
// document.location.href = "https://bsky.social/oauth/authorize?client_id=https://fedi.crowdedgames.group/oauth/client-metadata.json&request_uri=" + PAR.request_uri;
}