2 * Copyright (C) 2007 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "AuthenticationCF.h"
29 #include "AuthenticationChallenge.h"
30 #include "Credential.h"
31 #include "ProtectionSpace.h"
32 #include "ResourceHandle.h"
34 #include <CFNetwork/CFURLAuthChallengePriv.h>
35 #include <CFNetwork/CFURLCredentialPriv.h>
36 #include <CFNetwork/CFURLProtectionSpacePriv.h>
40 AuthenticationChallenge::AuthenticationChallenge(const ProtectionSpace
& protectionSpace
,
41 const Credential
& proposedCredential
,
42 unsigned previousFailureCount
,
43 const ResourceResponse
& response
,
44 const ResourceError
& error
)
45 : AuthenticationChallengeBase(protectionSpace
,
53 AuthenticationChallenge::AuthenticationChallenge(CFURLAuthChallengeRef cfChallenge
,
54 ResourceHandle
* sourceHandle
)
55 : AuthenticationChallengeBase(core(CFURLAuthChallengeGetProtectionSpace(cfChallenge
)),
56 core(CFURLAuthChallengeGetProposedCredential(cfChallenge
)),
57 CFURLAuthChallengeGetPreviousFailureCount(cfChallenge
),
58 (CFURLResponseRef
)CFURLAuthChallengeGetFailureResponse(cfChallenge
),
59 CFURLAuthChallengeGetError(cfChallenge
))
60 , m_sourceHandle(sourceHandle
)
61 , m_cfChallenge(cfChallenge
)
65 bool AuthenticationChallenge::platformCompare(const AuthenticationChallenge
& a
, const AuthenticationChallenge
& b
)
67 if (a
.sourceHandle() != b
.sourceHandle())
70 if (a
.cfURLAuthChallengeRef() != b
.cfURLAuthChallengeRef())
76 CFURLAuthChallengeRef
createCF(const AuthenticationChallenge
& coreChallenge
)
78 CFURLProtectionSpaceRef protectionSpace
= createCF(coreChallenge
.protectionSpace());
79 CFURLCredentialRef credential
= createCF(coreChallenge
.proposedCredential());
81 CFURLAuthChallengeRef result
= CFURLAuthChallengeCreate(0, protectionSpace
, credential
,
82 coreChallenge
.previousFailureCount(),
83 coreChallenge
.failureResponse().cfURLResponse(),
84 coreChallenge
.error());
85 CFRelease(protectionSpace
);
86 CFRelease(credential
);
90 CFURLCredentialRef
createCF(const Credential
& coreCredential
)
92 CFURLCredentialPersistence persistence
= kCFURLCredentialPersistenceNone
;
93 switch (coreCredential
.persistence()) {
94 case CredentialPersistenceNone
:
96 case CredentialPersistenceForSession
:
97 persistence
= kCFURLCredentialPersistenceForSession
;
99 case CredentialPersistencePermanent
:
100 persistence
= kCFURLCredentialPersistencePermanent
;
103 ASSERT_NOT_REACHED();
106 CFStringRef user
= coreCredential
.user().createCFString();
107 CFStringRef password
= coreCredential
.password().createCFString();
108 CFURLCredentialRef result
= CFURLCredentialCreate(0, user
, password
, 0, persistence
);
115 CFURLProtectionSpaceRef
createCF(const ProtectionSpace
& coreSpace
)
117 CFURLProtectionSpaceServerType serverType
= kCFURLProtectionSpaceServerHTTP
;
118 switch (coreSpace
.serverType()) {
119 case ProtectionSpaceServerHTTP
:
120 serverType
= kCFURLProtectionSpaceServerHTTP
;
122 case ProtectionSpaceServerHTTPS
:
123 serverType
= kCFURLProtectionSpaceServerHTTPS
;
125 case ProtectionSpaceServerFTP
:
126 serverType
= kCFURLProtectionSpaceServerFTP
;
128 case ProtectionSpaceServerFTPS
:
129 serverType
= kCFURLProtectionSpaceServerFTPS
;
131 case ProtectionSpaceProxyHTTP
:
132 serverType
= kCFURLProtectionSpaceProxyHTTP
;
134 case ProtectionSpaceProxyHTTPS
:
135 serverType
= kCFURLProtectionSpaceProxyHTTPS
;
137 case ProtectionSpaceProxyFTP
:
138 serverType
= kCFURLProtectionSpaceProxyFTP
;
140 case ProtectionSpaceProxySOCKS
:
141 serverType
= kCFURLProtectionSpaceProxySOCKS
;
144 ASSERT_NOT_REACHED();
147 CFURLProtectionSpaceAuthenticationScheme scheme
= kCFURLProtectionSpaceAuthenticationSchemeDefault
;
148 switch (coreSpace
.authenticationScheme()) {
149 case ProtectionSpaceAuthenticationSchemeDefault
:
150 scheme
= kCFURLProtectionSpaceAuthenticationSchemeDefault
;
152 case ProtectionSpaceAuthenticationSchemeHTTPBasic
:
153 scheme
= kCFURLProtectionSpaceAuthenticationSchemeHTTPBasic
;
155 case ProtectionSpaceAuthenticationSchemeHTTPDigest
:
156 scheme
= kCFURLProtectionSpaceAuthenticationSchemeHTTPDigest
;
158 case ProtectionSpaceAuthenticationSchemeHTMLForm
:
159 scheme
= kCFURLProtectionSpaceAuthenticationSchemeHTMLForm
;
161 case ProtectionSpaceAuthenticationSchemeNTLM
:
162 scheme
= kCFURLProtectionSpaceAuthenticationSchemeNTLM
;
164 case ProtectionSpaceAuthenticationSchemeNegotiate
:
165 scheme
= kCFURLProtectionSpaceAuthenticationSchemeNegotiate
;
168 ASSERT_NOT_REACHED();
171 CFStringRef host
= coreSpace
.host().createCFString();
172 CFStringRef realm
= coreSpace
.realm().createCFString();
173 CFURLProtectionSpaceRef result
= CFURLProtectionSpaceCreate(0, host
, coreSpace
.port(), serverType
, realm
, scheme
);
180 Credential
core(CFURLCredentialRef cfCredential
)
185 CredentialPersistence persistence
= CredentialPersistenceNone
;
186 switch (CFURLCredentialGetPersistence(cfCredential
)) {
187 case kCFURLCredentialPersistenceNone
:
189 case kCFURLCredentialPersistenceForSession
:
190 persistence
= CredentialPersistenceForSession
;
192 case kCFURLCredentialPersistencePermanent
:
193 persistence
= CredentialPersistencePermanent
;
196 ASSERT_NOT_REACHED();
199 return Credential(CFURLCredentialGetUsername(cfCredential
), CFURLCredentialCopyPassword(cfCredential
), persistence
);
202 ProtectionSpace
core(CFURLProtectionSpaceRef cfSpace
)
204 ProtectionSpaceServerType serverType
= ProtectionSpaceServerHTTP
;
206 switch (CFURLProtectionSpaceGetServerType(cfSpace
)) {
207 case kCFURLProtectionSpaceServerHTTP
:
209 case kCFURLProtectionSpaceServerHTTPS
:
210 serverType
= ProtectionSpaceServerHTTPS
;
212 case kCFURLProtectionSpaceServerFTP
:
213 serverType
= ProtectionSpaceServerFTP
;
215 case kCFURLProtectionSpaceServerFTPS
:
216 serverType
= ProtectionSpaceServerFTPS
;
218 case kCFURLProtectionSpaceProxyHTTP
:
219 serverType
= ProtectionSpaceProxyHTTP
;
221 case kCFURLProtectionSpaceProxyHTTPS
:
222 serverType
= ProtectionSpaceProxyHTTPS
;
224 case kCFURLProtectionSpaceProxyFTP
:
225 serverType
= ProtectionSpaceProxyFTP
;
227 case kCFURLProtectionSpaceProxySOCKS
:
228 serverType
= ProtectionSpaceProxySOCKS
;
231 ASSERT_NOT_REACHED();
234 ProtectionSpaceAuthenticationScheme scheme
= ProtectionSpaceAuthenticationSchemeDefault
;
236 switch (CFURLProtectionSpaceGetAuthenticationScheme(cfSpace
)) {
237 case kCFURLProtectionSpaceAuthenticationSchemeDefault
:
238 scheme
= ProtectionSpaceAuthenticationSchemeDefault
;
240 case kCFURLProtectionSpaceAuthenticationSchemeHTTPBasic
:
241 scheme
= ProtectionSpaceAuthenticationSchemeHTTPBasic
;
243 case kCFURLProtectionSpaceAuthenticationSchemeHTTPDigest
:
244 scheme
= ProtectionSpaceAuthenticationSchemeHTTPDigest
;
246 case kCFURLProtectionSpaceAuthenticationSchemeHTMLForm
:
247 scheme
= ProtectionSpaceAuthenticationSchemeHTMLForm
;
249 case kCFURLProtectionSpaceAuthenticationSchemeNTLM
:
250 scheme
= ProtectionSpaceAuthenticationSchemeNTLM
;
252 case kCFURLProtectionSpaceAuthenticationSchemeNegotiate
:
253 scheme
= ProtectionSpaceAuthenticationSchemeNegotiate
;
256 ASSERT_NOT_REACHED();
259 return ProtectionSpace(CFURLProtectionSpaceGetHost(cfSpace
),
260 CFURLProtectionSpaceGetPort(cfSpace
),
262 CFURLProtectionSpaceGetRealm(cfSpace
),