MediaWiki  1.33.0
SpecialOATHEnable.php
Go to the documentation of this file.
1 <?php
26  private $OATHRepository;
27 
29  private $OATHUser;
30 
37  public function __construct( OATHUserRepository $repository, OATHUser $user ) {
38  parent::__construct( 'OATH', 'oathauth-enable', false );
39 
40  $this->OATHRepository = $repository;
41  $this->OATHUser = $user;
42  }
43 
44  public function doesWrites() {
45  return true;
46  }
47 
53  public function alterForm( HTMLForm $form ) {
54  $form->setMessagePrefix( 'oathauth' );
55  $form->setWrapperLegend( false );
56  $form->getOutput()->setPageTitle( $this->msg( 'oathauth-enable' ) );
57  $form->getOutput()->addModules( 'ext.oath.showqrcode' );
58  $form->getOutput()->addModuleStyles( 'ext.oath.showqrcode.styles' );
59  }
60 
64  protected function getDisplayFormat() {
65  return 'ooui';
66  }
67 
71  public function requiresUnblock() {
72  return false;
73  }
74 
82  protected function checkExecutePermissions( User $user ) {
83  parent::checkExecutePermissions( $user );
84 
85  $this->requireLogin();
86  }
87 
91  protected function getFormFields() {
92  $key = $this->getRequest()->getSessionData( 'oathauth_key' );
93 
94  if ( $key === null ) {
96  $this->getRequest()->setSessionData( 'oathauth_key', $key );
97  }
98 
99  $secret = $key->getSecret();
100  $label = "{$this->OATHUser->getIssuer()}:{$this->OATHUser->getAccount()}";
101  $qrcodeUrl = "otpauth://totp/"
102  . rawurlencode( $label )
103  . "?secret="
104  . rawurlencode( $secret )
105  . "&issuer="
106  . rawurlencode( $this->OATHUser->getIssuer() );
107 
108  $qrcodeElement = Html::element( 'div', [
109  'data-mw-qrcode-url' => $qrcodeUrl,
110  'class' => 'mw-display-qrcode',
111  // Include width/height, so js won't re-arrange layout
112  // And non-js users will have this hidden with CSS
113  'style' => 'width: 256px; height: 256px;'
114  ] );
115 
116  return [
117  'app' => [
118  'type' => 'info',
119  'default' => $this->msg( 'oathauth-step1-test' )->escaped(),
120  'raw' => true,
121  'section' => 'step1',
122  ],
123  'qrcode' => [
124  'type' => 'info',
125  'default' => $qrcodeElement,
126  'raw' => true,
127  'section' => 'step2',
128  ],
129  'manual' => [
130  'type' => 'info',
131  'label-message' => 'oathauth-step2alt',
132  'default' =>
133  '<strong>' . $this->msg( 'oathauth-account' )->escaped() . '</strong><br/>'
134  . $this->OATHUser->getAccount() . '<br/><br/>'
135  . '<strong>' . $this->msg( 'oathauth-secret' )->escaped() . '</strong><br/>'
136  . '<kbd>' . $this->getSecretForDisplay( $key ) . '</kbd><br/>',
137  'raw' => true,
138  'section' => 'step2',
139  ],
140  'scratchtokens' => [
141  'type' => 'info',
142  'default' =>
143  $this->msg( 'oathauth-scratchtokens' )
144  . $this->createResourceList( $this->getScratchTokensForDisplay( $key ) ),
145  'raw' => true,
146  'section' => 'step3',
147  ],
148  'token' => [
149  'type' => 'text',
150  'default' => '',
151  'label-message' => 'oathauth-entertoken',
152  'name' => 'token',
153  'section' => 'step4',
154  ],
155  'returnto' => [
156  'type' => 'hidden',
157  'default' => $this->getRequest()->getVal( 'returnto' ),
158  'name' => 'returnto',
159  ],
160  'returntoquery' => [
161  'type' => 'hidden',
162  'default' => $this->getRequest()->getVal( 'returntoquery' ),
163  'name' => 'returntoquery',
164  ]
165  ];
166  }
167 
173  public function onSubmit( array $formData ) {
175  $key = $this->getRequest()->getSessionData( 'oathauth_key' );
176 
177  if ( $key->isScratchToken( $formData['token'] ) ) {
178  // A scratch token is not allowed for enrollement
179  \MediaWiki\Logger\LoggerFactory::getInstance( 'authentication' )->info(
180  'OATHAuth {user} attempted to enable 2FA using a scratch token from {clientip}', [
181  'user' => $this->getUser()->getName(),
182  'clientip' => $this->getRequest()->getIP(),
183  ]
184  );
185  return [ 'oathauth-noscratchforvalidation' ];
186  }
187  if ( !$key->verifyToken( $formData['token'], $this->OATHUser ) ) {
188  \MediaWiki\Logger\LoggerFactory::getInstance( 'authentication' )->info(
189  'OATHAuth {user} failed to provide a correct token while enabling 2FA from {clientip}', [
190  'user' => $this->getUser()->getName(),
191  'clientip' => $this->getRequest()->getIP(),
192  ]
193  );
194  return [ 'oathauth-failedtovalidateoath' ];
195  }
196 
197  $this->getRequest()->setSessionData( 'oathauth_key', null );
198  $this->OATHUser->setKey( $key );
199  $this->OATHRepository->persist( $this->OATHUser, $this->getRequest()->getIP() );
200 
201  return true;
202  }
203 
204  public function onSuccess() {
205  $this->getOutput()->addWikiMsg( 'oathauth-validatedoath' );
206  $this->getOutput()->returnToMain();
207  }
208 
213  private function createResourceList( $resources ) {
214  $resourceList = '';
215  foreach ( $resources as $resource ) {
216  $resourceList .= Html::rawElement( 'li', [], Html::rawElement( 'kbd', [], $resource ) );
217  }
218  return Html::rawElement( 'ul', [], $resourceList );
219  }
220 
229  protected function getSecretForDisplay( OATHAuthKey $key ) {
230  return $this->tokenFormatterFunction( $key->getSecret() );
231  }
232 
241  protected function getScratchTokensForDisplay( OATHAuthKey $key ) {
242  return array_map( [ $this, 'tokenFormatterFunction' ], $key->getScratchTokens() );
243  }
244 
251  private function tokenFormatterFunction( $token ) {
252  return implode( ' ', str_split( $token, 4 ) );
253  }
254 }
SpecialOATHEnable\checkExecutePermissions
checkExecutePermissions(User $user)
Require users to be logged in.
Definition: SpecialOATHEnable.php:82
$user
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1476
SpecialPage\msg
msg( $key)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:796
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:725
OATHUserRepository
Definition: OATHUserRepository.php:23
MediaWiki\Logger\LoggerFactory\getInstance
static getInstance( $channel)
Get a named logger instance from the currently configured logger factory.
Definition: LoggerFactory.php:92
SpecialOATHEnable
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
Definition: SpecialOATHEnable.php:24
SpecialOATHEnable\alterForm
alterForm(HTMLForm $form)
Set the page title and add JavaScript RL modules.
Definition: SpecialOATHEnable.php:53
OATHUser\getAccount
getAccount()
Definition: OATHUser.php:62
SpecialOATHEnable\onSuccess
onSuccess()
Do something exciting on successful processing of the form, most likely to show a confirmation messag...
Definition: SpecialOATHEnable.php:204
FormSpecialPage
Special page which uses an HTMLForm to handle processing.
Definition: FormSpecialPage.php:31
SpecialOATHEnable\createResourceList
createResourceList( $resources)
Definition: SpecialOATHEnable.php:213
OATHAuthKey\getScratchTokens
getScratchTokens()
Definition: OATHAuthKey.php:89
SpecialOATHEnable\$OATHRepository
OATHUserRepository $OATHRepository
Definition: SpecialOATHEnable.php:26
OATHAuthKey\getSecret
getSecret()
Definition: OATHAuthKey.php:82
SpecialPage\getName
getName()
Get the name of this Special Page.
Definition: SpecialPage.php:152
SpecialOATHEnable\requiresUnblock
requiresUnblock()
Definition: SpecialOATHEnable.php:71
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
OATHAuthKey
Class representing a two-factor key.
Definition: OATHAuthKey.php:29
SpecialOATHEnable\onSubmit
onSubmit(array $formData)
Definition: SpecialOATHEnable.php:173
SpecialOATHEnable\getDisplayFormat
getDisplayFormat()
Definition: SpecialOATHEnable.php:64
ContextSource\getOutput
getOutput()
Definition: ContextSource.php:112
OATHUser
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
Definition: OATHUser.php:24
SpecialPage\getUser
getUser()
Shortcut to get the User executing this instance.
Definition: SpecialPage.php:735
HTMLForm\setMessagePrefix
setMessagePrefix( $p)
Set the prefix for various default messages.
Definition: HTMLForm.php:1563
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
SpecialPage\requireLogin
requireLogin( $reasonMsg='exception-nologin-text', $titleMsg='exception-nologin')
If the user is not logged in, throws UserNotLoggedIn error.
Definition: SpecialPage.php:339
SpecialPage\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: SpecialPage.php:715
HTMLForm\setWrapperLegend
setWrapperLegend( $legend)
Prompt the whole form to be wrapped in a "<fieldset>", with this text as its "<legend>" element.
Definition: HTMLForm.php:1530
OATHUser\getIssuer
getIssuer()
Definition: OATHUser.php:51
SpecialOATHEnable\getSecretForDisplay
getSecretForDisplay(OATHAuthKey $key)
Retrieve the current secret for display purposes.
Definition: SpecialOATHEnable.php:229
SpecialOATHEnable\__construct
__construct(OATHUserRepository $repository, OATHUser $user)
Initialize the OATH user based on the current local User object in the context.
Definition: SpecialOATHEnable.php:37
SpecialOATHEnable\tokenFormatterFunction
tokenFormatterFunction( $token)
Formats a key or scratch token by creating groups of 4 separated by space characters.
Definition: SpecialOATHEnable.php:251
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
SpecialOATHEnable\getFormFields
getFormFields()
Definition: SpecialOATHEnable.php:91
SpecialOATHEnable\getScratchTokensForDisplay
getScratchTokensForDisplay(OATHAuthKey $key)
Retrieve current scratch tokens for display purposes.
Definition: SpecialOATHEnable.php:241
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:48
OATHUser\setKey
setKey(OATHAuthKey $key=null)
Set the key associated with this user.
Definition: OATHUser.php:80
SpecialOATHEnable\doesWrites
doesWrites()
Indicates whether this special page may perform database writes.
Definition: SpecialOATHEnable.php:44
HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms.
Definition: HTMLForm.php:133
OATHAuthKey\newFromRandom
static newFromRandom()
Make a new key from random values.
Definition: OATHAuthKey.php:53
SpecialOATHEnable\$OATHUser
OATHUser $OATHUser
Definition: SpecialOATHEnable.php:29