MediaWiki REL1_32
SpecialOATHEnable.php
Go to the documentation of this file.
1<?php
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}
Special page which uses an HTMLForm to handle processing.
Object handling generic submission, CSRF protection, layout and other logic for UI forms.
Definition HTMLForm.php:136
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
static newFromRandom()
Make a new key from random values.
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
Definition OATHUser.php:24
getAccount()
Definition OATHUser.php:62
getIssuer()
Definition OATHUser.php:51
setKey(OATHAuthKey $key=null)
Set the key associated with this user.
Definition OATHUser.php:80
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
checkExecutePermissions(User $user)
Require users to be logged in.
onSubmit(array $formData)
onSuccess()
Do something exciting on successful processing of the form, most likely to show a confirmation messag...
getSecretForDisplay(OATHAuthKey $key)
Retrieve the current secret for display purposes.
__construct(OATHUserRepository $repository, OATHUser $user)
Initialize the OATH user based on the current local User object in the context.
tokenFormatterFunction( $token)
Formats a key or scratch token by creating groups of 4 separated by space characters.
createResourceList( $resources)
OATHUserRepository $OATHRepository
alterForm(HTMLForm $form)
Set the page title and add JavaScript RL modules.
doesWrites()
Indicates whether this special page may perform database writes.
getScratchTokensForDisplay(OATHAuthKey $key)
Retrieve current scratch tokens for display purposes.
getOutput()
Get the OutputPage being used for this instance.
requireLogin( $reasonMsg='exception-nologin-text', $titleMsg='exception-nologin')
If the user is not logged in, throws UserNotLoggedIn error.
msg( $key)
Wrapper around wfMessage that sets the current context.
getRequest()
Get the WebRequest being used for this instance.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:47
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 local account $user
Definition hooks.txt:247
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))