Skip to content

Commit d728e49

Browse files
committed
Fix formatting
1 parent 61d1c60 commit d728e49

File tree

2 files changed

+56
-55
lines changed

2 files changed

+56
-55
lines changed

Sources/Basics/AuthorizationProvider.swift

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public enum AuthorizationProviderError: Error {
2828
case other(String)
2929
}
3030

31-
extension AuthorizationProvider {
32-
public func httpAuthorizationHeader(for url: Foundation.URL) -> String? {
31+
public extension AuthorizationProvider {
32+
func httpAuthorizationHeader(for url: Foundation.URL) -> String? {
3333
guard let (user, password) = self.authentication(for: url) else {
3434
return nil
3535
}
@@ -55,9 +55,9 @@ extension Foundation.URL {
5555
public struct NetrcAuthorizationProvider: AuthorizationProvider {
5656
let path: AbsolutePath
5757
private let fileSystem: FileSystem
58-
58+
5959
private var underlying: TSCUtility.Netrc?
60-
60+
6161
var machines: [TSCUtility.Netrc.Machine] {
6262
self.underlying?.machines ?? []
6363
}
@@ -67,17 +67,17 @@ public struct NetrcAuthorizationProvider: AuthorizationProvider {
6767
self.fileSystem = fileSystem
6868
self.underlying = try Self.load(from: path)
6969
}
70-
70+
7171
public mutating func addOrUpdate(for url: Foundation.URL, user: String, password: String, callback: @escaping (Result<Void, Error>) -> Void) {
7272
guard let machine = url.authenticationID else {
7373
return callback(.failure(AuthorizationProviderError.invalidURLHost))
7474
}
75-
75+
7676
// Same entry already exists, no need to add or update
7777
guard self.machines.first(where: { $0.name.lowercased() == machine && $0.login == user && $0.password == password }) == nil else {
7878
return
7979
}
80-
80+
8181
do {
8282
// Append to end of file
8383
try self.fileSystem.withLock(on: self.path, type: .exclusive) {
@@ -92,13 +92,13 @@ public struct NetrcAuthorizationProvider: AuthorizationProvider {
9292
stream.write("\n")
9393
}
9494
}
95-
95+
9696
// At this point the netrc file should exist and non-empty
9797
guard let netrc = try Self.load(from: self.path) else {
9898
throw AuthorizationProviderError.other("Failed to update netrc file at \(self.path)")
9999
}
100100
self.underlying = netrc
101-
101+
102102
callback(.success(()))
103103
} catch {
104104
callback(.failure(AuthorizationProviderError.other("Failed to update netrc file at \(self.path): \(error)")))
@@ -139,16 +139,16 @@ public struct NetrcAuthorizationProvider: AuthorizationProvider {
139139
#if canImport(Security)
140140
public struct KeychainAuthorizationProvider: AuthorizationProvider {
141141
public init() {}
142-
142+
143143
public func addOrUpdate(for url: Foundation.URL, user: String, password: String, callback: @escaping (Result<Void, Error>) -> Void) {
144144
guard let server = url.authenticationID else {
145145
return callback(.failure(AuthorizationProviderError.invalidURLHost))
146146
}
147147
guard let passwordData = password.data(using: .utf8) else {
148148
return callback(.failure(AuthorizationProviderError.cannotEncodePassword))
149149
}
150-
let `protocol` = self.`protocol`(for: url)
151-
150+
let `protocol` = self.protocol(for: url)
151+
152152
do {
153153
if !(try self.update(server: server, protocol: `protocol`, account: user, password: passwordData)) {
154154
try self.create(server: server, protocol: `protocol`, account: user, password: passwordData)
@@ -158,17 +158,18 @@ public struct KeychainAuthorizationProvider: AuthorizationProvider {
158158
callback(.failure(error))
159159
}
160160
}
161-
161+
162162
public func authentication(for url: Foundation.URL) -> (user: String, password: String)? {
163163
guard let server = url.authenticationID else {
164164
return nil
165165
}
166-
166+
167167
do {
168-
guard let existingItem = try self.search(server: server, protocol: self.`protocol`(for: url)) as? [String : Any],
168+
guard let existingItem = try self.search(server: server, protocol: self.protocol(for: url)) as? [String: Any],
169169
let passwordData = existingItem[kSecValueData as String] as? Data,
170170
let password = String(data: passwordData, encoding: String.Encoding.utf8),
171-
let account = existingItem[kSecAttrAccount as String] as? String else {
171+
let account = existingItem[kSecAttrAccount as String] as? String
172+
else {
172173
throw AuthorizationProviderError.other("Failed to extract credentials for server \(server) from keychain")
173174
}
174175
return (user: account, password: password)
@@ -184,27 +185,27 @@ public struct KeychainAuthorizationProvider: AuthorizationProvider {
184185
return nil
185186
}
186187
}
187-
188-
private func create(server: String, `protocol`: CFString, account: String, password: Data) throws {
188+
189+
private func create(server: String, protocol: CFString, account: String, password: Data) throws {
189190
let query: [String: Any] = [kSecClass as String: kSecClassInternetPassword,
190191
kSecAttrServer as String: server,
191192
kSecAttrProtocol as String: `protocol`,
192193
kSecAttrAccount as String: account,
193194
kSecValueData as String: password]
194-
195+
195196
let status = SecItemAdd(query as CFDictionary, nil)
196197
guard status == errSecSuccess else {
197198
throw AuthorizationProviderError.other("Failed to save credentials for server \(server) to keychain: status \(status)")
198199
}
199200
}
200-
201-
private func update(server: String, `protocol`: CFString, account: String, password: Data) throws -> Bool {
201+
202+
private func update(server: String, protocol: CFString, account: String, password: Data) throws -> Bool {
202203
let query: [String: Any] = [kSecClass as String: kSecClassInternetPassword,
203204
kSecAttrServer as String: server,
204205
kSecAttrProtocol as String: `protocol`]
205206
let attributes: [String: Any] = [kSecAttrAccount as String: account,
206207
kSecValueData as String: password]
207-
208+
208209
let status = SecItemUpdate(query as CFDictionary, attributes as CFDictionary)
209210
guard status != errSecItemNotFound else {
210211
return false
@@ -214,15 +215,15 @@ public struct KeychainAuthorizationProvider: AuthorizationProvider {
214215
}
215216
return true
216217
}
217-
218-
private func search(server: String, `protocol`: CFString) throws -> CFTypeRef? {
218+
219+
private func search(server: String, protocol: CFString) throws -> CFTypeRef? {
219220
let query: [String: Any] = [kSecClass as String: kSecClassInternetPassword,
220221
kSecAttrServer as String: server,
221222
kSecAttrProtocol as String: `protocol`,
222223
kSecMatchLimit as String: kSecMatchLimitOne,
223224
kSecReturnAttributes as String: true,
224225
kSecReturnData as String: true]
225-
226+
226227
var item: CFTypeRef?
227228
// Search keychain for server's username and password, if any.
228229
let status = SecItemCopyMatching(query as CFDictionary, &item)
@@ -232,10 +233,10 @@ public struct KeychainAuthorizationProvider: AuthorizationProvider {
232233
guard status == errSecSuccess else {
233234
throw AuthorizationProviderError.other("Failed to find credentials for server \(server) in keychain: status \(status)")
234235
}
235-
236+
236237
return item
237238
}
238-
239+
239240
private func `protocol`(for url: Foundation.URL) -> CFString {
240241
// See https://developer.apple.com/documentation/security/keychain_services/keychain_items/item_attribute_keys_and_values?language=swift
241242
// for a list of possible values for the `kSecAttrProtocol` attribute.
@@ -253,15 +254,15 @@ public struct KeychainAuthorizationProvider: AuthorizationProvider {
253254

254255
public struct CompositeAuthorizationProvider: AuthorizationProvider {
255256
private let providers: [AuthorizationProvider]
256-
257+
257258
public init(_ providers: AuthorizationProvider...) {
258259
self.init(providers)
259260
}
260-
261+
261262
public init(_ providers: [AuthorizationProvider]) {
262263
self.providers = providers
263264
}
264-
265+
265266
public func authentication(for url: Foundation.URL) -> (user: String, password: String)? {
266267
for provider in self.providers {
267268
if let authentication = provider.authentication(for: url) {

Tests/BasicsTests/AuthorizationProviderTests.swift

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,106 +19,106 @@ final class AuthorizationProviderTests: XCTestCase {
1919
let url = URL(string: "http://\(UUID().uuidString)")!
2020
let user = UUID().uuidString
2121
let password = UUID().uuidString
22-
22+
2323
let provider = TestProvider(map: [url: (user: user, password: password)])
2424
self.assertAuthentication(provider, for: url, expected: (user, password))
2525
}
26-
26+
2727
func testNetrc() throws {
2828
try testWithTemporaryDirectory { tmpPath in
2929
let netrcPath = tmpPath.appending(component: ".netrc")
30-
30+
3131
var provider = try NetrcAuthorizationProvider(path: netrcPath, fileSystem: localFileSystem)
3232

3333
let user = UUID().uuidString
34-
34+
3535
let url = URL(string: "http://\(UUID().uuidString)")!
3636
let password = UUID().uuidString
37-
37+
3838
let otherURL = URL(string: "https://\(UUID().uuidString)")!
3939
let otherPassword = UUID().uuidString
40-
40+
4141
// Add
4242
XCTAssertNoThrow(try tsc_await { callback in provider.addOrUpdate(for: url, user: user, password: password, callback: callback) })
4343
XCTAssertNoThrow(try tsc_await { callback in provider.addOrUpdate(for: otherURL, user: user, password: otherPassword, callback: callback) })
44-
44+
4545
self.assertAuthentication(provider, for: url, expected: (user, password))
46-
46+
4747
// Update - the new password is appended to the end of file
4848
let newPassword = UUID().uuidString
4949
XCTAssertNoThrow(try tsc_await { callback in provider.addOrUpdate(for: url, user: user, password: newPassword, callback: callback) })
50-
50+
5151
// .netrc file now contains two entries for `url`: one with `password` and the other with `newPassword`.
5252
// `NetrcAuthorizationProvider` returns the first entry it finds.
5353
self.assertAuthentication(provider, for: url, expected: (user, password))
54-
54+
5555
// Make sure the new entry is saved
5656
XCTAssertNotNil(provider.machines.first(where: { $0.name == url.host!.lowercased() && $0.login == user && $0.password == newPassword }))
57-
57+
5858
self.assertAuthentication(provider, for: otherURL, expected: (user, otherPassword))
5959
}
6060
}
61-
61+
6262
func testKeychain() throws {
6363
#if !canImport(Security) || !ENABLE_KEYCHAIN_TEST
6464
try XCTSkipIf(true)
6565
#else
6666
let provider = KeychainAuthorizationProvider()
6767

6868
let user = UUID().uuidString
69-
69+
7070
let url = URL(string: "http://\(UUID().uuidString)")!
7171
let password = UUID().uuidString
72-
72+
7373
let otherURL = URL(string: "https://\(UUID().uuidString)")!
7474
let otherPassword = UUID().uuidString
75-
75+
7676
// Add
7777
XCTAssertNoThrow(try tsc_await { callback in provider.addOrUpdate(for: url, user: user, password: password, callback: callback) })
7878
XCTAssertNoThrow(try tsc_await { callback in provider.addOrUpdate(for: otherURL, user: user, password: otherPassword, callback: callback) })
79-
79+
8080
self.assertAuthentication(provider, for: url, expected: (user, password))
81-
81+
8282
// Update
8383
let newPassword = UUID().uuidString
8484
XCTAssertNoThrow(try tsc_await { callback in provider.addOrUpdate(for: url, user: user, password: newPassword, callback: callback) })
85-
85+
8686
// Existing password is updated
8787
self.assertAuthentication(provider, for: url, expected: (user, newPassword))
88-
88+
8989
self.assertAuthentication(provider, for: otherURL, expected: (user, otherPassword))
9090
#endif
9191
}
92-
92+
9393
func testComposite() throws {
9494
let url = URL(string: "http://\(UUID().uuidString)")!
9595
let user = UUID().uuidString
9696
let passwordOne = UUID().uuidString
9797
let passwordTwo = UUID().uuidString
98-
98+
9999
let providerOne = TestProvider(map: [url: (user: user, password: passwordOne)])
100100
let providerTwo = TestProvider(map: [url: (user: user, password: passwordTwo)])
101-
101+
102102
do {
103103
// providerOne's password is returned first
104104
let provider = CompositeAuthorizationProvider(providerOne, providerTwo)
105105
self.assertAuthentication(provider, for: url, expected: (user, passwordOne))
106106
}
107-
107+
108108
do {
109109
// providerTwo's password is returned first
110110
let provider = CompositeAuthorizationProvider(providerTwo, providerOne)
111111
self.assertAuthentication(provider, for: url, expected: (user, passwordTwo))
112112
}
113-
113+
114114
do {
115115
// Neither has password
116116
let unknownURL = URL(string: "http://\(UUID().uuidString)")!
117117
let provider = CompositeAuthorizationProvider(providerOne, providerTwo)
118118
XCTAssertNil(provider.authentication(for: unknownURL))
119119
}
120120
}
121-
121+
122122
private func assertAuthentication(_ provider: AuthorizationProvider, for url: Foundation.URL, expected: (user: String, password: String)) {
123123
let authentication = provider.authentication(for: url)
124124
XCTAssertEqual(authentication?.user, expected.user)

0 commit comments

Comments
 (0)