Skip to content

Commit

Permalink
Merge pull request #21 from rcarver/use-auth
Browse files Browse the repository at this point in the history
Use the username/password for requests
  • Loading branch information
0xTim authored Nov 9, 2023
2 parents 598cda2 + db2df3b commit 90be7e2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Sources/ElasticsearchNIOClient/ElasticsearchClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public struct ElasticsearchClient {
guard let host = url.host, !host.isEmpty else { throw ValidationError.missingURLHost }

try self.init(
requester: HTTPClientElasticsearchRequester(eventLoop: eventLoop, logger: logger, client: httpClient),
requester: HTTPClientElasticsearchRequester(eventLoop: eventLoop, logger: logger, username: username, password: password, client: httpClient),
eventLoop: eventLoop,
logger: logger,
scheme: scheme,
Expand All @@ -59,7 +59,7 @@ public struct ElasticsearchClient {

public init(httpClient: HTTPClient, eventLoop: EventLoop, logger: Logger, scheme: String? = nil, host: String, port: Int? = defaultPort, username: String? = nil, password: String? = nil, jsonEncoder: JSONEncoder = JSONEncoder(), jsonDecoder: JSONDecoder = JSONDecoder()) throws {
try self.init(
requester: HTTPClientElasticsearchRequester(eventLoop: eventLoop, logger: logger, client: httpClient),
requester: HTTPClientElasticsearchRequester(eventLoop: eventLoop, logger: logger, username: username, password: password, client: httpClient),
eventLoop: eventLoop,
logger: logger,
scheme: scheme,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import NIOHTTP1
public struct HTTPClientElasticsearchRequester: ElasticsearchRequester {
let eventLoop: EventLoop
let logger: Logger
let username: String?
let password: String?
let client: HTTPClient

public func executeRequest(url urlString: String, method: HTTPMethod, headers: HTTPHeaders, body: ByteBuffer?) -> EventLoopFuture<HTTPClient.Response> {
Expand All @@ -19,6 +21,14 @@ public struct HTTPClientElasticsearchRequester: ElasticsearchRequester {
} else {
httpClientBody = nil
}
var headers = headers
if let username = self.username, let password = self.password {
let pair = "\(username):\(password)"
if let data = pair.data(using: .utf8) {
let basic = data.base64EncodedString()
headers.add(name: "Authorization", value: "Basic \(basic)")
}
}
let request: HTTPClient.Request
do {
request = try HTTPClient.Request(url: url, method: method, headers: headers, body: httpClientBody)
Expand Down

0 comments on commit 90be7e2

Please sign in to comment.