Skip to content

Commit 0486ed3

Browse files
committed
Create StateProviderInterface
1 parent fa7e498 commit 0486ed3

2 files changed

Lines changed: 76 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\SAML2;
6+
7+
interface StateProviderInterface
8+
{
9+
/**
10+
* Retrieve saved state.
11+
*
12+
* This function retrieves saved state information. If the state information has been lost,
13+
* it will attempt to restart the request by calling the restart URL which is embedded in the
14+
* state information. If there is no restart information available, an exception will be thrown.
15+
*
16+
* @param string $id State identifier (with embedded restart information).
17+
* @param string $stage The stage the state should have been saved in.
18+
* @param bool $allowMissing Whether to allow the state to be missing.
19+
*
20+
* @return array|null State information, or NULL if the state is missing and $allowMissing is true.
21+
* @psalm-return ($allowMissing is true ? array|null : array)
22+
*/
23+
public static function loadState(string $id, string $stage, bool $allowMissing = false): ?array;
24+
}

tests/SAML2/MockStateProvider.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Test\SAML2;
6+
7+
use SimpleSAML\SAML2\StateProviderInterface;
8+
9+
final class MockStateProvider implements StateProviderInterface
10+
{
11+
/** @var array|null $state */
12+
protected static ?array $state;
13+
14+
15+
/**
16+
* Save the state.
17+
*
18+
* This function saves the state, and returns an id which can be used to
19+
* retrieve it later.
20+
*
21+
* @param array &$state The login request state.
22+
* @param string $stage The current stage in the login process.
23+
* @param bool $rawId Return a raw ID, without a restart URL.
24+
*
25+
* @return string Identifier which can be used to retrieve the state later.
26+
*/
27+
public static function saveState(array &$state, string $stage, bool $rawId = false): string
28+
{
29+
self::$state['PHPUnit'][$stage] = $state;
30+
return 'PHPUnit';
31+
}
32+
33+
34+
/**
35+
* Retrieve saved state.
36+
*
37+
* This function retrieves saved state information. If the state information has been lost,
38+
* it will attempt to restart the request by calling the restart URL which is embedded in the
39+
* state information. If there is no restart information available, an exception will be thrown.
40+
*
41+
* @param string $id State identifier (with embedded restart information).
42+
* @param string $stage The stage the state should have been saved in.
43+
* @param bool $allowMissing Whether to allow the state to be missing.
44+
*
45+
* @return array|null State information, or NULL if the state is missing and $allowMissing is true.
46+
* @psalm-return ($allowMissing is true ? array|null : array)
47+
*/
48+
public static function loadState(string $id, string $stage, bool $allowMissing = false): ?array
49+
{
50+
return self::$state[$id][$stage];
51+
}
52+
}

0 commit comments

Comments
 (0)