1 | <?php |
||
2 | |||
3 | namespace Pelmered\LaravelHttpOAuthHelper; |
||
4 | |||
5 | use Carbon\Carbon; |
||
6 | use Closure; |
||
7 | use Illuminate\Support\Facades\Validator; |
||
8 | use Illuminate\Validation\Rule; |
||
9 | |||
10 | class Options |
||
11 | { |
||
12 | /** |
||
13 | * @param array<string> $scopes |
||
14 | */ |
||
15 | 32 | final public function __construct( |
|
16 | public array $scopes = [], |
||
17 | public string $authType = Credentials::AUTH_TYPE_BEARER, |
||
18 | public string $grantType = Credentials::GRANT_TYPE_CLIENT_CREDENTIALS, |
||
19 | public string $tokenType = AccessToken::TOKEN_TYPE_BEARER, |
||
20 | public string $tokenName = 'token', |
||
21 | public int|string|Closure|Carbon $expires = 3600, |
||
22 | public string|Closure $accessToken = 'access_token', |
||
23 | public ?Closure $tokenTypeCustomCallback = null, |
||
24 | public ?string $cacheKey = null, |
||
25 | public ?string $cacheDriver = null, |
||
26 | ) { |
||
27 | 32 | $this->validateOptions(); |
|
28 | } |
||
29 | |||
30 | /** |
||
31 | * @return array<string, string> |
||
32 | */ |
||
33 | public function toArray(): array |
||
34 | { |
||
35 | return get_object_vars($this); |
||
36 | } |
||
37 | |||
38 | 32 | protected function validateOptions(): void |
|
39 | { |
||
40 | // Note: closures can't be checked at this point since we don't have access to the response objects |
||
41 | 32 | Validator::make((array) $this, [ |
|
42 | 32 | 'scopes.*' => 'string', |
|
43 | 32 | 'authType' => Rule::in([ |
|
44 | 32 | Credentials::AUTH_TYPE_BEARER, |
|
45 | 32 | Credentials::AUTH_TYPE_BODY, |
|
46 | 32 | Credentials::AUTH_TYPE_QUERY, |
|
47 | 32 | Credentials::AUTH_TYPE_BASIC, |
|
48 | 32 | Credentials::AUTH_TYPE_CUSTOM, |
|
49 | 32 | ]), |
|
50 | 32 | 'grantType' => Rule::in([ |
|
51 | 32 | Credentials::GRANT_TYPE_CLIENT_CREDENTIALS, |
|
52 | 32 | Credentials::GRANT_TYPE_PASSWORD_CREDENTIALS, |
|
53 | 32 | ]), |
|
54 | 32 | 'tokenType' => Rule::in([ |
|
55 | 32 | AccessToken::TOKEN_TYPE_BEARER, |
|
56 | 32 | AccessToken::TOKEN_TYPE_QUERY, |
|
57 | 32 | AccessToken::TOKEN_TYPE_CUSTOM, |
|
58 | 32 | ]), |
|
59 | 32 | 'tokenName' => 'string', |
|
60 | 32 | ])->validate(); |
|
61 | } |
||
62 | |||
63 | 25 | public function getScopes(): string |
|
64 | { |
||
65 | 25 | return implode(' ', $this->scopes); |
|
66 | } |
||
67 | |||
68 | /** |
||
69 | * @param array<string, mixed> ...$parameters |
||
70 | */ |
||
71 | 5 | public static function make(...$parameters): static |
|
72 | { |
||
73 | 5 | $defaults = static::getDefaults(); |
|
74 | 5 | $options = array_merge($defaults, ...$parameters); |
|
75 | |||
76 | 5 | return new static(...$options); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @return array<string, mixed> |
||
81 | */ |
||
82 | 5 | protected static function getDefaults(): array |
|
83 | { |
||
84 | 5 | return [ |
|
85 | 5 | 'scopes' => [], |
|
86 | 5 | 'grantType' => Credentials::GRANT_TYPE_CLIENT_CREDENTIALS, |
|
87 | 5 | 'tokenType' => AccessToken::TOKEN_TYPE_BEARER, |
|
88 | 5 | 'authType' => Credentials::AUTH_TYPE_BEARER, |
|
89 | 5 | 'expires' => 3600, |
|
90 | 5 | 'accessToken' => 'access_token', |
|
91 | 5 | ]; |
|
92 | } |
||
93 | } |
||
94 |