f2921ec438e90b285a6c151bcd997f95f7672ec7
[WebKit-https.git] / WebCore / platform / network / cf / AuthenticationCF.cpp
1 /*
2  * Copyright (C) 2007 Apple Inc.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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. 
24  */
25
26 #include "config.h"
27 #include "AuthenticationCF.h"
28
29 #include "AuthenticationChallenge.h"
30 #include "Credential.h"
31 #include "ProtectionSpace.h"
32 #include "ResourceHandle.h"
33
34 #include <CFNetwork/CFURLAuthChallengePriv.h>
35 #include <CFNetwork/CFURLCredentialPriv.h>
36 #include <CFNetwork/CFURLProtectionSpacePriv.h>
37
38 namespace WebCore {
39
40 AuthenticationChallenge::AuthenticationChallenge(CFURLAuthChallengeRef cfChallenge, ResourceHandle* sourceHandle)
41     : m_isNull(false)
42     , m_protectionSpace(core(CFURLAuthChallengeGetProtectionSpace(cfChallenge)))
43     , m_proposedCredential(core(CFURLAuthChallengeGetProposedCredential(cfChallenge)))
44     , m_previousFailureCount(CFURLAuthChallengeGetPreviousFailureCount(cfChallenge))
45     , m_failureResponse((CFURLResponseRef)CFURLAuthChallengeGetFailureResponse(cfChallenge))
46     , m_error(CFURLAuthChallengeGetError(cfChallenge))
47     , m_sourceHandle(sourceHandle)
48     , m_cfChallenge(cfChallenge)
49 {
50 }
51
52 CFURLAuthChallengeRef createCF(const AuthenticationChallenge& coreChallenge)
53 {  
54     CFURLProtectionSpaceRef protectionSpace = createCF(coreChallenge.protectionSpace());
55     CFURLCredentialRef credential = createCF(coreChallenge.proposedCredential());
56     
57     CFURLAuthChallengeRef result = CFURLAuthChallengeCreate(0, protectionSpace, credential,
58                                         coreChallenge.previousFailureCount(),
59                                         coreChallenge.failureResponse().cfURLResponse(),
60                                         coreChallenge.error());
61     CFRelease(protectionSpace);
62     CFRelease(credential);
63     return result;
64 }
65
66 CFURLCredentialRef createCF(const Credential& coreCredential)
67 {
68     CFURLCredentialPersistence persistence = kCFURLCredentialPersistenceNone;
69     switch (coreCredential.persistence()) {
70     case CredentialPersistenceNone:
71         break;
72     case CredentialPersistenceForSession:
73         persistence = kCFURLCredentialPersistenceForSession;
74         break;
75     case CredentialPersistencePermanent:
76         persistence = kCFURLCredentialPersistencePermanent;
77         break;
78     default:
79         ASSERT_NOT_REACHED();
80     }
81     
82     CFStringRef user = coreCredential.user().createCFString();
83     CFStringRef password = coreCredential.password().createCFString();
84     CFURLCredentialRef result = CFURLCredentialCreate(0, user, password, 0, persistence);
85     CFRelease(user);
86     CFRelease(password);
87
88     return result;
89 }
90
91 CFURLProtectionSpaceRef createCF(const ProtectionSpace& coreSpace)
92 {
93     CFURLProtectionSpaceServerType serverType = kCFURLProtectionSpaceServerHTTP;
94     switch (coreSpace.serverType()) {
95     case ProtectionSpaceServerHTTP:
96         serverType = kCFURLProtectionSpaceServerHTTP;
97         break;
98     case ProtectionSpaceServerHTTPS:
99         serverType = kCFURLProtectionSpaceServerHTTPS;
100         break;
101     case ProtectionSpaceServerFTP:
102         serverType = kCFURLProtectionSpaceServerFTP;
103         break;
104     case ProtectionSpaceServerFTPS:
105         serverType = kCFURLProtectionSpaceServerFTPS;
106         break;
107     case ProtectionSpaceProxyHTTP:
108         serverType = kCFURLProtectionSpaceProxyHTTP;
109         break;
110     case ProtectionSpaceProxyHTTPS:
111         serverType = kCFURLProtectionSpaceProxyHTTPS;
112         break;
113     case ProtectionSpaceProxyFTP:
114         serverType = kCFURLProtectionSpaceProxyFTP;
115         break;
116     case ProtectionSpaceProxySOCKS:
117         serverType = kCFURLProtectionSpaceProxySOCKS;
118         break;
119     default:
120         ASSERT_NOT_REACHED();
121     }
122
123     CFURLProtectionSpaceAuthenticationScheme scheme = kCFURLProtectionSpaceAuthenticationSchemeDefault;
124     switch (coreSpace.authenticationScheme()) {
125     case ProtectionSpaceAuthenticationSchemeDefault:
126         scheme = kCFURLProtectionSpaceAuthenticationSchemeDefault;
127         break;
128     case ProtectionSpaceAuthenticationSchemeHTTPBasic:
129         scheme = kCFURLProtectionSpaceAuthenticationSchemeHTTPBasic;
130         break;
131     case ProtectionSpaceAuthenticationSchemeHTTPDigest:
132         scheme = kCFURLProtectionSpaceAuthenticationSchemeHTTPDigest;
133         break;
134     case ProtectionSpaceAuthenticationSchemeHTMLForm:
135         scheme = kCFURLProtectionSpaceAuthenticationSchemeHTMLForm;
136         break;
137     default:
138         ASSERT_NOT_REACHED();
139     }
140
141     CFStringRef host = coreSpace.host().createCFString();
142     CFStringRef realm = coreSpace.realm().createCFString();
143     CFURLProtectionSpaceRef result = CFURLProtectionSpaceCreate(0, host, coreSpace.port(), serverType, realm, scheme);
144     CFRelease(host);
145     CFRelease(realm);
146     
147     return result;
148 }
149
150 Credential core(CFURLCredentialRef cfCredential)
151 {
152     CredentialPersistence persistence = CredentialPersistenceNone;
153     switch (CFURLCredentialGetPersistence(cfCredential)) {
154     case kCFURLCredentialPersistenceNone:
155         break;
156     case kCFURLCredentialPersistenceForSession:
157         persistence = CredentialPersistenceForSession;
158         break;
159     case kCFURLCredentialPersistencePermanent:
160         persistence = CredentialPersistencePermanent;
161         break;
162     default:
163         ASSERT_NOT_REACHED();
164     }
165     
166     return Credential(CFURLCredentialGetUsername(cfCredential), CFURLCredentialCopyPassword(cfCredential), persistence);
167 }
168
169 ProtectionSpace core(CFURLProtectionSpaceRef cfSpace)
170 {
171     ProtectionSpaceServerType serverType = ProtectionSpaceServerHTTP;
172     
173     switch (CFURLProtectionSpaceGetServerType(cfSpace)) {
174     case kCFURLProtectionSpaceServerHTTP:
175         break;
176     case kCFURLProtectionSpaceServerHTTPS:
177         serverType = ProtectionSpaceServerHTTPS;
178         break;
179     case kCFURLProtectionSpaceServerFTP:
180         serverType = ProtectionSpaceServerFTP;
181         break;
182     case kCFURLProtectionSpaceServerFTPS:
183         serverType = ProtectionSpaceServerFTPS;
184         break;
185     case kCFURLProtectionSpaceProxyHTTP:
186         serverType = ProtectionSpaceProxyHTTP;
187         break;
188     case kCFURLProtectionSpaceProxyHTTPS:
189         serverType = ProtectionSpaceProxyHTTPS;
190         break;
191     case kCFURLProtectionSpaceProxyFTP:
192         serverType = ProtectionSpaceProxyFTP;
193         break;
194     case kCFURLProtectionSpaceProxySOCKS:
195         serverType = ProtectionSpaceProxySOCKS;
196         break;
197     default:
198         ASSERT_NOT_REACHED();
199     }
200
201     ProtectionSpaceAuthenticationScheme scheme = ProtectionSpaceAuthenticationSchemeDefault;
202     
203     switch (CFURLProtectionSpaceGetAuthenticationScheme(cfSpace)) {
204     case kCFURLProtectionSpaceAuthenticationSchemeDefault:
205         scheme = ProtectionSpaceAuthenticationSchemeDefault;
206         break;
207     case kCFURLProtectionSpaceAuthenticationSchemeHTTPBasic:
208         scheme = ProtectionSpaceAuthenticationSchemeHTTPBasic;
209         break;
210     case kCFURLProtectionSpaceAuthenticationSchemeHTTPDigest:
211         scheme = ProtectionSpaceAuthenticationSchemeHTTPDigest;
212         break;
213     case kCFURLProtectionSpaceAuthenticationSchemeHTMLForm:
214         scheme = ProtectionSpaceAuthenticationSchemeHTMLForm;
215         break;
216     default:
217         ASSERT_NOT_REACHED();
218     }
219         
220     return ProtectionSpace(CFURLProtectionSpaceGetHost(cfSpace), 
221                            CFURLProtectionSpaceGetPort(cfSpace),
222                            serverType,
223                            CFURLProtectionSpaceGetRealm(cfSpace),
224                            scheme);
225 }
226
227 };