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#import "config.h" 26#import "AuthenticationMac.h" 27 28#import "AuthenticationChallenge.h" 29#import "Credential.h" 30#import "ProtectionSpace.h" 31 32#import <Foundation/NSURLAuthenticationChallenge.h> 33#import <Foundation/NSURLCredential.h> 34#import <Foundation/NSURLProtectionSpace.h> 35 36 37namespace WebCore { 38 39 40NSMutableDictionary* WebCoreCredentialStorage::m_storage; 41 42AuthenticationChallenge::AuthenticationChallenge(const ProtectionSpace& protectionSpace, 43 const Credential& proposedCredential, 44 unsigned previousFailureCount, 45 const ResourceResponse& response, 46 const ResourceError& error) 47 : AuthenticationChallengeBase(protectionSpace, 48 proposedCredential, 49 previousFailureCount, 50 response, 51 error) 52{ 53} 54 55AuthenticationChallenge::AuthenticationChallenge(NSURLAuthenticationChallenge *macChallenge) 56 : AuthenticationChallengeBase(core([macChallenge protectionSpace]), 57 core([macChallenge proposedCredential]), 58 [macChallenge previousFailureCount], 59 [macChallenge failureResponse], 60 [macChallenge error]) 61 , m_sender([macChallenge sender]) 62 , m_macChallenge(macChallenge) 63{ 64} 65 66bool AuthenticationChallenge::platformCompare(const AuthenticationChallenge& a, const AuthenticationChallenge& b) 67{ 68 if (a.sender() != b.sender()) 69 return false; 70 71 if (a.nsURLAuthenticationChallenge() != b.nsURLAuthenticationChallenge()) 72 return false; 73 74 return true; 75} 76 77NSURLAuthenticationChallenge *mac(const AuthenticationChallenge& coreChallenge) 78{ 79 if (coreChallenge.nsURLAuthenticationChallenge()) 80 return coreChallenge.nsURLAuthenticationChallenge(); 81 82 return [[[NSURLAuthenticationChallenge alloc] initWithProtectionSpace:mac(coreChallenge.protectionSpace()) 83 proposedCredential:mac(coreChallenge.proposedCredential()) 84 previousFailureCount:coreChallenge.previousFailureCount() 85 failureResponse:coreChallenge.failureResponse().nsURLResponse() 86 error:coreChallenge.error() 87 sender:coreChallenge.sender()] autorelease]; 88} 89 90NSURLProtectionSpace *mac(const ProtectionSpace& coreSpace) 91{ 92 NSString *proxyType = nil; 93 NSString *protocol = nil; 94 switch (coreSpace.serverType()) { 95 case ProtectionSpaceServerHTTP: 96 protocol = @"http"; 97 break; 98 case ProtectionSpaceServerHTTPS: 99 protocol = @"https"; 100 break; 101 case ProtectionSpaceServerFTP: 102 protocol = @"ftp"; 103 break; 104 case ProtectionSpaceServerFTPS: 105 protocol = @"ftps"; 106 break; 107 case ProtectionSpaceProxyHTTP: 108 proxyType = NSURLProtectionSpaceHTTPProxy; 109 break; 110 case ProtectionSpaceProxyHTTPS: 111 proxyType = NSURLProtectionSpaceHTTPSProxy; 112 break; 113 case ProtectionSpaceProxyFTP: 114 proxyType = NSURLProtectionSpaceFTPProxy; 115 break; 116 case ProtectionSpaceProxySOCKS: 117 proxyType = NSURLProtectionSpaceSOCKSProxy; 118 break; 119 default: 120 ASSERT_NOT_REACHED(); 121 } 122 123 NSString *method = nil; 124 switch (coreSpace.authenticationScheme()) { 125 case ProtectionSpaceAuthenticationSchemeDefault: 126 method = NSURLAuthenticationMethodDefault; 127 break; 128 case ProtectionSpaceAuthenticationSchemeHTTPBasic: 129 method = NSURLAuthenticationMethodHTTPBasic; 130 break; 131 case ProtectionSpaceAuthenticationSchemeHTTPDigest: 132 method = NSURLAuthenticationMethodHTTPDigest; 133 break; 134 case ProtectionSpaceAuthenticationSchemeHTMLForm: 135 method = NSURLAuthenticationMethodHTMLForm; 136 break; 137 default: 138 ASSERT_NOT_REACHED(); 139 } 140 141 if (proxyType) 142 return [[[NSURLProtectionSpace alloc] initWithProxyHost:coreSpace.host() 143 port:coreSpace.port() 144 type:proxyType 145 realm:coreSpace.realm() 146 authenticationMethod:method] autorelease]; 147 return [[[NSURLProtectionSpace alloc] initWithHost:coreSpace.host() 148 port:coreSpace.port() 149 protocol:protocol 150 realm:coreSpace.realm() 151 authenticationMethod:method] autorelease]; 152} 153 154NSURLCredential *mac(const Credential& coreCredential) 155{ 156 NSURLCredentialPersistence persistence = NSURLCredentialPersistenceNone; 157 switch (coreCredential.persistence()) { 158 case CredentialPersistenceNone: 159 break; 160 case CredentialPersistenceForSession: 161 persistence = NSURLCredentialPersistenceForSession; 162 break; 163 case CredentialPersistencePermanent: 164 persistence = NSURLCredentialPersistencePermanent; 165 break; 166 default: 167 ASSERT_NOT_REACHED(); 168 } 169 170 return [[[NSURLCredential alloc] initWithUser:coreCredential.user() 171 password:coreCredential.password() 172 persistence:persistence] 173 autorelease]; 174} 175 176AuthenticationChallenge core(NSURLAuthenticationChallenge *macChallenge) 177{ 178 return AuthenticationChallenge(macChallenge); 179} 180 181ProtectionSpace core(NSURLProtectionSpace *macSpace) 182{ 183 ProtectionSpaceServerType serverType = ProtectionSpaceProxyHTTP; 184 185 if ([macSpace isProxy]) { 186 NSString *proxyType = [macSpace proxyType]; 187 if ([proxyType isEqualToString:NSURLProtectionSpaceHTTPProxy]) 188 serverType = ProtectionSpaceProxyHTTP; 189 else if ([proxyType isEqualToString:NSURLProtectionSpaceHTTPSProxy]) 190 serverType = ProtectionSpaceProxyHTTPS; 191 else if ([proxyType isEqualToString:NSURLProtectionSpaceFTPProxy]) 192 serverType = ProtectionSpaceProxyFTP; 193 else if ([proxyType isEqualToString:NSURLProtectionSpaceSOCKSProxy]) 194 serverType = ProtectionSpaceProxySOCKS; 195 else 196 ASSERT_NOT_REACHED(); 197 } else { 198 NSString *protocol = [macSpace protocol]; 199 if ([protocol caseInsensitiveCompare:@"http"] == NSOrderedSame) 200 serverType = ProtectionSpaceServerHTTP; 201 else if ([protocol caseInsensitiveCompare:@"https"] == NSOrderedSame) 202 serverType = ProtectionSpaceServerHTTPS; 203 else if ([protocol caseInsensitiveCompare:@"ftp"] == NSOrderedSame) 204 serverType = ProtectionSpaceServerFTP; 205 else if ([protocol caseInsensitiveCompare:@"ftps"] == NSOrderedSame) 206 serverType = ProtectionSpaceServerFTPS; 207 else 208 ASSERT_NOT_REACHED(); 209 } 210 211 ProtectionSpaceAuthenticationScheme scheme = ProtectionSpaceAuthenticationSchemeDefault; 212 NSString *method = [macSpace authenticationMethod]; 213 if ([method isEqualToString:NSURLAuthenticationMethodDefault]) 214 scheme = ProtectionSpaceAuthenticationSchemeDefault; 215 else if ([method isEqualToString:NSURLAuthenticationMethodHTTPBasic]) 216 scheme = ProtectionSpaceAuthenticationSchemeHTTPBasic; 217 else if ([method isEqualToString:NSURLAuthenticationMethodHTTPDigest]) 218 scheme = ProtectionSpaceAuthenticationSchemeHTTPDigest; 219 else if ([method isEqualToString:NSURLAuthenticationMethodHTMLForm]) 220 scheme = ProtectionSpaceAuthenticationSchemeHTMLForm; 221 else 222 ASSERT_NOT_REACHED(); 223 224 return ProtectionSpace([macSpace host], [macSpace port], serverType, [macSpace realm], scheme); 225 226} 227 228Credential core(NSURLCredential *macCredential) 229{ 230 CredentialPersistence persistence = CredentialPersistenceNone; 231 switch ([macCredential persistence]) { 232 case NSURLCredentialPersistenceNone: 233 break; 234 case NSURLCredentialPersistenceForSession: 235 persistence = CredentialPersistenceForSession; 236 break; 237 case NSURLCredentialPersistencePermanent: 238 persistence = CredentialPersistencePermanent; 239 break; 240 default: 241 ASSERT_NOT_REACHED(); 242 } 243 244 return Credential([macCredential user], [macCredential password], persistence); 245} 246 247}; 248