MediaWiki  master
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 
117  $req = AuthenticationRequest::getRequestByClass( $reqs, get_class( $needReq ) );
118  '@phan-var PasswordAuthenticationRequest $req';
119  if ( !$req || !array_key_exists( 'retype', $req->getFieldInfo() ) ) {
120  return AuthenticationResponse::newUI( $needReqs, $data->msg, 'warning' );
121  }
122 
123  if ( $req->password !== $req->retype ) {
124  return AuthenticationResponse::newUI( $needReqs, new \Message( 'badretype' ), 'error' );
125  }
126 
127  $req->username = $user->getName();
128  $status = $this->manager->allowsAuthenticationDataChange( $req );
129  if ( !$status->isGood() ) {
130  return AuthenticationResponse::newUI( $needReqs, $status->getMessage(), 'error' );
131  }
132  $this->manager->changeAuthenticationData( $req );
133 
134  $this->manager->removeAuthenticationSessionData( 'reset-pass' );
136  }
137 }
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
A base class that implements some of the boilerplate for a SecondaryAuthenticationProvider.
const ACTION_CHANGE
Change a user's credentials.
const OPTIONAL
Indicates that the request is not required for authentication to proceed.
const REQUIRED
Indicates that the request is required for authentication to proceed.
static getRequestByClass(array $reqs, $class, $allowSubclasses=false)
Select a request by class name.
static newUI(array $reqs, Message $msg, $msgtype='warning')
This is an authentication request that just implements a simple button.
static getRequestByName(array $reqs, $name)
Fetch a ButtonAuthenticationRequest or subclass by name.
This is a value object for authentication requests with a username and password.
Reset the local password, if signalled via $this->manager->setAuthenticationSessionData()
continueSecondaryAccountCreation( $user, $creator, array $reqs)
Continue an authentication flow.User being created (has been added to the database)....
continueSecondaryAuthentication( $user, array $reqs)
Continue an authentication flow.User being authenticated. This may become a "UserValue" in the future...
beginSecondaryAccountCreation( $user, $creator, array $reqs)
Start an account creation flow.
getAuthenticationRequests( $action, array $options)
Return the applicable list of AuthenticationRequests.
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition: Message.php:142
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:70
getName()
Get the user name, or the IP of an anonymous user.
Definition: User.php:1667