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