1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import React from 'react';
6 import { CustomStyleRulesCallback } from 'common/custom-theme';
7 import { Dialog, DialogActions, DialogTitle, DialogContent, Button, Typography } from '@mui/material';
8 import { WithStyles } from '@mui/styles';
9 import withStyles from '@mui/styles/withStyles';
10 import CopyToClipboard from 'react-copy-to-clipboard';
11 import { ArvadosTheme } from 'common/custom-theme';
12 import { withDialog } from 'store/dialog/with-dialog';
13 import { WithDialogProps } from 'store/dialog/with-dialog';
14 import { connect, DispatchProp } from 'react-redux';
19 } from 'store/token-dialog/token-dialog-actions';
20 import { DefaultCodeSnippet } from 'components/default-code-snippet/default-code-snippet';
21 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
22 import { getNewExtraToken } from 'store/auth/auth-action';
23 import { DetailsAttributeComponent } from 'components/details-attribute/details-attribute';
24 import moment from 'moment';
26 type CssRules = 'link' | 'paper' | 'button' | 'actionButton' | 'codeBlock';
28 const styles: CustomStyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
30 color: theme.palette.primary.main,
31 textDecoration: 'none',
35 padding: theme.spacing(1),
36 marginBottom: theme.spacing(2),
37 backgroundColor: theme.palette.grey["200"],
38 border: `1px solid ${theme.palette.grey["300"]}`
41 fontSize: '0.8125rem',
46 marginTop: theme.spacing(2),
47 marginBottom: theme.spacing(2),
48 marginRight: theme.spacing(2),
51 fontSize: '0.8125rem',
55 type TokenDialogProps = TokenDialogData & WithDialogProps<{}> & WithStyles<CssRules> & DispatchProp;
57 export class TokenDialogComponent extends React.Component<TokenDialogProps> {
58 onCopy = (message: string) => {
59 this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
62 kind: SnackbarKind.SUCCESS
66 onGetNewToken = async () => {
67 const newToken = await this.props.dispatch<any>(getNewExtraToken());
69 this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
70 message: 'New token retrieved',
72 kind: SnackbarKind.SUCCESS
75 this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
76 message: 'Creating new tokens is not allowed',
78 kind: SnackbarKind.WARNING
83 getSnippet = ({ apiHost, token }: TokenDialogData) =>
84 `HISTIGNORE=$HISTIGNORE:'export ARVADOS_API_TOKEN=*'
85 export ARVADOS_API_TOKEN=${token}
86 export ARVADOS_API_HOST=${apiHost}
87 unset ARVADOS_API_HOST_INSECURE`
90 const { classes, open, closeDialog, ...data } = this.props;
91 const tokenExpiration = data.tokenExpiration
92 ? `${data.tokenExpiration.toLocaleString()} (${moment(data.tokenExpiration).fromNow()})`
93 : `This token does not have an expiration date`;
100 <DialogTitle>Get API Token</DialogTitle>
102 <Typography paragraph={true}>
103 The Arvados API token is a secret key that enables the Arvados SDKs to access Arvados with the proper permissions.
104 <Typography component='span'>
105 For more information see
106 <a href='http://doc.arvados.org/user/reference/api-tokens.html' target='blank' rel="noopener" className={classes.link}>
107 Getting an API token.
112 <DetailsAttributeComponent label='API Host' value={data.apiHost} copyValue={data.apiHost} onCopy={this.onCopy} />
113 <DetailsAttributeComponent label='API Token' value={data.token} copyValue={data.token} onCopy={this.onCopy} />
114 <DetailsAttributeComponent label='Token expiration' value={tokenExpiration} />
115 {this.props.canCreateNewTokens && <Button
116 onClick={() => this.onGetNewToken()}
120 className={classes.actionButton}
125 <Typography paragraph={true}>
126 Paste the following lines at a shell prompt to set up the necessary environment for Arvados SDKs to authenticate to your account.
128 <DefaultCodeSnippet className={classes.codeBlock} lines={[this.getSnippet(data)]} />
129 <CopyToClipboard text={this.getSnippet(data)} onCopy={() => this.onCopy('Shell code block copied')}>
134 className={classes.actionButton}
136 Copy link to clipBOARD
141 <a href='http://doc.arvados.org/user/reference/api-tokens.html' target='blank' rel="noopener" className={classes.link}>virtual machines</a>
142 do this for you automatically. This setup is needed only when you use the API remotely (e.g., from your own workstation).
146 <Button onClick={closeDialog} className={classes.button} color="primary">CLOSE</Button>
152 export const TokenDialog =
154 connect(getTokenDialogData)(
155 withDialog(TOKEN_DIALOG_NAME)(TokenDialogComponent)));