Skip to content

Commit 4c9973b

Browse files
committed
remove environment. Client takes in url session
1 parent f453145 commit 4c9973b

2 files changed

Lines changed: 20 additions & 38 deletions

File tree

Sources/XcodesLoginKit/Client.swift

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,16 @@ public class Client {
1818
/// Security key client. Hold reference for cancelling if needed
1919
private var fido2: FIDO2?
2020

21-
public init() {}
21+
private let networkService: AsyncHTTPNetworkService
2222

23+
public init(urlSession: URLSession = .shared) {
24+
self.networkService = AsyncHTTPNetworkService(urlSession: urlSession)
25+
}
2326

27+
public var urlSession: URLSession {
28+
return networkService.urlSession
29+
}
30+
2431
// MARK: Login
2532

2633
@MainActor
@@ -30,15 +37,15 @@ public class Client {
3037
let a = clientKeys.public
3138

3239

33-
let serviceKeyResponse: ServiceKeyResponse = try await Current.network.networkService.requestObject(URLRequest.itcServiceKey)
40+
let serviceKeyResponse: ServiceKeyResponse = try await networkService.requestObject(URLRequest.itcServiceKey)
3441
let serviceKey = serviceKeyResponse.authServiceKey
3542

3643
// Fixes issue https://github.com/RobotsAndPencils/XcodesApp/issues/360
3744
// On 2023-02-23, Apple added a custom implementation of hashcash to their auth flow
3845
// Without this addition, Apple ID's would get set to locked
3946
let hashcash = try await loadHashcash(accountName: accountName, serviceKey: serviceKey)
4047

41-
let srp: ServerSRPInitResponse = try await Current.network.networkService.requestObject(URLRequest.SRPInit(serviceKey: serviceKey, a: Data(a.bytes).base64EncodedString(), accountName: accountName))
48+
let srp: ServerSRPInitResponse = try await networkService.requestObject(URLRequest.SRPInit(serviceKey: serviceKey, a: Data(a.bytes).base64EncodedString(), accountName: accountName))
4249

4350
// SRP
4451
guard let decodedB = Data(base64Encoded: srp.b) else {
@@ -58,7 +65,7 @@ public class Client {
5865
let m1 = client.calculateClientProof(username: accountName, salt: [UInt8](decodedSalt), clientPublicKey: a, serverPublicKey: .init([UInt8](decodedB)), sharedSecret: .init(sharedSecret.bytes))
5966
let m2 = client.calculateServerProof(clientPublicKey: a, clientProof: m1, sharedSecret: .init([UInt8](sharedSecret.bytes)))
6067

61-
let result: (Data, URLResponse) = try await Current.network.networkService.requestData(URLRequest.SRPComplete(serviceKey: serviceKey, hashcash: hashcash, accountName: accountName, c: srp.c, m1: Data(m1).base64EncodedString(), m2: Data(m2).base64EncodedString()), validators: [])
68+
let result: (Data, URLResponse) = try await networkService.requestData(URLRequest.SRPComplete(serviceKey: serviceKey, hashcash: hashcash, accountName: accountName, c: srp.c, m1: Data(m1).base64EncodedString(), m2: Data(m2).base64EncodedString()), validators: [])
6269

6370
guard let httpResponse = result.1 as? HTTPURLResponse else {
6471
throw NetworkError.invalidResponseFormat
@@ -77,7 +84,7 @@ public class Client {
7784

7885
switch httpResponse.statusCode {
7986
case 200:
80-
let authenticationSession: AppleSession = try await Current.network.networkService.requestObject(URLRequest.olympusSession)
87+
let authenticationSession: AppleSession = try await networkService.requestObject(URLRequest.olympusSession)
8188
return AuthenticationState.authenticated(authenticationSession)
8289
case 401:
8390
throw AuthenticationError.invalidUsernameOrPassword(username: accountName)
@@ -102,7 +109,7 @@ public class Client {
102109
let sessionID = (httpResponse.allHeaderFields["X-Apple-ID-Session-Id"] as! String)
103110
let scnt = (httpResponse.allHeaderFields["scnt"] as! String)
104111

105-
let authOptions: AuthOptionsResponse = try await Current.network.networkService.requestObject(URLRequest.authOptions(serviceKey: serviceKey, sessionID: sessionID, scnt: scnt))
112+
let authOptions: AuthOptionsResponse = try await networkService.requestObject(URLRequest.authOptions(serviceKey: serviceKey, sessionID: sessionID, scnt: scnt))
106113

107114
switch authOptions.kind {
108115
case .twoStep:
@@ -140,7 +147,7 @@ public class Client {
140147
@MainActor
141148
private func loadHashcash(accountName: String, serviceKey: String) async throws -> String {
142149

143-
let result: (Data, URLResponse) = try await Current.network.networkService.requestData(URLRequest.federate(account: accountName, serviceKey: serviceKey), validators: [])
150+
let result: (Data, URLResponse) = try await networkService.requestData(URLRequest.federate(account: accountName, serviceKey: serviceKey), validators: [])
144151
let response = result.1
145152

146153
guard let response = response as? HTTPURLResponse else {
@@ -214,13 +221,13 @@ public class Client {
214221
/// - Returns: AuthenticationState.waitingForSecondFactor
215222
public func requestSMSSecurityCode(to trustedPhoneNumber: AuthOptionsResponse.TrustedPhoneNumber, authOptions: AuthOptionsResponse, sessionData: AppleSessionData) async throws -> AuthenticationState {
216223

217-
let result = try await Current.network.networkService.requestVoid(URLRequest.requestSecurityCode(serviceKey: sessionData.serviceKey, sessionID: sessionData.sessionID, scnt: sessionData.scnt, trustedPhoneID: trustedPhoneNumber.id))
224+
let result = try await networkService.requestVoid(URLRequest.requestSecurityCode(serviceKey: sessionData.serviceKey, sessionID: sessionData.sessionID, scnt: sessionData.scnt, trustedPhoneID: trustedPhoneNumber.id))
218225

219226
return AuthenticationState.waitingForSecondFactor(.smsSent(trustedPhoneNumber), authOptions, sessionData)
220227
}
221228

222229
public func submitSecurityCode(_ code: SecurityCode, sessionData: AppleSessionData) async throws ->AuthenticationState {
223-
let result: (Data, URLResponse) = try await Current.network.networkService.requestData(URLRequest.submitSecurityCode(serviceKey: sessionData.serviceKey, sessionID: sessionData.sessionID, scnt: sessionData.scnt, code: code), validators: [])
230+
let result: (Data, URLResponse) = try await networkService.requestData(URLRequest.submitSecurityCode(serviceKey: sessionData.serviceKey, sessionID: sessionData.sessionID, scnt: sessionData.scnt, code: code), validators: [])
224231
let response = result.1
225232

226233
guard let response = response as? HTTPURLResponse else {
@@ -244,7 +251,7 @@ public class Client {
244251

245252
public func submitChallenge(response: Data, sessionData: AppleSessionData) async throws -> AuthenticationState {
246253

247-
let result: (Data, URLResponse) = try await Current.network.networkService.requestData(URLRequest.respondToChallenge(serviceKey: sessionData.serviceKey, sessionID: sessionData.sessionID, scnt: sessionData.scnt, response: response), validators: [])
254+
let result: (Data, URLResponse) = try await networkService.requestData(URLRequest.respondToChallenge(serviceKey: sessionData.serviceKey, sessionID: sessionData.sessionID, scnt: sessionData.scnt, response: response), validators: [])
248255
let response = result.1
249256

250257
guard let response = response as? HTTPURLResponse else {
@@ -267,12 +274,12 @@ public class Client {
267274
}
268275

269276
func updateSession(serviceKey: String, sessionID: String, scnt: String) async throws -> AuthenticationState {
270-
try await Current.network.networkService.requestVoid(URLRequest.trust(serviceKey: serviceKey, sessionID: sessionID, scnt: scnt))
277+
try await networkService.requestVoid(URLRequest.trust(serviceKey: serviceKey, sessionID: sessionID, scnt: scnt))
271278
return try await loadSession()
272279
}
273280

274281
func loadSession() async throws -> AuthenticationState {
275-
let authenticationSession: AppleSession = try await Current.network.networkService.requestObject(URLRequest.olympusSession)
282+
let authenticationSession: AppleSession = try await networkService.requestObject(URLRequest.olympusSession)
276283
return AuthenticationState.authenticated(authenticationSession)
277284
}
278285
}
@@ -316,7 +323,7 @@ extension Client {
316323
/// Clears any cookies from URLSession
317324
@MainActor
318325
public func signout() {
319-
Current.network.session.configuration.httpCookieStorage?.removeCookies(since: .distantPast)
326+
networkService.urlSession.configuration.httpCookieStorage?.removeCookies(since: .distantPast)
320327
}
321328
}
322329

Sources/XcodesLoginKit/Environment.swift

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)