-
-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathAuthorisationMiddleware.swift
More file actions
51 lines (46 loc) · 1.88 KB
/
AuthorisationMiddleware.swift
File metadata and controls
51 lines (46 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright (c) 2010-2026 Contributors to the openHAB project
//
// See the NOTICE file(s) distributed with this work for additional
// information.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0
//
// SPDX-License-Identifier: EPL-2.0
import Foundation
import HTTPTypes
import OpenAPIRuntime
import OpenAPIURLSession
import os
public func basicAuthHeader(username: String, password: String) -> String {
let credential = Data("\(username):\(password)".utf8).base64EncodedString()
return "Basic \(credential)"
}
public struct AuthorisationMiddleware {
private let username: String
private let password: String
private let alwaysSendBasicAuth: Bool
private let configuration: ConnectionConfiguration
public init(configuration: ConnectionConfiguration) {
username = configuration.username
password = configuration.password
alwaysSendBasicAuth = configuration.alwaysSendBasicAuth
self.configuration = configuration
}
}
extension AuthorisationMiddleware: ClientMiddleware {
public func intercept(_ request: HTTPRequest,
body: HTTPBody?,
baseURL: URL,
operationID: String,
next: @Sendable (HTTPRequest, HTTPBody?, URL) async throws -> (HTTPResponse, HTTPBody?)) async throws -> (HTTPResponse, HTTPBody?) {
// Use a mutable copy of request
var newRequest = request
if configuration.isCloudConnection || alwaysSendBasicAuth, !username.isEmpty, !password.isEmpty {
newRequest.headerFields[.authorization] = basicAuthHeader(username: username, password: password)
}
let (response, body) = try await next(newRequest, body, baseURL)
return (response, body)
}
}