// $Id: NSString+BasicAuth.m,v 1.1 2004/06/08 11:08:07 helge Exp $ #include "NSString+BasicAuth.h" #include #include "common.h" @implementation NSString(BasicAuth) /* eg: authorization: "basic aGVsZ2U6aGVsZ2VoZWxnZQ==" */ - (BOOL)isHTTPBasicAuthorizationValue { return [self hasPrefix:@"basic"]; } - (NSString *)decodedHTTPBasicAuthorizationValue { NSRange r; NSString *s; r = [self rangeOfString:@" " options:NSBackwardsSearch]; if (r.length == 0) return nil; s = [self substringFromIndex:(r.location + r.length)]; return [s stringByDecodingBase64]; } - (NSString *)loginOfHTTPBasicAuthorizationValue { NSString *s; unsigned idx; if ((s = [self decodedHTTPBasicAuthorizationValue]) == nil) return nil; if ((idx = [s indexOfString:@":"]) == NSNotFound) return nil; return [s substringToIndex:idx]; } - (NSString *)passwordOfHTTPBasicAuthorizationValue { NSString *s; unsigned idx; if ((s = [self decodedHTTPBasicAuthorizationValue]) == nil) return nil; if ((idx = [s indexOfString:@":"]) == NSNotFound) return nil; return [s substringFromIndex:(idx + 1)]; } @end /* NSString(BasicAuth) */