forked from calebd/SimpleAuth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleAuthFeedlyWebProvider.m
More file actions
155 lines (124 loc) · 4.84 KB
/
SimpleAuthFeedlyWebProvider.m
File metadata and controls
155 lines (124 loc) · 4.84 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//
// SimpleAuthFeedlyWebProvider.m
// SimpleAuth
//
// Created by Luís Portela Afonso on 26/02/14.
// Copyright (c) 2014 Byliner, Inc. All rights reserved.
//
#import "SimpleAuthFeedlyWebProvider.h"
#import "SimpleAuthFeedlyWebLoginViewController.h"
#import "UIViewController+SimpleAuthAdditions.h"
#import <ReactiveCocoa/ReactiveCocoa.h>
@implementation SimpleAuthFeedlyWebProvider
+ (NSString *)type
{
return @"feedly-web";
}
+ (NSDictionary *)defaultOptions
{
// Default present block
SimpleAuthInterfaceHandler presentBlock = ^(UIViewController *controller) {
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:controller];
navigation.modalPresentationStyle = UIModalPresentationFormSheet;
UIViewController *presented = [UIViewController SimpleAuth_presentedViewController];
[presented presentViewController:navigation animated:YES completion:nil];
};
// Default dismiss block
SimpleAuthInterfaceHandler dismissBlock = ^(id controller) {
[controller dismissViewControllerAnimated:YES completion:nil];
};
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithDictionary:[super defaultOptions]];
dictionary[SimpleAuthPresentInterfaceBlockKey] = presentBlock;
dictionary[SimpleAuthDismissInterfaceBlockKey] = dismissBlock;
return dictionary;
}
- (void)authorizeWithCompletion:(SimpleAuthRequestHandler)completion
{
[[[self authenticationCode] flattenMap:^(id response) {
NSArray *signals = @[
[self exchangeCodeForRefreshAndAccess:response],
[RACSignal return:response]
];
return signals[0];
}]
subscribeNext:^(id response) {
completion(response, nil);
}
error:^(NSError *error) {
completion(nil, error);
}];
}
#pragma mark - Private Methods
- (RACSignal*)authenticationCode
{
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
dispatch_async(dispatch_get_main_queue(), ^{
SimpleAuthFeedlyWebLoginViewController *login = [[SimpleAuthFeedlyWebLoginViewController alloc] initWithOptions:self.options];
login.completion = ^(UIViewController *controller, NSURL *URL, NSError *error) {
SimpleAuthInterfaceHandler block = self.options[SimpleAuthDismissInterfaceBlockKey];
block(controller);
// Parse URL
NSDictionary *dictionary = [CMDQueryStringSerialization dictionaryWithQueryString:[URL query]];
id apiError = dictionary[@"error"];
// Check for error
if (apiError != nil)
{
[subscriber sendError:error];
return;
}
NSString *code = dictionary[@"code"];
NSString *state = dictionary[@"state"];
// Send completion
[subscriber sendNext:@{@"code": code, @"state": state}];
[subscriber sendCompleted];
};
SimpleAuthInterfaceHandler block = self.options[SimpleAuthPresentInterfaceBlockKey];
block(login);
});
return nil;
}];
}
- (RACSignal*)exchangeCodeForRefreshAndAccess:(NSDictionary *)codeState
{
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
NSDictionary *parameters = @{
@"code" : codeState[@"code"],
@"client_id" : self.options[@"client_id"],
@"client_secret" : self.options[@"client_secret"],
@"redirect_uri" : self.options[@"redirect_uri"],
@"grant_type" : @"authorization_code",
@"state" : (codeState[@"state"] != nil ? codeState[@"state"] : @"state.passed.in")
};
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://feedly.com/v3/auth/token"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[[CMDQueryStringSerialization queryStringWithDictionary:parameters] dataUsingEncoding:NSUTF8StringEncoding]];
[NSURLConnection sendAsynchronousRequest:request queue:self.operationQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError != nil)
{
[subscriber sendError:connectionError];
}
else
{
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(200, 99)];
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
if ([indexSet containsIndex:statusCode] && data)
{
__weak NSError *parseError = nil;
__weak NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&parseError];
if (parseError != nil)
{
[subscriber sendError:parseError];
}
else
{
[subscriber sendNext:dictionary];
[subscriber sendCompleted];
}
}
}
}];
return nil;
}];
}
@end