mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-19 07:54:23 +01:00
Better data key for window
This commit is contained in:
@@ -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, {
|
||||
|
||||
@@ -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: [
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user