MediaWiki  1.32.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 
172  public function onSubmit( array $formData ) {
174  $key = $this->getRequest()->getSessionData( 'oathauth_key' );
175 
176  if ( $key->isScratchToken( $formData['token'] ) ) {
177  // A scratch token is not allowed for enrollement
178  return [ 'oathauth-noscratchforvalidation' ];
179  }
180  if ( !$key->verifyToken( $formData['token'], $this->OATHUser ) ) {
181  return [ 'oathauth-failedtovalidateoath' ];
182  }
183 
184  $this->getRequest()->setSessionData( 'oathauth_key', null );
185  $this->OATHUser->setKey( $key );
186  $this->OATHRepository->persist( $this->OATHUser );
187 
188  return true;
189  }
190 
191  public function onSuccess() {
192  $this->getOutput()->addWikiMsg( 'oathauth-validatedoath' );
193  $this->getOutput()->returnToMain();
194  }
195 
200  private function createResourceList( $resources ) {
201  $resourceList = '';
202  foreach ( $resources as $resource ) {
203  $resourceList .= Html::rawElement( 'li', [], Html::rawElement( 'kbd', [], $resource ) );
204  }
205  return Html::rawElement( 'ul', [], $resourceList );
206  }
207 
216  protected function getSecretForDisplay( OATHAuthKey $key ) {
217  return $this->tokenFormatterFunction( $key->getSecret() );
218  }
219 
228  protected function getScratchTokensForDisplay( OATHAuthKey $key ) {
229  return array_map( [ $this, 'tokenFormatterFunction' ], $key->getScratchTokens() );
230  }
231 
238  private function tokenFormatterFunction( $token ) {
239  return implode( ' ', str_split( $token, 4 ) );
240  }
241 }
SpecialOATHEnable\checkExecutePermissions
checkExecutePermissions(User $user)
Require users to be logged in.
Definition: SpecialOATHEnable.php:82
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:244
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:22
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:191
FormSpecialPage
Special page which uses an HTMLForm to handle processing.
Definition: FormSpecialPage.php:31
SpecialOATHEnable\createResourceList
createResourceList( $resources)
Definition: SpecialOATHEnable.php:200
OATHAuthKey\getScratchTokens
getScratchTokens()
Definition: OATHAuthKey.php:86
SpecialOATHEnable\$OATHRepository
OATHUserRepository $OATHRepository
Definition: SpecialOATHEnable.php:26
OATHAuthKey\getSecret
getSecret()
Definition: OATHAuthKey.php:79
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
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
Definition: OATHAuthKey.php:26
SpecialOATHEnable\onSubmit
onSubmit(array $formData)
Definition: SpecialOATHEnable.php:172
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
HTMLForm\setMessagePrefix
setMessagePrefix( $p)
Set the prefix for various default messages.
Definition: HTMLForm.php:1568
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:1535
OATHUser\getIssuer
getIssuer()
Definition: OATHUser.php:51
SpecialOATHEnable\getSecretForDisplay
getSecretForDisplay(OATHAuthKey $key)
Retrieve the current secret for display purposes.
Definition: SpecialOATHEnable.php:216
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:238
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
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:210
SpecialOATHEnable\getFormFields
getFormFields()
Definition: SpecialOATHEnable.php:91
SpecialOATHEnable\getScratchTokensForDisplay
getScratchTokensForDisplay(OATHAuthKey $key)
Retrieve current scratch tokens for display purposes.
Definition: SpecialOATHEnable.php:228
Html\element
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:232
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:47
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:136
OATHAuthKey\newFromRandom
static newFromRandom()
Make a new key from random values.
Definition: OATHAuthKey.php:50
SpecialOATHEnable\$OATHUser
OATHUser $OATHUser
Definition: SpecialOATHEnable.php:29