Skip to content

Commit 8efbaf4

Browse files
thesssplitbrain
authored andcommitted
Update GitHub authorization to use token in header instead of query
As outlined in [1], tokens have to be passed as header instead of query params to Github now. [1] https://developer.github.com/changes/2020-02-10-deprecating-auth-through-query-param/ Originally commited as cosmocode/dokuwiki-plugin-oauth#90
1 parent 919181e commit 8efbaf4

File tree

4 files changed

+8
-6
lines changed

4 files changed

+8
-6
lines changed

src/OAuth/OAuth2/Service/AbstractService.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ public function request($path, $method = 'GET', $body = null, array $extraHeader
166166
$uri->addToQuery('auth', $token->getAccessToken());
167167
} elseif (static::AUTHORIZATION_METHOD_HEADER_BEARER === $this->getAuthorizationMethod()) {
168168
$extraHeaders = array_merge(['Authorization' => 'Bearer ' . $token->getAccessToken()], $extraHeaders);
169+
} elseif (static::AUTHORIZATION_METHOD_HEADER_TOKEN === $this->getAuthorizationMethod()) {
170+
$extraHeaders = array_merge(array('Authorization' => 'token ' . $token->getAccessToken()), $extraHeaders);
169171
}
170172

171173
$extraHeaders = array_merge($this->getExtraApiHeaders(), $extraHeaders);

src/OAuth/OAuth2/Service/GitHub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public function getAccessTokenEndpoint()
159159
*/
160160
protected function getAuthorizationMethod()
161161
{
162-
return static::AUTHORIZATION_METHOD_QUERY_STRING;
162+
return static::AUTHORIZATION_METHOD_HEADER_TOKEN;
163163
}
164164

165165
/**

src/OAuth/OAuth2/Service/ServiceInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ interface ServiceInterface extends BaseServiceInterface
1919
const AUTHORIZATION_METHOD_QUERY_STRING_V2 = 3;
2020
const AUTHORIZATION_METHOD_QUERY_STRING_V3 = 4;
2121
const AUTHORIZATION_METHOD_QUERY_STRING_V4 = 5;
22+
const AUTHORIZATION_METHOD_HEADER_TOKEN = 6;
2223

2324
/**
2425
* Retrieves and stores/returns the OAuth2 access token after a successful authorization.

tests/Unit/OAuth2/Service/GitHubTest.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function testGetAccessTokenEndpoint(): void
9090
public function testGetAuthorizationMethod(): void
9191
{
9292
$client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
93-
$client->expects(self::once())->method('retrieveResponse')->willReturnArgument(0);
93+
$client->expects(self::once())->method('retrieveResponse')->willReturnArgument(2);
9494

9595
$token = $this->createMock('\\OAuth\\OAuth2\\Token\\TokenInterface');
9696
$token->expects(self::once())->method('getEndOfLife')->willReturn(TokenInterface::EOL_NEVER_EXPIRES);
@@ -105,10 +105,9 @@ public function testGetAuthorizationMethod(): void
105105
$storage
106106
);
107107

108-
$uri = $service->request('https://pieterhordijk.com/my/awesome/path');
109-
$absoluteUri = parse_url($uri->getAbsoluteUri());
110-
111-
self::assertSame('access_token=foo', $absoluteUri['query']);
108+
$headers = $service->request('https://pieterhordijk.com/my/awesome/path');
109+
self::assertArrayHasKey('Authorization', $headers);
110+
self::assertTrue(in_array('token foo', $headers, true));
112111
}
113112

114113
/**

0 commit comments

Comments
 (0)