Skip to content

Add Spotify auth provider #189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions ios/OAuthManager/OAuthManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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) {
Expand All @@ -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 = @{
Expand All @@ -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",
Expand All @@ -500,7 +512,7 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name
} else {
NSDictionary *resp = @{
@"status": @(statusCode),
@"data": data
@"data": data != nil ? data : @[]
};
callback(@[[NSNull null], resp]);
}
Expand All @@ -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) {
Expand Down Expand Up @@ -715,3 +727,4 @@ - (NSString *) stringHost:(NSURL *)url
}

@end

15 changes: 13 additions & 2 deletions lib/authProviders.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
export default authProviders;