/
MPOAuthAuthenticationMethodAuthExchange.m
98 lines (80 loc) · 3.91 KB
/
MPOAuthAuthenticationMethodAuthExchange.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//
// MPOAuthAuthenticationMethodAuthExchange.m
// MPOAuthMobile
//
// Created by Karl Adam on 09.12.20.
// Copyright 2009 matrixPointer. All rights reserved.
//
#import "MPOAuthAuthenticationMethodAuthExchange.h"
#import "MPOAuthAPI.h"
#import "MPOAuthAPIRequestLoader.h"
#import "MPOAuthCredentialStore.h"
#import "MPURLRequestParameter.h"
#import <libxml/parser.h>
#import <libxml/xpath.h>
#import <libxml/xpathInternals.h>
//TODO: Remove this!
#define kMPOAuthTokenRefreshDateDefaultsKey @"MPOAuthAutomaticTokenRefreshLastExpiryDate"
@interface MPOAuthAPI ()
@property (nonatomic, readwrite, assign) MPOAuthAuthenticationState authenticationState;
@end
@implementation MPOAuthAuthenticationMethodAuthExchange
- (id)initWithAPI:(MPOAuthAPI *)inAPI forURL:(NSURL *)inURL withConfiguration:(NSDictionary *)inConfig {
if (self = [super initWithAPI:inAPI forURL:inURL withConfiguration:inConfig]) {
self.oauthGetAccessTokenURL = [NSURL URLWithString:[inConfig objectForKey:MPOAuthAccessTokenURLKey]];
}
return self;
}
- (void)authenticate {
id <MPOAuthCredentialStore> credentials = [self.oauthAPI credentials];
if (!credentials.accessToken && !credentials.accessTokenSecret) {
MPLog(@"--> Performing Access Token Request: %@", self.oauthGetAccessTokenURL);
NSString *username = [[self.oauthAPI credentials] username];
NSString *password = [[self.oauthAPI credentials] password];
NSAssert(username, @"AuthExchange requires a Username credential");
NSAssert(password, @"AuthExchange requires a Password credential");
MPURLRequestParameter *usernameParameter = [[MPURLRequestParameter alloc] initWithName:@"fs_username" andValue:username];
MPURLRequestParameter *passwordParameter = [[MPURLRequestParameter alloc] initWithName:@"fs_password" andValue:password];
[self.oauthAPI performPOSTMethod:nil
atURL:self.oauthGetAccessTokenURL
withParameters:[NSArray arrayWithObjects:usernameParameter, passwordParameter, nil]
withTarget:self
andAction:nil];
} else if (credentials.accessToken && credentials.accessTokenSecret) {
NSTimeInterval expiryDateInterval = [[NSUserDefaults standardUserDefaults] doubleForKey:kMPOAuthTokenRefreshDateDefaultsKey];
NSDate *tokenExpiryDate = [NSDate dateWithTimeIntervalSinceReferenceDate:expiryDateInterval];
if ([tokenExpiryDate compare:[NSDate date]] == NSOrderedAscending) {
[self refreshAccessToken];
}
}
}
- (void)_performedLoad:(MPOAuthAPIRequestLoader *)inLoader receivingData:(NSData *)inData {
MPLog(@"loaded %@, and got:\n %@", inLoader, inData);
NSString *accessToken = nil;
NSString *accessTokenSecret = nil;
const char *xmlCString = (const char *)[inData bytes];
xmlParserCtxtPtr parserContext = xmlNewParserCtxt();
xmlDocPtr accessTokenXML = xmlCtxtReadMemory(parserContext, xmlCString, strlen(xmlCString), NULL, NULL, XML_PARSE_NOBLANKS);
xmlNodePtr rootNode = xmlDocGetRootElement(accessTokenXML);
xmlNodePtr currentNode = rootNode->children;
const char *currentNodeName = NULL;
for ( ; currentNode; currentNode = currentNode->next) {
currentNodeName = (const char *)currentNode->name;
if (strcmp("oauth_token", currentNodeName) == 0) {
xmlChar *oauthToken = xmlNodeGetContent(currentNode);
accessToken = [NSString stringWithUTF8String:(const char *)oauthToken];
} else if (strcmp("oauth_token_secret", currentNodeName) == 0) {
xmlChar *oauthTokenSecret = xmlNodeGetContent(currentNode);
accessTokenSecret = [NSString stringWithUTF8String:(const char *)oauthTokenSecret];
}
}
if (accessToken && accessTokenSecret) {
[self.oauthAPI removeCredentialNamed:kMPOAuthCredentialPassword];
[self.oauthAPI setCredential:accessToken withName:kMPOAuthCredentialAccessToken];
[self.oauthAPI setCredential:accessTokenSecret withName:kMPOAuthCredentialAccessTokenSecret];
}
[self.oauthAPI setAuthenticationState:MPOAuthAuthenticationStateAuthenticated];
xmlFreeDoc(accessTokenXML);
xmlFreeParserCtxt(parserContext);
}
@end