1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import React from 'react';
16 } from '@material-ui/core';
17 import 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';
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 import { DetailsAttributeComponent } from 'components/details-attribute/details-attribute';
31 import moment from 'moment';
33 type CssRules = 'link' | 'paper' | 'button' | 'actionButton' | 'codeBlock';
35 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
37 color: theme.palette.primary.main,
38 textDecoration: 'none',
42 padding: theme.spacing.unit,
43 marginBottom: theme.spacing.unit * 2,
44 backgroundColor: theme.palette.grey["200"],
45 border: `1px solid ${theme.palette.grey["300"]}`
48 fontSize: '0.8125rem',
53 marginTop: theme.spacing.unit * 2,
54 marginBottom: theme.spacing.unit * 2,
55 marginRight: theme.spacing.unit * 2,
58 fontSize: '0.8125rem',
62 type TokenDialogProps = TokenDialogData & WithDialogProps<{}> & WithStyles<CssRules> & DispatchProp;
64 export class TokenDialogComponent extends React.Component<TokenDialogProps> {
65 onCopy = (message: string) => {
66 this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
69 kind: SnackbarKind.SUCCESS
73 onGetNewToken = async () => {
74 const newToken = await this.props.dispatch<any>(getNewExtraToken());
76 this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
77 message: 'New token retrieved',
79 kind: SnackbarKind.SUCCESS
82 this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
83 message: 'Creating new tokens is not allowed',
85 kind: SnackbarKind.WARNING
90 getSnippet = ({ apiHost, token }: TokenDialogData) =>
91 `HISTIGNORE=$HISTIGNORE:'export ARVADOS_API_TOKEN=*'
92 export ARVADOS_API_TOKEN=${token}
93 export ARVADOS_API_HOST=${apiHost}
94 unset ARVADOS_API_HOST_INSECURE`
97 const { classes, open, closeDialog, ...data } = this.props;
98 const tokenExpiration = data.tokenExpiration
99 ? `${data.tokenExpiration.toLocaleString()} (${moment(data.tokenExpiration).fromNow()})`
100 : `This token does not have an expiration date`;
104 onClose={closeDialog}
107 <DialogTitle>Get API Token</DialogTitle>
109 <Typography paragraph={true}>
110 The Arvados API token is a secret key that enables the Arvados SDKs to access Arvados with the proper permissions.
111 <Typography component='span'>
112 For more information see
113 <a href='http://doc.arvados.org/user/reference/api-tokens.html' target='blank' rel="noopener" className={classes.link}>
114 Getting an API token.
119 <DetailsAttributeComponent label='API Host' value={data.apiHost} copyValue={data.apiHost} onCopy={this.onCopy} />
120 <DetailsAttributeComponent label='API Token' value={data.token} copyValue={data.token} onCopy={this.onCopy} />
121 <DetailsAttributeComponent label='Token expiration' value={tokenExpiration} />
122 {this.props.canCreateNewTokens && <Button
123 onClick={() => this.onGetNewToken()}
127 className={classes.actionButton}
132 <Typography paragraph={true}>
133 Paste the following lines at a shell prompt to set up the necessary environment for Arvados SDKs to authenticate to your account.
135 <DefaultCodeSnippet className={classes.codeBlock} lines={[this.getSnippet(data)]} />
136 <CopyToClipboard text={this.getSnippet(data)} onCopy={() => this.onCopy('Shell code block copied')}>
141 className={classes.actionButton}
143 Copy link to clipBOARD
148 <a href='http://doc.arvados.org/user/reference/api-tokens.html' target='blank' rel="noopener" className={classes.link}>virtual machines</a>
149 do this for you automatically. This setup is needed only when you use the API remotely (e.g., from your own workstation).
153 <Button onClick={closeDialog} className={classes.button} color="primary">CLOSE</Button>
159 export const TokenDialog =
161 connect(getTokenDialogData)(
162 withDialog(TOKEN_DIALOG_NAME)(TokenDialogComponent)));