BasicAuthenticationObserver
open class BasicAuthenticationObserver
extension BasicAuthenticationObserver: SAPURLSessionObserving
extension BasicAuthenticationObserver: TokenStoreManaging
Basic Authentication Observer
The BasicAuthenticationObserver is a SAPURLSessionObserving that can be used to automatically handle cases where communication requires a BasicCredential. The component listens on the didReceive challenge SAPURLSession delegate method.
The BasicAuthenticationObserver uses two delegates:
BasicCredentialStoring– managesBasicCredentialstorageBasicCredentialDiscovery– managesBasicCredentialcreation
You can implement the BasicCredentialStoring to manage the BasicCredential storage. Usually this is performed in the application itself:
- The observer calls the delegate to store, retrieve and delete the
BasicCredential. - The implementer stores the
BasicCredentialin a secure way. - The
URLcan be used to distinguishBasicCredentialsif the application uses more resource servers using, for example, thehostfrom the URL as a key.
func store(credential: BasicCredential, for: URL) throws -> Void {
// save the BasicCredential to a secure storage
}
func credential(for: URL) throws -> BasicCredential? {
// return the BasicCredential from the secure storage
}
func delete(for: URL) throws -> Void {
// delete the BasicCredential from the secure storage
}
When an authentication challenge occurs, BasicAuthenticationObserver first calls the BasicCredentialStoring delegate to retrieve the BasicCredential. If this step fails it calls the BasicCredentialDiscovery delegate to create a new BasicCredential.
The DefaultBasicCredentialDiscovery is a default implementation of BasicCredentialDiscovery protocol. It presents a UIAlertController with a host label and username, password UITextFields.
Usage
Initialization and registration
Initialize a BasicAuthenticationObserver
let basicAuthenticationObserver = BasicAuthenticationObserver(basicCredentialStore: self)
sapURLSessionForResource.register(basicAuthenticationObserver)
Start request to Resource URL
let request = URLRequest(url: <#resourceURL#>)
let dataTask = sapURLSessionForResource.dataTask(with: request) { data, response, error in
// Handle the error and the response
}
dataTask.resume()
Observer to collect the necessary credential for basic authentication
-
when set the observer calls this handler after a new authentication but before the authentication process finishes and all other requests continues
Declaration
Swift
public var authenticationHandler: AuthenticationHandling? -
Specify how long the credential will be kept
Declaration
Swift
public var persistence: URLCredential.Persistence -
Initializer for BasicAuthenticationObserver
Declaration
Swift
public init(basicCredentialDiscovery: BasicCredentialDiscovery = DefaultBasicCredentialDiscovery(), basicCredentialStore: BasicCredentialStoring, retryCount: Int = 3)Parameters
basicCredentialDiscoveryDiscovery implementation that implements the
BasicCredentialDiscoveryprotocol. The default value is a default implementation.basicCredentialStoreBasicCredentialStoringimplementation to store and handle the storedBasicCredential. The observer stores the basicCredentialStore as a strong reference.retryCountIntvalue to limit the retry count. -
Declaration
Swift
public func sapURLSession(_ session: SAPURLSession, task: SAPURLSessionTask, willSend request: URLRequest, completionHandler: @escaping (SAPURLSession.RequestDisposition) -> Swift.Void) -
Declaration
Swift
public func sapURLSession(_ session: SAPURLSession, task: SAPURLSessionTask, didCompleteWithError error: Error?) -
Declaration
Swift
public func sapURLSession(_ session: SAPURLSession, task: SAPURLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (SAPURLSession.HTTPRedirectDisposition) -> Swift.Void) -
Declaration
Swift
public func sapURLSession(_ session: SAPURLSession, task: SAPURLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (SAPURLSession.AuthChallengeDisposition) -> Swift.Void) -
Undocumented
Declaration
Swift
public func copy() -> Any? -
Declaration
Swift
public func clear()