Add debug logs for oauth plugin

This commit is contained in:
Gregory Schier
2025-06-03 09:27:54 -07:00
parent 9e68e276a1
commit 6eb16afd96
5 changed files with 26 additions and 7 deletions

View File

@@ -24,7 +24,7 @@ export async function getAccessToken(
params: HttpUrlParameter[];
},
): Promise<AccessTokenRawResponse> {
console.log('Getting access token', accessTokenUrl);
console.log('[oauth2] Getting access token', accessTokenUrl);
const httpRequest: Partial<HttpRequest> = {
method: 'POST',
url: accessTokenUrl,

View File

@@ -68,7 +68,7 @@ export async function getOrRefreshAccessToken(ctx: Context, contextId: string, {
if (resp.status === 401) {
// Bad refresh token, so we'll force it to fetch a fresh access token by deleting
// and returning null;
console.log('Unauthorized refresh_token request');
console.log('[oauth2] Unauthorized refresh_token request');
await deleteToken(ctx, contextId);
return null;
}

View File

@@ -70,7 +70,8 @@ export async function getAuthorizationCode(
return new Promise(async (resolve, reject) => {
const authorizationUrlStr = authorizationUrl.toString();
console.log('Authorizing', authorizationUrlStr);
const logsEnabled = (await ctx.store.get('enable_logs')) ?? false;
console.log('[oauth2] Authorizing', authorizationUrlStr);
let foundCode = false;
@@ -85,11 +86,15 @@ export async function getAuthorizationCode(
},
async onNavigate({ url: urlStr }) {
const url = new URL(urlStr);
if (logsEnabled) console.log('[oauth2] Navigated to', urlStr);
if (url.searchParams.has('error')) {
return reject(new Error(`Failed to authorize: ${url.searchParams.get('error')}`));
}
const code = url.searchParams.get('code');
if (!code) {
console.log('[oauth2] Code not found');
return; // Could be one of many redirects in a chain, so skip it
}
@@ -97,6 +102,7 @@ export async function getAuthorizationCode(
foundCode = true;
close();
console.log('[oauth2] Code found');
const response = await getAccessToken(ctx, {
grantType: 'authorization_code',
accessTokenUrl,

View File

@@ -53,6 +53,7 @@ const authorizationUrls = [
'https://www.dropbox.com/oauth2/authorize',
'https://www.linkedin.com/oauth/v2/authorization',
'https://MY_SHOP.myshopify.com/admin/oauth/access_token',
'https://appcenter.intuit.com/app/connect/oauth2/authorize',
];
const accessTokenUrls = [
@@ -69,6 +70,7 @@ const accessTokenUrls = [
'https://www.googleapis.com/oauth2/v4/token',
'https://www.linkedin.com/oauth/v2/accessToken',
'https://MY_SHOP.myshopify.com/admin/oauth/authorize',
'https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer',
];
export const plugin: PluginDefinition = {
@@ -109,6 +111,17 @@ export const plugin: PluginDefinition = {
await resetDataDirKey(ctx, contextId);
},
},
{
label: 'Toggle Debug Logs',
async onSelect(ctx) {
const enableLogs = await ctx.store.get('enable_logs');
await ctx.store.set('enable_logs', !enableLogs);
await ctx.toast.show({
message: `Debug logs ${enableLogs ? 'enabled' : 'disabled'}`,
color: 'info',
});
},
},
],
args: [
{

View File

@@ -37,12 +37,12 @@ export async function getDataDirKey(ctx: Context, contextId: string) {
return `${contextId}::${key}`;
}
function tokenStoreKey(context_id: string) {
return ['token', context_id].join('::');
function tokenStoreKey(contextId: string) {
return ['token', contextId].join('::');
}
function dataDirStoreKey(context_id: string) {
return ['data_dir', context_id].join('::');
function dataDirStoreKey(contextId: string) {
return ['data_dir', contextId].join('::');
}
export interface AccessToken {