Cocoa Basic HTTP Authentication : Advice Needed..

Posted by Kristiaan on Stack Overflow See other posts from Stack Overflow or by Kristiaan
Published on 2010-03-18T15:55:16Z Indexed on 2010/03/18 21:21 UTC
Read the original article Hit count: 415

Filed under:
|
|

Hello all, im looking to read the contents of a webpage that is secured with a user name and password. this is a mac OS X application NOT an iphone app so most of the things i have read on here or been suggested to read do not seem to work.

Also i am a total beginner with Xcode and Obj C i was told to have a look at a website that provided sample code to http auth however so far i have had little luck in getting this working.

below is the main code for the button press in my application, there is also another unit called Base64 below that has some code in i had to change to even get it compiling (no idea if what i changed is correct mind you).

NSURL *url = [NSURL URLWithString:@"my URL"];  
NSString *userName = @"UN";  
NSString *password = @"PW";  

NSError *myError = nil;  

// create a plaintext string in the format username:password  
NSMutableString *loginString = (NSMutableString*)[@"" stringByAppendingFormat:@"%@:%@", userName, password];  

// employ the Base64 encoding above to encode the authentication tokens  
char *encodedLoginData = [base64 encode:[loginString dataUsingEncoding:NSUTF8StringEncoding]];  

// create the contents of the header   
NSString *authHeader = [@"Basic " stringByAppendingFormat:@"%@", [NSString stringWithCString:encodedLoginData length:strlen(encodedLoginData)]];  
//NSString *authHeader = [@"Basic " stringByAppendingFormat:@"%@", loginString];//[NSString stringWithString:loginString length:strlen(loginString)]]; 

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: url cachePolicy: NSURLRequestReloadIgnoringCacheData timeoutInterval: 3];     

// add the header to the request.  Here's the $$$!!!  
[request addValue:authHeader forHTTPHeaderField:@"Authorization"];  

// perform the reqeust  
NSURLResponse *response;  

NSData *data = [NSURLConnection    
    sendSynchronousRequest: request    
    returningResponse: &response    
    error: &myError];    
//*error = myError;  

// POW, here's the content of the webserver's response.  
NSString *result = [NSString stringWithCString:[data bytes] length:[data length]];
[myTextView setString:result];

code from the BASE64 file

#import "base64.h"

static char *alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-";  

@implementation Base64  
    +(char *)encode:(NSData *)plainText {  
        // create an adequately sized buffer for the output.  every 3 bytes   
        // become four basically with padding to the next largest integer  
        // divisible by four.   
        char * encodedText = malloc((((([plainText length] % 3) +  
            [plainText length]) / 3) * 4) + 1);  
        char* inputBuffer = malloc([plainText length]);  
        inputBuffer = (char *)[plainText bytes];  

        int i;  
        int j = 0;  

        // encode, this expands every 3 bytes to 4  
        for(i = 0; i < [plainText length]; i += 3) {  
            encodedText[j++] = alphabet[(inputBuffer[i] & 0xFC) >> 2];  
            encodedText[j++] = alphabet[((inputBuffer[i] & 0x03) << 4)  
                | ((inputBuffer[i + 1] & 0xF0) >> 4)];  

            if(i + 1 >= [plainText length])  
                // padding  
                encodedText[j++] = '=';  
            else   
                encodedText[j++] = alphabet[((inputBuffer[i + 1] & 0x0F) << 2)  
                | ((inputBuffer[i + 2] & 0xC0) >> 6)];  

            if(i + 2 >= [plainText length])  
                // padding  
                encodedText[j++] = '=';  
            else  
                encodedText[j++] = alphabet[inputBuffer[i + 2] & 0x3F];  
        }  

        // terminate the string  
        encodedText[j] = 0;  

        return encodedText;//outputBuffer;  
    }  
@end  

when executing the code it stops on the following line with a EXC_BAD_ACCESS ?!?!?

NSString *authHeader = [@"Basic " stringByAppendingFormat:@"%@",   
    [NSString stringWithCString:encodedLoginData length:strlen(encodedLoginData)]]; 

any help would be appreciated as i am a little clueless on this problem, not being very literate with Cocoa, objective c, xcode is only adding fuel to this fire for me.

© Stack Overflow or respective owner

Related posts about cocoa

Related posts about basic-authentication