PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.8.1
GiveWP – Donation Plugin and Fundraising Platform v4.16.8.1
4.18.0 4.17.0 4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 All 257 releases
give / vendor / vendor-prefixed / stellarwp / licensing-api-client / src / Http / AuthState.php

AuthState.php in GiveWP – Donation Plugin and Fundraising Platform 4.16.8.1, at vendor/vendor-prefixed/stellarwp/licensing-api-client/src/Http/AuthState.php

99 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php declare(strict_types=1);
2
3 namespace Give\Vendors\LiquidWeb\LicensingApiClient\Http;
4
5 use InvalidArgumentException;
6 use Give\Vendors\LiquidWeb\LicensingApiClient\Exceptions\MissingAuthenticationException;
7 use Give\Vendors\LiquidWeb\LicensingApiClient\Value\AuthToken;
8
9 /**
10 * Combines auth policy and configured token state for request execution.
11 */
12 final class AuthState
13 {
14 private AuthContext $authContext;
15
16 private ?AuthToken $token;
17
18 /**
19 * @throws InvalidArgumentException
20 */
21 public function __construct(AuthContext $authContext, ?AuthToken $token = null) {
22 if ($authContext->mode() === AuthContext::MODE_EXPLICIT && $token === null) {
23 throw new InvalidArgumentException('Explicit auth mode requires a token.');
24 }
25
26 $this->authContext = $authContext;
27 $this->token = $token;
28 }
29
30 /**
31 * @throws MissingAuthenticationException
32 */
33 public function optionalToken(): ?AuthToken {
34 if ($this->authContext->mode() === AuthContext::MODE_NONE) {
35 return null;
36 }
37
38 if ($this->authContext->mode() === AuthContext::MODE_CONFIGURED && $this->token === null) {
39 throw new MissingAuthenticationException(
40 'This request requires authentication, but no token is available.'
41 );
42 }
43
44 return $this->token;
45 }
46
47 /**
48 * @throws MissingAuthenticationException
49 */
50 public function requiredToken(): AuthToken {
51 $token = $this->optionalToken();
52
53 if ($token === null) {
54 throw new MissingAuthenticationException(
55 'This request requires authentication, but no token is available.'
56 );
57 }
58
59 return $token;
60 }
61
62 public function withoutAuth(): self {
63 return $this->withAuthContext(new AuthContext(AuthContext::MODE_NONE), $this->token);
64 }
65
66 public function withConfiguredToken(): self {
67 return $this->withAuthContext(new AuthContext(AuthContext::MODE_CONFIGURED), $this->token);
68 }
69
70 public function withToken(string $token): self {
71 return $this->withAuthContext(
72 new AuthContext(AuthContext::MODE_EXPLICIT),
73 new AuthToken($token)
74 );
75 }
76
77 public function authContext(): AuthContext {
78 return $this->authContext;
79 }
80
81 public function token(): ?AuthToken {
82 return $this->token;
83 }
84
85 private function withAuthContext(AuthContext $authContext, ?AuthToken $token): self {
86 if (
87 $this->authContext->equals($authContext)
88 && (
89 ($this->token === null && $token === null)
90 || ($this->token !== null && $token !== null && $this->token->equals($token))
91 )
92 ) {
93 return $this;
94 }
95
96 return new self($authContext, $token);
97 }
98 }
99