|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Pelmered\LaravelHttpOAuthHelper; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Closure; |
|
7
|
|
|
use Illuminate\Http\Client\PendingRequest; |
|
8
|
|
|
use Illuminate\Http\Client\Response; |
|
9
|
|
|
use InvalidArgumentException; |
|
10
|
|
|
|
|
11
|
|
|
final class AccessToken |
|
12
|
|
|
{ |
|
13
|
|
|
public const TOKEN_TYPE_BEARER = 'Bearer'; |
|
14
|
|
|
|
|
15
|
|
|
public const TOKEN_TYPE_QUERY = 'query'; |
|
16
|
|
|
|
|
17
|
|
|
public const TOKEN_TYPE_CUSTOM = 'custom'; |
|
18
|
|
|
|
|
19
|
18 |
|
public function __construct( |
|
20
|
|
|
protected string $accessToken, |
|
21
|
|
|
protected Carbon $expiresAt, |
|
22
|
|
|
protected string $tokenType = self::TOKEN_TYPE_BEARER, |
|
23
|
|
|
protected string $tokenName = 'token', |
|
24
|
|
|
protected ?Closure $customCallback = null |
|
25
|
|
|
) { |
|
26
|
18 |
|
if ($tokenType === self::TOKEN_TYPE_CUSTOM && is_null($customCallback)) { |
|
27
|
1 |
|
throw new InvalidArgumentException('customCallback must be set when using AUTH_TYPE_CUSTOM'); |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
9 |
|
public function getAccessToken(): string |
|
32
|
|
|
{ |
|
33
|
9 |
|
return $this->accessToken; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
1 |
|
public function getExpiresAt(): Carbon |
|
37
|
|
|
{ |
|
38
|
1 |
|
return $this->expiresAt; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
8 |
|
public function getExpiresIn(): int |
|
42
|
|
|
{ |
|
43
|
8 |
|
return (int) round(Carbon::now()->diffInSeconds($this->expiresAt)); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
1 |
|
public function getTokenType(): string |
|
47
|
|
|
{ |
|
48
|
1 |
|
return $this->tokenType; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
public function getTokenName(): string |
|
52
|
|
|
{ |
|
53
|
1 |
|
return $this->tokenName; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
2 |
|
public function getCustomCallback(): ?Closure |
|
57
|
|
|
{ |
|
58
|
2 |
|
return $this->customCallback; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
6 |
|
public function getHttpClient(PendingRequest $httpClient): PendingRequest |
|
62
|
|
|
{ |
|
63
|
6 |
|
return match ($this->tokenType) { |
|
64
|
4 |
|
self::TOKEN_TYPE_BEARER => $httpClient->withToken($this->accessToken), |
|
65
|
1 |
|
self::TOKEN_TYPE_QUERY => $httpClient->withQueryParameters([$this->tokenName => $this->accessToken]), |
|
66
|
1 |
|
self::TOKEN_TYPE_CUSTOM => $this->resolveCustomAuth($httpClient), |
|
67
|
6 |
|
default => throw new InvalidArgumentException('Invalid auth type') |
|
68
|
6 |
|
}; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
protected function resolveCustomAuth(PendingRequest $httpClient): PendingRequest |
|
72
|
|
|
{ |
|
73
|
1 |
|
if (! is_callable($this->customCallback)) { |
|
74
|
|
|
throw new InvalidArgumentException('customCallback must be callable'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
return ($this->customCallback)($httpClient); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
public static function parseQueryTokenFromResponse(Response $response, string $queryKey = 'token'): ?string |
|
81
|
|
|
{ |
|
82
|
1 |
|
$uri = $response->effectiveUri(); |
|
83
|
|
|
|
|
84
|
1 |
|
if (! $uri) { |
|
|
|
|
|
|
85
|
|
|
return null; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
return self::parseTokenFromQueryString($response->effectiveUri()?->getQuery(), $queryKey); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
2 |
|
public static function parseQueryTokenFromUrl(string $url, string $queryKey = 'token'): string |
|
92
|
|
|
{ |
|
93
|
2 |
|
return self::parseTokenFromQueryString(parse_url($url, PHP_URL_QUERY), $queryKey); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
2 |
|
public static function parseTokenFromQueryString(string $queryString, string $queryKey = 'token'): string |
|
97
|
|
|
{ |
|
98
|
2 |
|
parse_str($queryString, $output); |
|
99
|
|
|
|
|
100
|
2 |
|
return $output[$queryKey]; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|