Skip to content
This repository has been archived by the owner on Aug 16, 2023. It is now read-only.

Added support for working with Mail Chimp #119

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ PODS:
- SimpleAuth/Tumblr (= 0.3.9)
- SimpleAuth/Twitter (= 0.3.9)
- SimpleAuth/TwitterWeb (= 0.3.9)
- SimpleAuth/MailChimp (= 0.3.9)
- SimpleAuth/BoxWeb (0.3.9):
- SimpleAuth/Core
- SimpleAuth/Core (0.3.9):
Expand Down Expand Up @@ -72,6 +73,8 @@ PODS:
- SimpleAuth/TwitterWeb (0.3.9):
- cocoa-oauth
- SimpleAuth/Core
- SimpleAuth/MailChimp (0.3.9):
- SimpleAuth/Core

DEPENDENCIES:
- SimpleAuth (from `..`)
Expand Down
1 change: 1 addition & 0 deletions Example/SimpleAuth/Providers.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
<string>trello-web</string>
<string>box-web</string>
<string>onedrive-web</string>
<string>mailchimp</string>
</array>
</plist>
4 changes: 4 additions & 0 deletions Example/SimpleAuth/SADAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ - (void)configureAuthorizaionProviders {

// client_id and client_secret are required
SimpleAuth.configuration[@"onedrive-web"] = @{};

// client_id, client_secret, and redirect_uri are required
SimpleAuth.configuration[@"mailchimp"] = @{};

}


Expand Down
16 changes: 16 additions & 0 deletions Pod/Providers/MailChimp/SimpleAuthMailChimpConstants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// SimpleAuthMailChimpConstants.h
// SimpleAuth
//
// Created by Tal Kain <[email protected]>.
// Copyright (c) 2015 Fire Place Inc. All rights reserved.
//

#ifndef SimpleAuthMailChimpConstants_h
#define SimpleAuthMailChimpConstants_h

FOUNDATION_EXPORT NSString *const MAIL_CHIMP_AUTHORIZE_URI;
FOUNDATION_EXPORT NSString *const MAIL_CHIMP_ACCESS_TOKEN_URI;
FOUNDATION_EXPORT NSString *const MAIL_CHIMP_META_DATA_END_POINT_URI;

#endif /* SimpleAuthMailChimpConstants_h */
13 changes: 13 additions & 0 deletions Pod/Providers/MailChimp/SimpleAuthMailChimpConstants.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// SimpleAuthMailChimpConstants.m
// SimpleAuth
//
// Created by Tal Kain <[email protected]>.
// Copyright (c) 2015 Fire Place Inc. All rights reserved.
//

// Based on https://apidocs.mailchimp.com/oauth2/

NSString *const MAIL_CHIMP_AUTHORIZE_URI = @"https://login.mailchimp.com/oauth2/authorize";
NSString *const MAIL_CHIMP_ACCESS_TOKEN_URI = @"https://login.mailchimp.com/oauth2/token";
NSString *const MAIL_CHIMP_META_DATA_END_POINT_URI = @"https://login.mailchimp.com/oauth2/metadata";
14 changes: 14 additions & 0 deletions Pod/Providers/MailChimp/SimpleAuthMailChimpLoginViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// SimpleAuthMailChimpLoginViewController.h
// SimpleAuth
//
// Created by Tal Kain <[email protected]>.
// 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 "SimpleAuthWebViewController.h"

@interface SimpleAuthMailChimpLoginViewController : SimpleAuthWebViewController

@end
38 changes: 38 additions & 0 deletions Pod/Providers/MailChimp/SimpleAuthMailChimpLoginViewController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// SimpleAuthMailChimpLoginViewController.m
// SimpleAuth
//
// Created by Tal Kain <[email protected]>.
// 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 "SimpleAuthMailChimpLoginViewController.h"
#import "SimpleAuthMailChimpConstants.h"

@implementation SimpleAuthMailChimpLoginViewController

#pragma mark - SimpleAuthWebViewController

- (instancetype)initWithOptions:(NSDictionary *)options requestToken:(NSDictionary *)requestToken {
if ((self = [super initWithOptions:options requestToken:requestToken])) {
self.title = @"MailChimp";
}
return self;
}

- (NSURLRequest *)initialRequest {
NSDictionary *parameters = @{
@"client_id" : self.options[@"client_id"],
@"redirect_uri" : self.options[SimpleAuthRedirectURIKey],
@"response_type" : @"code"
};
NSString *URLString = [NSString stringWithFormat:@"%@?%@",
MAIL_CHIMP_AUTHORIZE_URI,
[CMDQueryStringSerialization queryStringWithDictionary:parameters]];
NSURL *URL = [NSURL URLWithString:URLString];

return [NSURLRequest requestWithURL:URL];
}

@end
14 changes: 14 additions & 0 deletions Pod/Providers/MailChimp/SimpleAuthMailChimpProvider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// SimpleAuthMailChimpProvider.h
// SimpleAuth
//
// Created by Tal Kain <[email protected]>.
// 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 "SimpleAuthProvider.h"

@interface SimpleAuthMailChimpProvider : SimpleAuthProvider

@end
210 changes: 210 additions & 0 deletions Pod/Providers/MailChimp/SimpleAuthMailChimpProvider.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
//
// SimpleAuthMailChimpProvider.m
// SimpleAuth
//
// Created by Tal Kain <[email protected]>.
// 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
6 changes: 6 additions & 0 deletions SimpleAuth.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,10 @@ Pod::Spec.new do |s|
ss.private_header_files = 'Pod/Providers/OneDriveWeb/*.h'
end

s.subspec 'MailChimp' do |ss|
ss.dependency 'SimpleAuth/Core'
ss.source_files = 'Pod/Providers/MailChimp'
ss.private_header_files = 'Pod/Providers/MailChimp/*.h'
end

end