@@ -22,41 +22,14 @@ public protocol AuthorizationProvider: Sendable {
22
22
}
23
23
24
24
public protocol AuthorizationWriter {
25
- @available ( * , noasync, message: " Use the async alternative " )
26
25
func addOrUpdate(
27
26
for url: URL ,
28
27
user: String ,
29
28
password: String ,
30
- persist: Bool ,
31
- callback: @escaping ( Result < Void , Error > ) -> Void
32
- )
29
+ persist: Bool
30
+ ) async throws
33
31
34
- @available ( * , noasync, message: " Use the async alternative " )
35
- func remove( for url: URL , callback: @escaping ( Result < Void , Error > ) -> Void )
36
- }
37
-
38
- public extension AuthorizationWriter {
39
- func addOrUpdate(
40
- for url: URL ,
41
- user: String ,
42
- password: String ,
43
- persist: Bool = true
44
- ) async throws {
45
- try await safe_async {
46
- self . addOrUpdate (
47
- for: url,
48
- user: user,
49
- password: password,
50
- persist: persist,
51
- callback: $0)
52
- }
53
- }
54
-
55
- func remove( for url: URL ) async throws {
56
- try await safe_async {
57
- self . remove ( for: url, callback: $0)
58
- }
59
- }
32
+ func remove( for url: URL ) async throws
60
33
}
61
34
62
35
public enum AuthorizationProviderError : Error {
@@ -100,24 +73,23 @@ public final class NetrcAuthorizationProvider: AuthorizationProvider, Authorizat
100
73
for url: URL ,
101
74
user: String ,
102
75
password: String ,
103
- persist: Bool = true ,
104
- callback: @escaping ( Result < Void , Error > ) -> Void
105
- ) {
76
+ persist: Bool = true
77
+ ) async throws {
106
78
guard let machine = Self . machine ( for: url) else {
107
- return callback ( . failure ( AuthorizationProviderError . invalidURLHost) )
79
+ throw AuthorizationProviderError . invalidURLHost
108
80
}
109
81
110
82
if !persist {
111
83
self . cache [ machine] = ( user, password)
112
- return callback ( . success ( ( ) ) )
84
+ return
113
85
}
114
86
115
87
// Same entry already exists, no need to add or update
116
88
let netrc = try ? Self . load ( fileSystem: self . fileSystem, path: self . path)
117
89
guard netrc? . machines
118
90
. first ( where: { $0. name. lowercased ( ) == machine && $0. login == user && $0. password == password } ) == nil
119
91
else {
120
- return callback ( . success ( ( ) ) )
92
+ return
121
93
}
122
94
123
95
do {
@@ -134,21 +106,15 @@ public final class NetrcAuthorizationProvider: AuthorizationProvider, Authorizat
134
106
stream. write ( " \n " )
135
107
}
136
108
}
137
-
138
- callback ( . success( ( ) ) )
139
109
} catch {
140
- callback ( . failure(
141
- AuthorizationProviderError
142
- . other ( " Failed to update netrc file at \( self . path) : \( error. interpolationDescription) " )
143
- ) )
110
+ throw AuthorizationProviderError
111
+ . other ( " Failed to update netrc file at \( self . path) : \( error. interpolationDescription) " )
144
112
}
145
113
}
146
114
147
- public func remove( for url: URL , callback: @escaping ( Result < Void , Error > ) -> Void ) {
148
- callback ( . failure(
149
- AuthorizationProviderError
150
- . other ( " User must edit netrc file at \( self . path) manually to remove entries " )
151
- ) )
115
+ public func remove( for url: URL ) async throws {
116
+ throw AuthorizationProviderError
117
+ . other ( " User must edit netrc file at \( self . path) manually to remove entries " )
152
118
}
153
119
154
120
public func authentication( for url: URL ) -> ( user: String , password: String ) ? {
@@ -217,47 +183,36 @@ public final class KeychainAuthorizationProvider: AuthorizationProvider, Authori
217
183
for url: URL ,
218
184
user: String ,
219
185
password: String ,
220
- persist: Bool = true ,
221
- callback: @escaping ( Result < Void , Error > ) -> Void
222
- ) {
186
+ persist: Bool = true
187
+ ) async throws {
223
188
guard let protocolHostPort = ProtocolHostPort ( from: url) else {
224
- return callback ( . failure ( AuthorizationProviderError . invalidURLHost) )
189
+ throw AuthorizationProviderError . invalidURLHost
225
190
}
226
191
227
192
self . observabilityScope
228
193
. emit ( debug: " add/update credentials for ' \( protocolHostPort) ' [ \( url. absoluteString) ] in keychain " )
229
194
230
195
if !persist {
231
196
self . cache [ protocolHostPort. description] = ( user, password)
232
- return callback ( . success ( ( ) ) )
197
+ return
233
198
}
234
199
235
200
let passwordData = Data ( password. utf8)
236
201
237
- do {
238
- if !( try self . update ( protocolHostPort: protocolHostPort, account: user, password: passwordData) ) {
239
- try self . create ( protocolHostPort: protocolHostPort, account: user, password: passwordData)
240
- }
241
- callback ( . success( ( ) ) )
242
- } catch {
243
- callback ( . failure( error) )
202
+ if !( try self . update ( protocolHostPort: protocolHostPort, account: user, password: passwordData) ) {
203
+ try self . create ( protocolHostPort: protocolHostPort, account: user, password: passwordData)
244
204
}
245
205
}
246
206
247
- public func remove( for url: URL , callback : @escaping ( Result < Void , Error > ) -> Void ) {
207
+ public func remove( for url: URL ) async throws {
248
208
guard let protocolHostPort = ProtocolHostPort ( from: url) else {
249
- return callback ( . failure ( AuthorizationProviderError . invalidURLHost) )
209
+ throw AuthorizationProviderError . invalidURLHost
250
210
}
251
211
252
212
self . observabilityScope
253
213
. emit ( debug: " remove credentials for ' \( protocolHostPort) ' [ \( url. absoluteString) ] from keychain " )
254
214
255
- do {
256
- try self . delete ( protocolHostPort: protocolHostPort)
257
- callback ( . success( ( ) ) )
258
- } catch {
259
- callback ( . failure( error) )
260
- }
215
+ try self . delete ( protocolHostPort: protocolHostPort)
261
216
}
262
217
263
218
public func authentication( for url: URL ) -> ( user: String , password: String ) ? {
0 commit comments