raycast-akkoma-extension/src/oauth.ts
2023-04-14 21:37:51 +08:00

70 lines
2.3 KiB
TypeScript

import { OAuth, getPreferenceValues } from "@raycast/api";
import { Preference } from "./types";
import { fetchToken,createApp} from "./api";
export const client = new OAuth.PKCEClient({
redirectMethod: OAuth.RedirectMethod.Web,
providerName: "Akkoma",
providerIcon: "akkoma-icon.png",
providerId: "akkoma",
description: "Connect to your Akkoma / Pleroma acount",
});
const requestAccessToken = async (
clientId: string,
clientSecret: string,
authRequest: OAuth.AuthorizationRequest,
authCode: string
): Promise<OAuth.TokenResponse> => {
const params = new URLSearchParams();
params.append("client_id", clientId);
params.append("client_secret", clientSecret);
params.append("code", authCode);
params.append("code_verifier", authRequest.codeVerifier);
params.append("grant_type", "authorization_code");
params.append("redirect_uri", authRequest.redirectURI);
return await fetchToken(params, "fetch tokens error:");
};
const refreshToken = async (
clientId: string,
clientSecret: string,
refreshToken: string
): Promise<OAuth.TokenResponse> => {
const params = new URLSearchParams();
params.append("client_id", clientId);
params.append("client_secret", clientSecret);
params.append("refresh_token", refreshToken);
params.append("grant_type", "refresh_token");
const tokenResponse = await fetchToken(params, "refresh tokens error:");
tokenResponse.refresh_token = tokenResponse.refresh_token ?? refreshToken;
return tokenResponse;
};
export const authorize = async (): Promise<void> => {
const { instance } = getPreferenceValues<Preference>();
const tokenSet = await client.getTokens();
if (tokenSet?.accessToken) {
if (tokenSet.refreshToken && tokenSet.isExpired()) {
const { client_id, client_secret } = await createApp();
await client.setTokens(await refreshToken(client_id, client_secret, tokenSet.refreshToken));
}
return;
}
const { client_id, client_secret } = await createApp();
const authRequest = await client.authorizationRequest({
endpoint: `https://${instance}/oauth/authorize`,
clientId: client_id,
scope: "read write",
});
const { authorizationCode } = await client.authorize(authRequest);
await client.setTokens(await requestAccessToken(client_id, client_secret, authRequest, authorizationCode));
};