1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Add auth header value state + sync

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2023-01-12 12:02:22 -05:00
parent d83d223742
commit 4867286179
8 changed files with 149 additions and 0 deletions

View File

@ -0,0 +1,8 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getRequestChannel } from "../../../common/utils/channel/get-request-channel";
export const authHeaderChannel = getRequestChannel<void, string>("auth-header-value");

View File

@ -0,0 +1,31 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
const authHeaderStateInjectable = getInjectable({
id: "auth-header-state",
instantiate: () => {
let header: string;
return {
get: () => {
if (!header) {
throw new Error("Tried to get auth header value before being set");
}
return header;
},
set: (value: string) => {
if (header) {
throw new Error("Tried to set auth header value more than once");
}
header = value;
},
};
},
});
export default authHeaderStateInjectable;

View File

@ -0,0 +1,19 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getRequestChannelListenerInjectable } from "../../../main/utils/channel/channel-listeners/listener-tokens";
import { authHeaderChannel } from "../common/channel";
import authHeaderStateInjectable from "../common/header-state.injectable";
const authHeaderRequestListenerInjectable = getRequestChannelListenerInjectable({
channel: authHeaderChannel,
handler: (di) => {
const authHeaderState = di.inject(authHeaderStateInjectable);
return () => authHeaderState.get();
},
});
export default authHeaderRequestListenerInjectable;

View File

@ -0,0 +1,9 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getGlobalOverride } from "../../../common/test-utils/get-global-override";
import computeAuthHeaderValueInjectable from "./compute-value.injectable";
export default getGlobalOverride(computeAuthHeaderValueInjectable, () => async () => "some-auth-header-value");

View File

@ -0,0 +1,15 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { randomUUID } from "crypto";
const computeAuthHeaderValueInjectable = getInjectable({
id: "compute-auth-header-value",
instantiate: () => () => randomUUID(),
causesSideEffects: true,
});
export default computeAuthHeaderValueInjectable;

View File

@ -0,0 +1,24 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { beforeApplicationIsLoadingInjectionToken } from "../../../main/library";
import authHeaderStateInjectable from "../common/header-state.injectable";
import computeAuthHeaderValueInjectable from "./compute-value.injectable";
const initAuthHeaderStateInjectable = getInjectable({
id: "init-auth-header-state",
instantiate: (di) => ({
id: "init-auth-header-state",
run: async () => {
const state = di.inject(authHeaderStateInjectable);
const computeAuthHeaderValue = di.inject(computeAuthHeaderValueInjectable);
state.set(computeAuthHeaderValue());
},
}),
injectionToken: beforeApplicationIsLoadingInjectionToken,
});
export default initAuthHeaderStateInjectable;

View File

@ -0,0 +1,24 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { beforeFrameStartsFirstInjectionToken } from "../../../renderer/before-frame-starts/tokens";
import authHeaderStateInjectable from "../common/header-state.injectable";
import requestAuthHeaderValueInjectable from "./request-header.injectable";
const initAuthHeaderStateInjectable = getInjectable({
id: "init-auth-header-state",
instantiate: (di) => ({
id: "init-auth-header-state",
run: async () => {
const state = di.inject(authHeaderStateInjectable);
const requestAuthHeaderValue = di.inject(requestAuthHeaderValueInjectable);
state.set(await requestAuthHeaderValue());
},
}),
injectionToken: beforeFrameStartsFirstInjectionToken,
});
export default initAuthHeaderStateInjectable;

View File

@ -0,0 +1,19 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import requestFromChannelInjectable from "../../../renderer/utils/channel/request-from-channel.injectable";
import { authHeaderChannel } from "../common/channel";
const requestAuthHeaderValueInjectable = getInjectable({
id: "request-auth-header-value",
instantiate: (di) => {
const requestFromChannel = di.inject(requestFromChannelInjectable);
return () => requestFromChannel(authHeaderChannel);
},
});
export default requestAuthHeaderValueInjectable;