ed155541e41edea8402cdb284696fbb8d0fd2ab0
[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         }
77     }
78
79     getSnippet = ({ apiHost, token }: TokenDialogData) =>
80         `HISTIGNORE=$HISTIGNORE:'export ARVADOS_API_TOKEN=*'
81 export ARVADOS_API_TOKEN=${token}
82 export ARVADOS_API_HOST=${apiHost}
83 unset ARVADOS_API_HOST_INSECURE`
84
85     render() {
86         const { classes, open, closeDialog, ...data } = this.props;
87
88         return <Dialog
89             open={open}
90             onClose={closeDialog}
91             fullWidth={true}
92             maxWidth='md'>
93             <DialogTitle>Get API Token</DialogTitle>
94             <DialogContent>
95                 <Typography paragraph={true}>
96                     The Arvados API token is a secret key that enables the Arvados SDKs to access Arvados with the proper permissions.
97                         <Typography component='span'>
98                             For more information see
99                             <a href='http://doc.arvados.org/user/reference/api-tokens.html' target='blank' className={classes.link}>
100                                 Getting an API token.
101                             </a>
102                         </Typography>
103                 </Typography>
104                 <Typography paragraph={true}>
105                     Paste the following lines at a shell prompt to set up the necessary environment for Arvados SDKs to authenticate to your account.
106                 </Typography>
107                 <DefaultCodeSnippet lines={[this.getSnippet(data)]} />
108                 <CopyToClipboard text={this.getSnippet(data)} onCopy={() => this.onCopy('Token copied to clipboard')}>
109                     <Button
110                         color="primary"
111                         size="small"
112                         variant="contained"
113                         className={classes.actionButton}
114                     >
115                         COPY TO CLIPBOARD
116                     </Button>
117                 </CopyToClipboard>
118                 <Button
119                     onClick={() => this.onGetNewToken()}
120                     color="primary"
121                     size="small"
122                     variant="contained"
123                     className={classes.actionButton}
124                 >
125                     GET NEW TOKEN
126                 </Button>
127                 <Typography >
128                     Arvados
129                             <a href='http://doc.arvados.org/user/reference/api-tokens.html' target='blank' className={classes.link}>virtual machines</a>
130                     do this for you automatically. This setup is needed only when you use the API remotely (e.g., from your own workstation).
131                 </Typography>
132             </DialogContent>
133             <DialogActions>
134                 <Button onClick={closeDialog} className={classes.button} color="primary">CLOSE</Button>
135             </DialogActions>
136         </Dialog>;
137     }
138 }
139
140 export const TokenDialog =
141     withStyles(styles)(
142         connect(getTokenDialogData)(
143             withDialog(TOKEN_DIALOG_NAME)(TokenDialogComponent)));
144