-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathIRWebAPIGoogleReaderAuthenticator.m
More file actions
106 lines (65 loc) · 3.17 KB
/
IRWebAPIGoogleReaderAuthenticator.m
File metadata and controls
106 lines (65 loc) · 3.17 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
//
// IRWebAPIGoogleReaderAuthenticator.m
// IRWebAPIKit
//
// Created by Evadne Wu on 11/21/10.
// Copyright 2010 Iridia Productions. All rights reserved.
//
#import "IRWebAPIKit.h"
#import "IRWebAPIGoogleReaderAuthenticator.h"
@interface IRWebAPIGoogleReaderAuthenticator ()
@property (nonatomic, retain, readwrite) NSString *authToken;
@end
@implementation IRWebAPIGoogleReaderAuthenticator
@synthesize authToken;
- (void) createTransformerBlocks {
self.globalRequestPreTransformerBlock = [[^ (NSDictionary *inOriginalContext) {
if (!self.currentCredentials) return inOriginalContext;
if (!self.authToken) return inOriginalContext;
NSMutableDictionary *transformedContext = [[inOriginalContext mutableCopy] autorelease];
volatile NSMutableDictionary *headerFields = [transformedContext valueForKey:kIRWebAPIEngineRequestHTTPHeaderFields];
headerFields = headerFields ? [[headerFields mutableCopy] autorelease] : [NSMutableDictionary dictionary];
[transformedContext setObject:headerFields forKey:kIRWebAPIEngineRequestHTTPHeaderFields];
[headerFields setObject:[NSString stringWithFormat:@"GoogleLogin auth=%@", self.authToken] forKey:@"Authorization"];
return (NSDictionary *)transformedContext;
} copy] autorelease];
self.globalResponsePreTransformerBlock = [[^ (NSDictionary *inParsedResponse, NSDictionary *inResponseContext) {
return inParsedResponse;
} copy] autorelease];
}
- (void) associateWithEngine:(IRWebAPIEngine *)inEngine {
[self disassociateEngine];
self.authToken = nil;
self.engine = inEngine;
[super associateWithEngine:inEngine];
}
- (void) authenticateCredentials:(IRWebAPICredentials *)inCredentials onSuccess:(IRWebAPIAuthenticatorCallback)successHandler onFailure:(IRWebAPIAuthenticatorCallback)failureHandler {
[self.engine fireAPIRequestNamed:@"accounts/ClientLogin" withArguments:[NSDictionary dictionaryWithObjectsAndKeys:
// Does not quite work
// @"HOSTED_OR_GOOGLE", @"accountType",
@"reader", @"service",
inCredentials.identifier, @"Email",
inCredentials.qualifier, @"Passwd",
nil] options:[NSDictionary dictionaryWithObjectsAndKeys:
IRWebAPIResponseQueryResponseParserMake(), kIRWebAPIEngineParser,
@"POST", kIRWebAPIEngineRequestHTTPMethod,
[NSDictionary dictionaryWithObjectsAndKeys:@"application/x-www-form-urlencoded", @"Content-type", nil], kIRWebAPIEngineRequestHTTPHeaderFields,
nil] validator: ^ (NSDictionary *inResponseOrNil, NSDictionary *inResponseContext) {
if ([inResponseOrNil valueForKey:@"Auth"] == nil)
return NO;
if ([inResponseOrNil valueForKey:@"Error"])
return NO;
return YES;
} successHandler: ^ (NSDictionary *inResponseOrNil, NSDictionary *inResponseContext, BOOL *outNotifyDelegate, BOOL *outShouldRetry) {
self.authToken = [inResponseOrNil valueForKey:@"Auth"];
self.currentCredentials = inCredentials;
self.currentCredentials.authenticated = YES;
if (successHandler)
successHandler(self, YES, outShouldRetry);
} failureHandler: ^ (NSDictionary *inResponseOrNil, NSDictionary *inResponseContext, BOOL *outNotifyDelegate, BOOL *outShouldRetry) {
*outShouldRetry = NO;
if (failureHandler)
failureHandler(self, NO, outShouldRetry);
}];
}
@end