Skip to content

Fix: use bearer auth when pulling binaryTarget or packages from a package registry #7662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Sources/Basics/AuthorizationProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ extension AuthorizationProvider {
guard let (user, password) = self.authentication(for: url) else {
return nil
}
guard user != "token" else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using token as username for bearer auth is specific to registry auth. Adding this change would mean applying that logic for all SwiftPM auth. Is this what we want @MaxDesiatov?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @bnbarham. I feel like we need to revert it if you think the behavior change (even though for making it consistent) is undesired.

return "Bearer \(password)"
}
let authString = "\(user):\(password)"
let authData = Data(authString.utf8)
return "Basic \(authData.base64EncodedString())"
Expand Down
23 changes: 23 additions & 0 deletions Tests/BasicsTests/AuthorizationProviderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ final class AuthorizationProviderTests: XCTestCase {
}
}

func testBasicAPIsBearerToken() {
let url = URL("http://\(UUID().uuidString)")
let user = "token"
let token = UUID().uuidString

let provider = TestProvider(map: [url: (user: user, password: token)])
self.assertBearerAuthentication(provider, for: url, expected: token)
}

func testProtocolHostPort() throws {
#if !canImport(Security)
try XCTSkipIf(true)
Expand Down Expand Up @@ -258,6 +267,20 @@ final class AuthorizationProviderTests: XCTestCase {
"Basic " + Data("\(expected.user):\(expected.password)".utf8).base64EncodedString()
)
}

private func assertBearerAuthentication(
_ provider: AuthorizationProvider,
for url: URL,
expected: String
) {
let authentication = provider.authentication(for: url)
XCTAssertEqual(authentication?.user, "token")
XCTAssertEqual(authentication?.password, expected)
XCTAssertEqual(
provider.httpAuthorizationHeader(for: url),
"Bearer \(expected)"
)
}
}

private struct TestProvider: AuthorizationProvider {
Expand Down