oauth disabling API

This commit is contained in:
Jan Prochazka
2022-11-17 20:09:27 +01:00
parent 94a91d5fed
commit 07b2a3e923
4 changed files with 31 additions and 26 deletions

View File

@@ -24,12 +24,8 @@
let loadedApi = false;
let loadedPlugins = false;
const isOauthCallback = handleOauthCallback();
async function loadApi() {
if (isOauthCallback) {
return;
}
// if (shouldWaitForElectronInitialize()) {
// setTimeout(loadApi, 100);
// return;
@@ -80,7 +76,7 @@
<ErrorHandler />
{#if loadedApi && !isOauthCallback}
{#if loadedApi}
<DataGridRowHeightMeter />
<CommandListener />
<PluginsProvider />

View File

@@ -1,38 +1,38 @@
import { apiCall } from './utility/api';
import { apiCall, disableApi } from './utility/api';
import { getConfig } from './utility/metadataLoaders';
export function handleOauthCallback() {
export function isOauthCallback() {
const params = new URLSearchParams(location.search);
const sentCode = params.get('code');
const sentState = params.get('state');
if (
sentCode &&
sentState &&
sentState.startsWith('dbg-oauth:') &&
sentState == sessionStorage.getItem('oauthState')
) {
return (
sentCode && sentState && sentState.startsWith('dbg-oauth:') && sentState == sessionStorage.getItem('oauthState')
);
}
export function handleOauthCallback() {
const params = new URLSearchParams(location.search);
const sentCode = params.get('code');
if (isOauthCallback()) {
sessionStorage.removeItem('oauthState');
apiCall('auth/oauth-token', {
code: sentCode,
redirectUri: location.origin,
}).then(authResp => {
const { accessToken } = authResp;
console.log('Got new access token:', accessToken);
localStorage.setItem('accessToken', accessToken);
location.replace('/');
});
console.log('handleOauthCallback TRUE');
return true;
}
console.log('handleOauthCallback FALSE');
return false;
}
export async function handleAuthOnStartup(config) {
console.log('********************* handleAuthOnStartup');
if (config.oauth) {
if (localStorage.getItem('accessToken')) {
return;

View File

@@ -5,12 +5,16 @@ import './commands/stdCommands';
import localStorageGarbageCollector from './utility/localStorageGarbageCollector';
import { handleOauthCallback } from './clientAuth';
const isOauthCallback = handleOauthCallback();
localStorageGarbageCollector();
const app = new App({
target: document.body,
props: {},
});
const app = isOauthCallback
? null
: new App({
target: document.body,
props: {},
});
// const app = null;

View File

@@ -4,16 +4,17 @@ import { writable } from 'svelte/store';
import getElectron from './getElectron';
// import socket from './socket';
import { showSnackbarError } from '../utility/snackbar';
import { redirectToLogin } from '../clientAuth';
import { isOauthCallback, redirectToLogin } from '../clientAuth';
let eventSource;
let apiLogging = false;
// let cacheCleanerRegistered;
let apiDisabled = false;
const disabledOnOauth = isOauthCallback();
// export function disableApi() {
// apiDisabled = true;
// }
export function disableApi() {
apiDisabled = true;
}
function wantEventSource() {
if (!eventSource) {
@@ -45,6 +46,10 @@ export async function apiCall(route: string, args: {} = undefined) {
console.log('API disabled!!', route);
return;
}
if (disabledOnOauth && route != 'auth/oauth-token') {
console.log('API disabled because oauth callback!!', route);
return;
}
const electron = getElectron();
if (electron) {
@@ -62,7 +67,7 @@ export async function apiCall(route: string, args: {} = undefined) {
});
if (resp.status == 401 && !apiDisabled) {
apiDisabled = true;
disableApi();
console.log('Disabling API', route);
// unauthorized
redirectToLogin();