Skip to content

Instantly share code, notes, and snippets.

@w-i-n-s
Created June 7, 2019 18:51
Show Gist options
  • Save w-i-n-s/0fe29e358dddd9d145697d8c10b8ec2f to your computer and use it in GitHub Desktop.
Save w-i-n-s/0fe29e358dddd9d145697d8c10b8ec2f to your computer and use it in GitHub Desktop.
Boilerplate for Basic auth
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment