-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added: ability to set basic authentication on requests
Motivation: As an HTTP library, async-http-client should have authentication support. Modifications: This adds a `setBasicAuth()` method to both `HTTPClientRequest` and `HTTPClient.Request` and their related unit tests. Result: Library users will be able to leverage this method to use basic authentication on their requests without implementing this in their own projects. Signed-off-by: Agam Dua <agam_dua@apple.com>
- Loading branch information
Showing
5 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
Sources/AsyncHTTPClient/AsyncAwait/HTTPClientRequest+auth.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the AsyncHTTPClient open source project | ||
// | ||
// Copyright (c) 2024 Apple Inc. and the AsyncHTTPClient project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Foundation | ||
|
||
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) | ||
extension HTTPClientRequest { | ||
/// Set basic auth for a request. | ||
/// | ||
/// - parameters: | ||
/// - username: the username to authenticate with | ||
/// - password: authentication password associated with the username | ||
public mutating func setBasicAuth(username: String, password: String) { | ||
self.headers.setBasicAuth(username: username, password: password) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the AsyncHTTPClient open source project | ||
// | ||
// Copyright (c) 2024 Apple Inc. and the AsyncHTTPClient project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Foundation | ||
import NIOHTTP1 | ||
|
||
/// Generates base64 encoded username + password for http basic auth. | ||
/// | ||
/// - Parameters: | ||
/// - username: the username to authenticate with | ||
/// - password: authentication password associated with the username | ||
/// - Returns: encoded credentials to use the Authorization: Basic http header. | ||
func encodeBasicAuthCredentials(username: String, password: String) -> String { | ||
var value = Data() | ||
value.reserveCapacity(username.utf8.count + password.utf8.count + 1) | ||
value.append(contentsOf: username.utf8) | ||
value.append(UInt8(ascii: ":")) | ||
value.append(contentsOf: password.utf8) | ||
return value.base64EncodedString() | ||
} | ||
|
||
extension HTTPHeaders { | ||
/// Sets the basic auth header | ||
mutating func setBasicAuth(username: String, password: String) { | ||
let encoded = encodeBasicAuthCredentials(username: username, password: password) | ||
self.replaceOrAdd(name: "Authorization", value: "Basic \(encoded)") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters