Skip to content

Commit f8410d0

Browse files
committed
set auth header instead of token
1 parent a00956b commit f8410d0

File tree

4 files changed

+29
-22
lines changed

4 files changed

+29
-22
lines changed

src/HttpClient.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,6 @@ protected function resolveTokenProvider(): ?AccessTokenProviderInterface
9292
return $this->tokenProvider;
9393
}
9494

95-
protected function getAccessToken(): ?string
96-
{
97-
return $this->resolveTokenProvider()?->getAccessToken();
98-
}
99-
10095
protected function clientConfig(): array
10196
{
10297
return [
@@ -114,16 +109,15 @@ protected function clientConfig(): array
114109

115110
/**
116111
* Merge request options with per-request auth header.
117-
* Resolves the access token at request time to avoid stale tokens.
118-
* Only adds Authorization header when a token is available.
112+
* Only adds Authorization header when the provider returns a value.
119113
*/
120114
protected function requestOptions(array $options = []): array
121115
{
122-
$token = $this->getAccessToken();
123-
if ($token !== null) {
116+
$authHeader = $this->resolveTokenProvider()?->getAuthorizationHeader();
117+
if ($authHeader !== null) {
124118
$options[RequestOptions::HEADERS] = array_merge(
125119
$options[RequestOptions::HEADERS] ?? [],
126-
['Authorization' => 'Bearer ' . $token]
120+
['Authorization' => $authHeader]
127121
);
128122
}
129123
return $options;

src/auth/AccessTokenProviderInterface.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
interface AccessTokenProviderInterface
66
{
77
/**
8-
* Returns the current access token, or null when no token is available (skips auth header).
8+
* Returns the full Authorization header value (e.g. "Bearer xxx", "Basic xxx"),
9+
* or null when no auth is available (skips Authorization header).
910
*/
10-
public function getAccessToken(): ?string;
11+
public function getAuthorizationHeader(): ?string;
1112
}

src/auth/TokenProvider.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,21 @@
77
/**
88
* Configurable token provider for static API keys, service tokens, or no auth.
99
*
10-
* - Set $token for a static token
11-
* - Set neither for unauthenticated requests (null token → no Authorization header)
10+
* - Set $token and optionally $scheme to control the Authorization header
11+
* - Omit $token for unauthenticated requests (null → no Authorization header)
1212
*/
1313
class TokenProvider extends BaseObject implements AccessTokenProviderInterface
1414
{
1515
public ?string $token = null;
1616

17-
public function getAccessToken(): ?string
17+
public string $scheme = 'Bearer';
18+
19+
public function getAuthorizationHeader(): ?string
1820
{
19-
return $this->token ?: null;
21+
if (empty($this->token)) {
22+
return null;
23+
}
24+
25+
return $this->scheme . ' ' . $this->token;
2026
}
2127
}

tests/auth/TokenProviderTest.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,34 @@ public function testImplementsInterface(): void
1414
$this->assertInstanceOf(AccessTokenProviderInterface::class, $provider);
1515
}
1616

17-
public function testReturnsStaticToken(): void
17+
public function testReturnsBearerHeaderByDefault(): void
1818
{
1919
$provider = new TokenProvider(['token' => 'my-api-key']);
20-
$this->assertSame('my-api-key', $provider->getAccessToken());
20+
$this->assertSame('Bearer my-api-key', $provider->getAuthorizationHeader());
21+
}
22+
23+
public function testCustomScheme(): void
24+
{
25+
$provider = new TokenProvider(['token' => 'my-api-key', 'scheme' => 'Basic']);
26+
$this->assertSame('Basic my-api-key', $provider->getAuthorizationHeader());
2127
}
2228

2329
public function testReturnsNullWhenTokenIsNull(): void
2430
{
2531
$provider = new TokenProvider();
26-
$this->assertNull($provider->getAccessToken());
32+
$this->assertNull($provider->getAuthorizationHeader());
2733
}
2834

2935
public function testReturnsNullWhenTokenIsEmpty(): void
3036
{
3137
$provider = new TokenProvider(['token' => '']);
32-
$this->assertNull($provider->getAccessToken());
38+
$this->assertNull($provider->getAuthorizationHeader());
3339
}
3440

3541
public function testNullAuthWhenNothingConfigured(): void
3642
{
3743
$provider = new TokenProvider();
38-
$this->assertNull($provider->getAccessToken());
39-
$this->assertNull($provider->getAccessToken());
44+
$this->assertNull($provider->getAuthorizationHeader());
45+
$this->assertNull($provider->getAuthorizationHeader());
4046
}
4147
}

0 commit comments

Comments
 (0)