Skip to content

Commit 9631c57

Browse files
author
Curtis Thorne
committed
Add support for Square Register
1 parent a344d35 commit 9631c57

File tree

5 files changed

+176
-0
lines changed

5 files changed

+176
-0
lines changed

Demo/ViewController.m

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,20 @@ - (void)loadView {
4040
[uberButton addTarget:self action:@selector(uberPressed:) forControlEvents:UIControlEventTouchUpInside];
4141
[self.view addSubview:uberButton];
4242

43+
UIButton *squareButton = [UIButton buttonWithType:UIButtonTypeSystem];
44+
squareButton.translatesAutoresizingMaskIntoConstraints = NO;
45+
[squareButton setTitle:@"Login to Square" forState:UIControlStateNormal];
46+
[squareButton addTarget:self action:@selector(squarePressed:) forControlEvents:UIControlEventTouchUpInside];
47+
[self.view addSubview:squareButton];
48+
4349
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:googleButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f]];
4450
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:googleButton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:0.8f constant:0.0f]];
4551
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:slackButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f]];
4652
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:slackButton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f]];
4753
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:uberButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f]];
4854
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:uberButton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.2f constant:0.0f]];
55+
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:squareButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f]];
56+
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:squareButton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.4f constant:0.0f]];
4957
}
5058

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

