Skip to content
This repository has been archived by the owner on Mar 27, 2019. It is now read-only.

Add ability to show current token + renew lease #114

Merged
merged 2 commits into from
May 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions app/components/Authentication/Token/Token.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -413,12 +413,14 @@ export default class TokenAuthBackend extends React.Component {
open={this.state.accessorInfoDialog}
onRequestClose={() => this.setState({ accessorInfoDialog: false })}
>
<JsonEditor
rootName={`auth/token/accessors/${this.state.selectedAccessor}`}
value={this.state.accessorDetails[this.state.selectedAccessor]}
mode={'view'}
modes={['view']}
/>
<div>
<JsonEditor
rootName={`auth/token/accessors/${this.state.selectedAccessor}`}
value={this.state.accessorDetails[this.state.selectedAccessor]}
mode={'view'}
modes={['view']}
/>
</div>
</Dialog>
)
}
Expand Down
139 changes: 109 additions & 30 deletions app/components/shared/Header/Header.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ import Github from 'mui-icons/fontawesome/github';
import CountDown from './countdown.js'
import styles from './header.css';
import { callVaultApi, history } from '../../shared/VaultUtils.jsx';
import IconMenu from 'material-ui/IconMenu';
import MenuItem from 'material-ui/MenuItem';
import NavigationMenu from 'material-ui/svg-icons/navigation/menu';
import Divider from 'material-ui/Divider';
import Dialog from 'material-ui/Dialog';
import TextField from 'material-ui/TextField';
import sharedStyles from '../styles.css';
import RaisedButton from 'material-ui/RaisedButton';
import ContentContentCopy from 'material-ui/svg-icons/content/content-copy';
import copy from 'copy-to-clipboard';

var logout = () => {
window.localStorage.removeItem('vaultAccessToken');
Expand All @@ -23,8 +33,15 @@ class Header extends React.Component {
super(props);
this.state = {
serverAddr: window.localStorage.getItem('vaultUrl'),
version: ''
version: '',
tokenDialogOpened: false,
tokenRenewed: false,
ttl: 0
}
_.bindAll(
this,
'renewTokenLease'
);
}

static propTypes = {
Expand All @@ -49,8 +66,43 @@ class Header extends React.Component {
});
}

renewTokenLease() {
callVaultApi('post', 'auth/token/renew-self')
.then((resp) => {
snackBarMessage("Session renewed");
this.setState({ ttl: resp.data.auth.lease_duration, tokenRenewed: true });
})
.catch(snackBarMessage)
}


render() {

let tokenDialogOptions = [
<FlatButton label="Close" primary={true} onTouchTap={() => this.setState({ tokenDialogOpened: false })} />
];

let showToken = () => {
return (
<Dialog
title="Token"
modal={true}
open={this.state.tokenDialogOpened}
actions={tokenDialogOptions}
>
<div className={sharedStyles.newTokenCodeEmitted}>
<TextField
fullWidth={true}
disabled={true}
floatingLabelText="Token"
defaultValue={window.localStorage.getItem("vaultAccessToken")}
/>
<RaisedButton icon={<ContentContentCopy />} label="Copy to Clipboard" onTouchTap={() => { copy(window.localStorage.getItem("vaultAccessToken")) }} />
</div>
</Dialog>
)
}

let renderTokenInfo = () => {

let infoSectionItems = []
Expand All @@ -77,7 +129,16 @@ class Header extends React.Component {
</span>
)

if (this.props.tokenIdentity.ttl) {
if (this.state.tokenRenewed) {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Little bit of a hacky solution, but wasn't sure how to update the TTL once renewed since the value came from a super class. I'm definitely open to suggestions if there are better ways to handle this.

infoSectionItems.push(
<span key="infoSessionTimeout" className={styles.infoSectionItem}>
<span className={styles.infoSectionItemKey}>token ttl</span>
<span className={styles.infoSectionItemValue}>
<CountDown countDown={this.state.ttl} retrigger={this.props.tokenIdentity.last_renewal_time} />
</span>
</span>
);
} else if (this.props.tokenIdentity.ttl) {
infoSectionItems.push(
<span key="infoSessionTimeout" className={styles.infoSectionItem}>
<span className={styles.infoSectionItemKey}>token ttl</span>
Expand All @@ -101,34 +162,52 @@ class Header extends React.Component {
}

return (
<div id={styles.headerWrapper}>
<Toolbar style={{ backgroundColor: '#000000', height: '64px' }}>
<ToolbarGroup firstChild={true}>
<IconButton
onTouchTap={() => {
if(WEBPACK_DEF_TARGET_WEB) {
window.open('https://github.com/djenriquez/vault-ui', '_blank');
} else {
event.preventDefault();
require('electron').shell.openExternal('https://github.com/djenriquez/vault-ui')
}
}}
>
<Github className={styles.title}/>
</IconButton>
<ToolbarTitle className={styles.title}
onTouchTap={() => {
history.push('/');
}}
text="VAULT - UI" />
</ToolbarGroup>
<ToolbarGroup>
{renderTokenInfo()}
</ToolbarGroup>
<ToolbarGroup lastChild={true}>
<FlatButton className={styles.title} onTouchTap={logout} label="Logout" />
</ToolbarGroup>
</Toolbar>
<div>
{showToken()}
<div id={styles.headerWrapper}>
<Toolbar style={{ backgroundColor: '#000000', height: '64px' }}>
<ToolbarGroup firstChild={true}>
<IconButton
onTouchTap={() => {
if (WEBPACK_DEF_TARGET_WEB) {
window.open('https://github.com/djenriquez/vault-ui', '_blank');
} else {
event.preventDefault();
require('electron').shell.openExternal('https://github.com/djenriquez/vault-ui')
}
}}
>
<Github className={styles.title} />
</IconButton>
<ToolbarTitle className={styles.title}
onTouchTap={() => {
history.push('/');
}}
text="VAULT - UI" />
</ToolbarGroup>
<ToolbarGroup>
{renderTokenInfo()}
</ToolbarGroup>
<ToolbarGroup lastChild={true}>
<IconMenu iconButtonElement={<IconButton ><NavigationMenu color='white' /></IconButton>}>
<MenuItem
primaryText="Show token"
onTouchTap={() => this.setState({ tokenDialogOpened: true })}
/>
<Divider />
<MenuItem
primaryText="Renew token lease"
onTouchTap={this.renewTokenLease}
/>
<Divider />
<MenuItem
primaryText="Logout"
onTouchTap={logout}
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logout is preserved and added to the icon menu.

/>
</IconMenu>
</ToolbarGroup>
</Toolbar>
</div>
</div>
)
}
Expand Down
2 changes: 1 addition & 1 deletion app/components/shared/Header/countdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CountDown extends Component {
}

componentWillReceiveProps(nextProps) {
if(nextProps.retrigger !== this.props.retrigger) {
if (nextProps !== this.props) {
clearInterval(this.time);
this.time = nextProps.countDown;
this.stopTime = Date.now() + (nextProps.countDown * 1000)
Expand Down