CSRFTokenObserver
public class CSRFTokenObserver : SAPURLSessionObserving
This observer is responsible to set the required X-CSRF-Token header for communication with services exposed by SAPcpms.
To enable this functionality you need to register this observer to an SAPURLSession
instance.
For token storing the CSRFTokenStoring
protocol is used.
This is an example for a basic usage of this observer if you have one root URL for every CSRF token request:
The rootUrl
parameter is the URL from which the CSRF token will be requested.
let urlSession: SAPURLSession = <#SAPURLSession instance#>
let rootUrl: URL = <#your root URL#>
let csrfTokenObserver = CSRFTokenObserver(rootUrl: rootUrl)
urlSession.register(csrfTokenObserver)
If you have multiple CSRF protected backends and these backends accept different CSRF tokens you must implement CSRFTokenURLProviding
protocol to provide CSRF URL for different request URL’s. For token storing the CSRFTokenStoring
protocol is used.
class myCSRFTokenURLProvider: CSRFTokenURLProviding {
func csrfTokenURL(for: URL) -> URL {
// implement custom logic to create token URL from the request URL
// ...
return <#token URL#>
}
}
let csrfTokenObserver = CSRFTokenObserver(tokenUrlProvider: myCSRFTokenURLProvider, tokenStore: myCSRFTokenStore)
urlSession.register(csrfTokenObserver)
-
Initializes a CSRFTokenHTTPObserver instance. Use if you have only one root URL for every CSRF token request.
Declaration
Swift
public convenience init(rootUrl: URL, tokenStore: CSRFTokenStoring = CSRFTokenStorage(store: CompositeStorage()))
Parameters
rootUrl
the URL from which the CSRF token will be requested.
tokenStore
(optional) the
CSRFTokenStoring
implementation which can provide and store the obtained token. -
Initializes a CSRFTokenHTTPObserver instance. Use if you have multiple backends and backends accept different CSRF tokens.
Declaration
Swift
public init(tokenUrlProvider: CSRFTokenURLProviding, tokenStore: CSRFTokenStoring = CSRFTokenStorage(store: CompositeStorage()))
Parameters
tokenUrlProvider
the
CSRFTokenURLProviding
implementation which provides the CSRF URL for every request URL which are protected with CSRF.tokenStore
the
CSRFTokenStoring
implementation which can provide and store the obtained token. -
Declaration
Swift
public func sapURLSession(_ session: SAPURLSession, task: SAPURLSessionTask, willSend request: URLRequest, completionHandler: @escaping (SAPURLSession.RequestDisposition) -> Void)
-
Declaration
Swift
public func sapURLSession(_ session: SAPURLSession, dataTask: SAPURLSessionTask, didReceive response: URLResponse, completionHandler: @escaping (SAPURLSession.ResponseDisposition) -> Void)
-
Undocumented
Declaration
Swift
public func copy() -> Any?