MediaWiki master
ResetPasswordSecondaryAuthenticationProvider.php
Go to the documentation of this file.
1<?php
22namespace MediaWiki\Auth;
23
27
42
44 public function getAuthenticationRequests( $action, array $options ) {
45 return [];
46 }
47
49 public function beginSecondaryAuthentication( $user, array $reqs ) {
50 return $this->tryReset( $user, $reqs );
51 }
52
54 public function continueSecondaryAuthentication( $user, array $reqs ) {
55 return $this->tryReset( $user, $reqs );
56 }
57
59 public function beginSecondaryAccountCreation( $user, $creator, array $reqs ) {
60 return $this->tryReset( $user, $reqs );
61 }
62
64 public function continueSecondaryAccountCreation( $user, $creator, array $reqs ) {
65 return $this->tryReset( $user, $reqs );
66 }
67
74 protected function tryReset( User $user, array $reqs ) {
75 $data = $this->manager->getAuthenticationSessionData( 'reset-pass' );
76 if ( !$data ) {
78 }
79
80 if ( is_array( $data ) ) {
81 $data = (object)$data;
82 }
83 if ( !is_object( $data ) ) {
84 throw new \UnexpectedValueException( 'reset-pass is not valid' );
85 }
86
87 if ( !isset( $data->msg ) ) {
88 throw new \UnexpectedValueException( 'reset-pass msg is missing' );
89 } elseif ( !$data->msg instanceof Message ) {
90 throw new \UnexpectedValueException( 'reset-pass msg is not valid' );
91 } elseif ( !isset( $data->hard ) ) {
92 throw new \UnexpectedValueException( 'reset-pass hard is missing' );
93 } elseif ( isset( $data->req ) && (
94 !$data->req instanceof PasswordAuthenticationRequest ||
95 !array_key_exists( 'retype', $data->req->getFieldInfo() )
96 ) ) {
97 throw new \UnexpectedValueException( 'reset-pass req is not valid' );
98 }
99
100 if ( !$data->hard ) {
101 $req = ButtonAuthenticationRequest::getRequestByName( $reqs, 'skipReset' );
102 if ( $req ) {
103 $this->manager->removeAuthenticationSessionData( 'reset-pass' );
105 }
106 }
107
109 $needReq = $data->req ?? new PasswordAuthenticationRequest();
110 '@phan-var PasswordAuthenticationRequest $needReq';
111 if ( !$needReq->action ) {
112 $needReq->action = AuthManager::ACTION_CHANGE;
113 }
114 $needReq->required = $data->hard ? AuthenticationRequest::REQUIRED
116 $needReqs = [ $needReq ];
117 if ( !$data->hard ) {
118 $needReqs[] = new ButtonAuthenticationRequest(
119 'skipReset',
120 wfMessage( 'authprovider-resetpass-skip-label' ),
121 wfMessage( 'authprovider-resetpass-skip-help' )
122 );
123 }
124
126 $req = AuthenticationRequest::getRequestByClass( $reqs, get_class( $needReq ) );
127 '@phan-var PasswordAuthenticationRequest $req';
128 if ( !$req || !array_key_exists( 'retype', $req->getFieldInfo() ) ) {
129 return AuthenticationResponse::newUI( $needReqs, $data->msg, 'warning' );
130 }
131
132 if ( $req->password !== $req->retype ) {
133 return AuthenticationResponse::newUI( $needReqs, new Message( 'badretype' ), 'error' );
134 }
135
136 $req->username = $user->getName();
137 $status = $this->manager->allowsAuthenticationDataChange( $req );
138 if ( !$status->isGood() ) {
139 return AuthenticationResponse::newUI( $needReqs, $status->getMessage(), 'error' );
140 }
141 $scope = LoggerFactory::getContext()->addScoped( [
142 'context.passwordResetOnLogin' => $data->hard ? 'forced' : 'suggested',
143 ] );
144 $this->manager->changeAuthenticationData( $req );
145
146 $this->manager->removeAuthenticationSessionData( 'reset-pass' );
148 }
149}
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.AuthenticationResponse Expected responses:PASS: The user creation is ...
continueSecondaryAuthentication( $user, array $reqs)
Continue an authentication flow.AuthenticationResponse Expected responses:PASS: The user is authentic...
beginSecondaryAccountCreation( $user, $creator, array $reqs)
Start an account creation flow.There is no guarantee this will be called in a successful account crea...
getAuthenticationRequests( $action, array $options)
Return the applicable list of AuthenticationRequests.Possible values for $action depend on whether th...
beginSecondaryAuthentication( $user, array $reqs)
Start an authentication flow.Note that this may be called for a user even if beginSecondaryAccountCre...
Create PSR-3 logger objects.
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:157
User class for the MediaWiki software.
Definition User.php:123
getName()
Get the user name, or the IP of an anonymous user.
Definition User.php:1585