Skip to content

[6.0] Use bearer auth for binaryTarget or packages from a package registry #7670

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

Closed
Closed
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 {
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