Skip to content
Open
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
19 changes: 19 additions & 0 deletions AudioStreaming/Core/Network/NetworkSessionDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@
import Foundation
import OSLog

/// Provides a `URLCredential` in response to a client-certificate authentication challenge,
/// or `nil` to fall back to the system's default handling. Used to support mutual TLS (mTLS)
/// against servers that sit behind a client-certificate-authenticated proxy.
public typealias ClientCertificateProvider = (URLAuthenticationChallenge) -> URLCredential?

final class NetworkSessionDelegate: NSObject, URLSessionDataDelegate {
weak var taskProvider: StreamTaskProvider?
var clientCertificateProvider: ClientCertificateProvider?

func stream(for task: URLSessionTask) -> NetworkDataStream? {
guard let taskProvider = taskProvider else {
Expand Down Expand Up @@ -50,4 +56,17 @@ final class NetworkSessionDelegate: NSObject, URLSessionDataDelegate {
stream.didReceive(response: response as? HTTPURLResponse)
completionHandler(.allow)
}

func urlSession(_: URLSession,
didReceive challenge: URLAuthenticationChallenge,
completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)
{
guard challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodClientCertificate,
let credential = clientCertificateProvider?(challenge)
else {
completionHandler(.performDefaultHandling, nil)
return
}
completionHandler(.useCredential, credential)
}
}
4 changes: 3 additions & 1 deletion AudioStreaming/Core/Network/NetworkingClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ final class NetworkingClient {

init(configuration: URLSessionConfiguration = .networkingConfiguration,
delegate: NetworkSessionDelegate = NetworkSessionDelegate(),
networkQueue: DispatchQueue = DispatchQueue(label: "audio.streaming.session.network.queue"))
networkQueue: DispatchQueue = DispatchQueue(label: "audio.streaming.session.network.queue"),
clientCertificateProvider: ClientCertificateProvider? = nil)
{
delegate.clientCertificateProvider = clientCertificateProvider
let delegateQueue = operationQueue(underlyingQueue: networkQueue)
let session = URLSession(configuration: configuration, delegate: delegate, delegateQueue: delegateQueue)
self.session = session
Expand Down
5 changes: 3 additions & 2 deletions AudioStreaming/Streaming/AudioPlayer/AudioPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ open class AudioPlayer {
/// Tracks the current loop iteration (how many times we've looped so far)
private var currentLoopIteration: Int = 0

public init(configuration: AudioPlayerConfiguration = .default) {
public init(configuration: AudioPlayerConfiguration = .default,
clientCertificateProvider: ClientCertificateProvider? = nil) {
self.configuration = configuration.normalizeValues()
let engine = AVAudioEngine()
audioEngine = engine
Expand All @@ -195,7 +196,7 @@ open class AudioPlayer {
sourceQueue = DispatchQueue(label: "source.queue", qos: .default)

entryProvider = AudioEntryProvider(
networkingClient: NetworkingClient(),
networkingClient: NetworkingClient(clientCertificateProvider: clientCertificateProvider),
underlyingQueue: sourceQueue,
outputAudioFormat: outputAudioFormat
)
Expand Down