2012-05-07 21:54:44 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Special page to display key information to the user
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @ingroup Extensions
|
|
|
|
*/
|
|
|
|
|
2013-03-12 01:13:36 +00:00
|
|
|
class SpecialOATH extends UnlistedSpecialPage {
|
2012-05-07 21:54:44 +00:00
|
|
|
|
|
|
|
var $OATHUser;
|
|
|
|
|
|
|
|
function __construct() {
|
|
|
|
parent::__construct( 'OATH' );
|
|
|
|
|
|
|
|
$this->OATHUser = OATHUser::newFromUser( $this->getUser() );
|
|
|
|
}
|
|
|
|
|
|
|
|
function execute( $par ) {
|
2012-06-13 15:48:37 +00:00
|
|
|
if ( !$this->getUser()->isLoggedIn() ) {
|
|
|
|
$this->setHeaders();
|
|
|
|
$this->getOutput()->setPagetitle( wfMsg( 'oathauth-notloggedin' ) );
|
|
|
|
$this->getOutput()->addWikiMsg( 'oathauth-mustbeloggedin' );
|
|
|
|
return;
|
|
|
|
}
|
2012-05-07 21:54:44 +00:00
|
|
|
$action = $this->getRequest()->getVal( 'action' );
|
|
|
|
if ( $action == "enable" ) {
|
|
|
|
$this->enable();
|
|
|
|
} elseif ( $action == "validate" ) {
|
|
|
|
$this->validate();
|
|
|
|
} elseif ( $action == "reset" ) {
|
|
|
|
$this->reset();
|
|
|
|
} elseif ( $action == "disable" ) {
|
|
|
|
$this->disable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function enable() {
|
|
|
|
$this->setHeaders();
|
|
|
|
$this->getOutput()->setPagetitle( wfMsg( 'oathauth-enable' ) );
|
2013-03-12 01:13:36 +00:00
|
|
|
$returnto = $this->getRequest()->getVal( 'returnto' );
|
2012-05-07 21:54:44 +00:00
|
|
|
|
|
|
|
if ( !$this->OATHUser->isEnabled() ) {
|
|
|
|
$result = $this->OATHUser->enable();
|
|
|
|
if ( !$result ) {
|
|
|
|
$this->getOutput()->addWikiMsg( 'oathauth-failedtoenableoauth' );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} elseif ( $this->OATHUser->isEnabled() && $this->OATHUser->isValidated() ) {
|
|
|
|
$this->getOutput()->addWikiMsg( 'oathauth-alreadyenabled' );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$info['token'] = array(
|
|
|
|
'type' => 'text',
|
|
|
|
'default' => '',
|
|
|
|
'label-message' => 'oathauth-token',
|
|
|
|
'name' => 'token',
|
|
|
|
);
|
|
|
|
$info['mode'] = array(
|
|
|
|
'type' => 'hidden',
|
|
|
|
'default' => 'enable',
|
|
|
|
'name' => 'mode',
|
|
|
|
);
|
2013-03-12 01:13:36 +00:00
|
|
|
$info['returnto'] = array(
|
|
|
|
'type' => 'hidden',
|
|
|
|
'default' => $returnto,
|
|
|
|
'name' => 'returnto',
|
|
|
|
);
|
2012-05-07 21:54:44 +00:00
|
|
|
$info['action'] = array(
|
|
|
|
'type' => 'hidden',
|
|
|
|
'default' => 'validate',
|
|
|
|
'name' => 'action',
|
|
|
|
);
|
2014-01-07 15:29:17 +00:00
|
|
|
$form = new HTMLForm(
|
|
|
|
$info,
|
|
|
|
$this->getContext(),
|
|
|
|
'oathauth-verify'
|
|
|
|
);
|
2012-05-07 21:54:44 +00:00
|
|
|
$form->setSubmitID( 'oathauth-validate-submit' );
|
|
|
|
$form->setSubmitCallback( array( $this, 'tryValidateSubmit' ) );
|
|
|
|
$form->show();
|
|
|
|
|
|
|
|
$this->displaySecret();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-11 18:05:16 +00:00
|
|
|
/**
|
|
|
|
* @param $reset bool
|
|
|
|
*/
|
|
|
|
function displaySecret( $reset = false ) {
|
2012-05-07 21:54:44 +00:00
|
|
|
$this->getOutput()->addModules( 'ext.oathauth' );
|
|
|
|
if ( $reset ) {
|
|
|
|
$secret = $this->OATHUser->getSecretReset();
|
|
|
|
} else {
|
|
|
|
$secret = $this->OATHUser->getSecret();
|
|
|
|
}
|
|
|
|
$out = '<strong>' . wfMsgHtml( 'oathauth-account' ) . '</strong> ' . $this->OATHUser->getAccount() . '<br/>';
|
|
|
|
$out .= '<strong>' . wfMsgHtml( 'oathauth-secret' ) . '</strong> ' . $secret . '<br/>';
|
|
|
|
$out .= '<br/>';
|
|
|
|
$out .= '<div id="qrcode"></div>';
|
|
|
|
$this->getOutput()->addInlineScript( 'jQuery("#qrcode").qrcode("otpauth://totp/' . $this->OATHUser->getAccount() . '?secret=' . $secret . '")' );
|
|
|
|
|
|
|
|
$this->getOutput()->addHTML( $out );
|
|
|
|
$this->getOutput()->addWikiMsg( 'openstackmanager-scratchtokens' );
|
|
|
|
if ( $reset ) {
|
|
|
|
$this->getOutput()->addHTML( $this->createResourceList( $this->OATHUser->getScratchTokensReset() ) );
|
|
|
|
} else {
|
|
|
|
$this->getOutput()->addHTML( $this->createResourceList( $this->OATHUser->getScratchTokens() ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function validate() {
|
|
|
|
$this->setHeaders();
|
|
|
|
$this->getOutput()->setPagetitle( wfMsg( 'oathauth-enable' ) );
|
|
|
|
$mode = $this->getRequest()->getVal( 'mode' );
|
2013-03-12 01:13:36 +00:00
|
|
|
$returnto = $this->getRequest()->getVal( 'returnto' );
|
2012-05-07 21:54:44 +00:00
|
|
|
|
|
|
|
$info['token'] = array(
|
|
|
|
'type' => 'text',
|
|
|
|
'default' => '',
|
|
|
|
'label-message' => 'oathauth-token',
|
|
|
|
'name' => 'token',
|
|
|
|
);
|
|
|
|
$info['mode'] = array(
|
|
|
|
'type' => 'hidden',
|
|
|
|
'default' => $mode,
|
|
|
|
'name' => 'mode',
|
|
|
|
);
|
2013-03-12 01:13:36 +00:00
|
|
|
$info['returnto'] = array(
|
|
|
|
'type' => 'hidden',
|
|
|
|
'default' => $returnto,
|
|
|
|
'name' => 'returnto',
|
|
|
|
);
|
2012-05-07 21:54:44 +00:00
|
|
|
$info['action'] = array(
|
|
|
|
'type' => 'hidden',
|
|
|
|
'default' => 'validate',
|
|
|
|
'name' => 'action',
|
|
|
|
);
|
2014-01-07 15:29:17 +00:00
|
|
|
$form = new HTMLForm(
|
|
|
|
$info,
|
|
|
|
$this->getContext(),
|
|
|
|
'oathauth-verify'
|
|
|
|
);
|
2012-05-07 21:54:44 +00:00
|
|
|
$form->setSubmitID( 'oathauth-validate-submit' );
|
|
|
|
$form->setSubmitCallback( array( $this, 'tryValidateSubmit' ) );
|
|
|
|
$form->show();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function reset() {
|
|
|
|
$this->setHeaders();
|
|
|
|
$this->getOutput()->setPagetitle( wfMsg( 'oathauth-reset' ) );
|
2013-03-12 01:13:36 +00:00
|
|
|
$returnto = $this->getRequest()->getVal( 'returnto' );
|
2012-05-07 21:54:44 +00:00
|
|
|
|
|
|
|
$info['token'] = array(
|
|
|
|
'type' => 'text',
|
|
|
|
'label-message' => 'oathauth-currenttoken',
|
|
|
|
'name' => 'token',
|
|
|
|
);
|
2013-03-12 01:13:36 +00:00
|
|
|
$info['returnto'] = array(
|
|
|
|
'type' => 'hidden',
|
|
|
|
'default' => $returnto,
|
|
|
|
'name' => 'returnto',
|
|
|
|
);
|
2012-05-07 21:54:44 +00:00
|
|
|
$info['action'] = array(
|
|
|
|
'type' => 'hidden',
|
|
|
|
'default' => 'reset',
|
|
|
|
'name' => 'action',
|
|
|
|
);
|
2014-01-07 15:29:17 +00:00
|
|
|
$form = new HTMLForm(
|
|
|
|
$info,
|
|
|
|
$this->getContext(),
|
|
|
|
'oathauth-reset'
|
|
|
|
);
|
2012-05-07 21:54:44 +00:00
|
|
|
$form->setSubmitID( 'oauth-form-disablesubmit' );
|
|
|
|
$form->setSubmitCallback( array( $this, 'tryResetSubmit' ) );
|
|
|
|
$form->show();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function disable() {
|
|
|
|
$this->setHeaders();
|
|
|
|
$this->getOutput()->setPagetitle( wfMsg( 'oathauth-disable' ) );
|
2013-03-12 01:13:36 +00:00
|
|
|
$returnto = $this->getRequest()->getVal( 'returnto' );
|
2012-05-07 21:54:44 +00:00
|
|
|
|
|
|
|
$info['token'] = array(
|
|
|
|
'type' => 'text',
|
|
|
|
'label-message' => 'oathauth-token',
|
|
|
|
'name' => 'token',
|
|
|
|
);
|
2013-03-12 01:13:36 +00:00
|
|
|
$info['returnto'] = array(
|
|
|
|
'type' => 'hidden',
|
|
|
|
'default' => $returnto,
|
|
|
|
'name' => 'returnto',
|
|
|
|
);
|
2012-05-07 21:54:44 +00:00
|
|
|
$info['action'] = array(
|
|
|
|
'type' => 'hidden',
|
|
|
|
'default' => 'disable',
|
|
|
|
'name' => 'action',
|
|
|
|
);
|
2014-01-07 15:29:17 +00:00
|
|
|
$form = new HTMLForm(
|
|
|
|
$info,
|
|
|
|
$this->getContext(),
|
|
|
|
'oathauth-disable'
|
|
|
|
);
|
2012-05-07 21:54:44 +00:00
|
|
|
$form->setSubmitID( 'oauth-form-disablesubmit' );
|
|
|
|
$form->setSubmitCallback( array( $this, 'tryDisableSubmit' ) );
|
|
|
|
$form->show();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-11 18:05:16 +00:00
|
|
|
/**
|
|
|
|
* @param $resources array
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-05-07 21:54:44 +00:00
|
|
|
function createResourceList( $resources ) {
|
|
|
|
$resourceList = '';
|
|
|
|
foreach ( $resources as $resource ) {
|
|
|
|
$resourceList .= Html::rawElement( 'li', array(), $resource );
|
|
|
|
}
|
|
|
|
return Html::rawElement( 'ul', array(), $resourceList );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-05-11 18:05:16 +00:00
|
|
|
* @param $formData array
|
|
|
|
* @param $entryPoint string
|
2012-05-07 21:54:44 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function tryValidateSubmit( $formData, $entryPoint = 'internal' ) {
|
|
|
|
$mode = $formData['mode'];
|
|
|
|
if ( $mode == "reset" ) {
|
|
|
|
$reset = true;
|
|
|
|
} else {
|
|
|
|
$reset = false;
|
|
|
|
}
|
|
|
|
$verify = $this->OATHUser->verifyToken( $formData['token'], $reset );
|
|
|
|
if ( $verify ) {
|
|
|
|
if ( $reset ) {
|
|
|
|
$result = $this->OATHUser->reset();
|
|
|
|
} else {
|
|
|
|
$result = $this->OATHUser->validate();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
if ( $result ) {
|
|
|
|
$this->getOutput()->addWikiMsg( 'oathauth-validatedoath' );
|
2013-03-12 01:13:36 +00:00
|
|
|
if ( $formData['returnto'] ) {
|
|
|
|
$out = '<br />';
|
|
|
|
$title = Title::newFromText( $formData['returnto'] );
|
|
|
|
$out = Linker::link( $title, wfMsgHtml( 'oathauth-backtopreferences' ) );
|
|
|
|
}
|
2012-05-07 21:54:44 +00:00
|
|
|
} else {
|
|
|
|
$this->getOutput()->addWikiMsg( 'oathauth-failedtovalidateoauth' );
|
|
|
|
$out = '<br />';
|
|
|
|
|
|
|
|
if ( $reset ) {
|
2013-03-12 01:13:36 +00:00
|
|
|
$out .= Linker::link( $this->getTitle(), wfMsgHtml( 'oathauth-reattemptreset' ), array(), array( 'action' => 'enable', 'mode' => 'reset', 'returnto' => $formData['returnto'] ) );
|
2012-05-07 21:54:44 +00:00
|
|
|
} else {
|
2013-03-12 01:13:36 +00:00
|
|
|
$out .= Linker::link( $this->getTitle(), wfMsgHtml( 'oathauth-reattemptenable' ), array(), array( 'action' => 'enable', 'returnto' => $formData['returnto'] ) );
|
2012-05-07 21:54:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->getOutput()->addHTML( $out );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-05-11 18:05:16 +00:00
|
|
|
* @param $formData array
|
|
|
|
* @param $entryPoint string
|
2012-05-07 21:54:44 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function tryDisableSubmit( $formData, $entryPoint = 'internal' ) {
|
2012-11-21 20:11:19 +00:00
|
|
|
$verify = $this->OATHUser->verifyToken( $formData['token'] );
|
|
|
|
if ( !$verify ) {
|
|
|
|
$this->getOutput()->addWikiMsg( 'oathauth-failedtovalidateoauth' );
|
|
|
|
$out = '<br />';
|
|
|
|
$out .= Linker::link( $this->getTitle(), wfMsgHtml( 'oathauth-reattemptdisable' ), array(), array( 'action' => 'disable' ) );
|
|
|
|
$this->getOutput()->addHTML( $out );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = $this->OATHUser->disable();
|
2012-05-07 21:54:44 +00:00
|
|
|
if ( $result ) {
|
|
|
|
$this->getOutput()->addWikiMsg( 'oathauth-disabledoath' );
|
2013-03-12 01:13:36 +00:00
|
|
|
if ( $formData['returnto'] ) {
|
|
|
|
$out = '<br />';
|
|
|
|
$title = Title::newFromText( $formData['returnto'] );
|
|
|
|
$out .= Linker::link( $title, wfMsgHtml( 'oathauth-backtopreferences' ) );
|
|
|
|
$this->getOutput()->addHTML( $out );
|
|
|
|
}
|
2012-05-07 21:54:44 +00:00
|
|
|
} else {
|
|
|
|
$this->getOutput()->addWikiMsg( 'oathauth-failedtodisableoauth' );
|
|
|
|
$out = '<br />';
|
|
|
|
|
2013-03-12 01:13:36 +00:00
|
|
|
$out .= Linker::link( $this->getTitle(), wfMsgHtml( 'oathauth-reattemptdisable' ), array( 'action' => 'disable', 'returnto' => $formData['returnto'] ) );
|
2012-05-07 21:54:44 +00:00
|
|
|
$this->getOutput()->addHTML( $out );
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-05-11 18:05:16 +00:00
|
|
|
* @param $formData array
|
|
|
|
* @param $entryPoint string
|
2012-05-07 21:54:44 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function tryResetSubmit( $formData, $entryPoint = 'internal' ) {
|
|
|
|
$verify = $this->OATHUser->verifyToken( $formData['token'] );
|
|
|
|
if ( !$verify ) {
|
|
|
|
$this->getOutput()->addWikiMsg( 'oathauth-failedtovalidateoauth' );
|
|
|
|
$out = '<br />';
|
2013-03-12 01:13:36 +00:00
|
|
|
$out .= Linker::link( $this->getTitle(), wfMsgHtml( 'oathauth-reattemptreset' ), array(), array( 'action' => 'reset', 'returnto' => $formData['returnto'] ) );
|
2012-05-07 21:54:44 +00:00
|
|
|
$this->getOutput()->addHTML( $out );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->getOutput()->addWikiMsg( 'oathauth-donotdeleteoldsecret' );
|
|
|
|
$info['token'] = array(
|
|
|
|
'type' => 'text',
|
|
|
|
'default' => '',
|
|
|
|
'label-message' => 'oathauth-newtoken',
|
|
|
|
'name' => 'token',
|
|
|
|
);
|
|
|
|
$info['mode'] = array(
|
|
|
|
'type' => 'hidden',
|
|
|
|
'default' => 'reset',
|
|
|
|
'name' => 'mode',
|
|
|
|
);
|
2013-03-12 01:13:36 +00:00
|
|
|
$info['returnto'] = array(
|
|
|
|
'type' => 'hidden',
|
|
|
|
'default' => $formData['returnto'],
|
|
|
|
'name' => 'returnto',
|
|
|
|
);
|
2012-05-07 21:54:44 +00:00
|
|
|
$info['action'] = array(
|
|
|
|
'type' => 'hidden',
|
|
|
|
'default' => 'validate',
|
|
|
|
'name' => 'action',
|
|
|
|
);
|
|
|
|
$myContext = new DerivativeContext( $this->getContext() );
|
2013-03-12 01:13:36 +00:00
|
|
|
$myRequest = new DerivativeRequest( $this->getRequest(), array( 'action' => 'validate', 'mode' => 'reset', 'token' => '', 'returnto' => $formData['returnto'] ), false );
|
2012-05-07 21:54:44 +00:00
|
|
|
$myContext->setRequest( $myRequest );
|
|
|
|
$form = new HTMLForm( $info, $myContext );
|
|
|
|
$form->setSubmitID( 'oathauth-validate-submit' );
|
|
|
|
$form->setSubmitCallback( array( $this, 'tryValidateSubmit' ) );
|
|
|
|
$form->show();
|
|
|
|
|
|
|
|
$result = $this->OATHUser->setReset();
|
|
|
|
if ( $result ) {
|
|
|
|
$this->displaySecret( true );
|
|
|
|
} else {
|
|
|
|
$this->getOutput()->addWikiMsg( 'oathauth-failedtoresetoath' );
|
|
|
|
$out = '<br />';
|
2013-03-12 01:13:36 +00:00
|
|
|
$out .= Linker::link( $this->getTitle(), wfMsgHtml( 'oathauth-reattemptreset' ), array(), array( 'action' => 'reset', 'returnto' => $formData['returnto'] ) );
|
2012-05-07 21:54:44 +00:00
|
|
|
$this->getOutput()->addHTML( $out );
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|