mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-23 09:18:30 +02:00
Add debug logs for oauth plugin
This commit is contained in:
@@ -24,7 +24,7 @@ export async function getAccessToken(
|
|||||||
params: HttpUrlParameter[];
|
params: HttpUrlParameter[];
|
||||||
},
|
},
|
||||||
): Promise<AccessTokenRawResponse> {
|
): Promise<AccessTokenRawResponse> {
|
||||||
console.log('Getting access token', accessTokenUrl);
|
console.log('[oauth2] Getting access token', accessTokenUrl);
|
||||||
const httpRequest: Partial<HttpRequest> = {
|
const httpRequest: Partial<HttpRequest> = {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: accessTokenUrl,
|
url: accessTokenUrl,
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ export async function getOrRefreshAccessToken(ctx: Context, contextId: string, {
|
|||||||
if (resp.status === 401) {
|
if (resp.status === 401) {
|
||||||
// Bad refresh token, so we'll force it to fetch a fresh access token by deleting
|
// Bad refresh token, so we'll force it to fetch a fresh access token by deleting
|
||||||
// and returning null;
|
// and returning null;
|
||||||
console.log('Unauthorized refresh_token request');
|
console.log('[oauth2] Unauthorized refresh_token request');
|
||||||
await deleteToken(ctx, contextId);
|
await deleteToken(ctx, contextId);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,7 +70,8 @@ export async function getAuthorizationCode(
|
|||||||
|
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
const authorizationUrlStr = authorizationUrl.toString();
|
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;
|
let foundCode = false;
|
||||||
|
|
||||||
@@ -85,11 +86,15 @@ export async function getAuthorizationCode(
|
|||||||
},
|
},
|
||||||
async onNavigate({ url: urlStr }) {
|
async onNavigate({ url: urlStr }) {
|
||||||
const url = new URL(urlStr);
|
const url = new URL(urlStr);
|
||||||
|
if (logsEnabled) console.log('[oauth2] Navigated to', urlStr);
|
||||||
|
|
||||||
if (url.searchParams.has('error')) {
|
if (url.searchParams.has('error')) {
|
||||||
return reject(new Error(`Failed to authorize: ${url.searchParams.get('error')}`));
|
return reject(new Error(`Failed to authorize: ${url.searchParams.get('error')}`));
|
||||||
}
|
}
|
||||||
|
|
||||||
const code = url.searchParams.get('code');
|
const code = url.searchParams.get('code');
|
||||||
if (!code) {
|
if (!code) {
|
||||||
|
console.log('[oauth2] Code not found');
|
||||||
return; // Could be one of many redirects in a chain, so skip it
|
return; // Could be one of many redirects in a chain, so skip it
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,6 +102,7 @@ export async function getAuthorizationCode(
|
|||||||
foundCode = true;
|
foundCode = true;
|
||||||
close();
|
close();
|
||||||
|
|
||||||
|
console.log('[oauth2] Code found');
|
||||||
const response = await getAccessToken(ctx, {
|
const response = await getAccessToken(ctx, {
|
||||||
grantType: 'authorization_code',
|
grantType: 'authorization_code',
|
||||||
accessTokenUrl,
|
accessTokenUrl,
|
||||||
|
|||||||
@@ -53,6 +53,7 @@ const authorizationUrls = [
|
|||||||
'https://www.dropbox.com/oauth2/authorize',
|
'https://www.dropbox.com/oauth2/authorize',
|
||||||
'https://www.linkedin.com/oauth/v2/authorization',
|
'https://www.linkedin.com/oauth/v2/authorization',
|
||||||
'https://MY_SHOP.myshopify.com/admin/oauth/access_token',
|
'https://MY_SHOP.myshopify.com/admin/oauth/access_token',
|
||||||
|
'https://appcenter.intuit.com/app/connect/oauth2/authorize',
|
||||||
];
|
];
|
||||||
|
|
||||||
const accessTokenUrls = [
|
const accessTokenUrls = [
|
||||||
@@ -69,6 +70,7 @@ const accessTokenUrls = [
|
|||||||
'https://www.googleapis.com/oauth2/v4/token',
|
'https://www.googleapis.com/oauth2/v4/token',
|
||||||
'https://www.linkedin.com/oauth/v2/accessToken',
|
'https://www.linkedin.com/oauth/v2/accessToken',
|
||||||
'https://MY_SHOP.myshopify.com/admin/oauth/authorize',
|
'https://MY_SHOP.myshopify.com/admin/oauth/authorize',
|
||||||
|
'https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer',
|
||||||
];
|
];
|
||||||
|
|
||||||
export const plugin: PluginDefinition = {
|
export const plugin: PluginDefinition = {
|
||||||
@@ -109,6 +111,17 @@ export const plugin: PluginDefinition = {
|
|||||||
await resetDataDirKey(ctx, contextId);
|
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: [
|
args: [
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -37,12 +37,12 @@ export async function getDataDirKey(ctx: Context, contextId: string) {
|
|||||||
return `${contextId}::${key}`;
|
return `${contextId}::${key}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function tokenStoreKey(context_id: string) {
|
function tokenStoreKey(contextId: string) {
|
||||||
return ['token', context_id].join('::');
|
return ['token', contextId].join('::');
|
||||||
}
|
}
|
||||||
|
|
||||||
function dataDirStoreKey(context_id: string) {
|
function dataDirStoreKey(contextId: string) {
|
||||||
return ['data_dir', context_id].join('::');
|
return ['data_dir', contextId].join('::');
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AccessToken {
|
export interface AccessToken {
|
||||||
|
|||||||
Reference in New Issue
Block a user