Skip to content

Commit 486d0f7

Browse files
committed
Added slack support auth for iOS
1 parent 958da4c commit 486d0f7

File tree

3 files changed

+61
-5
lines changed

3 files changed

+61
-5
lines changed

ios/OAuthManager/OAuthManager.m

+39-2
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,8 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name
379379
return;
380380
}
381381

382+
NSDictionary *creds = [self credentialForAccount:providerName cfg:cfg];
383+
382384
// If we have the http in the string, use it as the URL, otherwise create one
383385
// with the configuration
384386
NSURL *apiUrl;
@@ -394,8 +396,18 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name
394396
NSDictionary *params = [opts objectForKey:@"params"];
395397
if (params != nil) {
396398
for (NSString *key in params) {
397-
NSURLQueryItem *item = [NSURLQueryItem queryItemWithName:key value:[params valueForKey:key]];
398-
[items addObject:item];
399+
400+
NSString *value = [params valueForKey:key];
401+
402+
if ([value isEqualToString:@"access_token"]) {
403+
value = [creds valueForKey:@"access_token"];
404+
}
405+
406+
NSURLQueryItem *item = [NSURLQueryItem queryItemWithName:key value:value];
407+
408+
if (item != nil) {
409+
[items addObject:item];
410+
}
399411
}
400412
}
401413

@@ -479,6 +491,31 @@ - (DCTAuthAccount *) accountForProvider:(NSString *) providerName
479491
}
480492
}
481493

494+
- (NSDictionary *) credentialForAccount:(NSString *)providerName
495+
cfg:(NSDictionary *)cfg
496+
{
497+
DCTAuthAccount *account = [self accountForProvider:providerName];
498+
if (!account) {
499+
return nil;
500+
}
501+
502+
id credentials;
503+
NSString *version = [cfg valueForKey:@"auth_version"];
504+
if ([version isEqualToString:@"1.0"]) {
505+
credentials = [account credential];
506+
} else if ([version isEqualToString:@"2.0"]) {
507+
credentials = [account credential];
508+
} else {
509+
return nil;
510+
}
511+
512+
NSDictionary *dict = @{
513+
@"access_token": [credentials accessToken]
514+
};
515+
516+
return dict;
517+
}
518+
482519
- (DCTAuthRequestMethod) getRequestMethodByString:(NSString *) method
483520
{
484521
if ([method compare:@"get" options:NSCaseInsensitiveSearch] == NSOrderedSame) {

lib/authProviders.js

+14
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,20 @@ export const authProviders = {
5959
api_url: 'https://api.github.com',
6060
callback_url: ({app_name}) => `${app_name}://oauth`,
6161
validate: validate()
62+
},
63+
'slack': {
64+
auth_version: '2.0',
65+
authorize_url: 'https://slack.com/oauth/authorize',
66+
access_token_url: 'https://slack.com/api/oauth.access',
67+
api_url: 'https://slack.com/api',
68+
callback_url: ({app_name}) => `${app_name}://oauth`,
69+
defaultParams: {
70+
token: 'access_token'
71+
},
72+
validate: validate({
73+
client_id: [notEmpty],
74+
client_secret: [notEmpty]
75+
})
6276
}
6377
}
6478

react-native-oauth.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ export default class OAuthManager {
6767
app_name: this.appName
6868
});
6969

70+
console.log('making request', provider, url, opts);
71+
7072
return promisify('makeRequest')(provider, url, options)
7173
.then(response => {
7274
// Little bit of a hack to support Android until we have a better
@@ -107,16 +109,19 @@ export default class OAuthManager {
107109
invariant(OAuthManager.isSupported(name), `The provider ${name} is not supported yet`);
108110

109111
const providerCfg = Object.assign({}, authProviders[name]);
110-
let { validate = identity, transform = identity, callback_url, api_url } = providerCfg;
112+
let { validate = identity, transform = identity, callback_url } = providerCfg;
111113
delete providerCfg.transform;
112114
delete providerCfg.validate;
113115

114116
let config = Object.assign({}, {
115117
app_name: this.appName,
116-
callback_url,
117-
api_url
118+
callback_url
118119
}, providerCfg, props);
119120

121+
if (config.defaultParams) {
122+
delete config.defaultParams;
123+
}
124+
120125
config = Object.keys(config)
121126
.reduce((sum, key) => ({
122127
...sum,

0 commit comments

Comments
 (0)