Better data key for window

This commit is contained in:
Gregory Schier
2025-02-24 22:34:10 -08:00
parent 52937c3097
commit c0dbe46318
5 changed files with 37 additions and 9 deletions

View File

@@ -2,7 +2,7 @@ import { Context } from '@yaakapp/api';
import { createHash, randomBytes } from 'node:crypto';
import { getAccessToken } from '../getAccessToken';
import { getOrRefreshAccessToken } from '../getOrRefreshAccessToken';
import { AccessToken, storeToken } from '../store';
import { AccessToken, getDataDirKey, storeToken } from '../store';
export const PKCE_SHA256 = 'S256';
export const PKCE_PLAIN = 'plain';
@@ -63,9 +63,18 @@ export async function getAuthorizationCode(
return new Promise(async (resolve, reject) => {
const authorizationUrlStr = authorizationUrl.toString();
console.log('Authorizing', authorizationUrlStr);
let foundCode = false;
let { close } = await ctx.window.openUrl({
url: authorizationUrlStr,
label: 'oauth-authorization-url',
dataDirKey: await getDataDirKey(ctx, contextId),
async onClose() {
if (!foundCode) {
reject(new Error('Authorization window closed'));
}
},
async onNavigate({ url: urlStr }) {
const url = new URL(urlStr);
if (url.searchParams.has('error')) {
@@ -77,6 +86,7 @@ export async function getAuthorizationCode(
}
// Close the window here, because we don't need it anymore!
foundCode = true;
close();
const response = await getAccessToken(ctx, {

View File

@@ -9,7 +9,7 @@ import { DEFAULT_PKCE_METHOD, getAuthorizationCode, PKCE_PLAIN, PKCE_SHA256 } fr
import { getClientCredentials } from './grants/clientCredentials';
import { getImplicit } from './grants/implicit';
import { getPassword } from './grants/password';
import { AccessToken, deleteToken, getToken } from './store';
import { AccessToken, deleteToken, getToken, resetDataDirKey } from './store';
type GrantType = 'authorization_code' | 'implicit' | 'password' | 'client_credentials';
@@ -71,7 +71,6 @@ export const plugin: PluginDefinition = {
actions: [
{
label: 'Copy Current Token',
icon: 'copy',
async onSelect(ctx, { contextId }) {
const token = await getToken(ctx, contextId);
if (token == null) {
@@ -84,7 +83,6 @@ export const plugin: PluginDefinition = {
},
{
label: 'Delete Token',
icon: 'trash',
async onSelect(ctx, { contextId }) {
if (await deleteToken(ctx, contextId)) {
await ctx.toast.show({ message: 'Token deleted', color: 'success' });
@@ -93,6 +91,12 @@ export const plugin: PluginDefinition = {
}
},
},
{
label: 'Clear Window Session',
async onSelect(ctx, { contextId }) {
await resetDataDirKey(ctx, contextId);
},
},
],
args: [
{

View File

@@ -22,10 +22,24 @@ export async function deleteToken(ctx: Context, contextId: string) {
return ctx.store.delete(tokenStoreKey(contextId));
}
export async function resetDataDirKey(ctx: Context, contextId: string) {
const key = new Date().toISOString();
return ctx.store.set<string>(dataDirStoreKey(contextId), key);
}
export async function getDataDirKey(ctx: Context, contextId: string) {
const key = (await ctx.store.get<string>(dataDirStoreKey(contextId))) ?? 'default';
return `${contextId}::${key}`;
}
function tokenStoreKey(context_id: string) {
return ['token', context_id].join('::');
}
function dataDirStoreKey(context_id: string) {
return ['data_dir', context_id].join('::');
}
export interface AccessToken {
response: AccessTokenRawResponse,
expiresAt: number | null;