1/*
2 * Copyright (C) 2013 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 INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#import "config.h"
27#import "WKNSURLAuthenticationChallenge.h"
28
29#if WK_API_ENABLED
30
31#import "AuthenticationDecisionListener.h"
32#import "WebCertificateInfo.h"
33#import "WebCredential.h"
34#import <WebCore/AuthenticationMac.h>
35
36using namespace WebCore;
37using namespace WebKit;
38
39@interface WKNSURLAuthenticationChallengeSender : NSObject <NSURLAuthenticationChallengeSender>
40@end
41
42@implementation WKNSURLAuthenticationChallenge
43
44- (NSObject *)_web_createTarget
45{
46    AuthenticationChallengeProxy& challenge = *reinterpret_cast<AuthenticationChallengeProxy*>(&self._apiObject);
47
48    static dispatch_once_t token;
49    static WKNSURLAuthenticationChallengeSender *sender;
50    dispatch_once(&token, ^{
51        sender = [[WKNSURLAuthenticationChallengeSender alloc] init];
52    });
53
54    return [[NSURLAuthenticationChallenge alloc] initWithAuthenticationChallenge:mac(challenge.core()) sender:sender];
55}
56
57- (AuthenticationChallengeProxy&)_web_authenticationChallengeProxy
58{
59    return *reinterpret_cast<AuthenticationChallengeProxy*>(&self._apiObject);
60}
61
62@end
63
64@implementation WKNSURLAuthenticationChallengeSender
65
66static void checkChallenge(NSURLAuthenticationChallenge *challenge)
67{
68    if ([challenge class] != [WKNSURLAuthenticationChallenge class])
69        [NSException raise:NSInvalidArgumentException format:@"The challenge was not sent by the receiver."];
70}
71
72- (void)cancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
73{
74    checkChallenge(challenge);
75    AuthenticationChallengeProxy& webChallenge = ((WKNSURLAuthenticationChallenge *)challenge)._web_authenticationChallengeProxy;
76    webChallenge.listener()->cancel();
77}
78
79- (void)continueWithoutCredentialForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
80{
81    checkChallenge(challenge);
82    AuthenticationChallengeProxy& webChallenge = ((WKNSURLAuthenticationChallenge *)challenge)._web_authenticationChallengeProxy;
83    webChallenge.listener()->useCredential(nullptr);
84}
85
86- (void)useCredential:(NSURLCredential *)credential forAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
87{
88    checkChallenge(challenge);
89    AuthenticationChallengeProxy& webChallenge = ((WKNSURLAuthenticationChallenge *)challenge)._web_authenticationChallengeProxy;
90    webChallenge.listener()->useCredential(WebCredential::create(core(credential)).get());
91}
92
93- (void)performDefaultHandlingForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
94{
95    checkChallenge(challenge);
96    AuthenticationChallengeProxy& webChallenge = ((WKNSURLAuthenticationChallenge *)challenge)._web_authenticationChallengeProxy;
97    webChallenge.listener()->performDefaultHandling();
98}
99
100- (void)rejectProtectionSpaceAndContinueWithChallenge:(NSURLAuthenticationChallenge *)challenge
101{
102    checkChallenge(challenge);
103    AuthenticationChallengeProxy& webChallenge = ((WKNSURLAuthenticationChallenge *)challenge)._web_authenticationChallengeProxy;
104    webChallenge.listener()->rejectProtectionSpaceAndContinue();
105}
106
107@end
108
109#endif // WK_API_ENABLED
110