diff --git a/ios/OAuthManager/OAuthManager.m b/ios/OAuthManager/OAuthManager.m index a9451bc..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]; @@ -443,16 +443,27 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name URL:apiUrl items:items]; + // Allow json body in POST / PUT requests NSDictionary *body = [opts objectForKey:@"body"]; if (body != nil) { + NSMutableArray *items = [NSMutableArray array]; + 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? + NSString *value = [body valueForKey:key]; + + DCTAuthContentItem *item = [[DCTAuthContentItem alloc] initWithName:key value:value]; + + if(item != nil) { + [items addObject: item]; + } } + + 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) { @@ -463,6 +474,8 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name request.HTTPHeaders = existingHeaders; } + request.account = existingAccount; + [request performRequestWithHandler:^(DCTAuthResponse *response, NSError *error) { if (error != nil) { NSDictionary *errorDict = @{ @@ -487,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", @@ -500,7 +512,7 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name } else { NSDictionary *resp = @{ @"status": @(statusCode), - @"data": data + @"data": data != nil ? data : @[] }; callback(@[[NSNull null], resp]); } @@ -527,7 +539,7 @@ - (DCTAuthAccount *) accountForProvider:(NSString *) providerName } - (NSDictionary *) credentialForAccount:(NSString *)providerName - cfg:(NSDictionary *)cfg + cfg:(NSDictionary *)cfg { DCTAuthAccount *account = [self accountForProvider:providerName]; if (!account) { @@ -715,3 +727,4 @@ - (NSString *) stringHost:(NSURL *)url } @end + 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;