Merge branch 'main' into 21720-material-ui-upgrade
[arvados.git] / services / workbench2 / src / views-components / token-dialog / token-dialog.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
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';
15 import {
16     TokenDialogData,
17     getTokenDialogData,
18     TOKEN_DIALOG_NAME,
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';
25
26 type CssRules = 'link' | 'paper' | 'button' | 'actionButton' | 'codeBlock';
27
28 const styles: CustomStyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
29     link: {
30         color: theme.palette.primary.main,
31         textDecoration: 'none',
32         margin: '0px 4px'
33     },
34     paper: {
35         padding: theme.spacing(1),
36         marginBottom: theme.spacing(2),
37         backgroundColor: theme.palette.grey["200"],
38         border: `1px solid ${theme.palette.grey["300"]}`
39     },
40     button: {
41         fontSize: '0.8125rem',
42         fontWeight: 600
43     },
44     actionButton: {
45         boxShadow: 'none',
46         marginTop: theme.spacing(2),
47         marginBottom: theme.spacing(2),
48         marginRight: theme.spacing(2),
49     },
50     codeBlock: {
51         fontSize: '0.8125rem',
52     },
53 });
54
55 type TokenDialogProps = TokenDialogData & WithDialogProps<{}> & WithStyles<CssRules> & DispatchProp;
56
57 export class TokenDialogComponent extends React.Component<TokenDialogProps> {
58     onCopy = (message: string) => {
59         this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
60             message,
61             hideDuration: 2000,
62             kind: SnackbarKind.SUCCESS
63         }));
64     }
65
66     onGetNewToken = async () => {
67         const newToken = await this.props.dispatch<any>(getNewExtraToken());
68         if (newToken) {
69             this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
70                 message: 'New token retrieved',
71                 hideDuration: 2000,
72                 kind: SnackbarKind.SUCCESS
73             }));
74         } else {
75             this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
76                 message: 'Creating new tokens is not allowed',
77                 hideDuration: 2000,
78                 kind: SnackbarKind.WARNING
79             }));
80         }
81     }
82
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`
88
89     render() {
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`;
94
95         return <Dialog
96             open={open}
97             onClose={closeDialog}
98             fullWidth={true}
99             maxWidth='md'>
100             <DialogTitle>Get API Token</DialogTitle>
101             <DialogContent>
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.
108                         </a>
109                     </Typography>
110                 </Typography>
111
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()}
117                     color="primary"
118                     size="small"
119                     variant="contained"
120                     className={classes.actionButton}
121                 >
122                     GET NEW TOKEN
123                 </Button>}
124
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.
127                 </Typography>
128                 <DefaultCodeSnippet className={classes.codeBlock} lines={[this.getSnippet(data)]} />
129                 <CopyToClipboard text={this.getSnippet(data)} onCopy={() => this.onCopy('Shell code block copied')}>
130                     <Button
131                         color="primary"
132                         size="small"
133                         variant="contained"
134                         className={classes.actionButton}
135                     >
136                         Copy link to clipBOARD
137                     </Button>
138                 </CopyToClipboard>
139                 <Typography>
140                     Arvados
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).
143                 </Typography>
144             </DialogContent>
145             <DialogActions>
146                 <Button onClick={closeDialog} className={classes.button} color="primary">CLOSE</Button>
147             </DialogActions>
148         </Dialog>;
149     }
150 }
151
152 export const TokenDialog =
153     withStyles(styles)(
154         connect(getTokenDialogData)(
155             withDialog(TOKEN_DIALOG_NAME)(TokenDialogComponent)));