WebPanelAuthenticationHandler.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (C) 2005 Apple Computer, 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. *
  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.
  16. *
  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.
  27. */
  28. #import <WebKit/WebPanelAuthenticationHandler.h>
  29. #import <Foundation/NSURLAuthenticationChallenge.h>
  30. #import <WebKit/WebAuthenticationPanel.h>
  31. #import <wtf/Assertions.h>
  32. static NSString *WebModalDialogPretendWindow = @"WebModalDialogPretendWindow";
  33. @implementation WebPanelAuthenticationHandler
  34. WebPanelAuthenticationHandler *sharedHandler;
  35. + (id)sharedHandler
  36. {
  37. if (sharedHandler == nil)
  38. sharedHandler = [[self alloc] init];
  39. return sharedHandler;
  40. }
  41. -(id)init
  42. {
  43. self = [super init];
  44. if (self != nil) {
  45. windowToPanel = [[NSMapTable alloc] initWithKeyOptions:NSPointerFunctionsStrongMemory valueOptions:NSPointerFunctionsStrongMemory capacity:0];
  46. challengeToWindow = [[NSMapTable alloc] initWithKeyOptions:NSPointerFunctionsStrongMemory valueOptions:NSPointerFunctionsStrongMemory capacity:0];
  47. windowToChallengeQueue = [[NSMapTable alloc] initWithKeyOptions:NSPointerFunctionsStrongMemory valueOptions:NSPointerFunctionsStrongMemory capacity:0];
  48. }
  49. return self;
  50. }
  51. -(void)dealloc
  52. {
  53. [windowToPanel release];
  54. [challengeToWindow release];
  55. [windowToChallengeQueue release];
  56. [super dealloc];
  57. }
  58. -(void)enqueueChallenge:(NSURLAuthenticationChallenge *)challenge forWindow:(id)window
  59. {
  60. NSMutableArray *queue = [windowToChallengeQueue objectForKey:window];
  61. if (!queue) {
  62. queue = [[NSMutableArray alloc] init];
  63. [windowToChallengeQueue setObject:queue forKey:window];
  64. [queue release];
  65. }
  66. [queue addObject:challenge];
  67. }
  68. -(void)tryNextChallengeForWindow:(id)window
  69. {
  70. NSMutableArray *queue = [windowToChallengeQueue objectForKey:window];
  71. if (!queue)
  72. return;
  73. NSURLAuthenticationChallenge *challenge = [[queue objectAtIndex:0] retain];
  74. [queue removeObjectAtIndex:0];
  75. if (![queue count])
  76. [windowToChallengeQueue removeObjectForKey:window];
  77. NSURLCredential *latestCredential = [[NSURLCredentialStorage sharedCredentialStorage] defaultCredentialForProtectionSpace:[challenge protectionSpace]];
  78. if ([latestCredential hasPassword]) {
  79. [[challenge sender] useCredential:latestCredential forAuthenticationChallenge:challenge];
  80. [challenge release];
  81. return;
  82. }
  83. [self startAuthentication:challenge window:(window == WebModalDialogPretendWindow ? nil : window)];
  84. [challenge release];
  85. }
  86. -(void)startAuthentication:(NSURLAuthenticationChallenge *)challenge window:(NSWindow *)w
  87. {
  88. id window = w ? (id)w : (id)WebModalDialogPretendWindow;
  89. if ([windowToPanel objectForKey:window] != nil) {
  90. [self enqueueChallenge:challenge forWindow:window];
  91. return;
  92. }
  93. // In this case, we have an attached sheet that's not one of our
  94. // authentication panels, so enqueing is not an option. Just
  95. // cancel loading instead, since this case is fairly
  96. // unlikely (how would you be loading a page if you had an error
  97. // sheet up?)
  98. if ([w attachedSheet] != nil) {
  99. [[challenge sender] cancelAuthenticationChallenge:challenge];
  100. return;
  101. }
  102. WebAuthenticationPanel *panel = [[WebAuthenticationPanel alloc] initWithCallback:self selector:@selector(_authenticationDoneWithChallenge:result:)];
  103. [challengeToWindow setObject:window forKey:challenge];
  104. [windowToPanel setObject:panel forKey:window];
  105. [panel release];
  106. if (window == WebModalDialogPretendWindow)
  107. [panel runAsModalDialogWithChallenge:challenge];
  108. else
  109. [panel runAsSheetOnWindow:window withChallenge:challenge];
  110. }
  111. -(void)cancelAuthentication:(NSURLAuthenticationChallenge *)challenge
  112. {
  113. id window = [challengeToWindow objectForKey:challenge];
  114. if (!window)
  115. return;
  116. WebAuthenticationPanel *panel = [windowToPanel objectForKey:window];
  117. [panel cancel:self];
  118. }
  119. -(void)_authenticationDoneWithChallenge:(NSURLAuthenticationChallenge *)challenge result:(NSURLCredential *)credential
  120. {
  121. id window = [challengeToWindow objectForKey:challenge];
  122. [window retain];
  123. if (window != nil) {
  124. [windowToPanel removeObjectForKey:window];
  125. [challengeToWindow removeObjectForKey:challenge];
  126. }
  127. if (credential == nil) {
  128. [[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge];
  129. } else {
  130. [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
  131. }
  132. [self tryNextChallengeForWindow:window];
  133. [window release];
  134. }
  135. @end