Skip to content

Commit

Permalink
Add support for Square Register
Browse files Browse the repository at this point in the history
  • Loading branch information
Curtis Thorne committed Aug 3, 2016
1 parent a344d35 commit 9631c57
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Demo/ViewController.m
Expand Up @@ -40,12 +40,20 @@ - (void)loadView {
[uberButton addTarget:self action:@selector(uberPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:uberButton];

UIButton *squareButton = [UIButton buttonWithType:UIButtonTypeSystem];
squareButton.translatesAutoresizingMaskIntoConstraints = NO;
[squareButton setTitle:@"Login to Square" forState:UIControlStateNormal];
[squareButton addTarget:self action:@selector(squarePressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:squareButton];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:googleButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:googleButton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:0.8f constant:0.0f]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:slackButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:slackButton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:uberButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:uberButton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.2f constant:0.0f]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:squareButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:squareButton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.4f constant:0.0f]];
}

- (void)googlePressed:(nullable id)sender {
Expand All @@ -63,6 +71,19 @@ - (void)uberPressed:(nullable id)sender {
[self loginWithSessionManager:sessionManager scope:WFUberUserProfileScope redirectURI:[NSURL URLWithString:@"https://localhost"]];
}

- (void)squarePressed:(nullable id)sender {
NSString *clienetID = @"";
NSString *clientSecret = @"";
if (!clientSecret.length || !clientSecret.length) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Need client credentials" message:@"There was no clientID or client secret given." preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:nil]];
[self presentViewController:alertController animated:YES completion:nil];
return;
}
WFSquareOAuth2SessionManager *sessionManager = [[WFSquareOAuth2SessionManager alloc] initWithClientID:clienetID clientSecret:clientSecret];
[self loginWithSessionManager:sessionManager scope:WFSquareMerchantProfileReadScope redirectURI:nil];
}

