Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/frontend/compute/auth-token.tsx
Views: 687
import { useEffect, useState } from "react";1import generateVouchers from "@cocalc/util/vouchers";2import { Button, Input, Popconfirm, Spin } from "antd";3import { Icon } from "@cocalc/frontend/components/icon";4import { PROXY_AUTH_TOKEN_FILE } from "@cocalc/util/compute/constants";5import { writeTextFileToComputeServer } from "./project";6import ShowError from "@cocalc/frontend/components/error";78function createToken() {9return generateVouchers({ count: 1, length: 16 })[0];10}1112export default function AuthToken({13id,14project_id,15setConfig,16configuration,17state,18IMAGES,19}) {20const [error, setError] = useState<string>("");21const [saving, setSaving] = useState<boolean>(false);22const { proxy, authToken } = IMAGES?.[configuration.image] ?? {};23const noAuthToken = proxy === false && !authToken;2425const updateAuthToken = async () => {26const authToken = createToken();27try {28setSaving(true);29setError("");30await setConfig({ authToken });31if (id && state == "running") {32// also attempt to write it directly to the file system, which updates33// the proxy server in realtime to use the new token.34await writeAuthToken({35compute_server_id: id,36project_id,37authToken,38});39}40} catch (err) {41setError(`${err}`);42} finally {43setSaving(false);44}45};46useEffect(() => {47if (noAuthToken) {48return;49}50// create token if it is not set but required51if (configuration.authToken == null) {52updateAuthToken();53}54}, [noAuthToken, configuration.authToken]);5556if (noAuthToken) {57// image that doesn't use authToken in any ways58return null;59}6061return (62<div style={{ color: "#666" }}>63<div style={{ marginTop: "15px", display: "flex" }}>64<div style={{ margin: "auto 30px auto 0" }}>65<b>Auth Token:</b>66</div>67<ShowError68error={error}69setError={setError}70style={{ margin: "15px 0" }}71/>72<Input.Password73style={{ width: "200px" }}74readOnly75value={configuration.authToken ?? ""}76/>77<Popconfirm78onConfirm={updateAuthToken}79okText="Change token"80title={"Change auth token?"}81description={82<div style={{ width: "400px" }}>83<b>84WARNING: Changing the auth token will prevent people who you85shared the old token with from using the site.86</b>87</div>88}89>90<Button91style={{ marginLeft: "30px" }}92disabled={93saving ||94(authToken &&95state != "deprovisioned" &&96state != "off") /* will get rid of soon */97}98>99<Icon name="refresh" />100Randomize...101{saving && <Spin />}102</Button>103</Popconfirm>104</div>105</div>106);107}108109async function writeAuthToken({ authToken, project_id, compute_server_id }) {110await writeTextFileToComputeServer({111value: authToken,112project_id,113compute_server_id,114sudo: true,115path: PROXY_AUTH_TOKEN_FILE,116});117}118119120