2 * Copyright (C) 2005 Apple Computer, 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
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #import <WebKit/WebPanelAuthenticationHandler.h>
31 #import <Foundation/NSURLAuthenticationChallenge.h>
32 #import <WebKit/WebAuthenticationPanel.h>
33 #import <WebKit/WebNSDictionaryExtras.h>
34 #import <wtf/Assertions.h>
36 static NSString *WebModalDialogPretendWindow = @"WebModalDialogPretendWindow";
38 @implementation WebPanelAuthenticationHandler
40 WebPanelAuthenticationHandler *sharedHandler;
44 if (sharedHandler == nil)
45 sharedHandler = [[self alloc] init];
53 windowToPanel = [[NSMutableDictionary alloc] init];
54 challengeToWindow = [[NSMutableDictionary alloc] init];
55 windowToChallengeQueue = [[NSMutableDictionary alloc] init];
62 [windowToPanel release];
63 [challengeToWindow release];
64 [windowToChallengeQueue release];
68 -(void)enqueueChallenge:(NSURLAuthenticationChallenge *)challenge forWindow:(id)window
70 NSMutableArray *queue = [windowToChallengeQueue objectForKey:window];
72 queue = [[NSMutableArray alloc] init];
73 [windowToChallengeQueue _webkit_setObject:queue forUncopiedKey:window];
76 [queue addObject:challenge];
79 -(void)tryNextChallengeForWindow:(id)window
81 NSMutableArray *queue = [windowToChallengeQueue objectForKey:window];
86 NSURLAuthenticationChallenge *challenge = [[queue objectAtIndex:0] retain];
87 [queue removeObjectAtIndex:0];
88 if ([queue count] == 0) {
89 [windowToChallengeQueue removeObjectForKey:window];
92 NSURLCredential *latestCredential = [[NSURLCredentialStorage sharedCredentialStorage] defaultCredentialForProtectionSpace:[challenge protectionSpace]];
94 if ([latestCredential hasPassword]) {
95 [[challenge sender] useCredential:latestCredential forAuthenticationChallenge:challenge];
100 [self startAuthentication:challenge window:(window == WebModalDialogPretendWindow ? nil : window)];
105 -(void)startAuthentication:(NSURLAuthenticationChallenge *)challenge window:(NSWindow *)w
107 id window = w ? (id)w : (id)WebModalDialogPretendWindow;
109 if ([windowToPanel objectForKey:window] != nil) {
110 [self enqueueChallenge:challenge forWindow:window];
114 // In this case, we have an attached sheet that's not one of our
115 // authentication panels, so enqueing is not an option. Just
116 // cancel loading instead, since this case is fairly
117 // unlikely (how would you be loading a page if you had an error
119 if ([w attachedSheet] != nil) {
120 [[challenge sender] cancelAuthenticationChallenge:challenge];
124 WebAuthenticationPanel *panel = [[WebAuthenticationPanel alloc] initWithCallback:self selector:@selector(_authenticationDoneWithChallenge:result:)];
125 [challengeToWindow _webkit_setObject:window forUncopiedKey:challenge];
126 [windowToPanel _webkit_setObject:panel forUncopiedKey:window];
129 if (window == WebModalDialogPretendWindow) {
130 [panel runAsModalDialogWithChallenge:challenge];
132 [panel runAsSheetOnWindow:window withChallenge:challenge];
136 -(void)cancelAuthentication:(NSURLAuthenticationChallenge *)challenge
138 id window = [challengeToWindow objectForKey:challenge];
140 WebAuthenticationPanel *panel = [windowToPanel objectForKey:window];
145 -(void)_authenticationDoneWithChallenge:(NSURLAuthenticationChallenge *)challenge result:(NSURLCredential *)credential
147 id window = [challengeToWindow objectForKey:challenge];
150 [windowToPanel removeObjectForKey:window];
151 [challengeToWindow removeObjectForKey:challenge];
154 if (credential == nil) {
155 [[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge];
157 [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
160 [self tryNextChallengeForWindow:window];