Skip to content

Instantly share code, notes, and snippets.

@w-i-n-s
Created June 7, 2019 18:51

Revisions

  1. w-i-n-s created this gist Jun 7, 2019.
    71 changes: 71 additions & 0 deletions BasicAuth.playground
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    import Foundation
    import PlaygroundSupport

    PlaygroundPage.current.needsIndefiniteExecution = true

    struct WebPage {
    let user: String
    let password: String
    let urlString: String
    }
    let testWebPage = WebPage(user: "guest", password: "guest", urlString: "https://jigsaw.w3.org/HTTP/Basic/")

    let currentPage = testWebPage
    let base64LoginString = "\(currentPage.user):\(currentPage.password)".data(using: String.Encoding.utf8)!.base64EncodedString()
    let url = URL(string: currentPage.urlString)!

    //// 1 URLProtectionSpace
    //print("--------- 1 ----------")
    //let credential = URLCredential(user: cred.user, password: cred.password, persistence: URLCredential.Persistence.forSession)
    //let protectionSpace = URLProtectionSpace(host: "...", port: 443, protocol: "http", realm: "Restricted", authenticationMethod: NSURLAuthenticationMethodHTTPBasic)
    //URLCredentialStorage.shared.setDefaultCredential(credential, for: protectionSpace)
    //
    //let config = URLSessionConfiguration.default
    //let session = URLSession(configuration: config)
    //
    //let task = session.dataTask(with: url) { (data, response, error) in
    // guard error == nil else {
    // print(error?.localizedDescription ?? "")
    // return
    // }
    //
    // print(String(data: data!, encoding: .utf8))
    //}
    //
    //task.resume()

    // 2 Header
    //print("--------- 2 ----------")
    var request = NSMutableURLRequest(url: url)// NSMutable is a key!
    request.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization")
    URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error) in
    guard let _ = data else { return }
    print(response!)
    }.resume()
    //
    //
    //// 3 Session configuration header
    //print("--------- 3 ----------")
    //let configuration = URLSessionConfiguration.default
    //configuration.httpAdditionalHeaders = ["Authorization" : "Basic \(base64LoginString)"]
    //let session2 = URLSession(configuration: configuration)
    //session2.dataTask(with: request as URLRequest) { (data, response, error) in
    // guard let _ = data else { return }
    // print(response!)
    //}.resume()
    //
    //

    //let request4 = NSMutableURLRequest(url: url as URL)
    //
    //let config4 = URLSessionConfiguration.default
    //let userPasswordString = "\(currentPage.user):\(currentPage.password)"
    //let userPasswordData = userPasswordString.data(using: String.Encoding.utf8)
    //let base64EncodedCredential = userPasswordData!.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0)) // Options is NOT a key
    //let authString = "Basic \(base64EncodedCredential)"
    //config4.httpAdditionalHeaders = ["Authorization" : authString]
    //let session4 = URLSession(configuration: config4)
    //let task4 = session4.dataTask(with: request4 as URLRequest) { (data, response, error) -> Void in
    // print(response)
    //}
    //task4.resume()