Extras: Add API authentication support

An API key is extremely important for ST-Extras servers that are
exposed to the internet.

Add an API key field below where the user enters the extras URL. For
convenience, this key is persisted whenever the user refreshes the
page.

Also modify the fetch requests to always include API keys if present.

See ST-Extras for more information on how this works.

Signed-off-by: kingbri <bdashore3@proton.me>
This commit is contained in:
kingbri
2023-06-02 22:21:21 -04:00
parent f4802952b6
commit 36ea41f0a6
7 changed files with 50 additions and 25 deletions

View File

@ -10,7 +10,7 @@ import {
eventSource,
appendImageToMessage
} from "../../../script.js";
import { getApiUrl, getContext, extension_settings, defaultRequestArgs, modules } from "../../extensions.js";
import { getApiUrl, getContext, extension_settings, doExtrasFetch, modules } from "../../extensions.js";
import { stringFormat, initScrollHeight, resetScrollHeight } from "../../utils.js";
export { MODULE_NAME };
@ -234,7 +234,7 @@ async function onModelChange() {
async function updateExtrasRemoteModel() {
const url = new URL(getApiUrl());
url.pathname = '/api/image/model';
const getCurrentModelResult = await fetch(url, {
const getCurrentModelResult = await doExtrasFetch(url, {
method: 'POST',
headers: postHeaders,
body: JSON.stringify({ model: extension_settings.sd.model }),
@ -285,7 +285,7 @@ async function loadExtrasSamplers() {
const url = new URL(getApiUrl());
url.pathname = '/api/image/samplers';
const result = await fetch(url, defaultRequestArgs);
const result = await doExtrasFetch(url);
if (result.ok) {
const data = await result.json();
@ -338,7 +338,7 @@ async function loadExtrasModels() {
const url = new URL(getApiUrl());
url.pathname = '/api/image/model';
const getCurrentModelResult = await fetch(url, defaultRequestArgs);
const getCurrentModelResult = await doExtrasFetch(url);
if (getCurrentModelResult.ok) {
const data = await getCurrentModelResult.json();
@ -346,7 +346,7 @@ async function loadExtrasModels() {
}
url.pathname = '/api/image/models';
const getModelsResult = await fetch(url, defaultRequestArgs);
const getModelsResult = await doExtrasFetch(url);
if (getModelsResult.ok) {
const data = await getModelsResult.json();
@ -493,7 +493,7 @@ async function generateExtrasImage(prompt, callback) {
console.log(extension_settings.sd);
const url = new URL(getApiUrl());
url.pathname = '/api/image';
const result = await fetch(url, {
const result = await doExtrasFetch(url, {
method: 'POST',
headers: postHeaders,
body: JSON.stringify({