Skip to content

Commit 312b752

Browse files
authored
Fix: use bearer auth when pulling binaryTarget or packages from a package registry (#7662)
Use bearer auth when pulling binary targets or a package from a package registry and the user is `token`. ### Motivation: When logging in to a package registry bearer auth works. However when fetching binaryTargets or pulling packages from a registry basic auth is used always. This either breaks services that don't use basic auth or leads to nasty workarounds like first creating a netrc file with "token" as the user and after login changing the user back to the actual user. Assuming that basic auth is allowed with an identiy token. ### Modifications: Check if the user is `token` when creating the authorization header and use Bearer auth in these cases. ### Result: When the user is `token` the `Authorization` header will be set to `Bearer {{access_token}}` instead of `Basic token:{{access_token}}`. Before: <img width="698" alt="Screenshot 2024-06-13 at 22 49 08" src="https://github.com/apple/swift-package-manager/assets/13999931/0f3eef32-55e3-417f-b129-c138ca452b08"> After: <img width="699" alt="Screenshot 2024-06-13 at 22 48 38" src="https://github.com/apple/swift-package-manager/assets/13999931/d98daaa7-1535-40d0-96ea-a6736f5d1e3e">
1 parent 9c0e48e commit 312b752

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

Sources/Basics/AuthorizationProvider.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ extension AuthorizationProvider {
7171
guard let (user, password) = self.authentication(for: url) else {
7272
return nil
7373
}
74+
guard user != "token" else {
75+
return "Bearer \(password)"
76+
}
7477
let authString = "\(user):\(password)"
7578
let authData = Data(authString.utf8)
7679
return "Basic \(authData.base64EncodedString())"

Tests/BasicsTests/AuthorizationProviderTests.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ final class AuthorizationProviderTests: XCTestCase {
6363
}
6464
}
6565

66+
func testBasicAPIsBearerToken() {
67+
let url = URL("http://\(UUID().uuidString)")
68+
let user = "token"
69+
let token = UUID().uuidString
70+
71+
let provider = TestProvider(map: [url: (user: user, password: token)])
72+
self.assertBearerAuthentication(provider, for: url, expected: token)
73+
}
74+
6675
func testProtocolHostPort() throws {
6776
#if !canImport(Security)
6877
try XCTSkipIf(true)
@@ -258,6 +267,20 @@ final class AuthorizationProviderTests: XCTestCase {
258267
"Basic " + Data("\(expected.user):\(expected.password)".utf8).base64EncodedString()
259268
)
260269
}
270+
271+
private func assertBearerAuthentication(
272+
_ provider: AuthorizationProvider,
273+
for url: URL,
274+
expected: String
275+
) {
276+
let authentication = provider.authentication(for: url)
277+
XCTAssertEqual(authentication?.user, "token")
278+
XCTAssertEqual(authentication?.password, expected)
279+
XCTAssertEqual(
280+
provider.httpAuthorizationHeader(for: url),
281+
"Bearer \(expected)"
282+
)
283+
}
261284
}
262285

263286
private struct TestProvider: AuthorizationProvider {

0 commit comments

Comments
 (0)