Skip to content

Commit

Permalink
Merge pull request #74 from Yelp/ssheldon_api_key
Browse files Browse the repository at this point in the history
Replace OAuth 2 authentication with new API keys
  • Loading branch information
SSheldon authored Jan 29, 2018
2 parents fe7ba9f + 5e36172 commit 1b8ee6d
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 95 deletions.
4 changes: 1 addition & 3 deletions Classes/Client/YLPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
56 changes: 4 additions & 52 deletions Classes/Client/YLPClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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
5 changes: 0 additions & 5 deletions Classes/Client/YLPClientPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 2 additions & 7 deletions Example/YelpAPI/YLPAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion YelpAPI.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 1 addition & 26 deletions YelpAPITests/Client/YLPClientTestCase.m
Original file line number Diff line number Diff line change
Expand Up @@ -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<NSString *, NSString *> *)paramsFromQueryString:(NSString *)string {
NSURLComponents *components = [[NSURLComponents alloc] init];
components.query = string;
Expand All @@ -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
2 changes: 1 addition & 1 deletion YelpAPITests/Client/YLPClientTestCaseBase.m
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

Expand Down

0 comments on commit 1b8ee6d

Please sign in to comment.