revert: refreshToken & requestAccessToken function

This commit is contained in:
sevichecc 2023-04-16 01:34:52 +08:00
parent a99e023829
commit 19263dd46e
Signed by untrusted user who does not match committer: SevicheCC
GPG key ID: C577000000000000
2 changed files with 24 additions and 26 deletions

View file

@ -10,33 +10,37 @@ export const client = new OAuth.PKCEClient({
description: "Connect to your Akkoma / Pleroma acount", description: "Connect to your Akkoma / Pleroma acount",
}); });
const requestToken = async ( const requestAccessToken = async (
clientId: string, clientId: string,
clientSecret: string, clientSecret: string,
grantType: string, authRequest: OAuth.AuthorizationRequest,
authRequest?: OAuth.AuthorizationRequest, authCode: string
authCode?: string,
refreshToken?: string
): Promise<OAuth.TokenResponse> => { ): Promise<OAuth.TokenResponse> => {
const params = new URLSearchParams(); const params = new URLSearchParams();
params.append("client_id", clientId); params.append("client_id", clientId);
params.append("client_secret", clientSecret); params.append("client_secret", clientSecret);
params.append("grant_type", grantType); params.append("code", authCode);
params.append("code_verifier", authRequest.codeVerifier);
params.append("grant_type", "authorization_code");
params.append("redirect_uri", authRequest.redirectURI);
if (grantType === "authorization_code") { return await apiServer.fetchToken(params, "fetch tokens error:");
params.append("code", authCode!); };
params.append("code_verifier", authRequest!.codeVerifier);
params.append("redirect_uri", authRequest!.redirectURI);
} else {
params.append("refresh_token", refreshToken!);
}
const tokenResponse = await apiServer.fetchToken(params, `Error while requesting ${grantType} tokens:`); 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");
if (grantType === "refresh_token") { const tokenResponse = await apiServer.fetchToken(params, "refresh tokens error:");
tokenResponse.refresh_token = tokenResponse.refresh_token ?? refreshToken;
}
tokenResponse.refresh_token = tokenResponse.refresh_token ?? refreshToken;
return tokenResponse; return tokenResponse;
}; };
@ -48,9 +52,7 @@ export const authorize = async (): Promise<void> => {
if (tokenSet.refreshToken && tokenSet.isExpired()) { if (tokenSet.refreshToken && tokenSet.isExpired()) {
LocalStorage.clear(); LocalStorage.clear();
const { client_id, client_secret } = await apiServer.createApp(); const { client_id, client_secret } = await apiServer.createApp();
await client.setTokens( await client.setTokens(await refreshToken(client_id, client_secret, tokenSet.refreshToken));
await requestToken(client_id, client_secret, "refresh_token", undefined, undefined, tokenSet.refreshToken)
);
} }
return; return;
} }
@ -63,13 +65,9 @@ export const authorize = async (): Promise<void> => {
}); });
const { authorizationCode } = await client.authorize(authRequest); const { authorizationCode } = await client.authorize(authRequest);
await client.setTokens( await client.setTokens(await requestAccessToken(client_id, client_secret, authRequest, authorizationCode));
await requestToken(client_id, client_secret, "authorization_code", authRequest, authorizationCode)
);
const { fqn, avatar_static } = await apiServer.fetchAccountInfo(); const { fqn, avatar_static } = await apiServer.fetchAccountInfo();
await LocalStorage.setItem("account-fqn", fqn); await LocalStorage.setItem("account-fqn", fqn);
await LocalStorage.setItem("account-avator", avatar_static); await LocalStorage.setItem("account-avator", avatar_static);
}; };
export default { authorize };

View file

@ -1,4 +1,4 @@
import type { Icon, LaunchProps } from "@raycast/api"; import type { Icon } from "@raycast/api";
export type VisibilityScope = "public" | "unlisted" | "direct" | "private" | "local"; export type VisibilityScope = "public" | "unlisted" | "direct" | "private" | "local";