Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not restart auth flow on failed auth callback #389

Merged
merged 3 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/great-geese-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@osdk/legacy-client": patch
---

Do not restart auth flow on failed auth callback
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {

export class PublicClientAuth implements Auth {
private palantirRefreshToken = "palantir_refresh_token" as const;
private palantirPcke = "palantir_pcke" as const;
private palantirPkce = "palantir_pkce" as const;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rename here and the move from local to session storage does leave behind old values but given that this state should only live for the lifetime of one auth flow there shouldn't be a risk of breaking a user's login


private nextSubscriberId = 0;
private onSignInSubscribers: { [id: string]: () => void } = {};
Expand Down Expand Up @@ -119,16 +119,16 @@ export class PublicClientAuth implements Auth {
}

// 2. If there is no refresh token we are likely trying to perform the callback
const pcke = localStorage.getItem(this.palantirPcke);
if (pcke && shouldMakeAuthRequest) {
const pkce = sessionStorage.getItem(this.palantirPkce);
if (pkce && shouldMakeAuthRequest) {
try {
const callbackUrl = window.location.href;
this.token = await getTokenWithCodeVerifier(
this.options.clientId,
this.options.redirectUrl,
callbackUrl,
this.options.url,
pcke,
pkce,
this.options.fetchFn,
this.options.multipassContextPath,
);
Expand All @@ -142,10 +142,11 @@ export class PublicClientAuth implements Auth {
} catch (e) {
// eslint-disable-next-line no-console
console.warn(
"Failed to get OAuth2 token using PCKE, removing PCKE and starting a new auth flow",
"Failed to get OAuth2 token using PKCE, removing PKCE",
e,
);
localStorage.removeItem(this.palantirPcke);
sessionStorage.removeItem(this.palantirPkce);
throw e;
}
}

Expand All @@ -158,7 +159,7 @@ export class PublicClientAuth implements Auth {
this.options.multipassContextPath,
this.options.scopes,
);
localStorage.setItem(this.palantirPcke, authorizeRequest.codeVerifier);
sessionStorage.setItem(this.palantirPkce, authorizeRequest.codeVerifier);
window.location.href = authorizeRequest.url;

// Give time for redirect to happen
Expand Down Expand Up @@ -216,7 +217,7 @@ export class PublicClientAuth implements Auth {
this.clearRefreshTimeout();

// Clean up local storage
localStorage.removeItem(this.palantirPcke);
sessionStorage.removeItem(this.palantirPkce);
localStorage.removeItem(this.palantirRefreshToken);

// Remove all references to this token
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ export async function getTokenWithCodeVerifier(
multipassContextPath?: string,
): Promise<OAuthToken> {
const parsedUrl = new URLSearchParams(url.substring(url.indexOf("?")));

const error = parsedUrl.get("error");
if (error) {
throw new Error("Error received from server: " + error);
}

const code = parsedUrl.get("code");
if (!code) {
throw new Error("Code parameter missing in URL: " + url);
Copy link
Contributor Author

@tzyl tzyl Jun 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we receive the callback with the error in the url query param then we would have failed here in the past so I
added the throw before this to include the actual error if it's present. In the default template the user will at least see the real reason rather than the cryptic code parameter missing in URL:
image

Expand Down
2 changes: 1 addition & 1 deletion packages/oauth/src/createPublicOauthClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ export function createPublicOauthClient(
if (process?.env?.NODE_ENV !== "production") {
// eslint-disable-next-line no-console
console.warn(
"Failed to get OAuth2 token using PCKE, removing PCKE and starting a new auth flow",
"Failed to get OAuth2 token using PKCE, removing PKCE and starting a new auth flow",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the 2.0 oauth is a bit out of sync now but applied this rename across the repo

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

e,
);
}
Expand Down