@@ -24,7 +24,13 @@ public protocol AuthorizationProvider {
2424}
2525
2626public protocol AuthorizationWriter {
27- func addOrUpdate( for url: URL , user: String , password: String , persist: Bool , callback: @escaping ( Result < Void , Error > ) -> Void )
27+ func addOrUpdate(
28+ for url: URL ,
29+ user: String ,
30+ password: String ,
31+ persist: Bool ,
32+ callback: @escaping ( Result < Void , Error > ) -> Void
33+ )
2834
2935 func remove( for url: URL , callback: @escaping ( Result < Void , Error > ) -> Void )
3036}
@@ -36,9 +42,9 @@ public enum AuthorizationProviderError: Error {
3642 case other( String )
3743}
3844
39- public extension AuthorizationProvider {
45+ extension AuthorizationProvider {
4046 @Sendable
41- func httpAuthorizationHeader( for url: URL ) -> String ? {
47+ public func httpAuthorizationHeader( for url: URL ) -> String ? {
4248 guard let ( user, password) = self . authentication ( for: url) else {
4349 return nil
4450 }
@@ -50,8 +56,8 @@ public extension AuthorizationProvider {
5056 }
5157}
5258
53- private extension URL {
54- var authenticationID : String ? {
59+ extension URL {
60+ fileprivate var authenticationID : String ? {
5561 guard let host = host? . lowercased ( ) else {
5662 return nil
5763 }
@@ -75,7 +81,13 @@ public class NetrcAuthorizationProvider: AuthorizationProvider, AuthorizationWri
7581 _ = try Self . load ( fileSystem: fileSystem, path: path)
7682 }
7783
78- public func addOrUpdate( for url: URL , user: String , password: String , persist: Bool = true , callback: @escaping ( Result < Void , Error > ) -> Void ) {
84+ public func addOrUpdate(
85+ for url: URL ,
86+ user: String ,
87+ password: String ,
88+ persist: Bool = true ,
89+ callback: @escaping ( Result < Void , Error > ) -> Void
90+ ) {
7991 guard let machine = url. authenticationID else {
8092 return callback ( . failure( AuthorizationProviderError . invalidURLHost) )
8193 }
@@ -87,7 +99,9 @@ public class NetrcAuthorizationProvider: AuthorizationProvider, AuthorizationWri
8799
88100 // Same entry already exists, no need to add or update
89101 let netrc = try ? Self . load ( fileSystem: self . fileSystem, path: self . path)
90- guard netrc? . machines. first ( where: { $0. name. lowercased ( ) == machine && $0. login == user && $0. password == password } ) == nil else {
102+ guard netrc? . machines
103+ . first ( where: { $0. name. lowercased ( ) == machine && $0. login == user && $0. password == password } ) == nil
104+ else {
91105 return callback ( . success( ( ) ) )
92106 }
93107
@@ -108,12 +122,18 @@ public class NetrcAuthorizationProvider: AuthorizationProvider, AuthorizationWri
108122
109123 callback ( . success( ( ) ) )
110124 } catch {
111- callback ( . failure( AuthorizationProviderError . other ( " Failed to update netrc file at \( self . path) : \( error) " ) ) )
125+ callback ( . failure(
126+ AuthorizationProviderError
127+ . other ( " Failed to update netrc file at \( self . path) : \( error. interpolationDescription) " )
128+ ) )
112129 }
113130 }
114131
115132 public func remove( for url: URL , callback: @escaping ( Result < Void , Error > ) -> Void ) {
116- callback ( . failure( AuthorizationProviderError . other ( " User must edit netrc file at \( self . path) manually to remove entries " ) ) )
133+ callback ( . failure(
134+ AuthorizationProviderError
135+ . other ( " User must edit netrc file at \( self . path) manually to remove entries " )
136+ ) )
117137 }
118138
119139 public func authentication( for url: URL ) -> ( user: String , password: String ) ? {
@@ -124,7 +144,9 @@ public class NetrcAuthorizationProvider: AuthorizationProvider, AuthorizationWri
124144 }
125145
126146 private func machine( for url: URL ) -> Basics . Netrc . Machine ? {
127- if let machine = url. authenticationID, let existing = self . machines. first ( where: { $0. name. lowercased ( ) == machine } ) {
147+ if let machine = url. authenticationID,
148+ let existing = self . machines. first ( where: { $0. name. lowercased ( ) == machine } )
149+ {
128150 return existing
129151 }
130152 if let existing = self . machines. first ( where: { $0. isDefault } ) {
@@ -164,7 +186,13 @@ public class KeychainAuthorizationProvider: AuthorizationProvider, Authorization
164186 self . observabilityScope = observabilityScope
165187 }
166188
167- public func addOrUpdate( for url: URL , user: String , password: String , persist: Bool = true , callback: @escaping ( Result < Void , Error > ) -> Void ) {
189+ public func addOrUpdate(
190+ for url: URL ,
191+ user: String ,
192+ password: String ,
193+ persist: Bool = true ,
194+ callback: @escaping ( Result < Void , Error > ) -> Void
195+ ) {
168196 guard let server = url. authenticationID else {
169197 return callback ( . failure( AuthorizationProviderError . invalidURLHost) )
170198 }
@@ -214,12 +242,14 @@ public class KeychainAuthorizationProvider: AuthorizationProvider, Authorization
214242 }
215243
216244 do {
217- guard let existingItem = try self . search ( server: server, protocol: self . protocol ( for: url) ) as? [ String : Any ] ,
218- let passwordData = existingItem [ kSecValueData as String ] as? Data ,
219- let password = String ( data: passwordData, encoding: String . Encoding. utf8) ,
220- let account = existingItem [ kSecAttrAccount as String ] as? String
245+ guard let existingItem = try self
246+ . search ( server: server, protocol: self . protocol ( for: url) ) as? [ String : Any ] ,
247+ let passwordData = existingItem [ kSecValueData as String ] as? Data ,
248+ let password = String ( data: passwordData, encoding: String . Encoding. utf8) ,
249+ let account = existingItem [ kSecAttrAccount as String ] as? String
221250 else {
222- throw AuthorizationProviderError . other ( " Failed to extract credentials for server \( server) from keychain " )
251+ throw AuthorizationProviderError
252+ . other ( " Failed to extract credentials for server \( server) from keychain " )
223253 }
224254 return ( user: account, password: password)
225255 } catch {
@@ -229,7 +259,10 @@ public class KeychainAuthorizationProvider: AuthorizationProvider, Authorization
229259 case AuthorizationProviderError . other( let detail) :
230260 self . observabilityScope. emit ( error: detail)
231261 default :
232- self . observabilityScope. emit ( error: " Failed to find credentials for server \( server) in keychain: \( error) " )
262+ self . observabilityScope. emit (
263+ error: " Failed to find credentials for server \( server) in keychain " ,
264+ underlyingError: error
265+ )
233266 }
234267 return nil
235268 }
@@ -244,7 +277,8 @@ public class KeychainAuthorizationProvider: AuthorizationProvider, Authorization
244277
245278 let status = SecItemAdd ( query as CFDictionary , nil )
246279 guard status == errSecSuccess else {
247- throw AuthorizationProviderError . other ( " Failed to save credentials for server \( server) to keychain: status \( status) " )
280+ throw AuthorizationProviderError
281+ . other ( " Failed to save credentials for server \( server) to keychain: status \( status) " )
248282 }
249283 }
250284
@@ -260,7 +294,8 @@ public class KeychainAuthorizationProvider: AuthorizationProvider, Authorization
260294 return false
261295 }
262296 guard status == errSecSuccess else {
263- throw AuthorizationProviderError . other ( " Failed to update credentials for server \( server) in keychain: status \( status) " )
297+ throw AuthorizationProviderError
298+ . other ( " Failed to update credentials for server \( server) in keychain: status \( status) " )
264299 }
265300 return true
266301 }
@@ -271,7 +306,8 @@ public class KeychainAuthorizationProvider: AuthorizationProvider, Authorization
271306 kSecAttrProtocol as String : `protocol`]
272307 let status = SecItemDelete ( query as CFDictionary )
273308 guard status == errSecSuccess else {
274- throw AuthorizationProviderError . other ( " Failed to delete credentials for server \( server) from keychain: status \( status) " )
309+ throw AuthorizationProviderError
310+ . other ( " Failed to delete credentials for server \( server) from keychain: status \( status) " )
275311 }
276312 }
277313
@@ -290,7 +326,8 @@ public class KeychainAuthorizationProvider: AuthorizationProvider, Authorization
290326 throw AuthorizationProviderError . notFound
291327 }
292328 guard status == errSecSuccess else {
293- throw AuthorizationProviderError . other ( " Failed to find credentials for server \( server) in keychain: status \( status) " )
329+ throw AuthorizationProviderError
330+ . other ( " Failed to find credentials for server \( server) in keychain: status \( status) " )
294331 }
295332
296333 return item
0 commit comments