|
| 1 | +import React, { useState, useRef } from "react"; |
| 2 | + |
| 3 | +import { Button, Modal, Alert } from "react-bootstrap"; |
| 4 | +import { useDispatch } from "react-redux"; |
| 5 | +import validate from "validate.js"; |
| 6 | +import { useTranslation } from "react-i18next"; |
| 7 | +import { credentialActions } from "../../_actions"; |
| 8 | + |
| 9 | +/** |
| 10 | + * Component used to display additional details about a credential, as well as allowing |
| 11 | + * the user to update the nickname, or delete |
| 12 | + * @param credential Data related to a specific credential |
| 13 | + * @returns |
| 14 | + */ |
| 15 | +const EditTrustedDevice = function ({ credential }) { |
| 16 | + const { t } = useTranslation(); |
| 17 | + |
| 18 | + const [show, setShow] = useState(false); |
| 19 | + |
| 20 | + const [nickname, setNickname] = useState(""); |
| 21 | + |
| 22 | + const [invalidNickname, setInvalidNickname] = useState(undefined); |
| 23 | + |
| 24 | + const [submitted, setSubmitted] = useState(false); |
| 25 | + |
| 26 | + const dispatch = useDispatch(); |
| 27 | + |
| 28 | + /** |
| 29 | + * Closes the modal |
| 30 | + */ |
| 31 | + const handleClose = () => setShow(false); |
| 32 | + const handleShow = () => { |
| 33 | + setNickname(credential.credentialNickname.value); |
| 34 | + setShow(true); |
| 35 | + }; |
| 36 | + |
| 37 | + const constraints = { |
| 38 | + nickname: { |
| 39 | + length: { |
| 40 | + minimum: 1, |
| 41 | + maximum: 20, |
| 42 | + }, |
| 43 | + }, |
| 44 | + }; |
| 45 | + /** |
| 46 | + * Handles the pressing of the delete button |
| 47 | + * IF the ID of the deleted device matches the ID of the Trusted Device locally stored |
| 48 | + * then it removes any local information related to the trusted device |
| 49 | + */ |
| 50 | + const handleDelete = () => { |
| 51 | + setShow(false); |
| 52 | + dispatch( |
| 53 | + credentialActions.delete(credential.credential.credentialId.base64url) |
| 54 | + ); |
| 55 | + if ( |
| 56 | + credential.credential.credentialId.base64url === |
| 57 | + localStorage.getItem("trustedDeviceID") |
| 58 | + ) { |
| 59 | + localStorage.removeItem("trustedDevice"); |
| 60 | + localStorage.removeItem("trustedDeviceID"); |
| 61 | + } |
| 62 | + }; |
| 63 | + |
| 64 | + /** |
| 65 | + * Handles when the user saves a new nickname for their security key |
| 66 | + * First validates if the nickname is valid before allowing an update |
| 67 | + */ |
| 68 | + const handleSave = () => { |
| 69 | + setSubmitted(true); |
| 70 | + const result = validate({ nickname }, constraints); |
| 71 | + if (result) { |
| 72 | + setInvalidNickname(result.nickname.join(". ")); |
| 73 | + return result; |
| 74 | + } |
| 75 | + setInvalidNickname(undefined); |
| 76 | + |
| 77 | + setShow(false); |
| 78 | + credential.credentialNickname.value = nickname; |
| 79 | + dispatch(credentialActions.update(credential)); |
| 80 | + }; |
| 81 | + |
| 82 | + /** |
| 83 | + * Used to validate nickname changes on user input |
| 84 | + * @param e Event triggered by user action |
| 85 | + */ |
| 86 | + const handleChange = (e) => { |
| 87 | + const { name, value } = e.target; |
| 88 | + setNickname(value); |
| 89 | + const result = validate({ nickname: value }, constraints); |
| 90 | + if (result) { |
| 91 | + setInvalidNickname(result.nickname.join(". ")); |
| 92 | + } else { |
| 93 | + setInvalidNickname(undefined); |
| 94 | + } |
| 95 | + }; |
| 96 | + const inputRef = useRef(null); |
| 97 | + |
| 98 | + return ( |
| 99 | + <> |
| 100 | + <Button variant="secondary" onClick={handleShow}> |
| 101 | + {t("trusted-device.edit-button")} |
| 102 | + </Button> |
| 103 | + <Modal show={show} onHide={handleClose}> |
| 104 | + <Modal.Header closeButton> |
| 105 | + <Modal.Title>{t("trusted-device.edit-header")}</Modal.Title> |
| 106 | + </Modal.Header> |
| 107 | + <Modal.Body> |
| 108 | + <label> |
| 109 | + {t("trusted-device.edit-form-label")}{" "} |
| 110 | + <input |
| 111 | + type="text" |
| 112 | + name="nickname" |
| 113 | + autoFocus |
| 114 | + value={nickname} |
| 115 | + ref={inputRef} |
| 116 | + onChange={handleChange} |
| 117 | + className={`form-control${ |
| 118 | + submitted && invalidNickname ? " is-invalid" : "" |
| 119 | + }`} |
| 120 | + onKeyPress={(ev) => { |
| 121 | + if (ev.key === "Enter") { |
| 122 | + handleSave(); |
| 123 | + ev.preventDefault(); |
| 124 | + } |
| 125 | + }} |
| 126 | + /> |
| 127 | + </label> |
| 128 | + <br /> |
| 129 | + {invalidNickname ? ( |
| 130 | + <Alert variant="danger">{invalidNickname}</Alert> |
| 131 | + ) : null} |
| 132 | + |
| 133 | + <br /> |
| 134 | + <h4>{t("trusted-device.yubico-att-label")}</h4> |
| 135 | + {credential?.attestationMetadata?.value?.description && ( |
| 136 | + <p> |
| 137 | + <em>{t("credential.att-device-name")}</em>{" "} |
| 138 | + {credential.attestationMetadata.value.description} |
| 139 | + </p> |
| 140 | + )} |
| 141 | + {credential?.attestationMetadata?.value?.aaguid && ( |
| 142 | + <p> |
| 143 | + <em>{t("trusted-device.att-aaguid")}</em>{" "} |
| 144 | + {credential.attestationMetadata.value.aaguid} |
| 145 | + </p> |
| 146 | + )} |
| 147 | + {credential?.attestationMetadata?.value?.aaid && ( |
| 148 | + <p> |
| 149 | + <em>{t("trusted-device.att-aaid")}</em>{" "} |
| 150 | + {credential.attestationMetadata.value.aaid} |
| 151 | + </p> |
| 152 | + )} |
| 153 | + {credential?.attestationMetadata?.value?.authenticatorTransport && |
| 154 | + credential?.attestationMetadata?.value?.authenticatorTransport |
| 155 | + .length > 0 && ( |
| 156 | + <p> |
| 157 | + <em>{t("trusted-device.att-device-interfaces")}</em>{" "} |
| 158 | + <ul> |
| 159 | + {credential.attestationMetadata.value.authenticatorTransport.map( |
| 160 | + (transport, index) => ( |
| 161 | + <li key={index}>{transport.id}</li> |
| 162 | + ) |
| 163 | + )} |
| 164 | + </ul> |
| 165 | + </p> |
| 166 | + )} |
| 167 | + <label> |
| 168 | + <em>{t("trusted-device.edit-usernameless")}</em>{" "} |
| 169 | + {credential.registrationRequest |
| 170 | + ? credential.registrationRequest.requireResidentKey.toString() |
| 171 | + : ""} |
| 172 | + </label> |
| 173 | + <br /> |
| 174 | + <label> |
| 175 | + <em>{t("trusted-device.last-time-used")}</em>{" "} |
| 176 | + {credential.lastUsedTime |
| 177 | + ? new Date( |
| 178 | + credential.lastUsedTime.seconds * 1000 |
| 179 | + ).toLocaleString() |
| 180 | + : ""} |
| 181 | + </label> |
| 182 | + <br /> |
| 183 | + <label> |
| 184 | + <em>{t("trusted-device.last-update-time")}</em>{" "} |
| 185 | + {credential.lastUpdatedTime |
| 186 | + ? new Date( |
| 187 | + credential.lastUpdatedTime.seconds * 1000 |
| 188 | + ).toLocaleString() |
| 189 | + : ""} |
| 190 | + </label> |
| 191 | + <br /> |
| 192 | + <label> |
| 193 | + <em>{t("trusted-device.registration-time")}</em>{" "} |
| 194 | + {credential.registrationTime |
| 195 | + ? new Date( |
| 196 | + credential.registrationTime.seconds * 1000 |
| 197 | + ).toLocaleString() |
| 198 | + : ""} |
| 199 | + </label> |
| 200 | + <br /> |
| 201 | + </Modal.Body> |
| 202 | + <Modal.Footer> |
| 203 | + <Button variant="secondary" onClick={handleClose}> |
| 204 | + {t("trusted-device.edit-cancel-button")} |
| 205 | + </Button> |
| 206 | + <Button variant="danger" onClick={handleDelete}> |
| 207 | + {t("trusted-device.edit-delete-button")} |
| 208 | + </Button> |
| 209 | + <Button variant="primary" onClick={handleSave}> |
| 210 | + {t("trusted-device.edit-save-button")} |
| 211 | + </Button> |
| 212 | + </Modal.Footer> |
| 213 | + </Modal> |
| 214 | + </> |
| 215 | + ); |
| 216 | +}; |
| 217 | + |
| 218 | +export default EditTrustedDevice; |
0 commit comments