MediaWiki master
ElevatedSecurityAuthenticationRequest.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Auth;
8
9use LogicException;
11use StatusValue;
12
32
34 public const int MAX_AGE = 300;
35
37 public int $userId;
38
40 public string $securityLevel;
41
42 private string $token;
43
44 private StatusValue $validationStatus;
45
47 private ?Session $session = null;
48
49 private function __construct() {
50 }
51
60 public static function create( Session $session, $securityLevel ) {
61 if ( $session->getUser()->isAnon() ) {
62 throw new LogicException( 'Cannot create an ElevatedSecurityAuthenticationRequest for anon' );
63 }
64
65 $req = new self();
66 $req->userId = $session->getUser()->getId();
67 $req->securityLevel = $securityLevel;
68 $req->session = $session;
69 $req->validationStatus = StatusValue::newFatal( 'authmanager-elevatedsecurity-not-validated' );
70
71 $token = $session->getToken( [ (string)$req->userId, $req->securityLevel ], 'reauth' );
72 $session->persist(); // sanity, in case the caller somehow forgets
73 $req->token = $token->toString();
74
75 return $req;
76 }
77
78 public function __sleep() {
79 // Don't try to serialize the session. Without a session validate() will fail,
80 // but that's OK, this request can only come from getAuthenticationRequests() so
81 // it will only be validated at the beginning of the begin/continue* methods when
82 // it has not gone through session storage yet.
83 return [ 'userId', 'securityLevel', 'token', 'validationStatus' ];
84 }
85
87 public function getFieldInfo() {
88 return [
89 'elevatedSecurityToken' => [
90 'type' => 'hidden',
91 'value' => $this->token,
92 'label' => wfMessage( 'authmanager-elevatedsecurity-token-label' ),
93 'help' => wfMessage( 'authmanager-elevatedsecurity-token-help' ),
94 'sensitive' => true,
95 'skippable' => true,
96 ],
97 ];
98 }
99
101 public function loadFromSubmission( array $data ) {
102 $this->validationStatus = StatusValue::newGood();
103
104 $dataToken = $data['elevatedSecurityToken'] ?? null;
105 if ( !$this->session ) {
106 // loadFromSubmission() was called after serialize+unserialize. This should not happen.
107 throw new LogicException( __METHOD__ . ' called on incomplete object' );
108 }
109 $sessionToken = $this->session->getToken( [ (string)$this->userId, $this->securityLevel ], 'reauth' );
110 if ( !$dataToken ) {
111 // Normally we handle missing fields by returning false but ElevatedSecurityAuthRequest
112 // has a special meaning; if it was present in the initial set of requests, it should not
113 // disappear during loading the data. So here we just record what's present, and will
114 // deal with errors in validate().
115 $this->validationStatus->fatal( 'authmanager-elevatedsecurity-missing-token' );
116 } elseif ( !$sessionToken->match( $dataToken, self::MAX_AGE ) ) {
117 $this->validationStatus->fatal( 'authmanager-elevatedsecurity-invalid-token' );
118 }
119
120 return true;
121 }
122
124 public function validate() {
125 return $this->validationStatus;
126 }
127
128 public static function __set_state( $data ) {
129 $ret = new static();
130 foreach ( $data as $k => $v ) {
131 $ret->$k = $v;
132 }
133 return $ret;
134 }
135
136}
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
This is a value object for authentication requests.
Authentication request for reauthenticating with elevated security.
loadFromSubmission(array $data)
Initialize form submitted form data.The default behavior is to check for each key of self::getFieldIn...
getFieldInfo()
Fetch input field info.This will be used in the AuthManager APIs and web UIs to define API input para...
validate()
Validate the submitted data.This is called by AuthManager sometime after request data was loaded with...
static create(Session $session, $securityLevel)
Create a new elevated-security reauthentication request, and store its data in the current session.
static __set_state( $data)
Implementing this mainly for use from the unit tests.
string $securityLevel
The security level of the reauthentication process.
const int MAX_AGE
Max allowed time between getAuthenticationRequests() and beginAuthentication().
Manages data for an authenticated session.
Definition Session.php:38
getToken( $salt='', $key='default')
Fetch a CSRF token from the session.
Definition Session.php:373
persist()
Make this session persisted across requests.
Definition Session.php:118
getUser()
Return the authenticated user for this session.
Definition Session.php:164
Generic operation result class Has warning/error list, boolean status and arbitrary value.