friendship ended with social-app. php is my new best friend
1<?php
2/**
3 * Class MemoryStorage
4 *
5 * @created 09.07.2017
6 * @author Smiley <smiley@chillerlan.net>
7 * @copyright 2017 Smiley
8 * @license MIT
9 */
10declare(strict_types=1);
11
12namespace chillerlan\OAuth\Storage;
13
14use chillerlan\OAuth\Core\AccessToken;
15
16/**
17 * Implements a memory storage adapter.
18 *
19 * Note: the memory storage is not persistent, as tokens are only stored during script runtime.
20 */
21class MemoryStorage extends OAuthStorageAbstract{
22
23 /**
24 * the storage array
25 *
26 * @var array<int|string, array<int|string, mixed>> (the int keys are to keep phpstan silent)
27 */
28 protected array $storage = [
29 self::KEY_TOKEN => [],
30 self::KEY_STATE => [],
31 self::KEY_VERIFIER => [],
32 ];
33
34
35 /*
36 * Access token
37 */
38
39 public function storeAccessToken(AccessToken $token, string $provider):static{
40 $this->storage[$this::KEY_TOKEN][$this->getProviderName($provider)] = $token;
41
42 return $this;
43 }
44
45 public function getAccessToken(string $provider):AccessToken{
46
47 if($this->hasAccessToken($provider)){
48 return $this->storage[$this::KEY_TOKEN][$this->getProviderName($provider)];
49 }
50
51 throw new ItemNotFoundException($this::KEY_TOKEN);
52 }
53
54 public function hasAccessToken(string $provider):bool{
55 return !empty($this->storage[$this::KEY_TOKEN][$this->getProviderName($provider)]);
56 }
57
58 public function clearAccessToken(string $provider):static{
59 unset($this->storage[$this::KEY_TOKEN][$this->getProviderName($provider)]);
60
61 return $this;
62 }
63
64 public function clearAllAccessTokens():static{
65 $this->storage[$this::KEY_TOKEN] = [];
66
67 return $this;
68 }
69
70
71 /*
72 * CSRF state
73 */
74
75 public function storeCSRFState(string $state, string $provider):static{
76 $this->storage[$this::KEY_STATE][$this->getProviderName($provider)] = $state;
77
78 return $this;
79 }
80
81 public function getCSRFState(string $provider):string{
82
83 if($this->hasCSRFState($provider)){
84 return $this->storage[$this::KEY_STATE][$this->getProviderName($provider)];
85 }
86
87 throw new ItemNotFoundException($this::KEY_STATE);
88 }
89
90 public function hasCSRFState(string $provider):bool{
91 return !empty($this->storage[$this::KEY_STATE][$this->getProviderName($provider)]);
92 }
93
94 public function clearCSRFState(string $provider):static{
95 unset($this->storage[$this::KEY_STATE][$this->getProviderName($provider)]);
96
97 return $this;
98 }
99
100 public function clearAllCSRFStates():static{
101 $this->storage[$this::KEY_STATE] = [];
102
103 return $this;
104 }
105
106
107 /*
108 * PKCE verifier
109 */
110
111 public function storeCodeVerifier(string $verifier, string $provider):static{
112 $this->storage[$this::KEY_VERIFIER][$this->getProviderName($provider)] = $verifier;
113
114 return $this;
115 }
116
117 public function getCodeVerifier(string $provider):string{
118
119 if($this->hasCodeVerifier($provider)){
120 return $this->storage[$this::KEY_VERIFIER][$this->getProviderName($provider)];
121 }
122
123 throw new ItemNotFoundException($this::KEY_VERIFIER);
124 }
125
126 public function hasCodeVerifier(string $provider):bool{
127 return !empty($this->storage[$this::KEY_VERIFIER][$this->getProviderName($provider)]);
128 }
129
130 public function clearCodeVerifier(string $provider):static{
131 unset($this->storage[$this::KEY_VERIFIER][$this->getProviderName($provider)]);
132
133 return $this;
134 }
135
136 public function clearAllCodeVerifiers():static{
137 $this->storage[$this::KEY_VERIFIER] = [];
138
139 return $this;
140 }
141
142}