-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMPOAuthAuthenticationMethod.m
More file actions
131 lines (100 loc) · 4.14 KB
/
MPOAuthAuthenticationMethod.m
File metadata and controls
131 lines (100 loc) · 4.14 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
// MPOAuthAuthenticationMethod.m
// MPOAuthConnection
//
// Created by Karl Adam on 09.12.19.
// Copyright 2009 matrixPointer. All rights reserved.
//
#import "MPOAuthAuthenticationMethod.h"
#import "MPOAuthAuthenticationMethodOAuth.h"
#import "MPOAuthCredentialConcreteStore.h"
#import "MPURLRequestParameter.h"
#import "NSURL+MPURLParameterAdditions.h"
NSString * const MPOAuthAccessTokenURLKey = @"MPOAuthAccessTokenURL";
@interface MPOAuthAuthenticationMethod ()
@property (nonatomic, readwrite, retain) NSTimer *refreshTimer;
+ (Class)_authorizationMethodClassForURL:(NSURL *)inBaseURL withConfiguration:(NSDictionary **)outConfig;
- (id)initWithAPI:(MPOAuthAPI *)inAPI forURL:(NSURL *)inURL withConfiguration:(NSDictionary *)inConfig;
- (void)_automaticallyRefreshAccessToken:(NSTimer *)inTimer;
@end
@implementation MPOAuthAuthenticationMethod
- (id)initWithAPI:(MPOAuthAPI *)inAPI forURL:(NSURL *)inURL {
return [self initWithAPI:inAPI forURL:inURL withConfiguration:nil];
}
- (id)initWithAPI:(MPOAuthAPI *)inAPI forURL:(NSURL *)inURL withConfiguration:(NSDictionary *)inConfig {
if ([[self class] isEqual:[MPOAuthAuthenticationMethod class]]) {
NSDictionary *configuration = nil;
Class methodClass = [[self class] _authorizationMethodClassForURL:inURL withConfiguration:&configuration];
[self release];
self = [[methodClass alloc] initWithAPI:inAPI forURL:inURL withConfiguration:configuration];
} else if (self = [super init]) {
self.oauthAPI = inAPI;
}
return self;
}
- (oneway void)dealloc {
self.oauthAPI = nil;
self.oauthGetAccessTokenURL = nil;
[self.refreshTimer invalidate];
self.refreshTimer = nil;
[super dealloc];
}
@synthesize oauthAPI = oauthAPI_;
@synthesize oauthGetAccessTokenURL = oauthGetAccessTokenURL_;
@synthesize refreshTimer = refreshTimer_;
#pragma mark -
+ (Class)_authorizationMethodClassForURL:(NSURL *)inBaseURL withConfiguration:(NSDictionary **)outConfig {
Class methodClass = [MPOAuthAuthenticationMethodOAuth class];
NSString *oauthConfigPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"oauthAutoConfig" ofType:@"plist"];
NSDictionary *oauthConfigDictionary = [NSDictionary dictionaryWithContentsOfFile:oauthConfigPath];
for ( NSString *domainString in [oauthConfigDictionary keyEnumerator]) {
if ([inBaseURL domainMatches:domainString]) {
NSDictionary *oauthConfig = [oauthConfigDictionary objectForKey:domainString];
NSArray *requestedMethods = [oauthConfig objectForKey:@"MPOAuthAuthenticationPreferredMethods"];
NSString *requestedMethod = nil;
for (requestedMethod in requestedMethods) {
Class requestedMethodClass = NSClassFromString(requestedMethod);
if (requestedMethodClass) {
methodClass = requestedMethodClass;
}
break;
}
if (requestedMethod) {
*outConfig = [oauthConfig objectForKey:requestedMethod];
} else {
*outConfig = oauthConfig;
}
break;
}
}
return methodClass;
}
#pragma mark -
- (void)authenticate {
[NSException raise:@"Not Implemented" format:@"All subclasses of MPOAuthAuthenticationMethod are required to implement -authenticate"];
}
- (void)setTokenRefreshInterval:(NSTimeInterval)inTimeInterval {
if (!self.refreshTimer && inTimeInterval > 0.0) {
self.refreshTimer = [NSTimer scheduledTimerWithTimeInterval:inTimeInterval target:self selector:@selector(_automaticallyRefreshAccessToken:) userInfo:nil repeats:YES];
}
}
- (void)refreshAccessToken {
MPURLRequestParameter *sessionHandleParameter = nil;
MPOAuthCredentialConcreteStore *credentials = (MPOAuthCredentialConcreteStore *)[self.oauthAPI credentials];
if (credentials.sessionHandle) {
sessionHandleParameter = [[MPURLRequestParameter alloc] init];
sessionHandleParameter.name = @"oauth_session_handle";
sessionHandleParameter.value = credentials.sessionHandle;
}
[self.oauthAPI performMethod:nil
atURL:self.oauthGetAccessTokenURL
withParameters:sessionHandleParameter ? [NSArray arrayWithObject:sessionHandleParameter] : nil
withTarget:nil
andAction:nil];
[sessionHandleParameter release];
}
#pragma mark -
- (void)_automaticallyRefreshAccessToken:(NSTimer *)inTimer {
[self refreshAccessToken];
}
@end