|
| 1 | +import React from 'react'; |
| 2 | +import { Container, Row, Col, Form, Button, Alert } from "react-bootstrap"; |
| 3 | +import Ajax from '../Ajax'; |
| 4 | + |
| 5 | +interface TotpEnableFormState { |
| 6 | + isLoading: boolean |
| 7 | + hasDataError: boolean |
| 8 | + stateInit: boolean |
| 9 | + stateConfirm: boolean |
| 10 | + stateFinish: boolean |
| 11 | + secret: string |
| 12 | + qrCode: string |
| 13 | + otp: string |
| 14 | +} |
| 15 | + |
| 16 | +export default class TotpEnableForm extends React.Component<{}, TotpEnableFormState> { |
| 17 | + constructor(props: any) { |
| 18 | + super(props); |
| 19 | + this.state = { |
| 20 | + isLoading: false, |
| 21 | + hasDataError: false, |
| 22 | + stateInit: true, |
| 23 | + stateConfirm: false, |
| 24 | + stateFinish: false, |
| 25 | + secret: "", |
| 26 | + qrCode: "", |
| 27 | + otp: "" |
| 28 | + }; |
| 29 | + this.handleChange = this.handleChange.bind(this); |
| 30 | + this.handleSubmitInit = this.handleSubmitInit.bind(this); |
| 31 | + this.handleSubmitConfirm = this.handleSubmitConfirm.bind(this); |
| 32 | + } |
| 33 | + |
| 34 | + handleChange(event: React.FormEvent) { |
| 35 | + let target = event.target as HTMLInputElement; |
| 36 | + this.setState({[target.name]: target.value} as React.ComponentState ); |
| 37 | + event.preventDefault(); |
| 38 | + } |
| 39 | + |
| 40 | + handleSubmitInit(event: React.FormEvent) { |
| 41 | + event.preventDefault(); |
| 42 | + this.setState({ |
| 43 | + isLoading: true, |
| 44 | + hasDataError: false |
| 45 | + }); |
| 46 | + Ajax.postData("/auth/otp/init").then(res => { |
| 47 | + if (res.status === 200) { |
| 48 | + this.setState({ |
| 49 | + isLoading: false, |
| 50 | + hasDataError: false, |
| 51 | + stateInit: false, |
| 52 | + stateConfirm: true, |
| 53 | + secret: res.json.secret, |
| 54 | + qrCode: "data:image/png;base64," + res.json.image |
| 55 | + }); |
| 56 | + return; |
| 57 | + } |
| 58 | + this.setState({ |
| 59 | + hasDataError: true, |
| 60 | + isLoading: false |
| 61 | + }); |
| 62 | + }).catch(() => { |
| 63 | + this.setState({ |
| 64 | + hasDataError: true, |
| 65 | + isLoading: false |
| 66 | + }); |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + handleSubmitConfirm(event: React.FormEvent) { |
| 71 | + event.preventDefault(); |
| 72 | + this.setState({ |
| 73 | + isLoading: true, |
| 74 | + hasDataError: false |
| 75 | + }); |
| 76 | + let payload = { |
| 77 | + passcode: this.state.otp |
| 78 | + }; |
| 79 | + Ajax.postData("/auth/otp/confirm", payload).then(res => { |
| 80 | + if (res.status === 204) { |
| 81 | + this.setState({ |
| 82 | + isLoading: false, |
| 83 | + hasDataError: false, |
| 84 | + stateConfirm: false, |
| 85 | + stateFinish: true |
| 86 | + }); |
| 87 | + return; |
| 88 | + } |
| 89 | + this.setState({ |
| 90 | + hasDataError: true, |
| 91 | + isLoading: false |
| 92 | + }); |
| 93 | + }).catch(() => { |
| 94 | + this.setState({ |
| 95 | + hasDataError: true, |
| 96 | + isLoading: false |
| 97 | + }); |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + render() { |
| 102 | + return ( |
| 103 | + <Container> |
| 104 | + <Row className="justify-content-md-center"> |
| 105 | + <Col lg="5"> |
| 106 | + <h1 className="display-4 font-weight-normal">Security</h1> |
| 107 | + <Alert variant="danger" show={this.state.hasDataError}>Please verify the data you have entered.</Alert> |
| 108 | + <Form onSubmit={this.handleSubmitInit} hidden={!this.state.stateInit}> |
| 109 | + <p>Two-Factor Authentication is not enabled yet. Enable it to add an additional layer of security to your account.</p> |
| 110 | + <Button variant="primary" type="submit" disabled={this.state.isLoading}>{this.state.isLoading ? 'Loading...' : 'Enable 2FA'}</Button> |
| 111 | + </Form> |
| 112 | + <Form onSubmit={this.handleSubmitConfirm} hidden={!this.state.stateConfirm}> |
| 113 | + <p>Scan the barcode below with your authenticator app, orenter the code below if you can't use the barcode.</p> |
| 114 | + <p><img src={this.state.qrCode} alt="" /></p> |
| 115 | + <p>{this.state.secret}</p> |
| 116 | + <Form.Group controlId="otp"> |
| 117 | + <Form.Label>Six-Digit Code from authenticator app</Form.Label> |
| 118 | + <Form.Control type="text" pattern="\d*" name="otp" placeholder="Time-based One-time Password (TOTP)" value={this.state.otp} onChange={this.handleChange} minLength={6} maxLength={6} required={true} /> |
| 119 | + </Form.Group> |
| 120 | + <Button variant="primary" type="submit" disabled={this.state.isLoading}>{this.state.isLoading ? 'Loading...' : 'Finish 2FA Setup'}</Button> |
| 121 | + </Form> |
| 122 | + <div hidden={!this.state.stateFinish}> |
| 123 | + <p>2FA is now enabled for your account.</p> |
| 124 | + </div> |
| 125 | + </Col> |
| 126 | + </Row> |
| 127 | + </Container> |
| 128 | + ); |
| 129 | + } |
| 130 | +} |
0 commit comments