MediaWiki  1.34.0
ResetPasswordSecondaryAuthenticationProvider.php
Go to the documentation of this file.
1 <?php
22 namespace MediaWiki\Auth;
23 
38 
39  public function getAuthenticationRequests( $action, array $options ) {
40  return [];
41  }
42 
43  public function beginSecondaryAuthentication( $user, array $reqs ) {
44  return $this->tryReset( $user, $reqs );
45  }
46 
47  public function continueSecondaryAuthentication( $user, array $reqs ) {
48  return $this->tryReset( $user, $reqs );
49  }
50 
51  public function beginSecondaryAccountCreation( $user, $creator, array $reqs ) {
52  return $this->tryReset( $user, $reqs );
53  }
54 
55  public function continueSecondaryAccountCreation( $user, $creator, array $reqs ) {
56  return $this->tryReset( $user, $reqs );
57  }
58 
65  protected function tryReset( \User $user, array $reqs ) {
66  $data = $this->manager->getAuthenticationSessionData( 'reset-pass' );
67  if ( !$data ) {
69  }
70 
71  if ( is_array( $data ) ) {
72  $data = (object)$data;
73  }
74  if ( !is_object( $data ) ) {
75  throw new \UnexpectedValueException( 'reset-pass is not valid' );
76  }
77 
78  if ( !isset( $data->msg ) ) {
79  throw new \UnexpectedValueException( 'reset-pass msg is missing' );
80  } elseif ( !$data->msg instanceof \Message ) {
81  throw new \UnexpectedValueException( 'reset-pass msg is not valid' );
82  } elseif ( !isset( $data->hard ) ) {
83  throw new \UnexpectedValueException( 'reset-pass hard is missing' );
84  } elseif ( isset( $data->req ) && (
85  !$data->req instanceof PasswordAuthenticationRequest ||
86  !array_key_exists( 'retype', $data->req->getFieldInfo() )
87  ) ) {
88  throw new \UnexpectedValueException( 'reset-pass req is not valid' );
89  }
90 
91  if ( !$data->hard ) {
92  $req = ButtonAuthenticationRequest::getRequestByName( $reqs, 'skipReset' );
93  if ( $req ) {
94  $this->manager->removeAuthenticationSessionData( 'reset-pass' );
96  }
97  }
98 
100  $needReq = $data->req ?? new PasswordAuthenticationRequest();
101  '@phan-var PasswordAuthenticationRequest $needReq';
102  if ( !$needReq->action ) {
103  $needReq->action = AuthManager::ACTION_CHANGE;
104  }
105  $needReq->required = $data->hard ? AuthenticationRequest::REQUIRED
107  $needReqs = [ $needReq ];
108  if ( !$data->hard ) {
109  $needReqs[] = new ButtonAuthenticationRequest(
110  'skipReset',
111  wfMessage( 'authprovider-resetpass-skip-label' ),
112  wfMessage( 'authprovider-resetpass-skip-help' )
113  );
114  }
115 
116  $req = AuthenticationRequest::getRequestByClass( $reqs, get_class( $needReq ) );
117  if ( !$req || !array_key_exists( 'retype', $req->getFieldInfo() ) ) {
118  return AuthenticationResponse::newUI( $needReqs, $data->msg, 'warning' );
119  }
120 
121  if ( $req->password !== $req->retype ) {
122  return AuthenticationResponse::newUI( $needReqs, new \Message( 'badretype' ), 'error' );
123  }
124 
125  $req->username = $user->getName();
126  $status = $this->manager->allowsAuthenticationDataChange( $req );
127  if ( !$status->isGood() ) {
128  return AuthenticationResponse::newUI( $needReqs, $status->getMessage(), 'error' );
129  }
130  $this->manager->changeAuthenticationData( $req );
131 
132  $this->manager->removeAuthenticationSessionData( 'reset-pass' );
134  }
135 }
MediaWiki\Auth\ResetPasswordSecondaryAuthenticationProvider\tryReset
tryReset(\User $user, array $reqs)
Try to reset the password.
Definition: ResetPasswordSecondaryAuthenticationProvider.php:65
MediaWiki\Auth\AuthenticationRequest\OPTIONAL
const OPTIONAL
Indicates that the request is not required for authentication to proceed.
Definition: AuthenticationRequest.php:40
MediaWiki\Auth\AbstractSecondaryAuthenticationProvider
A base class that implements some of the boilerplate for a SecondaryAuthenticationProvider.
Definition: AbstractSecondaryAuthenticationProvider.php:30
MediaWiki\Auth\ResetPasswordSecondaryAuthenticationProvider\beginSecondaryAccountCreation
beginSecondaryAccountCreation( $user, $creator, array $reqs)
Start an account creation flow.
Definition: ResetPasswordSecondaryAuthenticationProvider.php:51
MediaWiki\Auth\ResetPasswordSecondaryAuthenticationProvider
Reset the local password, if signalled via $this->manager->setAuthenticationSessionData()
Definition: ResetPasswordSecondaryAuthenticationProvider.php:37
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1264
Message
MediaWiki\Auth\AuthenticationRequest\getRequestByClass
static getRequestByClass(array $reqs, $class, $allowSubclasses=false)
Select a request by class name.
Definition: AuthenticationRequest.php:263
MediaWiki\Auth\ResetPasswordSecondaryAuthenticationProvider\continueSecondaryAuthentication
continueSecondaryAuthentication( $user, array $reqs)
Continue an authentication flow.
Definition: ResetPasswordSecondaryAuthenticationProvider.php:47
MediaWiki\Auth\AuthenticationResponse\newAbstain
static newAbstain()
Definition: AuthenticationResponse.php:170
MediaWiki\Auth\PasswordAuthenticationRequest
This is a value object for authentication requests with a username and password.
Definition: PasswordAuthenticationRequest.php:29
MediaWiki\Auth\ButtonAuthenticationRequest\getRequestByName
static getRequestByName(array $reqs, $name)
Fetch a ButtonAuthenticationRequest or subclass by name.
Definition: ButtonAuthenticationRequest.php:75
MediaWiki\Auth\ResetPasswordSecondaryAuthenticationProvider\beginSecondaryAuthentication
beginSecondaryAuthentication( $user, array $reqs)
Start an authentication flow.
Definition: ResetPasswordSecondaryAuthenticationProvider.php:43
MediaWiki\Auth\AuthManager\ACTION_CHANGE
const ACTION_CHANGE
Change a user's credentials.
Definition: AuthManager.php:105
MediaWiki\Auth\ResetPasswordSecondaryAuthenticationProvider\continueSecondaryAccountCreation
continueSecondaryAccountCreation( $user, $creator, array $reqs)
Continue an authentication flow.
Definition: ResetPasswordSecondaryAuthenticationProvider.php:55
MediaWiki\Auth\ResetPasswordSecondaryAuthenticationProvider\getAuthenticationRequests
getAuthenticationRequests( $action, array $options)
Return the applicable list of AuthenticationRequests.
Definition: ResetPasswordSecondaryAuthenticationProvider.php:39
MediaWiki\$action
string $action
Cache what action this request is.
Definition: MediaWiki.php:48
$status
return $status
Definition: SyntaxHighlight.php:347
MediaWiki\Auth\ButtonAuthenticationRequest
This is an authentication request that just implements a simple button.
Definition: ButtonAuthenticationRequest.php:31
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:51
MediaWiki\Auth
Definition: AbstractAuthenticationProvider.php:22
MediaWiki\Auth\AuthenticationResponse\newPass
static newPass( $username=null)
Definition: AuthenticationResponse.php:134
User\getName
getName()
Get the user name, or the IP of an anonymous user.
Definition: User.php:2232
MediaWiki\Auth\AuthenticationResponse\newUI
static newUI(array $reqs, Message $msg, $msgtype='warning')
Definition: AuthenticationResponse.php:183
MediaWiki\Auth\AuthenticationRequest\REQUIRED
const REQUIRED
Indicates that the request is required for authentication to proceed.
Definition: AuthenticationRequest.php:46