Skip to content

Custom authentication headers

Eric Marchand edited this page Jan 11, 2017 · 1 revision

If you want to customise authentication headers implement the protocol OAuthSwiftCredentialHeadersFactory and set your object into credential

oauthSwift.client.credential.headersFactory=<your custom object>

for instance change the header key name from "Authorization" to "X-Authorization"

class XHeaders: OAuthSwiftCredentialHeadersFactory {
    let credential: OAuthSwiftCredential
    init(credential: OAuthSwiftCredential) {
       self.credential = credential
    }
    
    func make(_ url:URL, method: OAuthSwiftHTTPRequest.Method, parameters: OAuthSwift.Parameters, body: Data?) -> Dictionary<String, String> {
        // assert  credential.version == .oauth2 
        return credential.oauthToken.isEmpty ? [:] : ["X-Authorization": "Bearer \(credential.oauthToken)"]
    }
}