MediaWiki  master
SessionInfo.php
Go to the documentation of this file.
1 <?php
24 namespace MediaWiki\Session;
25 
34 class SessionInfo {
36  public const MIN_PRIORITY = 1;
37 
39  public const MAX_PRIORITY = 100;
40 
42  private $provider;
43 
45  private $id;
46 
48  private $priority;
49 
51  private $userInfo = null;
52 
54  private $persisted = false;
55 
57  private $remembered = false;
58 
60  private $forceHTTPS = false;
61 
63  private $idIsSafe = false;
64 
66  private $forceUse = false;
67 
69  private $providerMetadata = null;
70 
96  public function __construct( $priority, array $data ) {
97  if ( $priority < self::MIN_PRIORITY || $priority > self::MAX_PRIORITY ) {
98  throw new \InvalidArgumentException( 'Invalid priority' );
99  }
100 
101  if ( isset( $data['copyFrom'] ) ) {
102  $from = $data['copyFrom'];
103  if ( !$from instanceof SessionInfo ) {
104  throw new \InvalidArgumentException( 'Invalid copyFrom' );
105  }
106  $data += [
107  'provider' => $from->provider,
108  'id' => $from->id,
109  'userInfo' => $from->userInfo,
110  'persisted' => $from->persisted,
111  'remembered' => $from->remembered,
112  'forceHTTPS' => $from->forceHTTPS,
113  'metadata' => $from->providerMetadata,
114  'idIsSafe' => $from->idIsSafe,
115  'forceUse' => $from->forceUse,
116  // @codeCoverageIgnoreStart
117  ];
118  // @codeCoverageIgnoreEnd
119  } else {
120  $data += [
121  'provider' => null,
122  'id' => null,
123  'userInfo' => null,
124  'persisted' => false,
125  'remembered' => true,
126  'forceHTTPS' => false,
127  'metadata' => null,
128  'idIsSafe' => false,
129  'forceUse' => false,
130  // @codeCoverageIgnoreStart
131  ];
132  // @codeCoverageIgnoreEnd
133  }
134 
135  if ( $data['id'] !== null && !SessionManager::validateSessionId( $data['id'] ) ) {
136  throw new \InvalidArgumentException( 'Invalid session ID' );
137  }
138 
139  if ( $data['userInfo'] !== null && !$data['userInfo'] instanceof UserInfo ) {
140  throw new \InvalidArgumentException( 'Invalid userInfo' );
141  }
142 
143  if ( !$data['provider'] && $data['id'] === null ) {
144  throw new \InvalidArgumentException(
145  'Must supply an ID when no provider is given'
146  );
147  }
148 
149  if ( $data['metadata'] !== null && !is_array( $data['metadata'] ) ) {
150  throw new \InvalidArgumentException( 'Invalid metadata' );
151  }
152 
153  $this->provider = $data['provider'];
154  if ( $data['id'] !== null ) {
155  $this->id = $data['id'];
156  $this->idIsSafe = $data['idIsSafe'];
157  $this->forceUse = $data['forceUse'] && $this->provider;
158  } else {
159  // @phan-suppress-next-line PhanUndeclaredMethod
160  $this->id = $this->provider->getManager()->generateSessionId();
161  $this->idIsSafe = true;
162  $this->forceUse = false;
163  }
164  $this->priority = (int)$priority;
165  $this->userInfo = $data['userInfo'];
166  $this->persisted = (bool)$data['persisted'];
167  if ( $data['provider'] !== null ) {
168  if ( $this->userInfo !== null && !$this->userInfo->isAnon() && $this->userInfo->isVerified() ) {
169  $this->remembered = (bool)$data['remembered'];
170  }
171  $this->providerMetadata = $data['metadata'];
172  }
173  $this->forceHTTPS = (bool)$data['forceHTTPS'];
174  }
175 
180  final public function getProvider() {
181  return $this->provider;
182  }
183 
188  final public function getId() {
189  return $this->id;
190  }
191 
204  final public function isIdSafe() {
205  return $this->idIsSafe;
206  }
207 
219  final public function forceUse() {
220  return $this->forceUse;
221  }
222 
227  final public function getPriority() {
228  return $this->priority;
229  }
230 
235  final public function getUserInfo() {
236  return $this->userInfo;
237  }
238 
243  final public function wasPersisted() {
244  return $this->persisted;
245  }
246 
251  final public function getProviderMetadata() {
253  }
254 
270  final public function wasRemembered() {
271  return $this->remembered;
272  }
273 
278  final public function forceHTTPS() {
279  return $this->forceHTTPS;
280  }
281 
282  public function __toString() {
283  return '[' . $this->getPriority() . ']' .
284  ( $this->getProvider() ?: 'null' ) .
285  ( $this->userInfo ?: '<null>' ) . $this->getId();
286  }
287 
294  public static function compare( $a, $b ) {
295  return $a->getPriority() <=> $b->getPriority();
296  }
297 
298 }
const MIN_PRIORITY
Minimum allowed priority.
Definition: SessionInfo.php:36
getUserInfo()
Return the user.
Object holding data about a session&#39;s user.
Definition: UserInfo.php:51
getPriority()
Return the priority.
static compare( $a, $b)
Compare two SessionInfo objects by priority.
getId()
Return the session ID.
forceUse()
Force use of this SessionInfo if validation fails.
getManager()
Get the session manager.
getProvider()
Return the provider.
__construct( $priority, array $data)
Definition: SessionInfo.php:96
getProviderMetadata()
Return provider metadata.
wasPersisted()
Return whether the session is persisted.
SessionProvider null $provider
Definition: SessionInfo.php:42
const MAX_PRIORITY
Maximum allowed priority.
Definition: SessionInfo.php:39
static validateSessionId( $id)
Validate a session ID.
forceHTTPS()
Whether this session should only be used over HTTPS.
Value object returned by SessionProvider.
Definition: SessionInfo.php:34
wasRemembered()
Return whether the user was remembered.
isIdSafe()
Indicate whether the ID is "safe".