/* ============================================================================ * Copyright (c) Cloud Annotations * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * ========================================================================== */ import React, { useState } from "react"; import clsx from "clsx"; import FormItem from "../FormItem"; import FormSelect from "../FormSelect"; import FormTextInput from "../FormTextInput"; import { useTypedDispatch, useTypedSelector } from "../hooks"; import styles from "../styles.module.css"; import { AuthState, Scheme, setAuthData, setSelectedAuth } from "./slice"; type Props = { mode: "locked" | "unlocked"; } & JSX.IntrinsicElements["button"]; function LockButton({ mode, children, style, ...rest }: Props) { return ( ); } function validateData(selectedAuth: Scheme[], data: AuthState["data"]) { for (const scheme of selectedAuth) { if (data[scheme.key] === undefined) { return false; } const hasMissingKeys = Object.values(data[scheme.key]).includes(undefined); if (hasMissingKeys) { return false; } } return true; } function Authorization() { const [editing, setEditing] = useState(false); const data = useTypedSelector((state) => state.auth.data); const options = useTypedSelector((state) => state.auth.options); const selected = useTypedSelector((state) => state.auth.selected); const dispatch = useTypedDispatch(); if (selected === undefined) { return null; } const selectedAuth = options[selected]; const authenticated = validateData(selectedAuth, data); const optionKeys = Object.keys(options); if (editing) { return (
{optionKeys.length > 1 && ( { dispatch(setSelectedAuth(e.target.value)); }} /> )} {selectedAuth.map((a) => { if (a.type === "http" && a.scheme === "bearer") { return ( { const value = e.target.value.trim(); dispatch( setAuthData({ scheme: a.key, key: "token", value: value ? value : undefined, }) ); }} /> ); } if (a.type === "http" && a.scheme === "basic") { return ( { const value = e.target.value.trim(); dispatch( setAuthData({ scheme: a.key, key: "username", value: value ? value : undefined, }) ); }} /> { const value = e.target.value.trim(); dispatch( setAuthData({ scheme: a.key, key: "password", value: value ? value : undefined, }) ); }} /> ); } return null; })} { setEditing(false); }} > Save
); } if (authenticated) { return ( { setEditing(true); }} > Authorized ); } return ( { setEditing(true); }} > Authorize ); } export default Authorization;