Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Curtis Thorne
committed
Aug 3, 2016
1 parent
a344d35
commit 9631c57
Showing
5 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
Sources/WFOAuth2/include/WFOAuth2/WFSquareOAuth2SessionManager.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters