From b2de1e7227252f92b786c689d66d94306fcb6a4e Mon Sep 17 00:00:00 2001 From: Steven Sheldon Date: Sun, 28 Jan 2018 22:23:11 -0800 Subject: [PATCH 1/2] Replace OAuth 2.0 authentication with new API keys --- Classes/Client/YLPClient.h | 4 +- Classes/Client/YLPClient.m | 56 ++------------------- Classes/Client/YLPClientPrivate.h | 5 -- Example/YelpAPI/YLPAppDelegate.m | 9 +--- YelpAPITests/Client/YLPClientTestCase.m | 27 +--------- YelpAPITests/Client/YLPClientTestCaseBase.m | 2 +- 6 files changed, 9 insertions(+), 94 deletions(-) diff --git a/Classes/Client/YLPClient.h b/Classes/Client/YLPClient.h index adacd3b..5c0ba9a 100644 --- a/Classes/Client/YLPClient.h +++ b/Classes/Client/YLPClient.h @@ -16,9 +16,7 @@ extern NSString *const kYLPAPIHost; - (instancetype)init NS_UNAVAILABLE; -+ (void)authorizeWithAppId:(NSString *)appId - secret:(NSString *)secret - completionHandler:(void (^)(YLPClient *_Nullable client, NSError *_Nullable error))completionHandler; +- (instancetype)initWithAPIKey:(NSString *)APIKey; @end diff --git a/Classes/Client/YLPClient.m b/Classes/Client/YLPClient.m index 6f61acb..54b5d7d 100644 --- a/Classes/Client/YLPClient.m +++ b/Classes/Client/YLPClient.m @@ -14,7 +14,7 @@ NSString *const kYLPErrorDomain = @"com.yelp.YelpAPI.ErrorDomain"; @interface YLPClient () -@property (strong, nonatomic) NSString *accessToken; +@property (strong, nonatomic) NSString *APIKey; @end @implementation YLPClient @@ -23,9 +23,9 @@ - (instancetype)init { return nil; } -- (instancetype)initWithAccessToken:(NSString *)accessToken { +- (instancetype)initWithAPIKey:(NSString *)APIKey { if (self = [super init]) { - _accessToken = accessToken; + _APIKey = APIKey; } return self; } @@ -47,7 +47,7 @@ - (NSURLRequest *)requestWithPath:(NSString *)path params:(NSDictionary *)params NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:urlComponents.URL]; request.HTTPMethod = @"GET"; - NSString *authHeader = [NSString stringWithFormat:@"Bearer %@", self.accessToken]; + NSString *authHeader = [NSString stringWithFormat:@"Bearer %@", self.APIKey]; [request setValue:authHeader forHTTPHeaderField:@"Authorization"]; return request; @@ -93,52 +93,4 @@ + (void)queryWithRequest:(NSURLRequest *)request return queryItems; } -+ (NSCharacterSet *)URLEncodeAllowedCharacters { - // unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" - NSMutableCharacterSet *allowedCharacters = [[NSMutableCharacterSet alloc] init]; - [allowedCharacters addCharactersInRange:NSMakeRange((NSUInteger)'A', 26)]; - [allowedCharacters addCharactersInRange:NSMakeRange((NSUInteger)'a', 26)]; - [allowedCharacters addCharactersInRange:NSMakeRange((NSUInteger)'0', 10)]; - [allowedCharacters addCharactersInString:@"-._~"]; - return allowedCharacters; -} - -#pragma mark Authorization - -+ (NSURLRequest *)authRequestWithAppId:(NSString *)appId secret:(NSString *)secret { - NSURLComponents *urlComponents = [[NSURLComponents alloc] init]; - urlComponents.scheme = @"https"; - urlComponents.host = kYLPAPIHost; - urlComponents.path = @"/oauth2/token"; - - NSCharacterSet *allowedCharacters = [self URLEncodeAllowedCharacters]; - NSString *body = [NSString stringWithFormat:@"grant_type=client_credentials&client_id=%@&client_secret=%@", - [appId stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters], - [secret stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters]]; - NSData *bodyData = [body dataUsingEncoding:NSUTF8StringEncoding]; - - NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:urlComponents.URL]; - request.HTTPMethod = @"POST"; - request.HTTPBody = bodyData; - [request setValue:[NSString stringWithFormat:@"%zd", bodyData.length] forHTTPHeaderField:@"Content-Length"]; - [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; - - return request; -} - -+ (void)authorizeWithAppId:(NSString *)appId - secret:(NSString *)secret - completionHandler:(void (^)(YLPClient *client, NSError *error))completionHandler { - NSURLRequest *request = [self authRequestWithAppId:appId secret:secret]; - [self queryWithRequest:request completionHandler:^(NSDictionary *jsonResponse, NSError *error) { - if (error) { - completionHandler(nil, error); - } else { - NSString *accessToken = jsonResponse[@"access_token"]; - YLPClient *client = [[YLPClient alloc] initWithAccessToken:accessToken]; - completionHandler(client, nil); - } - }]; -} - @end diff --git a/Classes/Client/YLPClientPrivate.h b/Classes/Client/YLPClientPrivate.h index b98f864..ea21b99 100644 --- a/Classes/Client/YLPClientPrivate.h +++ b/Classes/Client/YLPClientPrivate.h @@ -13,15 +13,10 @@ extern NSString *const kYLPErrorDomain; @interface YLPClient () -- (instancetype)initWithAccessToken:(NSString *)accessToken; - - (NSURLRequest *)requestWithPath:(NSString *)path; - (NSURLRequest *)requestWithPath:(NSString *)path params:(nullable NSDictionary *)params; - (void)queryWithRequest:(NSURLRequest *)request completionHandler:(void (^)(NSDictionary *responseDict, NSError *error))completionHandler; -+ (NSCharacterSet *)URLEncodeAllowedCharacters; -+ (NSURLRequest *)authRequestWithAppId:(NSString *)appId secret:(NSString *)secret; - @end NS_ASSUME_NONNULL_END diff --git a/Example/YelpAPI/YLPAppDelegate.m b/Example/YelpAPI/YLPAppDelegate.m index 0a7af93..9d270ec 100644 --- a/Example/YelpAPI/YLPAppDelegate.m +++ b/Example/YelpAPI/YLPAppDelegate.m @@ -24,13 +24,8 @@ + (YLPClient *)sharedClient { #pragma mark UIApplicationDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { - #warning Fill in the API keys below with your developer v3 keys. - [YLPClient authorizeWithAppId:@"" secret:@"" completionHandler:^(YLPClient *client, NSError *error) { - self.client = client; - if (!client) { - NSLog(@"Authentication failed: %@", error); - } - }]; + #warning Fill in the API key below with your developer v3 key. + self.client = [[YLPClient alloc] initWithAPIKey:@""]; return YES; } diff --git a/YelpAPITests/Client/YLPClientTestCase.m b/YelpAPITests/Client/YLPClientTestCase.m index 72cc504..5895b0e 100644 --- a/YelpAPITests/Client/YLPClientTestCase.m +++ b/YelpAPITests/Client/YLPClientTestCase.m @@ -98,18 +98,11 @@ - (void)testRequestHeaders { NSURLRequest *request = [self.client requestWithPath:self.bogusTestPath]; XCTAssertEqualObjects(request.HTTPMethod, @"GET"); - XCTAssertEqualObjects([request valueForHTTPHeaderField:@"Authorization"], @"Bearer accessToken"); + XCTAssertEqualObjects([request valueForHTTPHeaderField:@"Authorization"], @"Bearer API_KEY"); XCTAssertEqualObjects(request.URL.absoluteString, @"https://api.yelp.com/bogusPath"); XCTAssertEqual(request.HTTPBody.length, 0); } -- (void)testURLEncode { - NSCharacterSet *allowedCharacters = [YLPClient URLEncodeAllowedCharacters]; - - XCTAssertEqualObjects([@"abAB01_.-~" stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters], @"abAB01_.-~"); - XCTAssertEqualObjects([@"ab=AB&01" stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters], @"ab%3DAB%2601"); -} - - (NSDictionary *)paramsFromQueryString:(NSString *)string { NSURLComponents *components = [[NSURLComponents alloc] init]; components.query = string; @@ -122,22 +115,4 @@ - (void)testURLEncode { return params; } -- (void)testAuthRequest { - NSURLRequest *authRequest = [YLPClient authRequestWithAppId:@"appId" secret:@"appSecret"]; - XCTAssertEqualObjects(authRequest.HTTPMethod, @"POST"); - XCTAssertEqualObjects(authRequest.URL.absoluteString, @"https://api.yelp.com/oauth2/token"); - - NSString *body = [[NSString alloc] initWithData:authRequest.HTTPBody encoding:NSUTF8StringEncoding]; - NSDictionary *bodyParams = [self paramsFromQueryString:body]; - NSDictionary *expectedBodyParams = @{ - @"grant_type": @"client_credentials", - @"client_id": @"appId", - @"client_secret": @"appSecret", - }; - XCTAssertEqualObjects(bodyParams, expectedBodyParams); - - XCTAssertNotNil([authRequest valueForHTTPHeaderField:@"Content-Length"]); - XCTAssertEqualObjects([authRequest valueForHTTPHeaderField:@"Content-Type"], @"application/x-www-form-urlencoded"); -} - @end diff --git a/YelpAPITests/Client/YLPClientTestCaseBase.m b/YelpAPITests/Client/YLPClientTestCaseBase.m index a77009b..add28d1 100644 --- a/YelpAPITests/Client/YLPClientTestCaseBase.m +++ b/YelpAPITests/Client/YLPClientTestCaseBase.m @@ -15,7 +15,7 @@ @implementation YLPClientTestCaseBase - (void)setUp { [super setUp]; - self.client = [[YLPClient alloc] initWithAccessToken:@"accessToken"]; + self.client = [[YLPClient alloc] initWithAPIKey:@"API_KEY"]; self.bogusTestPath = @"/bogusPath"; } From 5e361721d20121afa2b1926d9ce60fe1ad103f58 Mon Sep 17 00:00:00 2001 From: Steven Sheldon Date: Sun, 28 Jan 2018 22:24:27 -0800 Subject: [PATCH 2/2] Bump to version 3.0.0 --- YelpAPI.podspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/YelpAPI.podspec b/YelpAPI.podspec index 301dd25..0f59e50 100644 --- a/YelpAPI.podspec +++ b/YelpAPI.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = "YelpAPI" - s.version = "2.0.0" + s.version = "3.0.0" s.summary = "Objective-C client library for accessing the Yelp Public API." s.description = <<-DESC