Skip to content

Commit 77128e0

Browse files
authored
Merge pull request #189 from thebradbain/master
Add Spotify auth provider
2 parents bd9ee8b + 2c475ab commit 77128e0

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

ios/OAuthManager/OAuthManager.m

+25-12
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ - (BOOL) _configureProvider:(NSString *)providerName andConfig:(NSDictionary *)c
153153
_callbackUrls = [arr copy];
154154
NSLog(@"Saved callback url: %@ in %@", saveCallbackUrl, _callbackUrls);
155155
}
156-
156+
157157

158158
// Convert objects of url type
159159
for (NSString *name in [config allKeys]) {
@@ -354,14 +354,14 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name
354354

355355
[manager addPending:client];
356356
_pendingAuthentication = YES;
357-
357+
358358
NSLog(@"Calling authorizeWithUrl: %@ with callbackURL: %@\n %@", providerName, callbackUrl, cfg);
359359

360360
[client authorizeWithUrl:providerName
361361
url:callbackUrl
362362
cfg:cfg
363363
onSuccess:^(DCTAuthAccount *account) {
364-
NSLog(@"on success called with account: %@", account);
364+
NSLog(@"on success called with account: %@", account);
365365
NSDictionary *accountResponse = [manager getAccountResponse:account cfg:cfg];
366366
_pendingAuthentication = NO;
367367
[manager removePending:client];
@@ -443,16 +443,27 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name
443443
URL:apiUrl
444444
items:items];
445445

446+
// Allow json body in POST / PUT requests
446447
NSDictionary *body = [opts objectForKey:@"body"];
447448
if (body != nil) {
449+
NSMutableArray *items = [NSMutableArray array];
450+
448451
for (NSString *key in body) {
449-
NSData *data = [[NSString stringWithFormat:@"%@", [body valueForKey:key]] dataUsingEncoding:NSUTF8StringEncoding];
450-
[request addMultiPartData:data withName:key type:@"application/json"]; // TODO: How should we handle different body types?
452+
NSString *value = [body valueForKey:key];
453+
454+
DCTAuthContentItem *item = [[DCTAuthContentItem alloc] initWithName:key value:value];
455+
456+
if(item != nil) {
457+
[items addObject: item];
458+
}
451459
}
460+
461+
DCTAuthContent *content = [[DCTAuthContent alloc] initWithEncoding:NSUTF8StringEncoding
462+
type:DCTAuthContentTypeJSON
463+
items:items];
464+
[request setContent:content];
452465
}
453466

454-
request.account = existingAccount;
455-
456467
// If there are headers
457468
NSDictionary *headers = [opts objectForKey:@"headers"];
458469
if (headers != nil) {
@@ -463,6 +474,8 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name
463474
request.HTTPHeaders = existingHeaders;
464475
}
465476

477+
request.account = existingAccount;
478+
466479
[request performRequestWithHandler:^(DCTAuthResponse *response, NSError *error) {
467480
if (error != nil) {
468481
NSDictionary *errorDict = @{
@@ -487,10 +500,9 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name
487500

488501
// Parse XML
489502
data = [XMLReader dictionaryForXMLData:rawData
490-
options:XMLReaderOptionsProcessNamespaces
491-
error:&err];
503+
options:XMLReaderOptionsProcessNamespaces
504+
error:&err];
492505
}
493-
494506
if (err != nil) {
495507
NSDictionary *errResp = @{
496508
@"status": @"error",
@@ -500,7 +512,7 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name
500512
} else {
501513
NSDictionary *resp = @{
502514
@"status": @(statusCode),
503-
@"data": data
515+
@"data": data != nil ? data : @[]
504516
};
505517
callback(@[[NSNull null], resp]);
506518
}
@@ -527,7 +539,7 @@ - (DCTAuthAccount *) accountForProvider:(NSString *) providerName
527539
}
528540

529541
- (NSDictionary *) credentialForAccount:(NSString *)providerName
530-
cfg:(NSDictionary *)cfg
542+
cfg:(NSDictionary *)cfg
531543
{
532544
DCTAuthAccount *account = [self accountForProvider:providerName];
533545
if (!account) {
@@ -715,3 +727,4 @@ - (NSString *) stringHost:(NSURL *)url
715727
}
716728

717729
@end
730+

lib/authProviders.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,18 @@ export const authProviders = {
7373
client_id: [notEmpty],
7474
client_secret: [notEmpty]
7575
})
76-
}
76+
},
77+
'spotify': {
78+
auth_version: "2.0",
79+
authorize_url: 'https://accounts.spotify.com/authorize',
80+
api_url: 'https://api.spotify.com/',
81+
callback_url: ({app_name}) => `${app_name}://authorize`,
82+
83+
validate: validate({
84+
client_id: [notEmpty],
85+
client_secret: [notEmpty]
86+
})
87+
},
7788
}
7889

79-
export default authProviders;
90+
export default authProviders;

0 commit comments

Comments
 (0)