-
Notifications
You must be signed in to change notification settings - Fork 214
/
react-native-oauth.js
151 lines (126 loc) · 3.94 KB
/
react-native-oauth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* @providesModule OAuthManager
* @flow
*/
import {
NativeModules,
AsyncStorage
} from 'react-native';
import invariant from 'invariant';
const OAuthManagerBridge = NativeModules.OAuthManager;
let configured = false;
const STORAGE_KEY = 'ReactNativeOAuth';
import promisify from './lib/promisify'
import defaultProviders from './lib/authProviders';
let authProviders = defaultProviders;
const identity = (props) => props;
/**
* Manager is the OAuth layer
**/
export default class OAuthManager {
constructor(appName, opts={}) {
invariant(appName && appName != '', `You must provide an appName to the OAuthManager`);
this.appName = appName;
this._options = opts;
}
addProvider(provider) {
authProviders = Object.assign({}, authProviders, provider);
}
configure(providerConfigs) {
return this.configureProviders(providerConfigs)
}
authorize(provider, opts={}) {
const options = Object.assign({}, this._options, opts, {
app_name: this.appName
})
return promisify('authorize')(provider, options);
}
savedAccounts(opts={}) {
// const options = Object.assign({}, this._options, opts, {
// app_name: this.appName
// })
// return promisify('getSavedAccounts')(options);
const promises = this.providers()
.map(name => {
return this.savedAccount(name)
.catch(err => ({provider: name, status: "error"}));
});
return Promise.all(promises)
.then((accountResp) => {
const accounts = accountResp.filter(acc => acc.status == "ok");
return { accounts }
});
}
savedAccount(provider) {
const options = Object.assign({}, this._options, {
app_name: this.appName
})
return promisify('getSavedAccount')(provider, options);
}
makeRequest(provider, url, opts={}) {
const options = Object.assign({}, this._options, opts, {
app_name: this.appName
});
console.log('making request', provider, url, opts);
return promisify('makeRequest')(provider, url, options)
.then(response => {
// Little bit of a hack to support Android until we have a better
// way of decoding the JSON response on the Android side
if (response && response.data && typeof response.data === "string") {
response.data = JSON.parse(response.data);
}
return response;
});
}
deauthorize(provider) {
return promisify('deauthorize')(provider);
}
providers() {
return OAuthManager.providers();
}
static providers() {
return Object.keys(authProviders);
}
static isSupported(name) {
return OAuthManager.providers().indexOf(name) >= 0;
}
// Private
/**
* Configure a single provider
*
*
* @param {string} name of the provider
* @param {object} additional configuration
*
**/
configureProvider(name, props) {
invariant(OAuthManager.isSupported(name), `The provider ${name} is not supported yet`);
const providerCfg = Object.assign({}, authProviders[name]);
let { validate = identity, transform = identity, callback_url } = providerCfg;
delete providerCfg.transform;
delete providerCfg.validate;
let config = Object.assign({}, {
app_name: this.appName,
callback_url
}, providerCfg, props);
if (config.defaultParams) {
delete config.defaultParams;
}
config = Object.keys(config)
.reduce((sum, key) => ({
...sum,
[key]: typeof config[key] === 'function' ? config[key](config) : config[key]
}), {});
validate(config);
return promisify('configureProvider')(name, config);
}
configureProviders(providerConfigs) {
providerConfigs = providerConfigs || this._options;
const promises = Object
.keys(providerConfigs)
.map(name =>
this.configureProvider(name, providerConfigs[name]));
return Promise.all(promises)
.then(() => this);
}
}