Skip to content

Instantly share code, notes, and snippets.

@msv
Created August 21, 2012 00:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msv/3410085 to your computer and use it in GitHub Desktop.
Save msv/3410085 to your computer and use it in GitHub Desktop.
Implement MAC Auth
/* This method returns the string needed for MAC Authorization for OAuth2. Add the request header Authorization: <string>, where <string> is the return string from this method.
It should look like this: MAC id="<id>",ts="<ts>",nonce="<nonce>",mac="<mac>"
Notice the quotation marks around the values. This is important.
id is the access token.
ts is the current timestamp in milliseconds.
nonce is a random number string (4 numbers prefixed with the letter n).
mac comes from base64 encoding data from a HMAC SHA1 algorithm.
CCHmac and CC_SHA1_DIGEST_LENGTH come from <CommonCrypto/CommonHMAC.h>.
*/
- (NSString *)createMACHeaderForHttpMethod:(NSString *)method path:(NSString *)path timestamp:(double)timestamp nonce:(NSString *)nonce
{
NSString *host = [[self baseURL] host];
NSString *port = [self getPort];
// create base
NSArray *baseArray = [NSArray arrayWithObjects:[NSString stringWithFormat:@"%.f", timestamp], nonce, method, path, host, port, nil];
unichar newline = 0x0A;
NSString *baseString = [baseArray componentsJoinedByString:[NSString stringWithFormat:@"%C", newline]];
baseString = [baseString stringByAppendingString:[NSString stringWithFormat:@"%C", newline]];
baseString = [baseString stringByAppendingString:[NSString stringWithFormat:@"%C", newline]];
const char *keyCString = [self.macKey cStringUsingEncoding:NSUTF8StringEncoding];
const char *baseCString = [baseString cStringUsingEncoding:NSUTF8StringEncoding];
char buffer[CC_SHA1_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA1, keyCString, strlen(keyCString), baseCString, strlen(baseCString), buffer);
NSData *digestData = [NSData dataWithBytes:buffer length:CC_SHA1_DIGEST_LENGTH];
NSString *mac = Base64EncodedStringFromData(digestData);
//return 'MAC id="' + id + '",ts="' + ts + '",nonce="' + nonce + '",mac="' + mac + '"'
unichar quotes = 0x22;
NSString *returnString = [NSString stringWithFormat:@"MAC id=%C%@%C,ts=%C%.f%C,nonce=%C%@%C,mac=%C%@%C", quotes, self.accessToken, quotes, quotes, timestamp, quotes, quotes, nonce, quotes, quotes, mac, quotes];
return returnString;
}
// The function below was inspired on
//
// AFOAuth2Client.m
//
// Copyright (c) 2011 Mattt Thompson (http://mattt.me/)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
NSString * Base64EncodedStringFromData(NSData *data)
{
NSUInteger length = [data length];
NSMutableData *mutableData = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
uint8_t *input = (uint8_t *)[data bytes];
uint8_t *output = (uint8_t *)[mutableData mutableBytes];
for (NSUInteger i = 0; i < length; i += 3) {
NSUInteger value = 0;
for (NSUInteger j = i; j < (i + 3); j++) {
value <<= 8;
if (j < length) value |= (0xFF & input[j]);
}
static uint8_t const kAFBase64EncodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
NSUInteger idx = (i / 3) * 4;
output[idx + 0] = kAFBase64EncodingTable[(value >> 18) & 0x3F];
output[idx + 1] = kAFBase64EncodingTable[(value >> 12) & 0x3F];
output[idx + 2] = (i + 1) < length ? kAFBase64EncodingTable[(value >> 6) & 0x3F] : '=';
output[idx + 3] = (i + 2) < length ? kAFBase64EncodingTable[(value >> 0) & 0x3F] : '=';
}
return [[NSString alloc] initWithData:mutableData encoding:NSASCIIStringEncoding];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment