1 | <?php |
||
2 | |||
3 | namespace Pelmered\LaravelHttpOAuthHelper; |
||
4 | |||
5 | use Closure; |
||
6 | use Illuminate\Http\Client\PendingRequest; |
||
7 | use Illuminate\Support\Facades\Validator; |
||
8 | use InvalidArgumentException; |
||
9 | |||
10 | class Credentials |
||
11 | { |
||
12 | public const AUTH_TYPE_BODY = 'body'; |
||
13 | |||
14 | public const AUTH_TYPE_QUERY = 'query'; |
||
15 | |||
16 | public const AUTH_TYPE_BASIC = 'basic'; |
||
17 | |||
18 | public const AUTH_TYPE_BEARER = 'Bearer'; |
||
19 | |||
20 | public const AUTH_TYPE_CUSTOM = 'custom'; |
||
21 | |||
22 | public const GRANT_TYPE_CLIENT_CREDENTIALS = 'client_credentials'; |
||
23 | |||
24 | public const GRANT_TYPE_PASSWORD_CREDENTIALS = 'password_credentials'; |
||
25 | |||
26 | //TODO: Add support for authorization_code and implicit grants |
||
27 | //public const GRANT_TYPE_AUTHORIZATION_CODE = 'authorization_code'; |
||
28 | //public const GRANT_TYPE_IMPLICIT = 'implicit'; |
||
29 | |||
30 | private ?Closure $customCallback = null; |
||
31 | |||
32 | protected ?Options $options; |
||
33 | |||
34 | /** |
||
35 | * @param array<string, mixed> $credentials |
||
36 | */ |
||
37 | 28 | public function __construct( |
|
38 | string|array|callable $credentials = [], |
||
39 | protected ?string $token = null, |
||
40 | protected ?string $clientId = null, |
||
41 | protected ?string $clientSecret = null, |
||
42 | ) { |
||
43 | 28 | if (! empty($credentials)) { |
|
44 | 23 | $this->parseCredentialsArray($credentials); |
|
45 | } |
||
46 | |||
47 | 26 | $this->validate(); |
|
48 | } |
||
49 | |||
50 | /** |
||
51 | * @return array<string, string> |
||
52 | */ |
||
53 | 26 | public function toArray(): array |
|
54 | { |
||
55 | 26 | return get_object_vars($this); |
|
56 | } |
||
57 | |||
58 | 26 | protected function validate(): void |
|
59 | { |
||
60 | 26 | Validator::make($this->toArray(), [ |
|
61 | 26 | 'token' => 'required_without_all:clientId,clientSecret,customCallback|string|nullable', |
|
62 | 26 | 'clientId' => 'required_with:clientSecret|string|nullable', |
|
63 | 26 | 'clientSecret' => 'required_with:clientId|string|nullable', |
|
64 | 26 | 'customCallback' => 'required_without_all:token,clientId,clientSecret|nullable', |
|
65 | 26 | ])->validate(); |
|
66 | } |
||
67 | |||
68 | 5 | public function setOptions(Options $options): self |
|
69 | { |
||
70 | 5 | $this->options = $options; |
|
71 | |||
72 | 5 | return $this; |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * @param string|array<string, mixed>|callable $credentials |
||
77 | */ |
||
78 | 23 | public function parseCredentialsArray(string|array|callable $credentials): void |
|
79 | { |
||
80 | 23 | if (is_string($credentials)) { |
|
81 | 7 | $this->setRefreshToken($credentials); |
|
82 | |||
83 | 7 | return; |
|
84 | } |
||
85 | |||
86 | 16 | if (is_callable($credentials)) { |
|
87 | 2 | $this->customCallback = $credentials(...); |
|
88 | |||
89 | 2 | return; |
|
90 | } |
||
91 | |||
92 | 14 | $credentials = array_filter($credentials); |
|
93 | 14 | $arrayLength = count($credentials); |
|
94 | |||
95 | 14 | if ($arrayLength > 0 && array_is_list($credentials)) { |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
96 | 14 | match ($arrayLength) { |
|
97 | 5 | 1 => $this->setRefreshToken($credentials[0]), |
|
98 | 7 | 2 => $this->setClientCredentialsPair($credentials[0], $credentials[1]), |
|
99 | 2 | default => throw new InvalidArgumentException('Invalid credentials. Check documentation/readme.'), |
|
100 | 14 | }; |
|
101 | |||
102 | 12 | return; |
|
103 | } |
||
104 | } |
||
105 | |||
106 | 25 | public function addAuthToRequest(PendingRequest $httpClient, Options $options): PendingRequest |
|
107 | { |
||
108 | 25 | if ($options->authType === self::AUTH_TYPE_BODY) { |
|
109 | 3 | return $httpClient; |
|
110 | } |
||
111 | 22 | if (is_callable($this->customCallback)) { |
|
112 | 2 | return ($this->customCallback)($httpClient); |
|
113 | } |
||
114 | |||
115 | 20 | if ($options->authType === self::AUTH_TYPE_BASIC) { |
|
116 | 8 | if (! $this->clientId || ! $this->clientSecret) { |
|
117 | 1 | throw new InvalidArgumentException('Basic auth requires client id and client secret. Check documentation/readme.'); |
|
118 | } |
||
119 | |||
120 | 7 | return $httpClient->withBasicAuth($this->clientId, $this->clientSecret); |
|
121 | } |
||
122 | |||
123 | 12 | if ($this->token) { |
|
124 | 11 | if ($options->authType === self::AUTH_TYPE_QUERY) { |
|
125 | 1 | return $httpClient->withQueryParameters([ |
|
126 | 1 | $options->tokenName => $this->token, |
|
127 | 1 | ]); |
|
128 | } |
||
129 | |||
130 | 10 | return $httpClient->withToken($this->token, $options->authType); |
|
131 | } |
||
132 | |||
133 | |||
134 | 1 | return $httpClient; |
|
135 | } |
||
136 | |||
137 | /** |
||
138 | * @param array<string, string> $requestBody |
||
139 | * @return array<string, string> |
||
140 | */ |
||
141 | 24 | public function addAuthToBody(array $requestBody, Options $options): array |
|
142 | { |
||
143 | 24 | if ($options->authType !== self::AUTH_TYPE_BODY) { |
|
144 | 21 | return $requestBody; |
|
145 | } |
||
146 | 3 | if ($this->clientId && $this->clientSecret) { |
|
147 | 2 | return $requestBody + ['client_id' => $this->clientId, 'client_secret' => $this->clientSecret]; |
|
148 | } |
||
149 | 1 | if ($this->token) { |
|
150 | 1 | return $requestBody + [$options->tokenName => $this->token]; |
|
151 | } |
||
152 | |||
153 | throw new InvalidArgumentException('Invalid credentials. Check documentation/readme.'); |
||
154 | } |
||
155 | |||
156 | 12 | public function setRefreshToken(string $token): void |
|
157 | { |
||
158 | 12 | $this->token = $token; |
|
159 | } |
||
160 | |||
161 | 7 | public function setClientCredentialsPair(string $clientId, string $clientSecret): void |
|
162 | { |
||
163 | 7 | $this->clientId = $clientId; |
|
164 | 7 | $this->clientSecret = $clientSecret; |
|
165 | } |
||
166 | } |
||
167 |