From ec53484dcf17c03e7045ff10168b0bf3e8a6328a Mon Sep 17 00:00:00 2001 From: Bradley Bain Date: Fri, 12 Jan 2018 17:15:59 -0600 Subject: [PATCH 1/3] add Spotify auth provider --- lib/authProviders.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/authProviders.js b/lib/authProviders.js index f104b05..21601b1 100644 --- a/lib/authProviders.js +++ b/lib/authProviders.js @@ -73,7 +73,18 @@ export const authProviders = { client_id: [notEmpty], client_secret: [notEmpty] }) - } + }, + 'spotify': { + auth_version: "2.0", + authorize_url: 'https://accounts.spotify.com/authorize', + api_url: 'https://api.spotify.com/', + callback_url: ({app_name}) => `${app_name}://authorize`, + + validate: validate({ + client_id: [notEmpty], + client_secret: [notEmpty] + }) + }, } -export default authProviders; \ No newline at end of file +export default authProviders; From e93162dea2089b98674614aff17a6be1988898cd Mon Sep 17 00:00:00 2001 From: Bradley Bain Date: Fri, 12 Jan 2018 17:24:06 -0600 Subject: [PATCH 2/3] Allow json body in POST / PUT requests --- ios/OAuthManager/OAuthManager.m | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/ios/OAuthManager/OAuthManager.m b/ios/OAuthManager/OAuthManager.m index a9451bc..6953c50 100644 --- a/ios/OAuthManager/OAuthManager.m +++ b/ios/OAuthManager/OAuthManager.m @@ -443,12 +443,18 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name URL:apiUrl items:items]; + // Allow json body in POST / PUT requests + NSDictionary *body = [opts objectForKey:@"body"]; if (body != nil) { - for (NSString *key in body) { - NSData *data = [[NSString stringWithFormat:@"%@", [body valueForKey:key]] dataUsingEncoding:NSUTF8StringEncoding]; - [request addMultiPartData:data withName:key type:@"application/json"]; // TODO: How should we handle different body types? - } + /*for (NSString *key in body) { + NSString *value = [body valueForKey:key]; + + if(value != nil) { + [request.content.HTTPBody addObject:]; + } + }*/ + [request.content setHTTPBody: body]; } request.account = existingAccount; From 2c475ab01bd6670e207f874ace0f1a54334f6189 Mon Sep 17 00:00:00 2001 From: Bradley Bain Date: Fri, 12 Jan 2018 19:36:12 -0600 Subject: [PATCH 3/3] Enable json body parsing --- ios/OAuthManager/OAuthManager.m | 39 +++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/ios/OAuthManager/OAuthManager.m b/ios/OAuthManager/OAuthManager.m index 6953c50..300f3de 100644 --- a/ios/OAuthManager/OAuthManager.m +++ b/ios/OAuthManager/OAuthManager.m @@ -153,7 +153,7 @@ - (BOOL) _configureProvider:(NSString *)providerName andConfig:(NSDictionary *)c _callbackUrls = [arr copy]; NSLog(@"Saved callback url: %@ in %@", saveCallbackUrl, _callbackUrls); } - + // Convert objects of url type for (NSString *name in [config allKeys]) { @@ -354,14 +354,14 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name [manager addPending:client]; _pendingAuthentication = YES; - + NSLog(@"Calling authorizeWithUrl: %@ with callbackURL: %@\n %@", providerName, callbackUrl, cfg); [client authorizeWithUrl:providerName url:callbackUrl cfg:cfg onSuccess:^(DCTAuthAccount *account) { - NSLog(@"on success called with account: %@", account); + NSLog(@"on success called with account: %@", account); NSDictionary *accountResponse = [manager getAccountResponse:account cfg:cfg]; _pendingAuthentication = NO; [manager removePending:client]; @@ -444,21 +444,26 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name items:items]; // Allow json body in POST / PUT requests - NSDictionary *body = [opts objectForKey:@"body"]; if (body != nil) { - /*for (NSString *key in body) { + NSMutableArray *items = [NSMutableArray array]; + + for (NSString *key in body) { NSString *value = [body valueForKey:key]; - if(value != nil) { - [request.content.HTTPBody addObject:]; + DCTAuthContentItem *item = [[DCTAuthContentItem alloc] initWithName:key value:value]; + + if(item != nil) { + [items addObject: item]; } - }*/ - [request.content setHTTPBody: body]; + } + + DCTAuthContent *content = [[DCTAuthContent alloc] initWithEncoding:NSUTF8StringEncoding + type:DCTAuthContentTypeJSON + items:items]; + [request setContent:content]; } - request.account = existingAccount; - // If there are headers NSDictionary *headers = [opts objectForKey:@"headers"]; if (headers != nil) { @@ -469,6 +474,8 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name request.HTTPHeaders = existingHeaders; } + request.account = existingAccount; + [request performRequestWithHandler:^(DCTAuthResponse *response, NSError *error) { if (error != nil) { NSDictionary *errorDict = @{ @@ -493,10 +500,9 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name // Parse XML data = [XMLReader dictionaryForXMLData:rawData - options:XMLReaderOptionsProcessNamespaces - error:&err]; + options:XMLReaderOptionsProcessNamespaces + error:&err]; } - if (err != nil) { NSDictionary *errResp = @{ @"status": @"error", @@ -506,7 +512,7 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name } else { NSDictionary *resp = @{ @"status": @(statusCode), - @"data": data + @"data": data != nil ? data : @[] }; callback(@[[NSNull null], resp]); } @@ -533,7 +539,7 @@ - (DCTAuthAccount *) accountForProvider:(NSString *) providerName } - (NSDictionary *) credentialForAccount:(NSString *)providerName - cfg:(NSDictionary *)cfg + cfg:(NSDictionary *)cfg { DCTAuthAccount *account = [self accountForProvider:providerName]; if (!account) { @@ -721,3 +727,4 @@ - (NSString *) stringHost:(NSURL *)url } @end +