- (void)loginWithSessionManager:(WFOAuth2SessionManager<WFOAuth2ProviderSessionManager> *)sessionManager scope:(nullable NSString *)scope redirectURI:(nullable NSURL *)redirectURI {
LoginViewController *loginViewController = [[LoginViewController alloc] initWithSessionManager:sessionManager scope:scope redirectURI:redirectURI];
loginViewController.delegate = self;
Expand Down
97 changes: 97 additions & 0 deletions Sources/WFOAuth2/WFSquareOAuth2SessionManager.m
@@ -0,0 +1,97 @@
//
// WFSquareOAuth2SessionManager.m
// WFOAuth2
//
// Created by Curtis Thorne on 7/26/16.
// Copyright © 2016 Conrad Kramer. All rights reserved.
//
#import <WFOAuth2/WFOAuth2SessionManagerPrivate.h>

#import <WFOAuth2/NSMutableURLRequest+WFOAuth2.h>
#import <WFOAuth2/WFSquareOAuth2SessionManager.h>


NS_ASSUME_NONNULL_BEGIN

NSString * const WFSquareMerchantProfileReadScope = @"MERCHANT_PROFILE_READ";
NSString * const WFSquarePaymentsReadScope = @"PAYMENTS_READ";
NSString * const WFSquarePaymentsWriteScope = @"PAYMENTS_WRITE";
NSString * const WFSquareCustomersReadScope = @"CUSTOMERS_READ";
NSString * const WFSquareCustomersWriteScope = @"CUSTOMERS_WRITE";
NSString * const WFSquareSettlementsReadScope = @"SETTLEMENTS_READ";
NSString * const WFSquareBankAccountsReadScope = @"BANK_ACCOUNTS_READ";
NSString * const WFSquareItemsReadScope = @"ITEMS_READ";
NSString * const WFSquareItemsWriteScope = @"ITEMS_WRITE";
NSString * const WFSquareOrdersReadScope = @"ORDERS_READ";
NSString * const WFSquareOrdersWriteScope = @"ORDERS_WRITE";
NSString * const WFSquareEmployeesReadScope = @"EMPLOYEES_READ";
NSString * const WFSquareEmployeesWriteScope = @"EMPLOYEES_WRITE";
NSString * const WFSquareTimecardsReadScope = @"TIMECARDS_READ";
NSString * const WFSquareTimecardsWriteScope = @"TIMECARDS_WRITE";

static NSString * const WFSquareOAuth2TokenPath = @"token";

@implementation WFSquareOAuth2SessionManager

- (instancetype)initWithClientID:(NSString *)clientID
clientSecret:(nullable NSString *)clientSecret {
return [self initWithSessionConfiguration:nil clientID:clientID clientSecret:clientSecret];
}

- (instancetype)initWithSessionConfiguration:(nullable NSURLSessionConfiguration *)configuration
clientID:(NSString *)clientID
clientSecret:(nullable NSString *)clientSecret {
return [super initWithSessionConfiguration:configuration baseURL:[NSURL URLWithString:@"https://connect.squareup.com/oauth2"] basicAuthEnabled:NO clientID:clientID clientSecret:clientSecret];
}

- (void)authenticateWithUsername:(NSString *)username
password:(NSString *)password
scope:(nullable NSString *)scope
completionHandler:(WFOAuth2AuthenticationHandler)completionHandler {
[super authenticateWithPath:WFSquareOAuth2TokenPath username:username password:password scope:scope completionHandler:completionHandler];
}

- (void)authenticateWithCode:(NSString *)code
redirectURI:(nullable NSURL *)redirectURI
completionHandler:(WFOAuth2AuthenticationHandler)completionHandler {
[super authenticateWithPath:WFSquareOAuth2TokenPath code:code redirectURI:redirectURI completionHandler:completionHandler];
}

- (void)authenticateWithRefreshCredential:(WFOAuth2Credential *)refreshCredential completionHandler:(WFOAuth2AuthenticationHandler)completionHandler {
[super authenticateWithPath:WFSquareOAuth2TokenPath refreshCredential:refreshCredential completionHandler:completionHandler];
}

- (void)revokeCredential:(WFOAuth2Credential *)credential completionHandler:(void (^__nullable)(BOOL success, NSError * __nullable error))completionHandler {
NSParameterAssert(credential);

NSArray<NSURLQueryItem *> *parameters = @[[NSURLQueryItem queryItemWithName:@"client_id" value:self.clientID],
[NSURLQueryItem queryItemWithName:@"access_token" value:credential.accessToken]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://connect.squareup.com/oauth2/revoke"]];
[request setHTTPMethod:@"POST"];

[request wfo_setBodyWithQueryItems:parameters];

if (self.clientSecret)
[request setValue:[NSString stringWithFormat:@"Authorization: Client %@", self.clientSecret] forHTTPHeaderField:@"Authorization"];

[[self.session dataTaskWithRequest:request completionHandler:^(NSData * __nullable __unused data, NSURLResponse * __nullable response, NSError * __nullable error) {
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
if (completionHandler)
completionHandler([[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(200, 100)] containsIndex:statusCode], error);
}] resume];
}

#if __has_include(<WebKit/WebKit.h>)

- (WKWebView *)authorizationWebViewWithScope:(nullable NSString *)scope
redirectURI:(nullable NSURL *)redirectURI
completionHandler:(WFOAuth2AuthenticationHandler)completionHandler {
return [super authorizationWebViewWithURL:[NSURL URLWithString:@"https://connect.squareup.com/oauth2/authorize"] scope:scope redirectURI:redirectURI tokenPath:WFSquareOAuth2TokenPath completionHandler:completionHandler];
}

#endif

@end

NS_ASSUME_NONNULL_END
1 change: 1 addition & 0 deletions Sources/WFOAuth2/include/WFOAuth2/WFOAuth2.h
Expand Up @@ -15,3 +15,4 @@
#import <WFOAuth2/WFGoogleOAuth2SessionManager.h>
#import <WFOAuth2/WFSlackOAuth2SessionManager.h>
#import <WFOAuth2/WFUberOAuth2SessionManager.h>
#import <WFOAuth2/WFSquareOAuth2SessionManager.h>
37 changes: 37 additions & 0 deletions Sources/WFOAuth2/include/WFOAuth2/WFSquareOAuth2SessionManager.h
@@ -0,0 +1,37 @@
//
// WFSquareOAuth2SessionManager.h
// WFOAuth2
//
// Created by Curtis Thorne on 7/26/16.
// Copyright © 2016 Conrad Kramer. All rights reserved.
//

#import <WFOAuth2/WFOAuth2ProviderSessionManager.h>
#import <WFOAuth2/WFOAuth2RevocableSessionManager.h>
#import <WFOAuth2/WFOAuth2Defines.h>

NS_ASSUME_NONNULL_BEGIN

WF_EXTERN NSString * const WFSquareMerchantProfileReadScope;
WF_EXTERN NSString * const WFSquarePaymentsReadScope;
WF_EXTERN NSString * const WFSquarePaymentsWriteScope;
WF_EXTERN NSString * const WFSquareCustomersReadScope;
WF_EXTERN NSString * const WFSquareCustomersWriteScope;
WF_EXTERN NSString * const WFSquareSettlementsReadScope;
WF_EXTERN NSString * const WFSquareBankAccountsReadScope;
WF_EXTERN NSString * const WFSquareItemsReadScope;
WF_EXTERN NSString * const WFSquareItemsWriteScope;
WF_EXTERN NSString * const WFSquareOrdersReadScope;
WF_EXTERN NSString * const WFSquareOrdersWriteScope;
WF_EXTERN NSString * const WFSquareEmployeesReadScope;
WF_EXTERN NSString * const WFSquareEmployeesWriteScope;
WF_EXTERN NSString * const WFSquareTimecardsWriteScope;


@interface WFSquareOAuth2SessionManager : WFOAuth2SessionManager <WFOAuth2ProviderSessionManager, WFOAuth2RevocableSessionManager>

- (instancetype)initWithClientID:(NSString *)clientID clientSecret:(nullable NSString *)clientSecret;

@end

NS_ASSUME_NONNULL_END
20 changes: 20 additions & 0 deletions WFOAuth2.xcodeproj/project.pbxproj
Expand Up @@ -99,6 +99,14 @@
D0B8F8161CA8C2D0006B1E0D /* WFOAuth2Defines.h in Headers */ = {isa = PBXBuildFile; fileRef = D0B8F8141CA8C2D0006B1E0D /* WFOAuth2Defines.h */; settings = {ATTRIBUTES = (Public, ); }; };
D0B8F8171CA8C2D0006B1E0D /* WFOAuth2Defines.h in Headers */ = {isa = PBXBuildFile; fileRef = D0B8F8141CA8C2D0006B1E0D /* WFOAuth2Defines.h */; settings = {ATTRIBUTES = (Public, ); }; };
D0B8F8181CA8C2D0006B1E0D /* WFOAuth2Defines.h in Headers */ = {isa = PBXBuildFile; fileRef = D0B8F8141CA8C2D0006B1E0D /* WFOAuth2Defines.h */; settings = {ATTRIBUTES = (Public, ); }; };
E9BD9FCD1D495159004BC387 /* WFSquareOAuth2SessionManager.m in Sources */ = {isa = PBXBuildFile; fileRef = E9BD9FC81D495159004BC387 /* WFSquareOAuth2SessionManager.m */; };
E9BD9FCE1D495159004BC387 /* WFSquareOAuth2SessionManager.m in Sources */ = {isa = PBXBuildFile; fileRef = E9BD9FC81D495159004BC387 /* WFSquareOAuth2SessionManager.m */; };
E9BD9FCF1D495159004BC387 /* WFSquareOAuth2SessionManager.m in Sources */ = {isa = PBXBuildFile; fileRef = E9BD9FC81D495159004BC387 /* WFSquareOAuth2SessionManager.m */; };
E9BD9FD01D495159004BC387 /* WFSquareOAuth2SessionManager.m in Sources */ = {isa = PBXBuildFile; fileRef = E9BD9FC81D495159004BC387 /* WFSquareOAuth2SessionManager.m */; };
E9BD9FD71D4962F1004BC387 /* WFSquareOAuth2SessionManager.h in Headers */ = {isa = PBXBuildFile; fileRef = E9BD9FD61D4962F1004BC387 /* WFSquareOAuth2SessionManager.h */; settings = {ATTRIBUTES = (Public, ); }; };
E9BD9FD81D4962F1004BC387 /* WFSquareOAuth2SessionManager.h in Headers */ = {isa = PBXBuildFile; fileRef = E9BD9FD61D4962F1004BC387 /* WFSquareOAuth2SessionManager.h */; settings = {ATTRIBUTES = (Public, ); }; };
E9BD9FD91D4962F1004BC387 /* WFSquareOAuth2SessionManager.h in Headers */ = {isa = PBXBuildFile; fileRef = E9BD9FD61D4962F1004BC387 /* WFSquareOAuth2SessionManager.h */; settings = {ATTRIBUTES = (Public, ); }; };
E9BD9FDA1D4962F1004BC387 /* WFSquareOAuth2SessionManager.h in Headers */ = {isa = PBXBuildFile; fileRef = E9BD9FD61D4962F1004BC387 /* WFSquareOAuth2SessionManager.h */; settings = {ATTRIBUTES = (Public, ); }; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -197,6 +205,8 @@
D0B8F80A1CA8BBA2006B1E0D /* WFUberOAuth2SessionManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = WFUberOAuth2SessionManager.h; path = include/WFOAuth2/WFUberOAuth2SessionManager.h; sourceTree = "<group>"; };
D0B8F80B1CA8BBA2006B1E0D /* WFUberOAuth2SessionManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WFUberOAuth2SessionManager.m; sourceTree = "<group>"; };
D0B8F8141CA8C2D0006B1E0D /* WFOAuth2Defines.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = WFOAuth2Defines.h; path = include/WFOAuth2/WFOAuth2Defines.h; sourceTree = "<group>"; };
E9BD9FC81D495159004BC387 /* WFSquareOAuth2SessionManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WFSquareOAuth2SessionManager.m; sourceTree = "<group>"; };
E9BD9FD61D4962F1004BC387 /* WFSquareOAuth2SessionManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = WFSquareOAuth2SessionManager.h; path = include/WFOAuth2/WFSquareOAuth2SessionManager.h; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -354,6 +364,8 @@
D044290F1CB65052005837F9 /* WFSlackOAuth2SessionManager.m */,
D0B8F80A1CA8BBA2006B1E0D /* WFUberOAuth2SessionManager.h */,
D0B8F80B1CA8BBA2006B1E0D /* WFUberOAuth2SessionManager.m */,
E9BD9FD61D4962F1004BC387 /* WFSquareOAuth2SessionManager.h */,
E9BD9FC81D495159004BC387 /* WFSquareOAuth2SessionManager.m */,
);
name = Providers;
sourceTree = "<group>";
Expand Down Expand Up @@ -428,6 +440,7 @@
D0B8F7841CA49B14006B1E0D /* NSMutableURLRequest+WFOAuth2.h in Headers */,
D0B8F77A1CA49840006B1E0D /* WFOAuth2Credential.h in Headers */,
D0B8F80C1CA8BBA2006B1E0D /* WFUberOAuth2SessionManager.h in Headers */,
E9BD9FD71D4962F1004BC387 /* WFSquareOAuth2SessionManager.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -447,6 +460,7 @@
D0B8F7851CA49B14006B1E0D /* NSMutableURLRequest+WFOAuth2.h in Headers */,
D0B8F77B1CA49840006B1E0D /* WFOAuth2Credential.h in Headers */,
D0B8F80D1CA8BBA2006B1E0D /* WFUberOAuth2SessionManager.h in Headers */,
E9BD9FD81D4962F1004BC387 /* WFSquareOAuth2SessionManager.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -466,6 +480,7 @@
D0B8F7861CA49B14006B1E0D /* NSMutableURLRequest+WFOAuth2.h in Headers */,
D0B8F77C1CA49840006B1E0D /* WFOAuth2Credential.h in Headers */,
D0B8F80E1CA8BBA2006B1E0D /* WFUberOAuth2SessionManager.h in Headers */,
E9BD9FD91D4962F1004BC387 /* WFSquareOAuth2SessionManager.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -485,6 +500,7 @@
D0B8F7871CA49B14006B1E0D /* NSMutableURLRequest+WFOAuth2.h in Headers */,
D0B8F77D1CA49840006B1E0D /* WFOAuth2Credential.h in Headers */,
D0B8F80F1CA8BBA2006B1E0D /* WFUberOAuth2SessionManager.h in Headers */,
E9BD9FDA1D4962F1004BC387 /* WFSquareOAuth2SessionManager.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -758,6 +774,7 @@
buildActionMask = 2147483647;
files = (
D0B8F77E1CA49840006B1E0D /* WFOAuth2Credential.m in Sources */,
E9BD9FCD1D495159004BC387 /* WFSquareOAuth2SessionManager.m in Sources */,
D0B8F7A71CA5FCE6006B1E0D /* WFGoogleOAuth2SessionManager.m in Sources */,
D0B8F7921CA49BF6006B1E0D /* WFOAuth2Error.m in Sources */,
D0B8F7881CA49B14006B1E0D /* NSMutableURLRequest+WFOAuth2.m in Sources */,
Expand All @@ -772,6 +789,7 @@
buildActionMask = 2147483647;
files = (
D0B8F77F1CA49840006B1E0D /* WFOAuth2Credential.m in Sources */,
E9BD9FCE1D495159004BC387 /* WFSquareOAuth2SessionManager.m in Sources */,
D0B8F7A81CA5FCE6006B1E0D /* WFGoogleOAuth2SessionManager.m in Sources */,
D0B8F7931CA49BF6006B1E0D /* WFOAuth2Error.m in Sources */,
D0B8F7891CA49B14006B1E0D /* NSMutableURLRequest+WFOAuth2.m in Sources */,
Expand All @@ -786,6 +804,7 @@
buildActionMask = 2147483647;
files = (
D0B8F7801CA49840006B1E0D /* WFOAuth2Credential.m in Sources */,
E9BD9FCF1D495159004BC387 /* WFSquareOAuth2SessionManager.m in Sources */,
D0B8F7A91CA5FCE6006B1E0D /* WFGoogleOAuth2SessionManager.m in Sources */,
D0B8F7941CA49BF6006B1E0D /* WFOAuth2Error.m in Sources */,
D0B8F78A1CA49B14006B1E0D /* NSMutableURLRequest+WFOAuth2.m in Sources */,
Expand All @@ -800,6 +819,7 @@
buildActionMask = 2147483647;
files = (
D0B8F7811CA49840006B1E0D /* WFOAuth2Credential.m in Sources */,
E9BD9FD01D495159004BC387 /* WFSquareOAuth2SessionManager.m in Sources */,
D0B8F7AA1CA5FCE6006B1E0D /* WFGoogleOAuth2SessionManager.m in Sources */,
D0B8F7951CA49BF6006B1E0D /* WFOAuth2Error.m in Sources */,
D0B8F78B1CA49B14006B1E0D /* NSMutableURLRequest+WFOAuth2.m in Sources */,
Expand Down

0 comments on commit 9631c57

Please sign in to comment.