2014-05-22 07:33:40 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Special page to display key information to the user
|
|
|
|
*
|
|
|
|
* @ingroup Extensions
|
|
|
|
*/
|
|
|
|
class SpecialOATHEnable extends FormSpecialPage {
|
|
|
|
/** @var OATHUserRepository */
|
|
|
|
private $OATHRepository;
|
|
|
|
|
|
|
|
/** @var OATHUser */
|
|
|
|
private $OATHUser;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the OATH user based on the current local User object in the context
|
|
|
|
*
|
|
|
|
* @param OATHUserRepository $repository
|
|
|
|
* @param OATHUser $user
|
|
|
|
*/
|
|
|
|
public function __construct( OATHUserRepository $repository, OATHUser $user ) {
|
2015-05-26 00:08:47 +00:00
|
|
|
parent::__construct( 'OATH', 'oathauth-enable', false );
|
2014-05-22 07:33:40 +00:00
|
|
|
|
|
|
|
$this->OATHRepository = $repository;
|
|
|
|
$this->OATHUser = $user;
|
|
|
|
}
|
|
|
|
|
2016-05-25 19:25:10 +00:00
|
|
|
public function doesWrites() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-22 07:33:40 +00:00
|
|
|
/**
|
|
|
|
* Set the page title and add JavaScript RL modules
|
|
|
|
*
|
|
|
|
* @param HTMLForm $form
|
|
|
|
*/
|
|
|
|
public function alterForm( HTMLForm $form ) {
|
|
|
|
$form->setMessagePrefix( 'oathauth' );
|
|
|
|
$form->setWrapperLegend( false );
|
2016-09-16 23:18:35 +00:00
|
|
|
$form->getOutput()->setPageTitle( $this->msg( 'oathauth-enable' ) );
|
2016-06-20 07:34:42 +00:00
|
|
|
$form->getOutput()->addModules( 'ext.oath.showqrcode' );
|
2014-05-22 07:33:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getDisplayFormat() {
|
|
|
|
return 'vform';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function requiresUnblock() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Require users to be logged in
|
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
*
|
|
|
|
* @return bool|void
|
|
|
|
*/
|
|
|
|
protected function checkExecutePermissions( User $user ) {
|
|
|
|
parent::checkExecutePermissions( $user );
|
|
|
|
|
|
|
|
$this->requireLogin();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array[]
|
|
|
|
*/
|
|
|
|
protected function getFormFields() {
|
|
|
|
$key = $this->getRequest()->getSessionData( 'oathauth_key' );
|
|
|
|
|
|
|
|
if ( $key === null ) {
|
|
|
|
$key = OATHAuthKey::newFromRandom();
|
|
|
|
$this->getRequest()->setSessionData( 'oathauth_key', $key );
|
|
|
|
}
|
|
|
|
|
|
|
|
$secret = $key->getSecret();
|
2016-06-20 07:34:42 +00:00
|
|
|
$qrcodeUrl = "otpauth://totp/"
|
|
|
|
. rawurlencode( $this->OATHUser->getAccount() )
|
|
|
|
. "?secret="
|
|
|
|
. rawurlencode( $secret );
|
2014-05-22 07:33:40 +00:00
|
|
|
|
2016-06-20 07:34:42 +00:00
|
|
|
$qrcodeElement = Html::element( 'div', [
|
|
|
|
'data-mw-qrcode-url' => $qrcodeUrl,
|
|
|
|
'class' => 'mw-display-qrcode',
|
|
|
|
// Include width/height, so js won't re-arrange layout
|
|
|
|
// OTOH, this will cause non-js browsers to see a big empty space.
|
|
|
|
'style' => 'width: 256px; height: 256px;'
|
|
|
|
] );
|
2014-05-22 07:33:40 +00:00
|
|
|
|
2016-09-16 23:18:35 +00:00
|
|
|
return [
|
|
|
|
'app' => [
|
2014-05-22 07:33:40 +00:00
|
|
|
'type' => 'info',
|
|
|
|
'default' => $this->msg( 'oathauth-step1-test' )->escaped(),
|
|
|
|
'raw' => true,
|
|
|
|
'section' => 'step1',
|
2016-09-16 23:18:35 +00:00
|
|
|
],
|
|
|
|
'qrcode' => [
|
2014-05-22 07:33:40 +00:00
|
|
|
'type' => 'info',
|
2016-06-20 07:34:42 +00:00
|
|
|
'default' => $qrcodeElement,
|
2014-05-22 07:33:40 +00:00
|
|
|
'raw' => true,
|
|
|
|
'section' => 'step2',
|
2016-09-16 23:18:35 +00:00
|
|
|
],
|
|
|
|
'manual' => [
|
2014-05-22 07:33:40 +00:00
|
|
|
'type' => 'info',
|
|
|
|
'label-message' => 'oathauth-step2alt',
|
|
|
|
'default' =>
|
|
|
|
'<strong>' . $this->msg( 'oathauth-account' )->escaped() . '</strong><br/>'
|
|
|
|
. $this->OATHUser->getAccount() . '<br/><br/>'
|
|
|
|
. '<strong>' . $this->msg( 'oathauth-secret' )->escaped() . '</strong><br/>'
|
|
|
|
. $key->getSecret() . '<br/>',
|
|
|
|
'raw' => true,
|
|
|
|
'section' => 'step2',
|
2016-09-16 23:18:35 +00:00
|
|
|
],
|
|
|
|
'scratchtokens' => [
|
2014-05-22 07:33:40 +00:00
|
|
|
'type' => 'info',
|
|
|
|
'default' =>
|
|
|
|
$this->msg( 'openstackmanager-scratchtokens' )
|
|
|
|
. $this->createResourceList( $key->getScratchTokens() ),
|
|
|
|
'raw' => true,
|
|
|
|
'section' => 'step3',
|
2016-09-16 23:18:35 +00:00
|
|
|
],
|
|
|
|
'token' => [
|
2014-05-22 07:33:40 +00:00
|
|
|
'type' => 'text',
|
|
|
|
'default' => '',
|
|
|
|
'label-message' => 'oathauth-entertoken',
|
|
|
|
'name' => 'token',
|
|
|
|
'section' => 'step4',
|
2016-09-16 23:18:35 +00:00
|
|
|
],
|
|
|
|
'returnto' => [
|
2014-05-22 07:33:40 +00:00
|
|
|
'type' => 'hidden',
|
|
|
|
'default' => $this->getRequest()->getVal( 'returnto' ),
|
|
|
|
'name' => 'returnto',
|
2016-09-16 23:18:35 +00:00
|
|
|
],
|
|
|
|
'returntoquery' => [
|
2014-05-22 07:33:40 +00:00
|
|
|
'type' => 'hidden',
|
|
|
|
'default' => $this->getRequest()->getVal( 'returntoquery' ),
|
2016-09-16 23:18:35 +00:00
|
|
|
'name' => 'returntoquery', ]
|
|
|
|
];
|
2014-05-22 07:33:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $formData
|
|
|
|
*
|
|
|
|
* @return array|bool
|
|
|
|
*/
|
|
|
|
public function onSubmit( array $formData ) {
|
|
|
|
/** @var OATHAuthKey $key */
|
|
|
|
$key = $this->getRequest()->getSessionData( 'oathauth_key' );
|
|
|
|
|
|
|
|
if ( !$key->verifyToken( $formData['token'], $this->OATHUser ) ) {
|
2016-09-16 23:18:35 +00:00
|
|
|
return [ 'oathauth-failedtovalidateoauth' ];
|
2014-05-22 07:33:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->getRequest()->setSessionData( 'oathauth_key', null );
|
|
|
|
$this->OATHUser->setKey( $key );
|
|
|
|
$this->OATHRepository->persist( $this->OATHUser );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onSuccess() {
|
|
|
|
$this->getOutput()->addWikiMsg( 'oathauth-validatedoath' );
|
|
|
|
$this->getOutput()->returnToMain();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $resources array
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function createResourceList( $resources ) {
|
|
|
|
$resourceList = '';
|
|
|
|
foreach ( $resources as $resource ) {
|
2016-09-16 23:18:35 +00:00
|
|
|
$resourceList .= Html::rawElement( 'li', [], $resource );
|
2014-05-22 07:33:40 +00:00
|
|
|
}
|
2016-09-16 23:18:35 +00:00
|
|
|
return Html::rawElement( 'ul', [], $resourceList );
|
2014-05-22 07:33:40 +00:00
|
|
|
}
|
|
|
|
}
|