Skip to content

Commit

Permalink
fix(login) use trust_token as a field name in the payload when reusin…
Browse files Browse the repository at this point in the history
…g a certificate in case the explicit_trust_token api extension is present

Signed-off-by: David Edler <david.edler@canonical.com>
  • Loading branch information
edlerd committed Jul 9, 2024
1 parent e0a757e commit 0fc2601
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/api/certificates.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ export const fetchCertificates = (): Promise<LxdCertificate[]> => {
});
};

export const addCertificate = (token: string): Promise<void> => {
export const addCertificate = (
token: string,
hasExplicitTrustToken: boolean,
): Promise<void> => {
return new Promise((resolve, reject) => {
const tokenFieldName = hasExplicitTrustToken ? "trust_token" : "password";
fetch(`/1.0/certificates`, {
method: "POST",
body: JSON.stringify({
type: "client",
password: token,
[tokenFieldName]: token,
}),
})
.then(handleResponse)
Expand Down
1 change: 1 addition & 0 deletions src/context/useSupportedFeatures.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ export const useSupportedFeatures = () => {
!!serverVersion && serverMajor >= 5 && serverMinor >= 19,
hasDocumentationObject:
!!serverVersion && serverMajor >= 5 && serverMinor >= 20,
hasExplicitTrustToken: apiExtensions.has("explicit_trust_token"),
};
};
4 changes: 3 additions & 1 deletion src/pages/login/CertificateAddForm.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { FC, useState } from "react";
import { Button, Form, Textarea, useNotify } from "@canonical/react-components";
import { addCertificate } from "api/certificates";
import { useSupportedFeatures } from "context/useSupportedFeatures";

const CertificateAddForm: FC = () => {
const notify = useNotify();
const [token, setToken] = useState("");
const { hasExplicitTrustToken } = useSupportedFeatures();

const useToken = () => {
const sanitisedToken =
Expand All @@ -13,7 +15,7 @@ const CertificateAddForm: FC = () => {
.split(/\r?\n|\r|\n/g)
.at(-1) ?? "";

addCertificate(sanitisedToken)
addCertificate(sanitisedToken, hasExplicitTrustToken)
.then(() => {
location.reload();
})
Expand Down

0 comments on commit 0fc2601

Please sign in to comment.