Home | History | Annotate | Download | only in Panels
      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 
     29 #import <WebKit/WebAuthenticationPanel.h>
     30 
     31 #import <Foundation/NSURLAuthenticationChallenge.h>
     32 #import <Foundation/NSURLProtectionSpace.h>
     33 #import <Foundation/NSURLCredential.h>
     34 #import <WebKit/WebKitNSStringExtras.h>
     35 #import <WebKit/WebLocalizableStrings.h>
     36 #import <WebKit/WebNSURLExtras.h>
     37 #import <wtf/Assertions.h>
     38 
     39 #import <WebKit/WebNSControlExtras.h>
     40 
     41 #define WebAuthenticationPanelNibName @"WebAuthenticationPanel"
     42 
     43 @implementation WebAuthenticationPanel
     44 
     45 -(id)initWithCallback:(id)cb selector:(SEL)sel
     46 {
     47     self = [self init];
     48     if (self != nil) {
     49         callback = [cb retain];
     50         selector = sel;
     51     }
     52     return self;
     53 }
     54 
     55 
     56 - (void)dealloc
     57 {
     58     [panel release];
     59 
     60     [callback release];
     61 
     62     [super dealloc];
     63 }
     64 
     65 // IB actions
     66 
     67 - (IBAction)cancel:(id)sender
     68 {
     69     // This is required because the body of this method is going to
     70     // remove all of the panel's remaining refs, which can cause a
     71     // crash later when finishing button hit tracking.  So we make
     72     // sure it lives on a bit longer.
     73     [[panel retain] autorelease];
     74 
     75     // This is required as a workaround for AppKit issue 4118422
     76     [[self retain] autorelease];
     77 
     78     [panel close];
     79     if (usingSheet) {
     80         [[NSApplication sharedApplication] endSheet:panel returnCode:1];
     81     } else {
     82         [[NSApplication sharedApplication] stopModalWithCode:1];
     83     }
     84 }
     85 
     86 - (IBAction)logIn:(id)sender
     87 {
     88     // This is required because the body of this method is going to
     89     // remove all of the panel's remaining refs, which can cause a
     90     // crash later when finishing button hit tracking.  So we make
     91     // sure it lives on a bit longer.
     92     [[panel retain] autorelease];
     93 
     94     [panel close];
     95     if (usingSheet) {
     96         [[NSApplication sharedApplication] endSheet:panel returnCode:0];
     97     } else {
     98         [[NSApplication sharedApplication] stopModalWithCode:0];
     99     }
    100 }
    101 
    102 - (BOOL)loadNib
    103 {
    104     if (!nibLoaded) {
    105         if ([NSBundle loadNibNamed:WebAuthenticationPanelNibName owner:self]) {
    106             nibLoaded = YES;
    107             [imageView setImage:[NSImage imageNamed:@"NSApplicationIcon"]];
    108         } else {
    109             LOG_ERROR("couldn't load nib named '%@'", WebAuthenticationPanelNibName);
    110             return FALSE;
    111         }
    112     }
    113     return TRUE;
    114 }
    115 
    116 // Methods related to displaying the panel
    117 
    118 -(void)setUpForChallenge:(NSURLAuthenticationChallenge *)chall
    119 {
    120     [self loadNib];
    121 
    122     NSURLProtectionSpace *space = [chall protectionSpace];
    123 
    124     NSString *host;
    125     if ([space port] == 0) {
    126         host = [[space host] _web_decodeHostName];
    127     } else {
    128         host = [NSString stringWithFormat:@"%@:%u", [[space host] _web_decodeHostName], [space port]];
    129     }
    130 
    131     NSString *realm = [space realm];
    132     NSString *message;
    133 
    134     // Consider the realm name to be "simple" if it does not contain any whitespace or newline characters.
    135     // If the realm name is determined to be complex, we will use a slightly different sheet layout, designed
    136     // to keep a malicious realm name from spoofing the wording in the sheet text.
    137     BOOL realmNameIsSimple = [realm rangeOfCharacterFromSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].location == NSNotFound;
    138 
    139     if ([chall previousFailureCount] == 0) {
    140         if ([space isProxy]) {
    141             message = [NSString stringWithFormat:UI_STRING("To view this page, you must log in to the %@ proxy server %@.",
    142                                                            "prompt string in authentication panel"),
    143                 [space proxyType], host];
    144         } else {
    145             if (realmNameIsSimple)
    146                 message = [NSString stringWithFormat:UI_STRING("To view this page, you must log in to area %@ on %@.",
    147                                                                "prompt string in authentication panel"), realm, host];
    148             else
    149                 message = [NSString stringWithFormat:UI_STRING("To view this page, you must log in to this area on %@:",
    150                                                                "prompt string in authentication panel"), host];
    151         }
    152     } else {
    153         if ([space isProxy]) {
    154             message = [NSString stringWithFormat:UI_STRING("The user name or password you entered for the %@ proxy server %@ was incorrect. Make sure youre entering them correctly, and then try again.",
    155                                                            "prompt string in authentication panel"),
    156                 [space proxyType], host];
    157         } else {
    158             if (realmNameIsSimple)
    159                 message = [NSString stringWithFormat:UI_STRING("The user name or password you entered for area %@ on %@ was incorrect. Make sure youre entering them correctly, and then try again.",
    160                                                                "prompt string in authentication panel"), realm, host];
    161             else
    162                 message = [NSString stringWithFormat:UI_STRING("The user name or password you entered for this area on %@ was incorrect. Make sure youre entering them correctly, and then try again.",
    163                                                                "prompt string in authentication panel"), host];
    164         }
    165     }
    166 
    167     if (![space isProxy] && !realmNameIsSimple) {
    168         [separateRealmLabel setHidden:NO];
    169         [separateRealmLabel setStringValue:realm];
    170         [separateRealmLabel setAutoresizingMask:NSViewMinYMargin];
    171         [separateRealmLabel sizeToFitAndAdjustWindowHeight];
    172         [separateRealmLabel setAutoresizingMask:NSViewMaxYMargin];
    173     } else {
    174         // In the proxy or "simple" realm name case, we need to hide the 'separateRealmLabel'
    175         // and move the rest of the contents up appropriately to fill the space.
    176         NSRect mainLabelFrame = [mainLabel frame];
    177         NSRect realmFrame = [separateRealmLabel frame];
    178         NSRect smallLabelFrame = [smallLabel frame];
    179 
    180         // Find the distance between the 'smallLabel' and the label above it, initially the 'separateRealmLabel'.
    181         // Then, find the current distance between 'smallLabel' and 'mainLabel'.  The difference between
    182         // these two is how much shorter the panel needs to be after hiding the 'separateRealmLabel'.
    183         CGFloat smallLabelMargin = NSMinY(realmFrame) - NSMaxY(smallLabelFrame);
    184         CGFloat smallLabelToMainLabel = NSMinY(mainLabelFrame) - NSMaxY(smallLabelFrame);
    185         CGFloat deltaMargin = smallLabelToMainLabel - smallLabelMargin;
    186 
    187         [separateRealmLabel setHidden:YES];
    188         NSRect windowFrame = [panel frame];
    189         windowFrame.size.height -= deltaMargin;
    190         [panel setFrame:windowFrame display:NO];
    191     }
    192 
    193     [mainLabel setStringValue:message];
    194     [mainLabel sizeToFitAndAdjustWindowHeight];
    195 
    196     if ([space receivesCredentialSecurely] || [[space protocol] _webkit_isCaseInsensitiveEqualToString:@"https"]) {
    197         [smallLabel setStringValue:
    198             UI_STRING("Your login information will be sent securely.",
    199                 "message in authentication panel")];
    200     } else {
    201         // Use this scary-sounding phrase only when using basic auth with non-https servers. In this case the password
    202         // could be sniffed by intercepting the network traffic.
    203         [smallLabel setStringValue:
    204             UI_STRING("Your password will be sent unencrypted.",
    205                 "message in authentication panel")];
    206     }
    207 
    208     if ([[chall proposedCredential] user] != nil) {
    209         [username setStringValue:[[chall proposedCredential] user]];
    210         [panel setInitialFirstResponder:password];
    211     } else {
    212         [username setStringValue:@""];
    213         [password setStringValue:@""];
    214         [panel setInitialFirstResponder:username];
    215     }
    216 }
    217 
    218 - (void)runAsModalDialogWithChallenge:(NSURLAuthenticationChallenge *)chall
    219 {
    220     [self setUpForChallenge:chall];
    221     usingSheet = FALSE;
    222     NSURLCredential *credential = nil;
    223 
    224     if ([[NSApplication sharedApplication] runModalForWindow:panel] == 0) {
    225         credential = [[NSURLCredential alloc] initWithUser:[username stringValue] password:[password stringValue] persistence:([remember state] == NSOnState) ? NSURLCredentialPersistencePermanent : NSURLCredentialPersistenceForSession];
    226     }
    227 
    228     [callback performSelector:selector withObject:chall withObject:credential];
    229     [credential release];
    230 }
    231 
    232 - (void)runAsSheetOnWindow:(NSWindow *)window withChallenge:(NSURLAuthenticationChallenge *)chall
    233 {
    234     ASSERT(!usingSheet);
    235 
    236     [self setUpForChallenge:chall];
    237 
    238     usingSheet = TRUE;
    239     challenge = [chall retain];
    240 
    241     [[NSApplication sharedApplication] beginSheet:panel modalForWindow:window modalDelegate:self didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:) contextInfo:NULL];
    242 }
    243 
    244 - (void)sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void  *)contextInfo
    245 {
    246     NSURLCredential *credential = nil;
    247     NSURLAuthenticationChallenge *chall;
    248 
    249     ASSERT(usingSheet);
    250     ASSERT(challenge != nil);
    251 
    252     if (returnCode == 0) {
    253         credential = [[NSURLCredential alloc] initWithUser:[username stringValue] password:[password stringValue] persistence:([remember state] == NSOnState) ? NSURLCredentialPersistencePermanent : NSURLCredentialPersistenceForSession];
    254     }
    255 
    256     // We take this tricky approach to nilling out and releasing the challenge
    257     // because the callback below might remove our last ref.
    258     chall = challenge;
    259     challenge = nil;
    260     [callback performSelector:selector withObject:chall withObject:credential];
    261     [credential release];
    262     [chall release];
    263 }
    264 
    265 @end
    266 
    267 @implementation NonBlockingPanel
    268 
    269 - (BOOL)_blocksActionWhenModal:(SEL)theAction
    270 {
    271     // This override of a private AppKit method allows the user to quit when a login dialog
    272     // is onscreen, which is nice in general but in particular prevents pathological cases
    273     // like 3744583 from requiring a Force Quit.
    274     //
    275     // It would be nice to allow closing the individual window as well as quitting the app when
    276     // a login sheet is up, but this _blocksActionWhenModal: mechanism doesn't support that.
    277     // This override matches those in NSOpenPanel and NSToolbarConfigPanel.
    278     if (theAction == @selector(terminate:)) {
    279         return NO;
    280     }
    281     return YES;
    282 }
    283 
    284 @end
    285