Created
October 16, 2021 16:52
-
-
Save kpostekk/b45dc534e600f5dc3d52a8f04ddc05ef to your computer and use it in GitHub Desktop.
Example of using NTLM authentication in Swift 5.5 (iOS 15)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Based on this gist
https://gist.github.com/stevenschobert/f374c999e5cba6ccf09653b846967c83