74+
- (void)squarePressed:(nullable id)sender {
75+
NSString *clienetID = @"";
76+
NSString *clientSecret = @"";
77+
if (!clientSecret.length || !clientSecret.length) {
78+
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Need client credentials" message:@"There was no clientID or client secret given." preferredStyle:UIAlertControllerStyleAlert];
79+
[alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:nil]];
80+
[self presentViewController:alertController animated:YES completion:nil];
81+
return;
82+
}
83+
WFSquareOAuth2SessionManager *sessionManager = [[WFSquareOAuth2SessionManager alloc] initWithClientID:clienetID clientSecret:clientSecret];
84+
[self loginWithSessionManager:sessionManager scope:WFSquareMerchantProfileReadScope redirectURI:nil];
85+
}
86+
6687
- (void)loginWithSessionManager:(WFOAuth2SessionManager<WFOAuth2ProviderSessionManager> *)sessionManager scope:(nullable NSString *)scope redirectURI:(nullable NSURL *)redirectURI {
6788
LoginViewController *loginViewController = [[LoginViewController alloc] initWithSessionManager:sessionManager scope:scope redirectURI:redirectURI];
6889
loginViewController.delegate = self;
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
//
2+
// WFSquareOAuth2SessionManager.m
3+
// WFOAuth2
4+
//
5+
// Created by Curtis Thorne on 7/26/16.
6+
// Copyright © 2016 Conrad Kramer. All rights reserved.
7+
//
8+
#import <WFOAuth2/WFOAuth2SessionManagerPrivate.h>
9+
10+
#import <WFOAuth2/NSMutableURLRequest+WFOAuth2.h>
11+
#import <WFOAuth2/WFSquareOAuth2SessionManager.h>
12+
13+
14+
NS_ASSUME_NONNULL_BEGIN
15+
16+
NSString * const WFSquareMerchantProfileReadScope = @"MERCHANT_PROFILE_READ";
17+
NSString * const WFSquarePaymentsReadScope = @"PAYMENTS_READ";
18+
NSString * const WFSquarePaymentsWriteScope = @"PAYMENTS_WRITE";
19+
NSString * const WFSquareCustomersReadScope = @"CUSTOMERS_READ";
20+
NSString * const WFSquareCustomersWriteScope = @"CUSTOMERS_WRITE";
21+
NSString * const WFSquareSettlementsReadScope = @"SETTLEMENTS_READ";
22+
NSString * const WFSquareBankAccountsReadScope = @"BANK_ACCOUNTS_READ";
23+
NSString * const WFSquareItemsReadScope = @"ITEMS_READ";
24+
NSString * const WFSquareItemsWriteScope = @"ITEMS_WRITE";
25+
NSString * const WFSquareOrdersReadScope = @"ORDERS_READ";
26+
NSString * const WFSquareOrdersWriteScope = @"ORDERS_WRITE";
27+
NSString * const WFSquareEmployeesReadScope = @"EMPLOYEES_READ";
28+
NSString * const WFSquareEmployeesWriteScope = @"EMPLOYEES_WRITE";
29+
NSString * const WFSquareTimecardsReadScope = @"TIMECARDS_READ";
30+
NSString * const WFSquareTimecardsWriteScope = @"TIMECARDS_WRITE";
31+
32+
static NSString * const WFSquareOAuth2TokenPath = @"token";
33+
34+
@implementation WFSquareOAuth2SessionManager
35+
36+
- (instancetype)initWithClientID:(NSString *)clientID
37+
clientSecret:(nullable NSString *)clientSecret {
38+
return [self initWithSessionConfiguration:nil clientID:clientID clientSecret:clientSecret];
39+
}
40+
41+
- (instancetype)initWithSessionConfiguration:(nullable NSURLSessionConfiguration *)configuration
42+
clientID:(NSString *)clientID
43+
clientSecret:(nullable NSString *)clientSecret {
44+
return [super initWithSessionConfiguration:configuration baseURL:[NSURL URLWithString:@"https://connect.squareup.com/oauth2"] basicAuthEnabled:NO clientID:clientID clientSecret:clientSecret];
45+
}
46+
47+
- (void)authenticateWithUsername:(NSString *)username
48+
password:(NSString *)password
49+
scope:(nullable NSString *)scope
50+
completionHandler:(WFOAuth2AuthenticationHandler)completionHandler {
51+
[super authenticateWithPath:WFSquareOAuth2TokenPath username:username password:password scope:scope completionHandler:completionHandler];
52+
}
53+
54+
- (void)authenticateWithCode:(NSString *)code
55+
redirectURI:(nullable NSURL *)redirectURI
56+
completionHandler:(WFOAuth2AuthenticationHandler)completionHandler {
57+
[super authenticateWithPath:WFSquareOAuth2TokenPath code:code redirectURI:redirectURI completionHandler:completionHandler];
58+
}
59+
60+
- (void)authenticateWithRefreshCredential:(WFOAuth2Credential *)refreshCredential completionHandler:(WFOAuth2AuthenticationHandler)completionHandler {
61+
[super authenticateWithPath:WFSquareOAuth2TokenPath refreshCredential:refreshCredential completionHandler:completionHandler];
62+
}
63+
64+
- (void)revokeCredential:(WFOAuth2Credential *)credential completionHandler:(void (^__nullable)(BOOL success, NSError * __nullable error))completionHandler {
65+
NSParameterAssert(credential);
66+
67+
NSArray<NSURLQueryItem *> *parameters = @[[NSURLQueryItem queryItemWithName:@"client_id" value:self.clientID],
68+
[NSURLQueryItem queryItemWithName:@"access_token" value:credential.accessToken]];
69+
70+
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://connect.squareup.com/oauth2/revoke"]];
71+
[request setHTTPMethod:@"POST"];
72+
73+
[request wfo_setBodyWithQueryItems:parameters];
74+
75+
if (self.clientSecret)
76+
[request setValue:[NSString stringWithFormat:@"Authorization: Client %@", self.clientSecret] forHTTPHeaderField:@"Authorization"];
77+
78+
[[self.session dataTaskWithRequest:request completionHandler:^(NSData * __nullable __unused data, NSURLResponse * __nullable response, NSError * __nullable error) {
79+
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
80+
if (completionHandler)
81+
completionHandler([[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(200, 100)] containsIndex:statusCode], error);
82+
}] resume];
83+
}
84+
85+
#if __has_include(<WebKit/WebKit.h>)
86+
87+
- (WKWebView *)authorizationWebViewWithScope:(nullable NSString *)scope
88+
redirectURI:(nullable NSURL *)redirectURI
89+
completionHandler:(WFOAuth2AuthenticationHandler)completionHandler {
90+
return [super authorizationWebViewWithURL:[NSURL URLWithString:@"https://connect.squareup.com/oauth2/authorize"] scope:scope redirectURI:redirectURI tokenPath:WFSquareOAuth2TokenPath completionHandler:completionHandler];
91+
}
92+
93+
#endif
94+
95+
@end
96+
97+
NS_ASSUME_NONNULL_END

Sources/WFOAuth2/include/WFOAuth2/WFOAuth2.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
#import <WFOAuth2/WFGoogleOAuth2SessionManager.h>
1616
#import <WFOAuth2/WFSlackOAuth2SessionManager.h>
1717
#import <WFOAuth2/WFUberOAuth2SessionManager.h>
18+
#import <WFOAuth2/WFSquareOAuth2SessionManager.h>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// WFSquareOAuth2SessionManager.h
3+
// WFOAuth2
4+
//
5+
// Created by Curtis Thorne on 7/26/16.
6+
// Copyright © 2016 Conrad Kramer. All rights reserved.
7+
//
8+
9+
#import <WFOAuth2/WFOAuth2ProviderSessionManager.h>
10+
#import <WFOAuth2/WFOAuth2RevocableSessionManager.h>
11+
#import <WFOAuth2/WFOAuth2Defines.h>
12+
13+
NS_ASSUME_NONNULL_BEGIN
14+
15+
WF_EXTERN NSString * const WFSquareMerchantProfileReadScope;
16+
WF_EXTERN NSString * const WFSquarePaymentsReadScope;
17+
WF_EXTERN NSString * const WFSquarePaymentsWriteScope;
18+
WF_EXTERN NSString * const WFSquareCustomersReadScope;
19+
WF_EXTERN NSString * const WFSquareCustomersWriteScope;
20+
WF_EXTERN NSString * const WFSquareSettlementsReadScope;
21+
WF_EXTERN NSString * const WFSquareBankAccountsReadScope;
22+
WF_EXTERN NSString * const WFSquareItemsReadScope;
23+
WF_EXTERN NSString * const WFSquareItemsWriteScope;
24+
WF_EXTERN NSString * const WFSquareOrdersReadScope;
25+
WF_EXTERN NSString * const WFSquareOrdersWriteScope;
26+
WF_EXTERN NSString * const WFSquareEmployeesReadScope;
27+
WF_EXTERN NSString * const WFSquareEmployeesWriteScope;
28+
WF_EXTERN NSString * const WFSquareTimecardsWriteScope;
29+
30+
31+
@interface WFSquareOAuth2SessionManager : WFOAuth2SessionManager <WFOAuth2ProviderSessionManager, WFOAuth2RevocableSessionManager>
32+
33+
- (instancetype)initWithClientID:(NSString *)clientID clientSecret:(nullable NSString *)clientSecret;
34+
35+
@end
36+
37+
NS_ASSUME_NONNULL_END

WFOAuth2.xcodeproj/project.pbxproj

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@
9999
D0B8F8161CA8C2D0006B1E0D /* WFOAuth2Defines.h in Headers */ = {isa = PBXBuildFile; fileRef = D0B8F8141CA8C2D0006B1E0D /* WFOAuth2Defines.h */; settings = {ATTRIBUTES = (Public, ); }; };
100100
D0B8F8171CA8C2D0006B1E0D /* WFOAuth2Defines.h in Headers */ = {isa = PBXBuildFile; fileRef = D0B8F8141CA8C2D0006B1E0D /* WFOAuth2Defines.h */; settings = {ATTRIBUTES = (Public, ); }; };
101101
D0B8F8181CA8C2D0006B1E0D /* WFOAuth2Defines.h in Headers */ = {isa = PBXBuildFile; fileRef = D0B8F8141CA8C2D0006B1E0D /* WFOAuth2Defines.h */; settings = {ATTRIBUTES = (Public, ); }; };
102+
E9BD9FCD1D495159004BC387 /* WFSquareOAuth2SessionManager.m in Sources */ = {isa = PBXBuildFile; fileRef = E9BD9FC81D495159004BC387 /* WFSquareOAuth2SessionManager.m */; };
103+
E9BD9FCE1D495159004BC387 /* WFSquareOAuth2SessionManager.m in Sources */ = {isa = PBXBuildFile; fileRef = E9BD9FC81D495159004BC387 /* WFSquareOAuth2SessionManager.m */; };
104+
E9BD9FCF1D495159004BC387 /* WFSquareOAuth2SessionManager.m in Sources */ = {isa = PBXBuildFile; fileRef = E9BD9FC81D495159004BC387 /* WFSquareOAuth2SessionManager.m */; };
105+
E9BD9FD01D495159004BC387 /* WFSquareOAuth2SessionManager.m in Sources */ = {isa = PBXBuildFile; fileRef = E9BD9FC81D495159004BC387 /* WFSquareOAuth2SessionManager.m */; };
106+
E9BD9FD71D4962F1004BC387 /* WFSquareOAuth2SessionManager.h in Headers */ = {isa = PBXBuildFile; fileRef = E9BD9FD61D4962F1004BC387 /* WFSquareOAuth2SessionManager.h */; settings = {ATTRIBUTES = (Public, ); }; };
107+
E9BD9FD81D4962F1004BC387 /* WFSquareOAuth2SessionManager.h in Headers */ = {isa = PBXBuildFile; fileRef = E9BD9FD61D4962F1004BC387 /* WFSquareOAuth2SessionManager.h */; settings = {ATTRIBUTES = (Public, ); }; };
108+
E9BD9FD91D4962F1004BC387 /* WFSquareOAuth2SessionManager.h in Headers */ = {isa = PBXBuildFile; fileRef = E9BD9FD61D4962F1004BC387 /* WFSquareOAuth2SessionManager.h */; settings = {ATTRIBUTES = (Public, ); }; };
109+
E9BD9FDA1D4962F1004BC387 /* WFSquareOAuth2SessionManager.h in Headers */ = {isa = PBXBuildFile; fileRef = E9BD9FD61D4962F1004BC387 /* WFSquareOAuth2SessionManager.h */; settings = {ATTRIBUTES = (Public, ); }; };
102110
/* End PBXBuildFile section */
103111

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

202212
/* Begin PBXFrameworksBuildPhase section */
@@ -354,6 +364,8 @@
354364
D044290F1CB65052005837F9 /* WFSlackOAuth2SessionManager.m */,
355365
D0B8F80A1CA8BBA2006B1E0D /* WFUberOAuth2SessionManager.h */,
356366
D0B8F80B1CA8BBA2006B1E0D /* WFUberOAuth2SessionManager.m */,
367+
E9BD9FD61D4962F1004BC387 /* WFSquareOAuth2SessionManager.h */,
368+
E9BD9FC81D495159004BC387 /* WFSquareOAuth2SessionManager.m */,
357369
);
358370
name = Providers;
359371
sourceTree = "<group>";
@@ -428,6 +440,7 @@
428440
D0B8F7841CA49B14006B1E0D /* NSMutableURLRequest+WFOAuth2.h in Headers */,
429441
D0B8F77A1CA49840006B1E0D /* WFOAuth2Credential.h in Headers */,
430442
D0B8F80C1CA8BBA2006B1E0D /* WFUberOAuth2SessionManager.h in Headers */,
443+
E9BD9FD71D4962F1004BC387 /* WFSquareOAuth2SessionManager.h in Headers */,
431444
);
432445
runOnlyForDeploymentPostprocessing = 0;
433446
};
@@ -447,6 +460,7 @@
447460
D0B8F7851CA49B14006B1E0D /* NSMutableURLRequest+WFOAuth2.h in Headers */,
448461
D0B8F77B1CA49840006B1E0D /* WFOAuth2Credential.h in Headers */,
449462
D0B8F80D1CA8BBA2006B1E0D /* WFUberOAuth2SessionManager.h in Headers */,
463+
E9BD9FD81D4962F1004BC387 /* WFSquareOAuth2SessionManager.h in Headers */,
450464
);
451465
runOnlyForDeploymentPostprocessing = 0;
452466
};
@@ -466,6 +480,7 @@
466480
D0B8F7861CA49B14006B1E0D /* NSMutableURLRequest+WFOAuth2.h in Headers */,
467481
D0B8F77C1CA49840006B1E0D /* WFOAuth2Credential.h in Headers */,
468482
D0B8F80E1CA8BBA2006B1E0D /* WFUberOAuth2SessionManager.h in Headers */,
483+
E9BD9FD91D4962F1004BC387 /* WFSquareOAuth2SessionManager.h in Headers */,
469484
);
470485
runOnlyForDeploymentPostprocessing = 0;
471486
};
@@ -485,6 +500,7 @@
485500
D0B8F7871CA49B14006B1E0D /* NSMutableURLRequest+WFOAuth2.h in Headers */,
486501
D0B8F77D1CA49840006B1E0D /* WFOAuth2Credential.h in Headers */,
487502
D0B8F80F1CA8BBA2006B1E0D /* WFUberOAuth2SessionManager.h in Headers */,
503+
E9BD9FDA1D4962F1004BC387 /* WFSquareOAuth2SessionManager.h in Headers */,
488504
);
489505
runOnlyForDeploymentPostprocessing = 0;
490506
};
@@ -758,6 +774,7 @@
758774
buildActionMask = 2147483647;
759775
files = (
760776
D0B8F77E1CA49840006B1E0D /* WFOAuth2Credential.m in Sources */,
777+
E9BD9FCD1D495159004BC387 /* WFSquareOAuth2SessionManager.m in Sources */,
761778
D0B8F7A71CA5FCE6006B1E0D /* WFGoogleOAuth2SessionManager.m in Sources */,
762779
D0B8F7921CA49BF6006B1E0D /* WFOAuth2Error.m in Sources */,
763780
D0B8F7881CA49B14006B1E0D /* NSMutableURLRequest+WFOAuth2.m in Sources */,
@@ -772,6 +789,7 @@
772789
buildActionMask = 2147483647;
773790
files = (
774791
D0B8F77F1CA49840006B1E0D /* WFOAuth2Credential.m in Sources */,
792+
E9BD9FCE1D495159004BC387 /* WFSquareOAuth2SessionManager.m in Sources */,
775793
D0B8F7A81CA5FCE6006B1E0D /* WFGoogleOAuth2SessionManager.m in Sources */,
776794
D0B8F7931CA49BF6006B1E0D /* WFOAuth2Error.m in Sources */,
777795
D0B8F7891CA49B14006B1E0D /* NSMutableURLRequest+WFOAuth2.m in Sources */,
@@ -786,6 +804,7 @@
786804
buildActionMask = 2147483647;
787805
files = (
788806
D0B8F7801CA49840006B1E0D /* WFOAuth2Credential.m in Sources */,
807+
E9BD9FCF1D495159004BC387 /* WFSquareOAuth2SessionManager.m in Sources */,
789808
D0B8F7A91CA5FCE6006B1E0D /* WFGoogleOAuth2SessionManager.m in Sources */,
790809
D0B8F7941CA49BF6006B1E0D /* WFOAuth2Error.m in Sources */,
791810
D0B8F78A1CA49B14006B1E0D /* NSMutableURLRequest+WFOAuth2.m in Sources */,
@@ -800,6 +819,7 @@
800819
buildActionMask = 2147483647;
801820
files = (
802821
D0B8F7811CA49840006B1E0D /* WFOAuth2Credential.m in Sources */,
822+
E9BD9FD01D495159004BC387 /* WFSquareOAuth2SessionManager.m in Sources */,
803823
D0B8F7AA1CA5FCE6006B1E0D /* WFGoogleOAuth2SessionManager.m in Sources */,
804824
D0B8F7951CA49BF6006B1E0D /* WFOAuth2Error.m in Sources */,
805825
D0B8F78B1CA49B14006B1E0D /* NSMutableURLRequest+WFOAuth2.m in Sources */,

0 commit comments

Comments
 (0)