Skip to content

Instantly share code, notes, and snippets.

@kpostekk
Created October 16, 2021 16:52
Show Gist options
  • Save kpostekk/b45dc534e600f5dc3d52a8f04ddc05ef to your computer and use it in GitHub Desktop.
Save kpostekk/b45dc534e600f5dc3d52a8f04ddc05ef to your computer and use it in GitHub Desktop.
Example of using NTLM authentication in Swift 5.5 (iOS 15)
import Foundation
class GakkoTimetableFetcher: NSObject, URLSessionTaskDelegate {
let login: String
let password: String
var session: URLSession?
init(login: String, password: String) {
self.login = login
self.password = password
}
/// Performs auth for NTLM
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
let creds = URLCredential(user: self.login, password: self.password, persistence: .permanent)
challenge.sender?.use(creds, for: challenge)
completionHandler(.useCredential, creds)
}
func schedule(begins: Date, ends: Date) async throws -> (Data, URLResponse) {
if session == nil {
session = URLSession(configuration: .default, delegate: self, delegateQueue: nil)
}
let targetUrl = URL(string: "https://ws.pjwstk.edu.pl/test/Service.svc/XMLService/GetStudentScheduleJson")!
let req = URLRequest(url: targetUrl)
return try await session!.data(for: req, delegate: self)
}
}
@kpostekk
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment