hmm... the API isn't working?

This commit is contained in:
CatAClock 2025-05-01 12:44:37 -07:00
parent 8052582b5e
commit abc59af2a3
6 changed files with 168 additions and 121 deletions

View file

@ -1,6 +1,8 @@
export async function GetBlueskyDID(PDS, Handle, NonceName, AccessToken) {
let DPoP = ClientDPoPPDS("GET", PDS + "/xrpc/com.atproto.identity.resolveDid", document.cookie.split("; ").find((row) => row.startsWith(NonceName + "="))?.split("="), AccessToken);
let request = fetch(PDS + "/xrpc/com.atproto.identity.resolveDid?handle=" + Handle, { method: "GET", headers: {"Authorization": "DPoP " + AccessToken, "DPoP": DPoP})
import * as Cookie from "./Cookies.js";
export async function GetBlueskyDID(PDS, Handle) {
let DPoP = ClientDPoPPDS("GET", PDS + "/xrpc/com.atproto.identity.resolveDid");
let request = fetch(PDS + "/xrpc/com.atproto.identity.resolveDid?handle=" + Handle, { method: "GET", headers: {"Authorization": "DPoP " + Cookie.BlueskyAccessTokenCookie, "DPoP": DPoP}})
.then((response) => response.json());
return request;
}
@ -42,17 +44,17 @@ export async function CreatePKCECodeChallenge(CodeVerifier) {
}
// Component 3/4
export async function PARrequest(PAREndpoint, State, ChallengeCode) {
return fetch(PAREndpoint, {method: "POST", body: new URLSearchParams({ response_type: "code", code_challenge_method: "S256", scope: "atproto transition:generic", client_id: "https://fedi.crowdedgames.group/oauth/client-metadata.json", redirect_uri: "https://fedi.crowdedgames.group/HTML/setting.html", code_challenge: ChallengeCode, state: State, login_hint: "crowdedgames.group" }), headers: {"Content-Type": "application/x-www-form-urlencoded"}});
export async function PARrequest(PAREndpoint, State) {
return fetch(PAREndpoint, {method: "POST", body: new URLSearchParams({ response_type: "code", code_challenge_method: "S256", scope: "atproto transition:generic", client_id: "https://fedi.crowdedgames.group/oauth/client-metadata.json", redirect_uri: "https://fedi.crowdedgames.group/HTML/setting.html", code_challenge: Cookie.BlueskyPKCEChallengeCookie, state: State, login_hint: "crowdedgames.group" }), headers: {"Content-Type": "application/x-www-form-urlencoded"}});
}
export async function AuthRequest(TokenEndpoint, ChallengeVerifier, code, DPoP) {
return fetch(TokenEndpoint, {method: "POST", body: new URLSearchParams({ grant_type: "authorization_code", code: code, client_id: "https://fedi.crowdedgames.group/oauth/client-metadata.json", redirect_uri: "https://fedi.crowdedgames.group/HTML/setting.html", code_verifier: ChallengeVerifier}), headers: { "DPoP": DPoP, "Content-Type": "application/x-www-form-urlencoded"}})
export async function AuthRequest(TokenEndpoint, code, DPoP) {
return fetch(TokenEndpoint, {method: "POST", body: new URLSearchParams({ grant_type: "authorization_code", code: code, client_id: "https://fedi.crowdedgames.group/oauth/client-metadata.json", redirect_uri: "https://fedi.crowdedgames.group/HTML/setting.html", code_verifier: Cookie.BlueskyPKCEVeriferCookie}), headers: { "DPoP": DPoP, "Content-Type": "application/x-www-form-urlencoded"}})
.then((response) => response.json());
}
// Component 4/4
export async function ClientDPoPToken(POSTorGET, RequestURL, DPoPNonce) {
export async function ClientDPoPToken(POSTorGET, RequestURL) {
let KeyPair = await crypto.subtle.generateKey({name: "ECDSA", namedCurve: "P-256"}, true, ["sign", "verify"]);
// Header
@ -71,7 +73,7 @@ export async function ClientDPoPToken(POSTorGET, RequestURL, DPoPNonce) {
Payload.htm = POSTorGET;
Payload.htu = RequestURL;
Payload.iat = Math.floor(new Date(Date.now()).getTime() / 1000);
Payload.nonce = DPoPNonce;
Payload.nonce = Cookie.BlueskyNonceCookie;
var sHeader = JSON.stringify(Header);
var sPayload = JSON.stringify(Payload);
@ -87,7 +89,7 @@ export async function ClientDPoPToken(POSTorGET, RequestURL, DPoPNonce) {
}
// So far does nothing? Don't touch :3
export async function ClientDPoPPDS(POSTorGET, RequestURL, DPoPNonce, AccessToken) {
export async function ClientDPoPPDS(POSTorGET, RequestURL) {
let KeyPair = await crypto.subtle.generateKey({name: "ECDSA", namedCurve: "P-256"}, true, ["sign", "verify"]);
// Header
@ -106,8 +108,8 @@ export async function ClientDPoPPDS(POSTorGET, RequestURL, DPoPNonce, AccessToke
Payload.htm = POSTorGET;
Payload.htu = RequestURL;
Payload.iat = Math.floor(new Date(Date.now()).getTime() / 1000);
Payload.nonce = DPoPNonce;
Payload.ath = await CreatePKCECodeChallenge(AccessToken);
Payload.nonce = Cookie.BlueskyNonceCookie;
Payload.ath = await CreatePKCECodeChallenge(Cookie.BlueskyAccessTokenCookie);
var sHeader = JSON.stringify(Header);
var sPayload = JSON.stringify(Payload);
@ -122,27 +124,27 @@ export async function ClientDPoPPDS(POSTorGET, RequestURL, DPoPNonce, AccessToke
return JWT;
}
export async function HandleAuthorization(BlueskyPKCEverifer, BlueskyPKCEchallenge, BlueskyNonce) {
export async function HandleAuthorization() {
// Declare Variables
let KeyPair = await crypto.subtle.generateKey({name: "ECDSA", namedCurve: "P-256"}, true, ["sign", "verify"]);
let WellKnown = await GetPDSWellKnown();
let PAREndpoint = WellKnown.pushed_authorization_request_endpoint;
let State = crypto.randomUUID();
let PKCEverifier = await CreatePKCECodeVerifier();
let PKCEchallenge = await CreatePKCECodeChallenge(PKCEverifier);
// Save these
Cookie.InputCookie(Cookie.BlueskyPKCEVeriferName, PKCEverifier)
Cookie.InputCookie(Cookie.BlueskyPKCEChallengeName, PKCEchallenge)
// PAR request (beginning)
let PAR = PARrequest(WellKnown.pushed_authorization_request_endpoint, State, PKCEchallenge);
let PAR = PARrequest(WellKnown.pushed_authorization_request_endpoint, State);
let body = await PAR.then((response) => response.json());
let nonce = await PAR.then((response) => response.headers.get("dpop-nonce"));
// Save nonce
Cookie.InputCookie(Cookie.BlueskyNonceName, nonce);
let ExportedKey1 = await crypto.subtle.exportKey("raw", KeyPair.publicKey);
// Save these things.
document.cookie = BlueskyPKCEverifer + "=" + PKCEverifier + ";samesite=strict;path=/;expires=Fri, 31 Dec 9999 23:59:59 GMT;";
document.cookie = BlueskyPKCEchallenge + "=" + PKCEchallenge + ";samesite=strict;path=/;expires=Fri, 31 Dec 9999 23:59:59 GMT;";
document.cookie = BlueskyNonce + "=" + nonce + ";samesite=strict;path=/;expires=Fri, 31 Dec 9999 23:59:59 GMT;";
// Don't remove the keys. They are important.
// if (document.cookie.split("; ").find((row) => row.startsWith(BlueskyPublicKey + "="))?.split("=").length == 1 || document.cookie.split("; ").find((row) => row.startsWith(BlueskyPrivateKey + "="))?.split("=").length == 1) {
// document.cookie = BlueskyPublicKey + "=" + ExportedKey1 + ";samesite=strict;path=/;expires=Fri, 31 Dec 9999 23:59:59 GMT;";
@ -151,18 +153,19 @@ export async function HandleAuthorization(BlueskyPKCEverifer, BlueskyPKCEchallen
document.location.href = "https://bsky.social/oauth/authorize?client_id=https://fedi.crowdedgames.group/oauth/client-metadata.json&request_uri=" + body.request_uri;
}
export async function GainTokens(PKCEcodeName, NonceName, AccessToken, RefreshToken) {
export async function GainTokens() {
let WellKnown = await GetPDSWellKnown();
// Check to see if something's a miss...
if ((document.location.href.split("state=").length > 1 && document.location.href.split("iss=").length > 1 && document.location.href.split("code=").length > 1) && document.cookie.split("; ").find((row) => row.startsWith(PKCEcodeName + "="))?.split("=").length > 1 && document.cookie.split("; ").find((row) => row.startsWith(AccessToken + "="))?.split("=").length == 1) {
if ((document.location.href.split("state=").length > 1 && document.location.href.split("iss=").length > 1 && document.location.href.split("code=").length > 1) && Cookie.IsCookieReal(Cookie.BlueskyPKCEVeriferCookie)) {
// Create varaibles, be aware of waits because of internet.
let DPoP = await ClientDPoPToken("POST", "https://bsky.social/oauth/token", document.cookie.split("; ").find((row) => row.startsWith(NonceName + "="))?.split("=")[1]);
let PKCE = document.cookie.split("; ").find((row) => row.startsWith(PKCEcodeName + "="))?.split("=")[1];
let DPoP = await ClientDPoPToken("POST", WellKnown.token_endpoint);
let code = document.location.href.split("code=")[1];
// Authentication
let Auth = await AuthRequest("https://bsky.social/oauth/token", PKCE, code, DPoP);
let Auth = await AuthRequest(WellKnown.token_endpoint, code, DPoP);
// Save the tokens and be done
document.cookie = AccessToken + "=" + Auth.access_token + ";samesite=strict;path=/;expires=Fri, 31 Dec 9999 23:59:59 GMT;";
document.cookie = RefreshToken + "=" + Auth.refresh_token + ";samesite=strict;path=/;expires=Fri, 31 Dec 9999 23:59:59 GMT;";
Cookie.InputCookie(Cookie.BlueskyAccessTokenName, Auth.access_token);
Cookie.InputCookie(Cookie.BlueskyRefreshTokenName, Auth.refresh_token);
}
}

81
JS/Cookies.js Normal file
View file

@ -0,0 +1,81 @@
// STRINGS TODO: make sure to seperate stuff that a user will want to input. Ex: MastodonWebsiteName, BlueskyPDS, etc.
// Mastodon
export const MastodonWebsiteName = "https://wetdry.world";
export const MastodonClientIDName = "mastodon_client_id";
export const MastodonClientSecretName = "mastodon_client_secret";
export const MastodonAccessTokenName = "mastodon_access_token";
export const MastodonTokenTypeName = "mastodon_token_type";
// Bluesky
export const BlueskyAppName = "https://bsky.app";
export const BlueskyPDSName = "https://bsky.social";
export const BlueskyPKCEVeriferName = "bluesky_pkce_verifier";
export const BlueskyPKCEChallengeName = "bluesky_pkce_challenge";
export const BlueskyPublicKeyName = "bluesky_public_key";
export const BlueskyNonceName = "bluesky_nonce";
export const BlueskyAccessTokenName = "bluesky_access_token";
export const BlueskyRefreshTokenName = "bluesky_refresh_token";
// Tumblr
export const TumblrWebsiteName = "https://www.tumblr.com";
// COOKIES TODO: person inputted stuff (like MastodonWebsiteName) should be implemented.
// Mastodon
export const MastodonClientIDCookie = GetCookie(MastodonClientIDName);
export const MastodonClientSecretCookie = GetCookie(MastodonClientSecretName);
export const MastodonAccessTokenCookie = GetCookie(MastodonAccessTokenName);
export const MastodonTokenTypeCookie = GetCookie(MastodonTokenTypeName);
// Bluesky
export const BlueskyPKCEVeriferCookie = GetCookie(BlueskyPKCEVeriferName);
export const BlueskyPKCEChallengeCookie = GetCookie(BlueskyPKCEChallengeName);
export const BlueskyPublicKeyCookie = GetCookie(BlueskyPublicKeyName);
export const BlueskyNonceCookie = GetCookie(BlueskyNonceName);
export const BlueskyAccessTokenCookie = GetCookie(BlueskyAccessTokenName);
export const BlueskyRefreshTokenCookie = GetCookie(BlueskyRefreshTokenName);
// Tumblr
// None lmao.
// FUNCTIONS
// Get the cookie from cookie storage.
export function GetCookie(CookieName = "") {
// Check if you put in nothing.
if (CookieName == 0) {
console.error("Where is the cookie name? Returning nothing...");
return "";
}
// Get the cookie.
let Cookie = document.cookie.split("; ").find((row) => row.startsWith(CookieName + "="))?.split("=");
// If the cookie doesn't exist...
if (Cookie == undefined || Cookie.length == 1) {
console.log("Cookie not found. Returning nothing...");
return "";
} else {
return Cookie[1];
}
}
// Check if a cookie is real (as in the value isn't nonexistant).
export function IsCookieReal(Cookie = "") {
if (Cookie.length == 0) {
return false;
}
return true;
}
// Remove the cookie.
export function ExpireCookie(CookieName = "") {
document.cookie = CookieName + "=nothing;samesite=strict;path=/;expires=Thu, 01 Jan 1970 00:00:00 GMT;";
return;
}
// Add a new cookie (or change it's value).
export function InputCookie(CookieName = "", Value = "") {
if (Value == 0 || CookieName == 0) {
console.error("You forgot to put in a value. Stopping...");
return;
}
document.cookie = CookieName + "=" + Value + ";samesite=strict;path=/;expires=Fri, 31 Dec 9999 23:59:59 GMT;"
return;
}

View file

@ -1,3 +1,5 @@
import * as Cookie from "./Cookies.js";
export const Scopes = "read write follow push";
// Gets the public timeline.
@ -22,49 +24,43 @@ export async function GetPublicTimeline(Local = false, Remote = false, Website)
}
// Gets the favorites of a user that you have access to.
export async function GetFavorites(Website, MastodonAccessToken, MastodonTokenType) {
export async function GetFavorites(Website) {
let Favorites;
// Check for a token.
if (document.cookie.split("; ").find((row) => row.startsWith(MastodonAccessToken + "="))?.split("=").length > 1) {
if (Cookie.IsCookieReal(Cookie.MastodonAccessTokenCookie)) {
// Get the varaibles that are stored in cookies.
let AccessToken = document.cookie.split("; ").find((row) => row.startsWith(MastodonAccessToken + "="))?.split("=")[1];
let TokenType = document.cookie.split("; ").find((row) => row.startsWith(MastodonTokenType + "="))?.split("=")[1];
Favorites = await fetch(Website + "/api/v1/favourites", {method: "GET", headers: {"Authorization": TokenType + " " + AccessToken}})
Favorites = await fetch(Website + "/api/v1/favourites", {method: "GET", headers: {"Authorization": Cookie.MastodonTokenTypeCookie + " " + Cookie.MastodonAccessTokenCookie}})
.then((response) => response.json());
}
return Favorites;
}
// Gets the bookmarks of a user that you have access to.
export async function GetBookmarks(Website, MastodonAccessToken, MastodonTokenType) {
export async function GetBookmarks(Website) {
let Bookmarks;
// Check for a token.
if (document.cookie.split("; ").find((row) => row.startsWith(MastodonAccessToken + "="))?.split("=").length > 1) {
if (Cookie.IsCookieReal(Cookie.MastodonAccessTokenCookie)) {
// Get the varaibles that are stored in cookies.
let AccessToken = document.cookie.split("; ").find((row) => row.startsWith(MastodonAccessToken + "="))?.split("=")[1];
let TokenType = document.cookie.split("; ").find((row) => row.startsWith(MastodonTokenType + "="))?.split("=")[1];
Bookmarks = await fetch(Website + "/api/v1/bookmarks", {method: "GET", headers: {"Authorization": TokenType + " " + AccessToken}})
Bookmarks = await fetch(Website + "/api/v1/bookmarks", {method: "GET", headers: {"Authorization": Cookie.MastodonTokenTypeCookie + " " + Cookie.MastodonAccessTokenCookie}})
.then((response) => response.json());
}
return Bookmarks;
}
// Gets the notifications of a user that you have access to.
export async function GetNotifications(Website, MastodonAccessToken, MastodonTokenType) {
export async function GetNotifications(Website) {
let Notifications;
// Check for a token.
if (document.cookie.split("; ").find((row) => row.startsWith(MastodonAccessToken + "="))?.split("=").length > 1) {
// Get the varaibles that are stored in cookies.
let AccessToken = document.cookie.split("; ").find((row) => row.startsWith(MastodonAccessToken + "="))?.split("=")[1];
let TokenType = document.cookie.split("; ").find((row) => row.startsWith(MastodonTokenType + "="))?.split("=")[1];
Notifications = await fetch(Website + "/api/v1/notifications", {method: "GET", headers: {"Authorization": TokenType + " " + AccessToken}})
if (Cookie.IsCookieReal(Cookie.MastodonAccessTokenCookie)) {
// Get the varaibles that are stored in cookies and then input it.
Notifications = await fetch(Website + "/api/v1/notifications", {method: "GET", headers: {"Authorization": Cookie.MastodonTokenTypeCookie + " " + Cookie.MastodonAccessTokenCookie}})
.then((response) => response.json());
}
return Notifications;
}
// The first step to using the app.
export async function HandleAuthentication(Website, CookieClientID, CookieClientSecret) {
export async function HandleAuthentication(Website) {
// See if the user is smart enough to put https.
let InstanceData = "";
// Quickly check to see if it has something before :// so it doesn't screw the link.
@ -77,24 +73,22 @@ export async function HandleAuthentication(Website, CookieClientID, CookieClient
InstanceData = await fetch(Website + "/api/v1/apps?client_name=Channel Viewer&redirect_uris=" + document.location.href + "&scopes=" + Scopes, {method: "POST"})
.then((response) => response.json());
// Save the client stuff as cookies.
document.cookie = CookieClientID + "=" + InstanceData.client_id + ";samesite=strict;path=/;expires=Fri, 31 Dec 9999 23:59:59 GMT;";
document.cookie = CookieClientSecret + "=" + InstanceData.client_secret + ";samesite=strict;path=/;expires=Fri, 31 Dec 9999 23:59:59 GMT;";
Cookie.InputCookie(Cookie.MastodonClientIDName, InstanceData.client_id);
Cookie.InputCookie(Cookie.MastodonClientSecretName, InstanceData.client_secret);
// Now authenticate the app.
document.location.href = Website + "/oauth/authorize?client_id=" + InstanceData.client_id + "&redirect_uri=" + document.location.href + "&response_type=code&scope=" + Scopes;
}
// This specific functino goes after HandleAuthentication for when login happens.
export async function GainToken(Website, CookieClientID, CookieClientSecret, CookieAccessToken, CookieTokenType) {
export async function GainToken(Website) {
// check if you both have a code and have a current authentication.
if (document.location.href.split("code=").length > 1 && document.cookie.split("; ").find((row) => row.startsWith(CookieClientID + "="))?.split("=").length > 1) {
if (document.location.href.split("code=").length > 1 && Cookie.IsCookieReal(Cookie.MastodonClientIDCookie)) {
let code = document.location.href.split("code=")[1];
let ClientID = document.cookie.split("; ").find((row) => row.startsWith(CookieClientID + "="))?.split("=")[1];
let ClientSecret = document.cookie.split("; ").find((row) => row.startsWith(CookieClientSecret + "="))?.split("=")[1];
let AuthenticationToken = await fetch(Website + "/oauth/token?client_id=" + ClientID + "&client_secret=" + ClientSecret + "&redirect_uri=" + document.location.href + "&grant_type=authorization_code&code=" + code, {method: "POST"})
let AuthenticationToken = await fetch(Website + "/oauth/token?client_id=" + Cookie.MastodonClientIDCookie + "&client_secret=" + Cookie.MastodonClientSecretCookie + "&redirect_uri=" + document.location.href + "&grant_type=authorization_code&code=" + code, {method: "POST"})
.then((response) => response.json());
// Cookify These
document.cookie = CookieAccessToken + "=" + AuthenticationToken.access_token + ";samesite=strict;path=/;expires=Fri, 31 Dec 9999 23:59:59 GMT;";
document.cookie = CookieTokenType + "=" + AuthenticationToken.token_type + ";samesite=strict;path=/;expires=Fri, 31 Dec 9999 23:59:59 GMT;";
Cookie.InputCookie(Cookie.MastodonAccessTokenName, AuthenticationToken.access_token);
Cookie.InputCookie(Cookie.MastodonTokenTypeName, AuthenticationToken.token_type);
}
}

View file

@ -1,4 +1,7 @@
import * as MastodonAPI from "./MastodonAPI.js";
import * as BlueskyAPI from "./BlueskyAPI.js";
import * as TumblrAPI from "./TumblrAPI.js";
import * as Cookie from "./Cookies.js";
// GLOBAL VARS
// fuck you. I see why website developers use divs so fucking often.
@ -18,9 +21,6 @@ const ButtonSound = new Audio("Audio/button-305770.mp3");
const WarningClick = new Audio("Audio/button-pressed-38129.mp3");
const BackgroundMusic = new Audio("Audio/soft-piano-music-312509.mp3");
// Websites
let MastodonWebsite = "https://wetdry.world";
// Update a timer
function UpdateTime() {
let TimeNow = new Date();
@ -104,12 +104,12 @@ ArrowsButton[0].onclick = (event) => {
// MastodonAPI integration
async function PosterContainerUpdate() {
// Cookies for the public timelines
let LocalCookie = document.cookie.split("; ").find((row) => row.startsWith("Local="))?.split("=")[1];
var LocalTrue = (LocalCookie === 'true');
let RemoteCookie = document.cookie.split("; ").find((row) => row.startsWith("Remote="))?.split("=")[1];
var RemoteTrue = (RemoteCookie === 'true');
let LocalCookie = Cookie.GetCookie("Local");
var LocalTrue = (LocalCookie === "true");
let RemoteCookie = Cookie.GetCookie("Remote");
var RemoteTrue = (RemoteCookie === "true");
let Timeline = await MastodonAPI.GetPublicTimeline(LocalTrue, RemoteTrue, MastodonWebsite);
let Timeline = await MastodonAPI.GetPublicTimeline(LocalTrue, RemoteTrue, Cookie.MastodonWebsiteName);
let Content = [];
let Users = [];
for (let i in Timeline) {

View file

@ -1,26 +1,11 @@
import * as MastodonAPI from "./MastodonAPI.js";
import * as BlueskyAPI from "./BlueskyAPI.js";
import * as TumblrAPI from "./TumblrAPI.js";
// 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 BlueskyPublicKey = "bluesky_public_key";
let BlueskyNonce = "bluesky_nonce";
import * as Cookie from "./Cookies.js";
// Below is the thing it populates if you login.
async function PopulateFavorites() {
let Favorites = await MastodonAPI.GetFavorites(MastodonWebsite, MastodonAccessToken, MastodonTokenType);
let Favorites = await MastodonAPI.GetFavorites(Cookie.MastodonWebsiteName);
let FavoritesArea = document.getElementsByClassName("Favorites")[0];
// Populate the favorites area.
@ -31,7 +16,7 @@ async function PopulateFavorites() {
}
async function PopulateBookmarks() {
let Bookmarks = await MastodonAPI.GetBookmarks(MastodonWebsite, MastodonAccessToken, MastodonTokenType);
let Bookmarks = await MastodonAPI.GetBookmarks(Cookie.MastodonWebsiteName);
let BookmarksArea = document.getElementsByClassName("Bookmarks")[0];
// Populate the Bookmarks area.
@ -42,7 +27,7 @@ async function PopulateBookmarks() {
}
async function PopulateNotifications() {
let Notifications = await MastodonAPI.GetNotifications(MastodonWebsite, MastodonAccessToken, MastodonTokenType);
let Notifications = await MastodonAPI.GetNotifications(Cookie.MastodonWebsiteName);
let NotificationsArea = document.getElementsByClassName("Notifications")[0];
// Populate the Conversations area.

View file

@ -1,6 +1,7 @@
import * as MastodonAPI from "./MastodonAPI.js";
import * as BlueskyAPI from "./BlueskyAPI.js";
import * as TumblrAPI from "./TumblrAPI.js";
import * as Cookie from "./Cookies.js";
// Settings buttons
let LocalButton = document.getElementsByClassName("Local")[0];
@ -13,96 +14,79 @@ 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 BlueskyPublicKey = "bluesky_public_key";
let BlueskyNonce = "bluesky_nonce";
let BlueskyAccessToken = "bluesky_access_token";
let BlueskyRefreshToken = "bluesky_refresh_token";
// 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;";
if (Cookie.IsCookieReal(GetCookie("Local"))) {
Cookie.ExpireCookie(GetCookie("Local"));
} else {
document.cookie = "Local=true;samesite=strict;path=/;";
Cookie.InputCookie("Local", "true");
}
}
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;";
if (Cookie.IsCookieReal(GetCookie("Remote"))) {
Cookie.ExpireCookie(GetCookie("Remote"));
} else {
document.cookie = "Remote=true;samesite=strict;path=/;";
Cookie.InputCookie("Remote", "true");
}
}
// Mastodon buttons
// Login
MastodonLoginButton.onclick = (event) => {
if (MastodonWebInput != "") {
MastodonAPI.HandleAuthentication(MastodonWebsite, MastodonClientID, MastodonClientSecret);
MastodonAPI.HandleAuthentication(Cookie.MastodonWebsiteName);
}
}
// Logout
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;";
Cookie.ExpireCookie(Cookie.MastodonClientIDName);
Cookie.ExpireCookie(Cookie.MastodonClientSecretName);
Cookie.ExpireCookie(Cookie.MastodonAccessTokenName);
Cookie.ExpireCookie(Cookie.MastodonTokenTypeName);
document.location.href = document.location.href;
}
// Bluesky Buttons
// Login
BlueskyLoginButton.onclick = (event) => {
if (BlueskyWebInput != "") {
BlueskyAPI.HandleAuthorization(BlueskyPKCEverifer, BlueskyPKCEchallenge, BlueskyNonce);
BlueskyAPI.HandleAuthorization();
}
}
// Logout
BlueskyLogoutButton.onclick = (event) => {
document.cookie = BlueskyPKCEverifer + "=nothing;" + ";samesite=strict;path=/;expires=Thu, 01 Jan 1970 00:00:00 GMT;";
document.cookie = BlueskyPKCEchallenge + "=nothing;" + ";samesite=strict;path=/;expires=Thu, 01 Jan 1970 00:00:00 GMT;";
document.cookie = BlueskyNonce + "=nothing;" + ";samesite=strict;path=/;expires=Thu, 01 Jan 1970 00:00:00 GMT;";
Cookie.ExpireCookie(Cookie.BlueskyPKCEVeriferName);
Cookie.ExpireCookie(Cookie.BlueskyPKCEChallengeName);
Cookie.ExpireCookie(Cookie.BlueskyNonceName);
document.location.href = document.location.href;
}
// if an access token is found, login.
function CheckLogin() {
async 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) {
if (Cookie.IsCookieReal(Cookie.MastodonAccessTokenCookie)) {
// Swap the buttons
MastodonLoginButton.remove();
MastodonWebInput.remove();
MastodonLogoutButton.setAttribute("style", "");
} else {
// Auto log in
await MastodonAPI.GainToken(MastodonWebsite, MastodonClientID, MastodonClientSecret, MastodonAccessToken, MastodonTokenType);
MastodonAPI.GainToken(Cookie.MastodonWebsiteName);
}
// Check for a bluesky token.
if ((document.location.href.split("state=").length == 1 && document.location.href.split("iss=").length == 1 && document.location.href.split("code=").length == 1) && document.cookie.split("; ").find((row) => row.startsWith(BlueskyAccessToken + "="))?.split("=").length > 1) {
if (Cookie.IsCookieReal(Cookie.BlueskyAccessTokenCookie)) {
// Swap the buttons
BlueskyLoginButton.remove();
BlueskyWebInput.remove();
BlueskyLogoutButton.setAttribute("style", "");
} else {
// Auto log in
await BlueskyAPI.GainTokens(BlueskyPKCEverifer, BlueskyNonce, BlueskyAccessToken, BlueskyRefreshToken);
BlueskyAPI.GainTokens();
}
}
@ -111,4 +95,4 @@ function CheckLogin() {
CheckLogin();
// TESTING!
BlueskyAPI.GetBlueskyDID(BlueskyPDS, "crowdedgames.group", document.cookie.split("; ").find((row) => row.startsWith(BlueskyAccessToken + "="))?.split("=")[1]);
BlueskyAPI.GetBlueskyDID(Cookie.BlueskyPDSName, "crowdedgames.group", Cookie.BlueskyAccessTokenCookie);