Skip to content

Commit

Permalink
Fix body-encoding not being used
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieudutour committed Jun 20, 2024
1 parent 5e65297 commit bb8964f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 deletions.
4 changes: 4 additions & 0 deletions docs/utils-reference/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ npm install --save @raycast/utils

## Changelog

### v1.16.1

- Fixed an issue where `bodyEncoding` wasn't properly used in OAuthService.

### v1.16.0

- Add a `failureToastOptions` prop to `useFetch`, `useCachedPromise`, and `usePromise` to make it possible to customize the error displayed instead of a generic "Failed to fetch latest data".
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@raycast/utils",
"version": "1.16.0",
"version": "1.16.1",
"description": "Set of utilities to streamline building Raycast extensions",
"author": "Raycast Technologies Ltd.",
"homepage": "https://developers.raycast.com/utils-reference",
Expand Down
39 changes: 17 additions & 22 deletions src/oauth/OAuthService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ export interface OAuthServiceOptions {
tokenRefreshResponseParser?: (response: unknown) => OAuth.TokenResponse;
}

type FetchTokensArgs = { authRequest: OAuth.AuthorizationRequest; authorizationCode: string } & Pick<
OAuthServiceOptions,
"clientId" | "tokenUrl" | "bodyEncoding"
>;

type RefreshTokensArgs = { token: string } & Pick<OAuthServiceOptions, "clientId" | "tokenUrl" | "bodyEncoding">;

/**
* Class allowing to create an OAuth service using the the PKCE (Proof Key for Code Exchange) flow.
*
Expand Down Expand Up @@ -182,9 +175,7 @@ export class OAuthService implements OAuthServiceOptions {
if (currentTokenSet.refreshToken && currentTokenSet.isExpired()) {
const tokens = await this.refreshTokens({
token: currentTokenSet.refreshToken,
clientId: this.clientId,
tokenUrl: this.refreshTokenUrl ?? this.tokenUrl,
})
});
await this.client.setTokens(tokens);
return tokens.access_token;
}
Expand All @@ -202,20 +193,24 @@ export class OAuthService implements OAuthServiceOptions {
const tokens = await this.fetchTokens({
authRequest,
authorizationCode,
clientId: this.clientId,
tokenUrl: this.tokenUrl,
});

await this.client.setTokens(tokens);

return tokens.access_token;
}

private async fetchTokens({ authRequest, authorizationCode, clientId, tokenUrl, bodyEncoding }: FetchTokensArgs) {
private async fetchTokens({
authRequest,
authorizationCode,
}: {
authRequest: OAuth.AuthorizationRequest;
authorizationCode: string;
}) {
let options;
if (bodyEncoding === "url-encoded") {
if (this.bodyEncoding === "url-encoded") {
const params = new URLSearchParams();
params.append("client_id", clientId);
params.append("client_id", this.clientId);
params.append("code", authorizationCode);
params.append("code_verifier", authRequest.codeVerifier);
params.append("grant_type", "authorization_code");
Expand All @@ -225,7 +220,7 @@ export class OAuthService implements OAuthServiceOptions {
} else {
options = {
body: JSON.stringify({
client_id: clientId,
client_id: this.clientId,
code: authorizationCode,
code_verifier: authRequest.codeVerifier,
grant_type: "authorization_code",
Expand All @@ -235,7 +230,7 @@ export class OAuthService implements OAuthServiceOptions {
};
}

const response = await fetch(tokenUrl, { method: "POST", ...options });
const response = await fetch(this.tokenUrl, { method: "POST", ...options });
if (!response.ok) {
const responseText = await response.text();
console.error("fetch tokens error:", responseText);
Expand All @@ -247,27 +242,27 @@ export class OAuthService implements OAuthServiceOptions {
return Array.isArray(tokens.scope) ? { ...tokens, scope: tokens.scope.join(" ") } : tokens;
}

private async refreshTokens({ token, clientId, tokenUrl, bodyEncoding }: RefreshTokensArgs) {
private async refreshTokens({ token }: { token: string }) {
let options;
if (bodyEncoding === "url-encoded") {
if (this.bodyEncoding === "url-encoded") {
const params = new URLSearchParams();
params.append("client_id", clientId);
params.append("client_id", this.clientId);
params.append("refresh_token", token);
params.append("grant_type", "refresh_token");

options = { body: params };
} else {
options = {
body: JSON.stringify({
client_id: clientId,
client_id: this.clientId,
refresh_token: token,
grant_type: "refresh_token",
}),
headers: { "Content-Type": "application/json" },
};
}

const response = await fetch(tokenUrl, { method: "POST", ...options });
const response = await fetch(this.refreshTokenUrl ?? this.tokenUrl, { method: "POST", ...options });
if (!response.ok) {
const responseText = await response.text();
console.error("refresh tokens error:", responseText);
Expand Down

0 comments on commit bb8964f

Please sign in to comment.