pod-builder 1.9.3 → 2.0.0.beta.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +9 -0
- data/README.md +50 -13
- data/exe/pod_builder +39 -28
- data/lib/pod_builder/analyze.rb +32 -7
- data/lib/pod_builder/analyzer.rb +16 -0
- data/lib/pod_builder/command/build.rb +44 -161
- data/lib/pod_builder/command/build_all.rb +2 -2
- data/lib/pod_builder/command/clean.rb +34 -55
- data/lib/pod_builder/command/clear_lldbinit.rb +7 -3
- data/lib/pod_builder/command/deintegrate.rb +29 -7
- data/lib/pod_builder/command/generate_lfs.rb +3 -3
- data/lib/pod_builder/command/generate_podspec.rb +3 -2
- data/lib/pod_builder/command/info.rb +1 -1
- data/lib/pod_builder/command/init.rb +37 -14
- data/lib/pod_builder/command/install_sources.rb +21 -14
- data/lib/pod_builder/command/none.rb +2 -2
- data/lib/pod_builder/command/restore_all.rb +4 -4
- data/lib/pod_builder/command/switch.rb +137 -95
- data/lib/pod_builder/command/sync_podfile.rb +5 -3
- data/lib/pod_builder/command/update.rb +5 -6
- data/lib/pod_builder/command/update_lldbinit.rb +11 -9
- data/lib/pod_builder/configuration.rb +88 -13
- data/lib/pod_builder/core.rb +93 -12
- data/lib/pod_builder/info.rb +32 -98
- data/lib/pod_builder/install.rb +255 -195
- data/lib/pod_builder/licenses.rb +4 -4
- data/lib/pod_builder/podfile.rb +283 -73
- data/lib/pod_builder/podfile/post_actions.rb +9 -15
- data/lib/pod_builder/podfile_cp.rb +93 -0
- data/lib/pod_builder/podfile_item.rb +181 -82
- data/lib/pod_builder/podspec.rb +144 -135
- data/lib/pod_builder/rome/post_install.rb +240 -0
- data/lib/pod_builder/rome/pre_install.rb +6 -0
- data/lib/pod_builder/templates/build_podfile.template +3 -3
- data/lib/pod_builder/version.rb +1 -1
- data/pod-builder.gemspec +6 -6
- metadata +27 -80
- data/Example/Frameworks/PodBuilder.json +0 -32
- data/Example/PodBuilderExample.xcodeproj/project.pbxproj +0 -416
- data/Example/PodBuilderExample.xcodeproj/project.xcworkspace/contents.xcworkspacedata +0 -7
- data/Example/PodBuilderExample.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +0 -8
- data/Example/PodBuilderExample.xcodeproj/project.xcworkspace/xcuserdata/tomas.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
- data/Example/PodBuilderExample.xcodeproj/xcuserdata/tomas.xcuserdatad/xcschemes/xcschememanagement.plist +0 -14
- data/Example/PodBuilderExample.xcworkspace/contents.xcworkspacedata +0 -10
- data/Example/PodBuilderExample/AppDelegate.swift +0 -47
- data/Example/PodBuilderExample/Assets.xcassets/AppIcon.appiconset/Contents.json +0 -98
- data/Example/PodBuilderExample/Assets.xcassets/Contents.json +0 -6
- data/Example/PodBuilderExample/Base.lproj/LaunchScreen.storyboard +0 -25
- data/Example/PodBuilderExample/Base.lproj/Main.storyboard +0 -24
- data/Example/PodBuilderExample/Info.plist +0 -45
- data/Example/PodBuilderExample/ViewController.swift +0 -25
- data/Example/Podfile +0 -8
- data/Example/Podfile.lock +0 -16
- data/Example/Pods/Alamofire/LICENSE +0 -19
- data/Example/Pods/Alamofire/README.md +0 -242
- data/Example/Pods/Alamofire/Source/AFError.swift +0 -460
- data/Example/Pods/Alamofire/Source/Alamofire.swift +0 -465
- data/Example/Pods/Alamofire/Source/DispatchQueue+Alamofire.swift +0 -37
- data/Example/Pods/Alamofire/Source/MultipartFormData.swift +0 -580
- data/Example/Pods/Alamofire/Source/NetworkReachabilityManager.swift +0 -233
- data/Example/Pods/Alamofire/Source/Notifications.swift +0 -55
- data/Example/Pods/Alamofire/Source/ParameterEncoding.swift +0 -483
- data/Example/Pods/Alamofire/Source/Request.swift +0 -654
- data/Example/Pods/Alamofire/Source/Response.swift +0 -567
- data/Example/Pods/Alamofire/Source/ResponseSerialization.swift +0 -715
- data/Example/Pods/Alamofire/Source/Result.swift +0 -300
- data/Example/Pods/Alamofire/Source/ServerTrustPolicy.swift +0 -307
- data/Example/Pods/Alamofire/Source/SessionDelegate.swift +0 -725
- data/Example/Pods/Alamofire/Source/SessionManager.swift +0 -896
- data/Example/Pods/Alamofire/Source/TaskDelegate.swift +0 -466
- data/Example/Pods/Alamofire/Source/Timeline.swift +0 -136
- data/Example/Pods/Alamofire/Source/Validation.swift +0 -315
- data/Example/Pods/Manifest.lock +0 -16
- data/Example/Pods/Pods.xcodeproj/project.pbxproj +0 -673
- data/Example/Pods/Pods.xcodeproj/xcuserdata/tomas.xcuserdatad/xcschemes/Alamofire.xcscheme +0 -60
- data/Example/Pods/Pods.xcodeproj/xcuserdata/tomas.xcuserdatad/xcschemes/Pods-PodBuilderExample.xcscheme +0 -60
- data/Example/Pods/Pods.xcodeproj/xcuserdata/tomas.xcuserdatad/xcschemes/xcschememanagement.plist +0 -21
- data/Example/Pods/Target Support Files/Alamofire/Alamofire-dummy.m +0 -5
- data/Example/Pods/Target Support Files/Alamofire/Alamofire-prefix.pch +0 -12
- data/Example/Pods/Target Support Files/Alamofire/Alamofire-umbrella.h +0 -16
- data/Example/Pods/Target Support Files/Alamofire/Alamofire.modulemap +0 -6
- data/Example/Pods/Target Support Files/Alamofire/Alamofire.xcconfig +0 -9
- data/Example/Pods/Target Support Files/Alamofire/Info.plist +0 -26
- data/Example/Pods/Target Support Files/Pods-PodBuilderExample/Info.plist +0 -26
- data/Example/Pods/Target Support Files/Pods-PodBuilderExample/Pods-PodBuilderExample-acknowledgements.markdown +0 -26
- data/Example/Pods/Target Support Files/Pods-PodBuilderExample/Pods-PodBuilderExample-acknowledgements.plist +0 -58
- data/Example/Pods/Target Support Files/Pods-PodBuilderExample/Pods-PodBuilderExample-dummy.m +0 -5
- data/Example/Pods/Target Support Files/Pods-PodBuilderExample/Pods-PodBuilderExample-frameworks.sh +0 -153
- data/Example/Pods/Target Support Files/Pods-PodBuilderExample/Pods-PodBuilderExample-resources.sh +0 -118
- data/Example/Pods/Target Support Files/Pods-PodBuilderExample/Pods-PodBuilderExample-umbrella.h +0 -16
- data/Example/Pods/Target Support Files/Pods-PodBuilderExample/Pods-PodBuilderExample.debug.xcconfig +0 -11
- data/Example/Pods/Target Support Files/Pods-PodBuilderExample/Pods-PodBuilderExample.modulemap +0 -6
- data/Example/Pods/Target Support Files/Pods-PodBuilderExample/Pods-PodBuilderExample.release.xcconfig +0 -11
- data/lib/pod_builder/cocoapods/specification.rb +0 -27
@@ -1,300 +0,0 @@
|
|
1
|
-
//
|
2
|
-
// Result.swift
|
3
|
-
//
|
4
|
-
// Copyright (c) 2014-2018 Alamofire Software Foundation (http://alamofire.org/)
|
5
|
-
//
|
6
|
-
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
-
// of this software and associated documentation files (the "Software"), to deal
|
8
|
-
// in the Software without restriction, including without limitation the rights
|
9
|
-
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
-
// copies of the Software, and to permit persons to whom the Software is
|
11
|
-
// furnished to do so, subject to the following conditions:
|
12
|
-
//
|
13
|
-
// The above copyright notice and this permission notice shall be included in
|
14
|
-
// all copies or substantial portions of the Software.
|
15
|
-
//
|
16
|
-
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
-
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
-
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
-
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
-
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
-
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22
|
-
// THE SOFTWARE.
|
23
|
-
//
|
24
|
-
|
25
|
-
import Foundation
|
26
|
-
|
27
|
-
/// Used to represent whether a request was successful or encountered an error.
|
28
|
-
///
|
29
|
-
/// - success: The request and all post processing operations were successful resulting in the serialization of the
|
30
|
-
/// provided associated value.
|
31
|
-
///
|
32
|
-
/// - failure: The request encountered an error resulting in a failure. The associated values are the original data
|
33
|
-
/// provided by the server as well as the error that caused the failure.
|
34
|
-
public enum Result<Value> {
|
35
|
-
case success(Value)
|
36
|
-
case failure(Error)
|
37
|
-
|
38
|
-
/// Returns `true` if the result is a success, `false` otherwise.
|
39
|
-
public var isSuccess: Bool {
|
40
|
-
switch self {
|
41
|
-
case .success:
|
42
|
-
return true
|
43
|
-
case .failure:
|
44
|
-
return false
|
45
|
-
}
|
46
|
-
}
|
47
|
-
|
48
|
-
/// Returns `true` if the result is a failure, `false` otherwise.
|
49
|
-
public var isFailure: Bool {
|
50
|
-
return !isSuccess
|
51
|
-
}
|
52
|
-
|
53
|
-
/// Returns the associated value if the result is a success, `nil` otherwise.
|
54
|
-
public var value: Value? {
|
55
|
-
switch self {
|
56
|
-
case .success(let value):
|
57
|
-
return value
|
58
|
-
case .failure:
|
59
|
-
return nil
|
60
|
-
}
|
61
|
-
}
|
62
|
-
|
63
|
-
/// Returns the associated error value if the result is a failure, `nil` otherwise.
|
64
|
-
public var error: Error? {
|
65
|
-
switch self {
|
66
|
-
case .success:
|
67
|
-
return nil
|
68
|
-
case .failure(let error):
|
69
|
-
return error
|
70
|
-
}
|
71
|
-
}
|
72
|
-
}
|
73
|
-
|
74
|
-
// MARK: - CustomStringConvertible
|
75
|
-
|
76
|
-
extension Result: CustomStringConvertible {
|
77
|
-
/// The textual representation used when written to an output stream, which includes whether the result was a
|
78
|
-
/// success or failure.
|
79
|
-
public var description: String {
|
80
|
-
switch self {
|
81
|
-
case .success:
|
82
|
-
return "SUCCESS"
|
83
|
-
case .failure:
|
84
|
-
return "FAILURE"
|
85
|
-
}
|
86
|
-
}
|
87
|
-
}
|
88
|
-
|
89
|
-
// MARK: - CustomDebugStringConvertible
|
90
|
-
|
91
|
-
extension Result: CustomDebugStringConvertible {
|
92
|
-
/// The debug textual representation used when written to an output stream, which includes whether the result was a
|
93
|
-
/// success or failure in addition to the value or error.
|
94
|
-
public var debugDescription: String {
|
95
|
-
switch self {
|
96
|
-
case .success(let value):
|
97
|
-
return "SUCCESS: \(value)"
|
98
|
-
case .failure(let error):
|
99
|
-
return "FAILURE: \(error)"
|
100
|
-
}
|
101
|
-
}
|
102
|
-
}
|
103
|
-
|
104
|
-
// MARK: - Functional APIs
|
105
|
-
|
106
|
-
extension Result {
|
107
|
-
/// Creates a `Result` instance from the result of a closure.
|
108
|
-
///
|
109
|
-
/// A failure result is created when the closure throws, and a success result is created when the closure
|
110
|
-
/// succeeds without throwing an error.
|
111
|
-
///
|
112
|
-
/// func someString() throws -> String { ... }
|
113
|
-
///
|
114
|
-
/// let result = Result(value: {
|
115
|
-
/// return try someString()
|
116
|
-
/// })
|
117
|
-
///
|
118
|
-
/// // The type of result is Result<String>
|
119
|
-
///
|
120
|
-
/// The trailing closure syntax is also supported:
|
121
|
-
///
|
122
|
-
/// let result = Result { try someString() }
|
123
|
-
///
|
124
|
-
/// - parameter value: The closure to execute and create the result for.
|
125
|
-
public init(value: () throws -> Value) {
|
126
|
-
do {
|
127
|
-
self = try .success(value())
|
128
|
-
} catch {
|
129
|
-
self = .failure(error)
|
130
|
-
}
|
131
|
-
}
|
132
|
-
|
133
|
-
/// Returns the success value, or throws the failure error.
|
134
|
-
///
|
135
|
-
/// let possibleString: Result<String> = .success("success")
|
136
|
-
/// try print(possibleString.unwrap())
|
137
|
-
/// // Prints "success"
|
138
|
-
///
|
139
|
-
/// let noString: Result<String> = .failure(error)
|
140
|
-
/// try print(noString.unwrap())
|
141
|
-
/// // Throws error
|
142
|
-
public func unwrap() throws -> Value {
|
143
|
-
switch self {
|
144
|
-
case .success(let value):
|
145
|
-
return value
|
146
|
-
case .failure(let error):
|
147
|
-
throw error
|
148
|
-
}
|
149
|
-
}
|
150
|
-
|
151
|
-
/// Evaluates the specified closure when the `Result` is a success, passing the unwrapped value as a parameter.
|
152
|
-
///
|
153
|
-
/// Use the `map` method with a closure that does not throw. For example:
|
154
|
-
///
|
155
|
-
/// let possibleData: Result<Data> = .success(Data())
|
156
|
-
/// let possibleInt = possibleData.map { $0.count }
|
157
|
-
/// try print(possibleInt.unwrap())
|
158
|
-
/// // Prints "0"
|
159
|
-
///
|
160
|
-
/// let noData: Result<Data> = .failure(error)
|
161
|
-
/// let noInt = noData.map { $0.count }
|
162
|
-
/// try print(noInt.unwrap())
|
163
|
-
/// // Throws error
|
164
|
-
///
|
165
|
-
/// - parameter transform: A closure that takes the success value of the `Result` instance.
|
166
|
-
///
|
167
|
-
/// - returns: A `Result` containing the result of the given closure. If this instance is a failure, returns the
|
168
|
-
/// same failure.
|
169
|
-
public func map<T>(_ transform: (Value) -> T) -> Result<T> {
|
170
|
-
switch self {
|
171
|
-
case .success(let value):
|
172
|
-
return .success(transform(value))
|
173
|
-
case .failure(let error):
|
174
|
-
return .failure(error)
|
175
|
-
}
|
176
|
-
}
|
177
|
-
|
178
|
-
/// Evaluates the specified closure when the `Result` is a success, passing the unwrapped value as a parameter.
|
179
|
-
///
|
180
|
-
/// Use the `flatMap` method with a closure that may throw an error. For example:
|
181
|
-
///
|
182
|
-
/// let possibleData: Result<Data> = .success(Data(...))
|
183
|
-
/// let possibleObject = possibleData.flatMap {
|
184
|
-
/// try JSONSerialization.jsonObject(with: $0)
|
185
|
-
/// }
|
186
|
-
///
|
187
|
-
/// - parameter transform: A closure that takes the success value of the instance.
|
188
|
-
///
|
189
|
-
/// - returns: A `Result` containing the result of the given closure. If this instance is a failure, returns the
|
190
|
-
/// same failure.
|
191
|
-
public func flatMap<T>(_ transform: (Value) throws -> T) -> Result<T> {
|
192
|
-
switch self {
|
193
|
-
case .success(let value):
|
194
|
-
do {
|
195
|
-
return try .success(transform(value))
|
196
|
-
} catch {
|
197
|
-
return .failure(error)
|
198
|
-
}
|
199
|
-
case .failure(let error):
|
200
|
-
return .failure(error)
|
201
|
-
}
|
202
|
-
}
|
203
|
-
|
204
|
-
/// Evaluates the specified closure when the `Result` is a failure, passing the unwrapped error as a parameter.
|
205
|
-
///
|
206
|
-
/// Use the `mapError` function with a closure that does not throw. For example:
|
207
|
-
///
|
208
|
-
/// let possibleData: Result<Data> = .failure(someError)
|
209
|
-
/// let withMyError: Result<Data> = possibleData.mapError { MyError.error($0) }
|
210
|
-
///
|
211
|
-
/// - Parameter transform: A closure that takes the error of the instance.
|
212
|
-
/// - Returns: A `Result` instance containing the result of the transform. If this instance is a success, returns
|
213
|
-
/// the same instance.
|
214
|
-
public func mapError<T: Error>(_ transform: (Error) -> T) -> Result {
|
215
|
-
switch self {
|
216
|
-
case .failure(let error):
|
217
|
-
return .failure(transform(error))
|
218
|
-
case .success:
|
219
|
-
return self
|
220
|
-
}
|
221
|
-
}
|
222
|
-
|
223
|
-
/// Evaluates the specified closure when the `Result` is a failure, passing the unwrapped error as a parameter.
|
224
|
-
///
|
225
|
-
/// Use the `flatMapError` function with a closure that may throw an error. For example:
|
226
|
-
///
|
227
|
-
/// let possibleData: Result<Data> = .success(Data(...))
|
228
|
-
/// let possibleObject = possibleData.flatMapError {
|
229
|
-
/// try someFailableFunction(taking: $0)
|
230
|
-
/// }
|
231
|
-
///
|
232
|
-
/// - Parameter transform: A throwing closure that takes the error of the instance.
|
233
|
-
///
|
234
|
-
/// - Returns: A `Result` instance containing the result of the transform. If this instance is a success, returns
|
235
|
-
/// the same instance.
|
236
|
-
public func flatMapError<T: Error>(_ transform: (Error) throws -> T) -> Result {
|
237
|
-
switch self {
|
238
|
-
case .failure(let error):
|
239
|
-
do {
|
240
|
-
return try .failure(transform(error))
|
241
|
-
} catch {
|
242
|
-
return .failure(error)
|
243
|
-
}
|
244
|
-
case .success:
|
245
|
-
return self
|
246
|
-
}
|
247
|
-
}
|
248
|
-
|
249
|
-
/// Evaluates the specified closure when the `Result` is a success, passing the unwrapped value as a parameter.
|
250
|
-
///
|
251
|
-
/// Use the `withValue` function to evaluate the passed closure without modifying the `Result` instance.
|
252
|
-
///
|
253
|
-
/// - Parameter closure: A closure that takes the success value of this instance.
|
254
|
-
/// - Returns: This `Result` instance, unmodified.
|
255
|
-
@discardableResult
|
256
|
-
public func withValue(_ closure: (Value) -> Void) -> Result {
|
257
|
-
if case let .success(value) = self { closure(value) }
|
258
|
-
|
259
|
-
return self
|
260
|
-
}
|
261
|
-
|
262
|
-
/// Evaluates the specified closure when the `Result` is a failure, passing the unwrapped error as a parameter.
|
263
|
-
///
|
264
|
-
/// Use the `withError` function to evaluate the passed closure without modifying the `Result` instance.
|
265
|
-
///
|
266
|
-
/// - Parameter closure: A closure that takes the success value of this instance.
|
267
|
-
/// - Returns: This `Result` instance, unmodified.
|
268
|
-
@discardableResult
|
269
|
-
public func withError(_ closure: (Error) -> Void) -> Result {
|
270
|
-
if case let .failure(error) = self { closure(error) }
|
271
|
-
|
272
|
-
return self
|
273
|
-
}
|
274
|
-
|
275
|
-
/// Evaluates the specified closure when the `Result` is a success.
|
276
|
-
///
|
277
|
-
/// Use the `ifSuccess` function to evaluate the passed closure without modifying the `Result` instance.
|
278
|
-
///
|
279
|
-
/// - Parameter closure: A `Void` closure.
|
280
|
-
/// - Returns: This `Result` instance, unmodified.
|
281
|
-
@discardableResult
|
282
|
-
public func ifSuccess(_ closure: () -> Void) -> Result {
|
283
|
-
if isSuccess { closure() }
|
284
|
-
|
285
|
-
return self
|
286
|
-
}
|
287
|
-
|
288
|
-
/// Evaluates the specified closure when the `Result` is a failure.
|
289
|
-
///
|
290
|
-
/// Use the `ifFailure` function to evaluate the passed closure without modifying the `Result` instance.
|
291
|
-
///
|
292
|
-
/// - Parameter closure: A `Void` closure.
|
293
|
-
/// - Returns: This `Result` instance, unmodified.
|
294
|
-
@discardableResult
|
295
|
-
public func ifFailure(_ closure: () -> Void) -> Result {
|
296
|
-
if isFailure { closure() }
|
297
|
-
|
298
|
-
return self
|
299
|
-
}
|
300
|
-
}
|
@@ -1,307 +0,0 @@
|
|
1
|
-
//
|
2
|
-
// ServerTrustPolicy.swift
|
3
|
-
//
|
4
|
-
// Copyright (c) 2014-2018 Alamofire Software Foundation (http://alamofire.org/)
|
5
|
-
//
|
6
|
-
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
-
// of this software and associated documentation files (the "Software"), to deal
|
8
|
-
// in the Software without restriction, including without limitation the rights
|
9
|
-
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
-
// copies of the Software, and to permit persons to whom the Software is
|
11
|
-
// furnished to do so, subject to the following conditions:
|
12
|
-
//
|
13
|
-
// The above copyright notice and this permission notice shall be included in
|
14
|
-
// all copies or substantial portions of the Software.
|
15
|
-
//
|
16
|
-
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
-
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
-
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
-
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
-
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
-
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22
|
-
// THE SOFTWARE.
|
23
|
-
//
|
24
|
-
|
25
|
-
import Foundation
|
26
|
-
|
27
|
-
/// Responsible for managing the mapping of `ServerTrustPolicy` objects to a given host.
|
28
|
-
open class ServerTrustPolicyManager {
|
29
|
-
/// The dictionary of policies mapped to a particular host.
|
30
|
-
public let policies: [String: ServerTrustPolicy]
|
31
|
-
|
32
|
-
/// Initializes the `ServerTrustPolicyManager` instance with the given policies.
|
33
|
-
///
|
34
|
-
/// Since different servers and web services can have different leaf certificates, intermediate and even root
|
35
|
-
/// certficates, it is important to have the flexibility to specify evaluation policies on a per host basis. This
|
36
|
-
/// allows for scenarios such as using default evaluation for host1, certificate pinning for host2, public key
|
37
|
-
/// pinning for host3 and disabling evaluation for host4.
|
38
|
-
///
|
39
|
-
/// - parameter policies: A dictionary of all policies mapped to a particular host.
|
40
|
-
///
|
41
|
-
/// - returns: The new `ServerTrustPolicyManager` instance.
|
42
|
-
public init(policies: [String: ServerTrustPolicy]) {
|
43
|
-
self.policies = policies
|
44
|
-
}
|
45
|
-
|
46
|
-
/// Returns the `ServerTrustPolicy` for the given host if applicable.
|
47
|
-
///
|
48
|
-
/// By default, this method will return the policy that perfectly matches the given host. Subclasses could override
|
49
|
-
/// this method and implement more complex mapping implementations such as wildcards.
|
50
|
-
///
|
51
|
-
/// - parameter host: The host to use when searching for a matching policy.
|
52
|
-
///
|
53
|
-
/// - returns: The server trust policy for the given host if found.
|
54
|
-
open func serverTrustPolicy(forHost host: String) -> ServerTrustPolicy? {
|
55
|
-
return policies[host]
|
56
|
-
}
|
57
|
-
}
|
58
|
-
|
59
|
-
// MARK: -
|
60
|
-
|
61
|
-
extension URLSession {
|
62
|
-
private struct AssociatedKeys {
|
63
|
-
static var managerKey = "URLSession.ServerTrustPolicyManager"
|
64
|
-
}
|
65
|
-
|
66
|
-
var serverTrustPolicyManager: ServerTrustPolicyManager? {
|
67
|
-
get {
|
68
|
-
return objc_getAssociatedObject(self, &AssociatedKeys.managerKey) as? ServerTrustPolicyManager
|
69
|
-
}
|
70
|
-
set (manager) {
|
71
|
-
objc_setAssociatedObject(self, &AssociatedKeys.managerKey, manager, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
|
72
|
-
}
|
73
|
-
}
|
74
|
-
}
|
75
|
-
|
76
|
-
// MARK: - ServerTrustPolicy
|
77
|
-
|
78
|
-
/// The `ServerTrustPolicy` evaluates the server trust generally provided by an `NSURLAuthenticationChallenge` when
|
79
|
-
/// connecting to a server over a secure HTTPS connection. The policy configuration then evaluates the server trust
|
80
|
-
/// with a given set of criteria to determine whether the server trust is valid and the connection should be made.
|
81
|
-
///
|
82
|
-
/// Using pinned certificates or public keys for evaluation helps prevent man-in-the-middle (MITM) attacks and other
|
83
|
-
/// vulnerabilities. Applications dealing with sensitive customer data or financial information are strongly encouraged
|
84
|
-
/// to route all communication over an HTTPS connection with pinning enabled.
|
85
|
-
///
|
86
|
-
/// - performDefaultEvaluation: Uses the default server trust evaluation while allowing you to control whether to
|
87
|
-
/// validate the host provided by the challenge. Applications are encouraged to always
|
88
|
-
/// validate the host in production environments to guarantee the validity of the server's
|
89
|
-
/// certificate chain.
|
90
|
-
///
|
91
|
-
/// - performRevokedEvaluation: Uses the default and revoked server trust evaluations allowing you to control whether to
|
92
|
-
/// validate the host provided by the challenge as well as specify the revocation flags for
|
93
|
-
/// testing for revoked certificates. Apple platforms did not start testing for revoked
|
94
|
-
/// certificates automatically until iOS 10.1, macOS 10.12 and tvOS 10.1 which is
|
95
|
-
/// demonstrated in our TLS tests. Applications are encouraged to always validate the host
|
96
|
-
/// in production environments to guarantee the validity of the server's certificate chain.
|
97
|
-
///
|
98
|
-
/// - pinCertificates: Uses the pinned certificates to validate the server trust. The server trust is
|
99
|
-
/// considered valid if one of the pinned certificates match one of the server certificates.
|
100
|
-
/// By validating both the certificate chain and host, certificate pinning provides a very
|
101
|
-
/// secure form of server trust validation mitigating most, if not all, MITM attacks.
|
102
|
-
/// Applications are encouraged to always validate the host and require a valid certificate
|
103
|
-
/// chain in production environments.
|
104
|
-
///
|
105
|
-
/// - pinPublicKeys: Uses the pinned public keys to validate the server trust. The server trust is considered
|
106
|
-
/// valid if one of the pinned public keys match one of the server certificate public keys.
|
107
|
-
/// By validating both the certificate chain and host, public key pinning provides a very
|
108
|
-
/// secure form of server trust validation mitigating most, if not all, MITM attacks.
|
109
|
-
/// Applications are encouraged to always validate the host and require a valid certificate
|
110
|
-
/// chain in production environments.
|
111
|
-
///
|
112
|
-
/// - disableEvaluation: Disables all evaluation which in turn will always consider any server trust as valid.
|
113
|
-
///
|
114
|
-
/// - customEvaluation: Uses the associated closure to evaluate the validity of the server trust.
|
115
|
-
public enum ServerTrustPolicy {
|
116
|
-
case performDefaultEvaluation(validateHost: Bool)
|
117
|
-
case performRevokedEvaluation(validateHost: Bool, revocationFlags: CFOptionFlags)
|
118
|
-
case pinCertificates(certificates: [SecCertificate], validateCertificateChain: Bool, validateHost: Bool)
|
119
|
-
case pinPublicKeys(publicKeys: [SecKey], validateCertificateChain: Bool, validateHost: Bool)
|
120
|
-
case disableEvaluation
|
121
|
-
case customEvaluation((_ serverTrust: SecTrust, _ host: String) -> Bool)
|
122
|
-
|
123
|
-
// MARK: - Bundle Location
|
124
|
-
|
125
|
-
/// Returns all certificates within the given bundle with a `.cer` file extension.
|
126
|
-
///
|
127
|
-
/// - parameter bundle: The bundle to search for all `.cer` files.
|
128
|
-
///
|
129
|
-
/// - returns: All certificates within the given bundle.
|
130
|
-
public static func certificates(in bundle: Bundle = Bundle.main) -> [SecCertificate] {
|
131
|
-
var certificates: [SecCertificate] = []
|
132
|
-
|
133
|
-
let paths = Set([".cer", ".CER", ".crt", ".CRT", ".der", ".DER"].map { fileExtension in
|
134
|
-
bundle.paths(forResourcesOfType: fileExtension, inDirectory: nil)
|
135
|
-
}.joined())
|
136
|
-
|
137
|
-
for path in paths {
|
138
|
-
if
|
139
|
-
let certificateData = try? Data(contentsOf: URL(fileURLWithPath: path)) as CFData,
|
140
|
-
let certificate = SecCertificateCreateWithData(nil, certificateData)
|
141
|
-
{
|
142
|
-
certificates.append(certificate)
|
143
|
-
}
|
144
|
-
}
|
145
|
-
|
146
|
-
return certificates
|
147
|
-
}
|
148
|
-
|
149
|
-
/// Returns all public keys within the given bundle with a `.cer` file extension.
|
150
|
-
///
|
151
|
-
/// - parameter bundle: The bundle to search for all `*.cer` files.
|
152
|
-
///
|
153
|
-
/// - returns: All public keys within the given bundle.
|
154
|
-
public static func publicKeys(in bundle: Bundle = Bundle.main) -> [SecKey] {
|
155
|
-
var publicKeys: [SecKey] = []
|
156
|
-
|
157
|
-
for certificate in certificates(in: bundle) {
|
158
|
-
if let publicKey = publicKey(for: certificate) {
|
159
|
-
publicKeys.append(publicKey)
|
160
|
-
}
|
161
|
-
}
|
162
|
-
|
163
|
-
return publicKeys
|
164
|
-
}
|
165
|
-
|
166
|
-
// MARK: - Evaluation
|
167
|
-
|
168
|
-
/// Evaluates whether the server trust is valid for the given host.
|
169
|
-
///
|
170
|
-
/// - parameter serverTrust: The server trust to evaluate.
|
171
|
-
/// - parameter host: The host of the challenge protection space.
|
172
|
-
///
|
173
|
-
/// - returns: Whether the server trust is valid.
|
174
|
-
public func evaluate(_ serverTrust: SecTrust, forHost host: String) -> Bool {
|
175
|
-
var serverTrustIsValid = false
|
176
|
-
|
177
|
-
switch self {
|
178
|
-
case let .performDefaultEvaluation(validateHost):
|
179
|
-
let policy = SecPolicyCreateSSL(true, validateHost ? host as CFString : nil)
|
180
|
-
SecTrustSetPolicies(serverTrust, policy)
|
181
|
-
|
182
|
-
serverTrustIsValid = trustIsValid(serverTrust)
|
183
|
-
case let .performRevokedEvaluation(validateHost, revocationFlags):
|
184
|
-
let defaultPolicy = SecPolicyCreateSSL(true, validateHost ? host as CFString : nil)
|
185
|
-
let revokedPolicy = SecPolicyCreateRevocation(revocationFlags)
|
186
|
-
SecTrustSetPolicies(serverTrust, [defaultPolicy, revokedPolicy] as CFTypeRef)
|
187
|
-
|
188
|
-
serverTrustIsValid = trustIsValid(serverTrust)
|
189
|
-
case let .pinCertificates(pinnedCertificates, validateCertificateChain, validateHost):
|
190
|
-
if validateCertificateChain {
|
191
|
-
let policy = SecPolicyCreateSSL(true, validateHost ? host as CFString : nil)
|
192
|
-
SecTrustSetPolicies(serverTrust, policy)
|
193
|
-
|
194
|
-
SecTrustSetAnchorCertificates(serverTrust, pinnedCertificates as CFArray)
|
195
|
-
SecTrustSetAnchorCertificatesOnly(serverTrust, true)
|
196
|
-
|
197
|
-
serverTrustIsValid = trustIsValid(serverTrust)
|
198
|
-
} else {
|
199
|
-
let serverCertificatesDataArray = certificateData(for: serverTrust)
|
200
|
-
let pinnedCertificatesDataArray = certificateData(for: pinnedCertificates)
|
201
|
-
|
202
|
-
outerLoop: for serverCertificateData in serverCertificatesDataArray {
|
203
|
-
for pinnedCertificateData in pinnedCertificatesDataArray {
|
204
|
-
if serverCertificateData == pinnedCertificateData {
|
205
|
-
serverTrustIsValid = true
|
206
|
-
break outerLoop
|
207
|
-
}
|
208
|
-
}
|
209
|
-
}
|
210
|
-
}
|
211
|
-
case let .pinPublicKeys(pinnedPublicKeys, validateCertificateChain, validateHost):
|
212
|
-
var certificateChainEvaluationPassed = true
|
213
|
-
|
214
|
-
if validateCertificateChain {
|
215
|
-
let policy = SecPolicyCreateSSL(true, validateHost ? host as CFString : nil)
|
216
|
-
SecTrustSetPolicies(serverTrust, policy)
|
217
|
-
|
218
|
-
certificateChainEvaluationPassed = trustIsValid(serverTrust)
|
219
|
-
}
|
220
|
-
|
221
|
-
if certificateChainEvaluationPassed {
|
222
|
-
outerLoop: for serverPublicKey in ServerTrustPolicy.publicKeys(for: serverTrust) as [AnyObject] {
|
223
|
-
for pinnedPublicKey in pinnedPublicKeys as [AnyObject] {
|
224
|
-
if serverPublicKey.isEqual(pinnedPublicKey) {
|
225
|
-
serverTrustIsValid = true
|
226
|
-
break outerLoop
|
227
|
-
}
|
228
|
-
}
|
229
|
-
}
|
230
|
-
}
|
231
|
-
case .disableEvaluation:
|
232
|
-
serverTrustIsValid = true
|
233
|
-
case let .customEvaluation(closure):
|
234
|
-
serverTrustIsValid = closure(serverTrust, host)
|
235
|
-
}
|
236
|
-
|
237
|
-
return serverTrustIsValid
|
238
|
-
}
|
239
|
-
|
240
|
-
// MARK: - Private - Trust Validation
|
241
|
-
|
242
|
-
private func trustIsValid(_ trust: SecTrust) -> Bool {
|
243
|
-
var isValid = false
|
244
|
-
|
245
|
-
var result = SecTrustResultType.invalid
|
246
|
-
let status = SecTrustEvaluate(trust, &result)
|
247
|
-
|
248
|
-
if status == errSecSuccess {
|
249
|
-
let unspecified = SecTrustResultType.unspecified
|
250
|
-
let proceed = SecTrustResultType.proceed
|
251
|
-
|
252
|
-
|
253
|
-
isValid = result == unspecified || result == proceed
|
254
|
-
}
|
255
|
-
|
256
|
-
return isValid
|
257
|
-
}
|
258
|
-
|
259
|
-
// MARK: - Private - Certificate Data
|
260
|
-
|
261
|
-
private func certificateData(for trust: SecTrust) -> [Data] {
|
262
|
-
var certificates: [SecCertificate] = []
|
263
|
-
|
264
|
-
for index in 0..<SecTrustGetCertificateCount(trust) {
|
265
|
-
if let certificate = SecTrustGetCertificateAtIndex(trust, index) {
|
266
|
-
certificates.append(certificate)
|
267
|
-
}
|
268
|
-
}
|
269
|
-
|
270
|
-
return certificateData(for: certificates)
|
271
|
-
}
|
272
|
-
|
273
|
-
private func certificateData(for certificates: [SecCertificate]) -> [Data] {
|
274
|
-
return certificates.map { SecCertificateCopyData($0) as Data }
|
275
|
-
}
|
276
|
-
|
277
|
-
// MARK: - Private - Public Key Extraction
|
278
|
-
|
279
|
-
private static func publicKeys(for trust: SecTrust) -> [SecKey] {
|
280
|
-
var publicKeys: [SecKey] = []
|
281
|
-
|
282
|
-
for index in 0..<SecTrustGetCertificateCount(trust) {
|
283
|
-
if
|
284
|
-
let certificate = SecTrustGetCertificateAtIndex(trust, index),
|
285
|
-
let publicKey = publicKey(for: certificate)
|
286
|
-
{
|
287
|
-
publicKeys.append(publicKey)
|
288
|
-
}
|
289
|
-
}
|
290
|
-
|
291
|
-
return publicKeys
|
292
|
-
}
|
293
|
-
|
294
|
-
private static func publicKey(for certificate: SecCertificate) -> SecKey? {
|
295
|
-
var publicKey: SecKey?
|
296
|
-
|
297
|
-
let policy = SecPolicyCreateBasicX509()
|
298
|
-
var trust: SecTrust?
|
299
|
-
let trustCreationStatus = SecTrustCreateWithCertificates(certificate, policy, &trust)
|
300
|
-
|
301
|
-
if let trust = trust, trustCreationStatus == errSecSuccess {
|
302
|
-
publicKey = SecTrustCopyPublicKey(trust)
|
303
|
-
}
|
304
|
-
|
305
|
-
return publicKey
|
306
|
-
}
|
307
|
-
}
|