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

feat: handle auth flow natively #2869

Merged
merged 7 commits into from
Dec 6, 2023
Merged
Changes from 3 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
72 changes: 51 additions & 21 deletions src/extension/background-script/connectors/alby.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,9 @@ export default class Alby implements Connector {
throw new Error("OAuth client credentials missing");
}

const redirectURL = browser.identity.getRedirectURL();
const redirectURL =
"https://9dfeeffab3456fdb4be6e2296ca1a4c6b124ee94.extensions.allizom.org/";

const authClient = new auth.OAuth2User({
request_options: this._getRequestOptions(),
client_id: clientId,
Expand All @@ -259,9 +261,7 @@ export default class Alby implements Connector {
authClient.on("tokenRefreshed", (token: Token) => {
this._updateOAuthToken(token);
});
// Currently the JS SDK guarantees request of a new refresh token is done synchronously.
pavanjoshi914 marked this conversation as resolved.
Show resolved Hide resolved
// The only way a refresh should fail is if the refresh token has expired, which is handled when the connector is initialized.
// If a token refresh fails after init then the connector will be unusable, but we will still log errors here so that this can be debugged if it does ever happen.

authClient.on("tokenRefreshFailed", (error: Error) => {
console.error("Failed to Refresh token", error);
});
Expand All @@ -273,8 +273,6 @@ export default class Alby implements Connector {
}
return authClient;
} catch (error) {
// if auth token refresh fails, the refresh token has probably expired or is invalid
// the user will be asked to re-login
console.error("Failed to request new auth token", error);
}
}
Expand All @@ -285,28 +283,60 @@ export default class Alby implements Connector {
});

authUrl += "&webln=false"; // stop getalby.com login modal launching lnurl auth
const authResult = await this.launchWebAuthFlow(authUrl);
const code = new URL(authResult).searchParams.get("code");
if (!code) {
throw new Error("Authentication failed: missing authResult");
}

const token = await authClient.requestAccessToken(code);
await this._updateOAuthToken(token.token);
return authClient;
const oAuthTab = await browser.tabs.create({ url: authUrl });

return new Promise<auth.OAuth2User>((resolve, reject) => {
const handleUpdated = (
tabId: number,
changeInfo: browser.Tabs.OnUpdatedChangeInfoType,
tab: browser.Tabs.Tab
) => {
if (changeInfo.status === "complete" && tabId === oAuthTab.id) {
const authorizationCode = this.extractCodeFromTabUrl(tab.url);

if (authorizationCode) {
pavanjoshi914 marked this conversation as resolved.
Show resolved Hide resolved
authClient
.requestAccessToken(authorizationCode)
.then((token) => {
this._updateOAuthToken(token.token);
resolve(authClient);
})
.catch((error) => {
console.error("Failed to request new auth token", error);
reject(error);
})
.finally(() => {
browser.tabs.remove(tabId);
browser.tabs.onUpdated.removeListener(handleUpdated);
});
}
}
};
const handleRemoved = (tabId: number) => {
pavanjoshi914 marked this conversation as resolved.
Show resolved Hide resolved
if (tabId === oAuthTab.id) {
// The user closed the authentication tab without completing the flow
const error = new Error("OAuth authentication canceled by user");
reject(error);
browser.tabs.onRemoved.removeListener(handleRemoved);
}
};

browser.tabs.onUpdated.addListener(handleUpdated);
browser.tabs.onRemoved.addListener(handleRemoved);
});
} catch (error) {
console.error(error);
throw error;
}
}

async launchWebAuthFlow(authUrl: string) {
const authResult = await browser.identity.launchWebAuthFlow({
interactive: true,
url: authUrl,
});

return authResult;
private extractCodeFromTabUrl(url: string | undefined): string | null {
if (!url) {
return null;
}
const urlSearchParams = new URLSearchParams(url.split("?")[1]);
return urlSearchParams.get("code") || null;
}

private async _request<T>(func: (client: Client) => T) {
Expand Down
Loading