This repository was archived by the owner on Aug 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathSimpleAuthMailChimpProvider.m
More file actions
210 lines (175 loc) · 8.01 KB
/
SimpleAuthMailChimpProvider.m
File metadata and controls
210 lines (175 loc) · 8.01 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//
// SimpleAuthMailChimpProvider.m
// SimpleAuth
//
// Created by Tal Kain <tal@kain.net>.
// Based on BoxWeb's provider created by dkhamsing and FoursquareWeb's provider created by Julien Seren-Rosso
// Copyright (c) 2015 Fire Place Inc. All rights reserved.
//
#import "SimpleAuthMailChimpProvider.h"
#import "SimpleAuthMailChimpLoginViewController.h"
#import "SimpleAuthMailChimpConstants.h"
#import <ReactiveCocoa/ReactiveCocoa.h>
#import "UIViewController+SimpleAuthAdditions.h"
@implementation SimpleAuthMailChimpProvider
#pragma mark - SimpleAuthProvider
+ (NSString *)type {
return @"mailchimp";
}
+ (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 accessToken]
flattenMap:^(NSDictionary *response) {
NSArray *signals = @[
[self accountWithAccessToken:response],
[RACSignal return:response]
];
return [self rac_liftSelector:@selector(dictionaryWithAccount:accessToken:) withSignalsFromArray:signals];
}]
subscribeNext:^(NSDictionary *response) {
completion(response, nil);
}
error:^(NSError *error) {
completion(nil, error);
}];
}
#pragma mark - Private
- (RACSignal *)accessToken {
return [[self authorizationCode] flattenMap:^(id responseObject) {
return [self accessTokenWithAuthorizationCode:responseObject];
}];
}
- (RACSignal *)authorizationCode {
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
dispatch_async(dispatch_get_main_queue(), ^{
SimpleAuthMailChimpLoginViewController *login = [[SimpleAuthMailChimpLoginViewController alloc] initWithOptions:self.options];
login.completion = ^(UIViewController *login, NSURL *URL, NSError *error) {
SimpleAuthInterfaceHandler dismissBlock = self.options[SimpleAuthDismissInterfaceBlockKey];
dismissBlock(login);
// Parse URL
NSString *fragment = [URL query];
NSDictionary *dictionary = [CMDQueryStringSerialization dictionaryWithQueryString:fragment];
NSString *code = dictionary[@"code"];
// Check for error
if (![code length]) {
[subscriber sendError:error];
return;
}
// Send completion
[subscriber sendNext:code];
[subscriber sendCompleted];
};
SimpleAuthInterfaceHandler block = self.options[SimpleAuthPresentInterfaceBlockKey];
block(login);
});
return nil;
}];
}
- (RACSignal *)accessTokenWithAuthorizationCode:(NSString *)code {
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
// Build parameters
NSDictionary *parameters = @{
@"code" : code,
@"client_id" : self.options[@"client_id"],
@"client_secret" : self.options[@"client_secret"],
@"redirect_uri" : self.options[SimpleAuthRedirectURIKey],
@"grant_type" : @"authorization_code"
};
// Build request
NSString *query = [CMDQueryStringSerialization queryStringWithDictionary:parameters];
NSURL *URL = [NSURL URLWithString:MAIL_CHIMP_ACCESS_TOKEN_URI];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[query dataUsingEncoding:NSUTF8StringEncoding]];
// Run request
[NSURLConnection sendAsynchronousRequest:request queue:self.operationQueue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(200, 99)];
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
if ([indexSet containsIndex:statusCode] && data) {
NSError *parseError = nil;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&parseError];
if (dictionary) {
[subscriber sendNext:dictionary];
[subscriber sendCompleted];
}
else {
[subscriber sendError:parseError];
}
}
else {
[subscriber sendError:connectionError];
}
}];
return nil;
}];
}
- (RACSignal *)accountWithAccessToken:(NSDictionary *)accessToken {
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
NSURL *URL = [NSURL URLWithString:MAIL_CHIMP_META_DATA_END_POINT_URI];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
NSString *oauthCode = [NSString stringWithFormat:@"OAuth %@", accessToken[@"access_token"]];
[request setValue:oauthCode forHTTPHeaderField:@"Authorization"];
[NSURLConnection sendAsynchronousRequest:request queue:self.operationQueue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(200, 99)];
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
if ([indexSet containsIndex:statusCode] && data) {
NSError *parseError = nil;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&parseError];
if (dictionary) {
[subscriber sendNext:dictionary];
[subscriber sendCompleted];
}
else {
[subscriber sendError:parseError];
}
}
else {
[subscriber sendError:connectionError];
}
}];
return nil;
}];
}
- (NSDictionary *)dictionaryWithAccount:(NSDictionary *)account accessToken:(NSDictionary *)accessToken {
NSMutableDictionary *dictionary = [NSMutableDictionary new];
// Provider
dictionary[@"provider"] = [[self class] type];
// Credentials
dictionary[@"credentials"] = @{
@"token" : accessToken[@"access_token"],
@"type" : @"authorization_code",
@"expires_in": accessToken[@"expires_in"],
};
// User ID
dictionary[@"id"] = account[@"id"];
// Raw response
dictionary[@"extra"] = @{
@"raw_info" : account
};
// User info
NSMutableDictionary *user = [NSMutableDictionary new];
user[@"login"] = account[@"login"];
user[@"name"] = account[@"name"];
dictionary[@"info"] = user;
return dictionary;
}
@end