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

Constant Contact provider #120

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 @@ -15,6 +15,7 @@ PODS:
- SimpleAuth (0.3.9):
- SimpleAuth/BoxWeb (= 0.3.9)
- SimpleAuth/Core (= 0.3.9)
- SimpleAuth/ConstantContact (= 0.3.9)
- SimpleAuth/DropboxWeb (= 0.3.9)
- SimpleAuth/Facebook (= 0.3.9)
- SimpleAuth/FacebookWeb (= 0.3.9)
Expand All @@ -36,6 +37,8 @@ PODS:
- SimpleAuth/Core (0.3.9):
- CMDQueryStringSerialization
- ReactiveCocoa
- SimpleAuth/ConstantContact (0.3.9):
- SimpleAuth/Core
- SimpleAuth/DropboxWeb (0.3.9):
- SimpleAuth/Core
- SimpleAuth/Facebook (0.3.9):
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>constantcontact</string>
</array>
</plist>
3 changes: 3 additions & 0 deletions Example/SimpleAuth/SADAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ - (void)configureAuthorizaionProviders {

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

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


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

#ifndef SimpleAuthConstantContactConstants_h
#define SimpleAuthConstantContactConstants_h

FOUNDATION_EXPORT NSString *const CONSTANT_CONTACT_AUTHORIZE_URI;
FOUNDATION_EXPORT NSString *const CONSTANT_CONTACT_INFO_END_POINT_URI;

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

// Based on https://developer.constantcontact.com/docs/authentication/oauth-2.0-client-flow.html

NSString *const CONSTANT_CONTACT_AUTHORIZE_URI = @"https://oauth2.constantcontact.com/oauth2/oauth/siteowner/authorize";
NSString *const CONSTANT_CONTACT_INFO_END_POINT_URI = @"https://api.constantcontact.com/v2/account/info";
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// SimpleAuthConstantContactLoginViewController.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 SimpleAuthConstantContactLoginViewController : SimpleAuthWebViewController

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// SimpleAuthConstantContactLoginViewController.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 "SimpleAuthConstantContactLoginViewController.h"
#import "SimpleAuthConstantContactConstants.h"

@implementation SimpleAuthConstantContactLoginViewController

#pragma mark - SimpleAuthWebViewController

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

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

return [NSURLRequest requestWithURL:URL];
}

@end
14 changes: 14 additions & 0 deletions Pod/Providers/ConstantContact/SimpleAuthConstantContactProvider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// SimpleAuthConstantContactProvider.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 SimpleAuthConstantContactProvider : SimpleAuthProvider

@end
162 changes: 162 additions & 0 deletions Pod/Providers/ConstantContact/SimpleAuthConstantContactProvider.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
//
// SimpleAuthConstantContactProvider.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 "SimpleAuthConstantContactProvider.h"
#import "SimpleAuthConstantContactLoginViewController.h"
#import "SimpleAuthConstantContactConstants.h"

#import <ReactiveCocoa/ReactiveCocoa.h>
#import "UIViewController+SimpleAuthAdditions.h"

@implementation SimpleAuthConstantContactProvider

#pragma mark - SimpleAuthProvider

+ (NSString *)type {
return @"constantcontact";
}


+ (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 [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
dispatch_async(dispatch_get_main_queue(), ^{
SimpleAuthConstantContactLoginViewController *login = [[SimpleAuthConstantContactLoginViewController alloc] initWithOptions:self.options];
login.completion = ^(UIViewController *login, NSURL *URL, NSError *error) {
SimpleAuthInterfaceHandler dismissBlock = self.options[SimpleAuthDismissInterfaceBlockKey];
dismissBlock(login);

// Parse URL
NSString *fragment = [URL fragment];
NSDictionary *dictionary = [CMDQueryStringSerialization dictionaryWithQueryString:fragment];
NSString *token = dictionary[@"access_token"];

// Check for error
if (![token length]) {
[subscriber sendError:error];
return;
}

// Send completion
[subscriber sendNext:token];
[subscriber sendCompleted];
};

SimpleAuthInterfaceHandler block = self.options[SimpleAuthPresentInterfaceBlockKey];
block(login);
});
return nil;
}];
}

- (RACSignal *)accountWithAccessToken:(NSDictionary *)accessToken {
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
NSDictionary *parameters = @{ @"api_key" : self.options[@"client_id"] };
NSString *query = [CMDQueryStringSerialization queryStringWithDictionary:parameters];
NSString *URLString = [NSString stringWithFormat:@"%@?%@", CONSTANT_CONTACT_INFO_END_POINT_URI, query];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URLString]];
NSString *oauthCode = [NSString stringWithFormat:@"Bearer %@", accessToken];
[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,
@"type" : @"token",
};

// User ID
dictionary[@"id"] = account[@"email"];

// Raw response
dictionary[@"extra"] = @{
@"raw_info" : account
};

// User info
NSMutableDictionary *user = [NSMutableDictionary new];
user[@"login"] = account[@"email"];
user[@"name"] = [NSString stringWithFormat:@"%@ %@", account[@"first_name"], account[@"last_name"]];
user[@"phone"] = account[@"phone"];
dictionary[@"info"] = user;

return dictionary;
}

@end
5 changes: 5 additions & 0 deletions SimpleAuth.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,9 @@ Pod::Spec.new do |s|
ss.private_header_files = 'Pod/Providers/OneDriveWeb/*.h'
end

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