Nonce_Service.php
1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
namespace Wpo\Services;
use Wpo\Core\Wpmu_Helpers;
// Prevent public access to this script
defined('ABSPATH') or die();
if (!class_exists('\Wpo\Services\Nonce_Service')) {
class Nonce_Service
{
/**
* Creates a nonce to ensure the request for an Azure AD token
* originates from the current server.
*
* @since 21.6
*
* @return string
*/
public static function create_nonce()
{
$nonce_stack = Wpmu_Helpers::mu_get_transient('wpo365_nonces');
if (empty($nonce_stack)) {
$nonce_stack = array();
}
$nonce = uniqid();
$nonce_stack[] = $nonce;
// When the stack grows to 200 it's downsized to 150
if (sizeof($nonce_stack) > 200) {
array_splice($nonce_stack, 0, 50);
}
Wpmu_Helpers::mu_set_transient('wpo365_nonces', $nonce_stack, 300);
return $nonce;
}
/**
* Verifies the nonce that Microsoft returns together with the requested token.
*
* @param mixed $nonce
* @return bool
*/
public static function verify_nonce($nonce)
{
$nonce_stack = Wpmu_Helpers::mu_get_transient('wpo365_nonces');
if (empty($nonce_stack)) {
return false;
}
$index = array_search($nonce, $nonce_stack);
if (false === $index) {
return false;
}
array_splice($nonce_stack, $index, 1);
Wpmu_Helpers::mu_set_transient('wpo365_nonces', $nonce_stack, 300);
return true;
}
}
}