@@ -40,8 +40,12 @@ extension URLCredential {
40
40
@discussion This class is an immutable object representing an authentication credential. The actual type of the credential is determined by the constructor called in the categories declared below.
41
41
*/
42
42
open class URLCredential : NSObject , NSSecureCoding , NSCopying {
43
- private var _user : String
44
- private var _password : String
43
+ private var _user : String ?
44
+ private var _password : String ?
45
+ // _privateClientKey contains the private client key in DER format
46
+ private var _privateClientKey : Data ?
47
+ // _privateClientCertificate contains the private client certificate in DER format
48
+ private var _privateClientCertificate : Data ?
45
49
private var _persistence : Persistence
46
50
47
51
/*!
@@ -55,6 +59,25 @@ open class URLCredential : NSObject, NSSecureCoding, NSCopying {
55
59
public init ( user: String , password: String , persistence: Persistence ) {
56
60
_user = user
57
61
_password = password
62
+ _privateClientKey = nil
63
+ _privateClientCertificate = nil
64
+ _persistence = persistence
65
+ super. init ( )
66
+ }
67
+
68
+ /*!
69
+ @method initWithUser:password:persistence:
70
+ @abstract Initialize a URLCredential with a user and password
71
+ @param user the username
72
+ @param password the password
73
+ @param persistence enum that says to store per session, permanently or not at all
74
+ @result The initialized URLCredential
75
+ */
76
+ public init ( clientKey: Data , clientCertificate: Data , persistence: Persistence ) {
77
+ _user = nil
78
+ _password = nil
79
+ _privateClientKey = clientKey
80
+ _privateClientCertificate = clientCertificate
58
81
_persistence = persistence
59
82
super. init ( )
60
83
}
@@ -76,24 +99,34 @@ open class URLCredential : NSObject, NSSecureCoding, NSCopying {
76
99
func bridgeString( _ value: NSString ) -> String ? {
77
100
return String . _unconditionallyBridgeFromObjectiveC ( value)
78
101
}
79
-
80
- let encodedUser = aDecoder. decodeObject ( forKey: " NS._user " ) as! NSString
81
- self . _user = bridgeString ( encodedUser) !
82
-
83
- let encodedPassword = aDecoder. decodeObject ( forKey: " NS._password " ) as! NSString
84
- self . _password = bridgeString ( encodedPassword) !
85
-
86
- let encodedPersistence = aDecoder. decodeObject ( forKey: " NS._persistence " ) as! NSNumber
87
- self . _persistence = Persistence ( rawValue: encodedPersistence. uintValue) !
102
+
103
+ if let encodedUser = aDecoder. decodeObject ( forKey: " NS._user " ) as? NSString {
104
+ self . _user = bridgeString ( encodedUser) !
105
+ }
106
+
107
+ if let encodedPassword = aDecoder. decodeObject ( forKey: " NS._password " ) as? NSString {
108
+ self . _password = bridgeString ( encodedPassword) !
109
+ }
110
+
111
+ if let encodedPersistence = aDecoder. decodeObject ( forKey: " NS._persistence " ) as? NSNumber {
112
+ self . _persistence = Persistence ( rawValue: encodedPersistence. uintValue) !
113
+ } else {
114
+ self . _persistence = Persistence . none
115
+ }
88
116
}
89
117
90
118
open func encode( with aCoder: NSCoder ) {
91
119
guard aCoder. allowsKeyedCoding else {
92
120
preconditionFailure ( " Unkeyed coding is unsupported. " )
93
121
}
94
-
95
- aCoder. encode ( self . _user. _bridgeToObjectiveC ( ) , forKey: " NS._user " )
96
- aCoder. encode ( self . _password. _bridgeToObjectiveC ( ) , forKey: " NS._password " )
122
+
123
+ if let user = self . _user {
124
+ aCoder. encode ( user. _bridgeToObjectiveC ( ) , forKey: " NS._user " )
125
+ }
126
+ if let password = self . _password {
127
+ aCoder. encode ( password. _bridgeToObjectiveC ( ) , forKey: " NS._password " )
128
+ }
129
+
97
130
aCoder. encode ( self . _persistence. rawValue. _bridgeToObjectiveC ( ) , forKey: " NS._persistence " )
98
131
}
99
132
@@ -141,6 +174,20 @@ open class URLCredential : NSObject, NSSecureCoding, NSCopying {
141
174
*/
142
175
open var password : String ? { return _password }
143
176
177
+ /*!
178
+ @method privateClientKey
179
+ @abstract Get the private client key
180
+ @result The private key binary blob
181
+ */
182
+ open var privateClientKey : Data ? { return _privateClientKey }
183
+
184
+ /*!
185
+ @method privateClientCertificate
186
+ @abstract Get the private client key
187
+ @result The private key binary blob
188
+ */
189
+ open var privateClientCertificate : Data ? { return _privateClientCertificate }
190
+
144
191
/*!
145
192
@method hasPassword
146
193
@abstract Find out if this credential has a password, without trying to get it
@@ -152,6 +199,6 @@ open class URLCredential : NSObject, NSSecureCoding, NSCopying {
152
199
*/
153
200
open var hasPassword : Bool {
154
201
// Currently no support for SecTrust/SecIdentity, always return true
155
- return true
202
+ return _password != nil
156
203
}
157
204
}
0 commit comments