Skip to content

Commit a61d54a

Browse files
committed
FoundationNetworking/URLCredential: Enable client key and certificate
1 parent 9dfca90 commit a61d54a

File tree

1 file changed

+62
-15
lines changed

1 file changed

+62
-15
lines changed

Sources/FoundationNetworking/URLCredential.swift

+62-15
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,12 @@ extension URLCredential {
4040
@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.
4141
*/
4242
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?
4549
private var _persistence : Persistence
4650

4751
/*!
@@ -55,6 +59,25 @@ open class URLCredential : NSObject, NSSecureCoding, NSCopying {
5559
public init(user: String, password: String, persistence: Persistence) {
5660
_user = user
5761
_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
5881
_persistence = persistence
5982
super.init()
6083
}
@@ -76,24 +99,34 @@ open class URLCredential : NSObject, NSSecureCoding, NSCopying {
7699
func bridgeString(_ value: NSString) -> String? {
77100
return String._unconditionallyBridgeFromObjectiveC(value)
78101
}
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+
}
88116
}
89117

90118
open func encode(with aCoder: NSCoder) {
91119
guard aCoder.allowsKeyedCoding else {
92120
preconditionFailure("Unkeyed coding is unsupported.")
93121
}
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+
97130
aCoder.encode(self._persistence.rawValue._bridgeToObjectiveC(), forKey: "NS._persistence")
98131
}
99132

@@ -141,6 +174,20 @@ open class URLCredential : NSObject, NSSecureCoding, NSCopying {
141174
*/
142175
open var password: String? { return _password }
143176

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+
144191
/*!
145192
@method hasPassword
146193
@abstract Find out if this credential has a password, without trying to get it
@@ -152,6 +199,6 @@ open class URLCredential : NSObject, NSSecureCoding, NSCopying {
152199
*/
153200
open var hasPassword: Bool {
154201
// Currently no support for SecTrust/SecIdentity, always return true
155-
return true
202+
return _password != nil
156203
}
157204
}

0 commit comments

Comments
 (0)