forked from redpwn/rctf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request redpwn#200 from redpwn/feature/client-ctftime
Add ctftime login and registration frontend
- Loading branch information
Showing
13 changed files
with
276 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { Component } from 'preact' | ||
import Form from '../components/form' | ||
import config from '../../../config/client' | ||
import 'linkstate/polyfill' | ||
import withStyles from '../components/jss' | ||
import { register } from '../api/auth' | ||
import UserCircle from '../icons/user-circle.svg' | ||
|
||
export default withStyles({ | ||
root: { | ||
padding: '1.5em' | ||
}, | ||
submit: { | ||
marginTop: '1.5em' | ||
}, | ||
title: { | ||
textAlign: 'center' | ||
} | ||
}, class CtftimeAdditional extends Component { | ||
state = { | ||
showName: false, | ||
disabledButton: false, | ||
division: '', | ||
name: '', | ||
errors: {} | ||
} | ||
|
||
render ({ classes }, { showName, disabledButton, division, name, errors }) { | ||
return ( | ||
<div class='row u-center'> | ||
<h4 class={`col-12 ${classes.title}`}>Finish registration</h4> | ||
<Form class={`${classes.root} col-6`} onSubmit={this.handleSubmit} disabled={disabledButton} errors={errors} buttonText='Register'> | ||
<select required class='select' name='division' value={division} onChange={this.linkState('division')}> | ||
<option value='' disabled selected>Division</option> | ||
{ | ||
Object.entries(config.divisions).map(([name, code]) => { | ||
return <option key={code} value={code}>{name}</option> | ||
}) | ||
} | ||
</select> | ||
{showName && ( | ||
<input autofocus required icon={<UserCircle />} name='name' placeholder='Team Name' type='text' value={name} onChange={this.linkState('name')} /> | ||
)} | ||
</Form> | ||
</div> | ||
) | ||
} | ||
|
||
handleSubmit = (e) => { | ||
e.preventDefault() | ||
|
||
this.setState({ | ||
disabledButton: true | ||
}) | ||
|
||
register({ | ||
ctftimeToken: this.props.ctftimeToken, | ||
division: this.state.division, | ||
name: this.state.name || undefined | ||
}) | ||
.then(errors => { | ||
if (!errors) { | ||
return | ||
} | ||
if (errors.name) { | ||
this.setState({ | ||
showName: true | ||
}) | ||
} | ||
|
||
this.setState({ | ||
errors, | ||
disabledButton: false | ||
}) | ||
}) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Component } from 'preact' | ||
import Ctftime from '../icons/ctftime.svg' | ||
import openPopup from '../util/ctftime' | ||
import withStyles from '../components/jss' | ||
import { ctftimeCallback } from '../api/auth' | ||
import { withToast } from '../components/toast' | ||
|
||
export default withStyles({ | ||
ctftimeButton: { | ||
margin: 'auto', | ||
lineHeight: '0', | ||
padding: '10px', | ||
'& svg': { | ||
width: '150px' | ||
} | ||
}, | ||
or: { | ||
textAlign: 'center', | ||
display: 'block', | ||
margin: '15px auto' | ||
} | ||
}, withToast(class CtftimeButton extends Component { | ||
componentDidMount () { | ||
window.addEventListener('message', this.handlePostMessage) | ||
} | ||
|
||
componentWillUnmount () { | ||
window.removeEventListener('message', this.handlePostMessage) | ||
} | ||
|
||
oauthState = null | ||
|
||
handlePostMessage = async (evt) => { | ||
if (evt.origin !== location.origin) { | ||
return | ||
} | ||
if (evt.data.kind !== 'ctftimeCallback') { | ||
return | ||
} | ||
if (this.oauthState === null || evt.data.state !== this.oauthState) { | ||
return | ||
} | ||
const { kind, message, data } = await ctftimeCallback({ | ||
ctftimeCode: evt.data.ctftimeCode | ||
}) | ||
if (kind !== 'goodCtftimeToken') { | ||
this.props.toast({ | ||
body: message, | ||
type: 'error' | ||
}) | ||
return | ||
} | ||
this.props.onCtftimeDone(data.ctftimeToken) | ||
} | ||
|
||
handleClick = () => { | ||
this.oauthState = openPopup() | ||
} | ||
|
||
render ({ classes, ...props }) { | ||
return ( | ||
<div {...props} > | ||
<div class={classes.or}> | ||
<h5>or</h5> | ||
</div> | ||
<button class={classes.ctftimeButton} onClick={this.handleClick}> | ||
<Ctftime /> | ||
</button> | ||
</div> | ||
) | ||
} | ||
})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Component } from 'preact' | ||
|
||
export default class CtftimeCallback extends Component { | ||
componentDidMount () { | ||
window.opener.postMessage({ | ||
kind: 'ctftimeCallback', | ||
state: this.props.state, | ||
ctftimeCode: this.props.code | ||
}) | ||
window.close() | ||
} | ||
|
||
render () { | ||
return null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.