16848: Adds expiration date to the "Get API Token" dialog.
[arvados-workbench2.git] / 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 * as React from 'react';
6 import {
7     Dialog,
8     DialogActions,
9     DialogTitle,
10     DialogContent,
11     WithStyles,
12     withStyles,
13     StyleRulesCallback,
14     Button,
15     Typography
16 } from '@material-ui/core';
17 import * as CopyToClipboard from 'react-copy-to-clipboard';
18 import { ArvadosTheme } from '~/common/custom-theme';
19 import { withDialog } from '~/store/dialog/with-dialog';
20 import { WithDialogProps } from '~/store/dialog/with-dialog';
21 import { connect, DispatchProp } from 'react-redux';
22 import {
23     TokenDialogData,
24     getTokenDialogData,
25     TOKEN_DIALOG_NAME,
26 } from '~/store/token-dialog/token-dialog-actions';
27 import { DefaultCodeSnippet } from '~/components/default-code-snippet/default-code-snippet';
28 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
29 import { getNewExtraToken } from '~/store/auth/auth-action';
30
31 type CssRules = 'link' | 'paper' | 'button' | 'actionButton';
32
33 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
34     link: {
35         color: theme.palette.primary.main,
36         textDecoration: 'none',
37         margin: '0px 4px'
38     },
39     paper: {
40         padding: theme.spacing.unit,
41         marginBottom: theme.spacing.unit * 2,
42         backgroundColor: theme.palette.grey["200"],
43         border: `1px solid ${theme.palette.grey["300"]}`
44     },
45     button: {
46         fontSize: '0.8125rem',
47         fontWeight: 600
48     },
49     actionButton: {
50         boxShadow: 'none',
51         marginTop: theme.spacing.unit * 2,
52         marginBottom: theme.spacing.unit * 2,
53         marginRight: theme.spacing.unit * 2,
54     }
55 });
56
57 type TokenDialogProps = TokenDialogData & WithDialogProps<{}> & WithStyles<CssRules> & DispatchProp;
58
59 export class TokenDialogComponent extends React.Component<TokenDialogProps> {
60     onCopy = (message: string) => {
61         this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
62             message,
63             hideDuration: 2000,
64             kind: SnackbarKind.SUCCESS
65         }));
66     }
67
68     onGetNewToken = async () => {
69         const newToken = await this.props.dispatch<any>(getNewExtraToken());
70         if (newToken) {
71             this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
72                 message: 'New token retrieved',
73                 hideDuration: 2000,
74                 kind: SnackbarKind.SUCCESS
75             }));
76         } else {
77             this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
78                 message: 'Creating new tokens is not allowed',
79                 hideDuration: 2000,
80                 kind: SnackbarKind.WARNING
81             }));
82         }
83     }
84
85     getSnippet = ({ apiHost, token }: TokenDialogData) =>
86         `HISTIGNORE=$HISTIGNORE:'export ARVADOS_API_TOKEN=*'
87 export ARVADOS_API_TOKEN=${token}
88 export ARVADOS_API_HOST=${apiHost}
89 unset ARVADOS_API_HOST_INSECURE`
90
91     render() {
92         const { classes, open, closeDialog, ...data } = this.props;
93
94         return <Dialog
95             open={open}
96             onClose={closeDialog}
97             fullWidth={true}
98             maxWidth='md'>
99             <DialogTitle>Get API Token</DialogTitle>
100             <DialogContent>
101                 <Typography paragraph={true}>
102                     The Arvados API token is a secret key that enables the Arvados SDKs to access Arvados with the proper permissions.
103                         <Typography component='span'>
104                             For more information see
105                             <a href='http://doc.arvados.org/user/reference/api-tokens.html' target='blank' className={classes.link}>
106                                 Getting an API token.
107                             </a>
108                         </Typography>
109                 </Typography>
110                 <Typography paragraph={true}>
111                     Paste the following lines at a shell prompt to set up the necessary environment for Arvados SDKs to authenticate to your account.
112                 </Typography>
113                 <DefaultCodeSnippet lines={[this.getSnippet(data)]} />
114                 { data.tokenExpiration
115                     ? <Typography component='span'>Expires at: {data.tokenExpiration.toLocaleString()}</Typography>
116                     : null
117                 }
118                 <CopyToClipboard text={this.getSnippet(data)} onCopy={() => this.onCopy('Token copied to clipboard')}>
119                     <Button
120                         color="primary"
121                         size="small"
122                         variant="contained"
123                         className={classes.actionButton}
124                     >
125                         COPY TO CLIPBOARD
126                     </Button>
127                 </CopyToClipboard>
128                 { this.props.canCreateNewTokens && <Button
129                     onClick={() => this.onGetNewToken()}
130                     color="primary"
131                     size="small"
132                     variant="contained"
133                     className={classes.actionButton}
134                 >
135                     GET NEW TOKEN
136                 </Button> }
137                 <Typography >
138                     Arvados
139                             <a href='http://doc.arvados.org/user/reference/api-tokens.html' target='blank' className={classes.link}>virtual machines</a>
140                     do this for you automatically. This setup is needed only when you use the API remotely (e.g., from your own workstation).
141                 </Typography>
142             </DialogContent>
143             <DialogActions>
144                 <Button onClick={closeDialog} className={classes.button} color="primary">CLOSE</Button>
145             </DialogActions>
146         </Dialog>;
147     }
148 }
149
150 export const TokenDialog =
151     withStyles(styles)(
152         connect(getTokenDialogData)(
153             withDialog(TOKEN_DIALOG_NAME)(TokenDialogComponent)));
154