Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3078 otp expiration timer #3200

Merged
merged 2 commits into from
Apr 6, 2023
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
10 changes: 10 additions & 0 deletions monkey/monkey_island/cc/ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions monkey/monkey_island/cc/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"react-router-dom": "6.8.2",
"react-spinners": "0.13.8",
"react-table": "^6.11.5",
"react-timer-hook": "^3.0.5",
"react-tsparticles": "^1.43.1",
"remark-breaks": "3.0.2",
"request": "^2.88.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import GenerateLocalLinuxCurl from '../commands/local_linux_curl';
import CommandDisplay from '../utils/CommandDisplay';
import {Button, Form, Col} from 'react-bootstrap';
import IslandHttpClient, { APIEndpoint } from '../../../IslandHttpClient';
import { useTimer } from 'react-timer-hook';
import { CommandExpirationTimer } from '../utils/CommandExpirationTimer';


const LocalManualRunOptions = (props) => {
Expand All @@ -24,6 +26,12 @@ const getContents = (props) => {
[OS_TYPES.LINUX_64]: 'Linux 64bit'
}

const {
seconds,
minutes,
restart
} = useTimer({ expiryTimestamp: new Date(), onExpire: () => getOtp() });

const [otp, setOtp] = useState('');
const [osType, setOsType] = useState(OS_TYPES.WINDOWS_64);
const [selectedIp, setSelectedIp] = useState(props.ips[0]);
Expand Down Expand Up @@ -52,8 +60,9 @@ const getContents = (props) => {
}

function getOtp() {
IslandHttpClient.get(APIEndpoint.agent_otp).then(res =>{
IslandHttpClient.get(APIEndpoint.agent_otp).then(res => {
setOtp(res.body.otp);
restart(newExpirationTime());
});
}

Expand All @@ -66,6 +75,13 @@ const getContents = (props) => {
}
}

function newExpirationTime() {
const time = new Date();
time.setSeconds(time.getSeconds() + 120);
console.log('timeout is now: ' + time);
return time;
}

return (
<>
<DropdownSelect defaultKey={OS_TYPES.WINDOWS_64} options={osTypes} onClick={setOsType} variant={'outline-monkey'}/>
Expand All @@ -84,9 +100,14 @@ const getContents = (props) => {
</div>
</div>
<CommandDisplay commands={commands} onCopy={getOtp} />
<Col lg={{span:3, offset: 9}} md={{span:4, offset: 8}} sm={{span:4, offset: 8}} xs={12}>
<Button style={{'float': 'right'}} title="Refresh OTP" onClick={getOtp}>Refresh OTP</Button>
</Col>
<div style={{marginTop: '-0.5em', marginBottom: '0.5em'}}>
<CommandExpirationTimer minutes={minutes} seconds={seconds}/>
</div>
<div style={{textAlign: 'right'}}>
<span>
<Button title="Copy to Clipboard" onClick={getOtp}>Refresh OTP</Button>
</span>
</div>
</>
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from 'react';

export function CommandExpirationTimer({ minutes, seconds }) {

function formatDigits(number) {
return number.toLocaleString('en-US', { minimumIntegerDigits: 2, useGrouping: false})
}

function generateText() {
if (minutes === 0 && seconds === 0) {
return 'Command expired';
Comment on lines +10 to +11
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we regenerate the command and restart the timer here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: The useTimer method allows one to register an onExpire callback

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Screen.Recording.2023-04-06.at.7.38.29.AM.mov

} else {
return 'Command expires in: ' + formatDigits(minutes) + ':' + formatDigits(seconds);
}
}

return (
<div style={{textAlign: 'right'}}>
{generateText()}
</div>
);
}