17
17
18
18
import java .io .IOException ;
19
19
import java .util .Map ;
20
+ import java .util .Set ;
20
21
import java .util .HashMap ;
21
22
import java .util .List ;
22
23
import java .util .ArrayList ;
62
63
import com .github .scribejava .core .model .Verb ;
63
64
import com .github .scribejava .core .oauth .OAuth10aService ;
64
65
65
- interface KeySetterFn {
66
- String setKeyOrDefault (String a , String b );
66
+ class ProviderNotConfiguredException extends Exception {
67
+ public ProviderNotConfiguredException (String message ) {
68
+ super (message );
69
+ }
67
70
}
68
71
69
72
@ SuppressWarnings ("WeakerAccess" )
@@ -75,12 +78,13 @@ class OAuthManagerModule extends ReactContextBaseJavaModule {
75
78
76
79
private HashMap _configuration = new HashMap <String , HashMap <String ,String >>();
77
80
private ArrayList _callbackUrls = new ArrayList <String >();
78
- private SharedPreferences _credentialsStore ;
81
+ private OAuthManagerStore _credentialsStore ;
79
82
80
83
public OAuthManagerModule (ReactApplicationContext reactContext ) {
81
84
super (reactContext );
82
85
mReactContext = reactContext ;
83
86
87
+ _credentialsStore = OAuthManagerStore .getOAuthManagerStore (mReactContext , TAG , Context .MODE_PRIVATE );
84
88
Log .d (TAG , "New instance" );
85
89
}
86
90
@@ -142,17 +146,16 @@ public void configureProvider(
142
146
public void authorize (
143
147
final String providerName ,
144
148
@ Nullable final ReadableMap params ,
145
- final Callback callback ) throws Exception
149
+ final Callback callback )
146
150
{
147
- Log .i (TAG , "authorize called for " + providerName );
148
-
149
- HashMap <String ,String > cfg = this .getConfiguration (providerName );
150
- final String authVersion = (String ) cfg .get ("auth_version" );
151
- Activity activity = mReactContext .getCurrentActivity ();
152
- FragmentManager fragmentManager = activity .getFragmentManager ();
153
- String callbackUrl = "http://localhost/" + providerName ;
154
-
155
151
try {
152
+ final OAuthManagerModule self = this ;
153
+ HashMap <String ,String > cfg = this .getConfiguration (providerName );
154
+ final String authVersion = (String ) cfg .get ("auth_version" );
155
+ Activity activity = mReactContext .getCurrentActivity ();
156
+ FragmentManager fragmentManager = activity .getFragmentManager ();
157
+ String callbackUrl = "http://localhost/" + providerName ;
158
+
156
159
if (authVersion .equals ("1.0" )) {
157
160
final OAuth10aService service =
158
161
OAuthManagerProviders .getApiFor10aProvider (providerName , cfg , callbackUrl );
@@ -163,21 +166,10 @@ public void authorize(
163
166
ctrl .requestAuth (new OAuthManagerOnAccessTokenListener () {
164
167
public void onRequestTokenError (final Exception ex ) {}
165
168
public void onOauth1AccessToken (final OAuth1AccessToken accessToken ) {
166
- Log .d (TAG , "onAccessToken: " + accessToken .getRawResponse ());
167
- WritableMap resp = Arguments .createMap ();
168
- WritableMap response = Arguments .createMap ();
169
-
170
- resp .putString ("status" , "ok" );
171
- response .putString ("uuid" , accessToken .getParameter ("user_id" ));
172
- response .putString ("provider" , providerName );
173
-
174
- WritableMap credentials = Arguments .createMap ();
175
- credentials .putString ("oauth_token" , accessToken .getToken ());
176
- credentials .putString ("oauth_secret" , accessToken .getTokenSecret ());
177
- response .putMap ("credentials" , credentials );
178
-
179
- resp .putMap ("response" , response );
169
+ _credentialsStore .store (providerName , accessToken );
170
+ _credentialsStore .commit ();
180
171
172
+ WritableMap resp = self .accessTokenResponse (providerName , accessToken , authVersion );
181
173
callback .invoke (null , resp );
182
174
}
183
175
});
@@ -237,7 +229,30 @@ public void getSavedAccount(
237
229
final ReadableMap options ,
238
230
final Callback onComplete )
239
231
{
240
- Log .i (TAG , "getSavedAccount for " + providerName );
232
+ try {
233
+ HashMap <String ,String > cfg = this .getConfiguration (providerName );
234
+ final String authVersion = (String ) cfg .get ("auth_version" );
235
+
236
+ Log .i (TAG , "getSavedAccount for " + providerName );
237
+
238
+ if (authVersion .equals ("1.0" )) {
239
+ OAuth1AccessToken token = _credentialsStore .get (providerName , OAuth1AccessToken .class );
240
+ Log .d (TAG , "Found token: " + token );
241
+
242
+ WritableMap resp = this .accessTokenResponse (providerName , token , authVersion );
243
+ onComplete .invoke (null , resp );
244
+ } else {
245
+
246
+ }
247
+ } catch (ProviderNotConfiguredException ex ) {
248
+ Log .e (TAG , "Provider not yet configured: " + providerName );
249
+ exceptionCallback (ex , onComplete );
250
+ } catch (Exception ex ) {
251
+ Log .e (TAG , "An exception occurred getSavedAccount: " + ex .getMessage ());
252
+ ex .printStackTrace ();
253
+ exceptionCallback (ex , onComplete );
254
+ }
255
+
241
256
242
257
// try {
243
258
// Credential creds = this.loadCredentialForProvider(providerName, options);
@@ -283,7 +298,20 @@ public void getSavedAccount(
283
298
284
299
@ ReactMethod
285
300
public void deauthorize (final String providerName , final Callback onComplete ) {
286
- Log .i (TAG , "deauthorizing " + providerName );
301
+ try {
302
+ Log .i (TAG , "deauthorizing " + providerName );
303
+ HashMap <String ,String > cfg = this .getConfiguration (providerName );
304
+ final String authVersion = (String ) cfg .get ("auth_version" );
305
+
306
+ _credentialsStore .delete (providerName );
307
+
308
+ WritableMap resp = Arguments .createMap ();
309
+ resp .putString ("status" , "ok" );
310
+
311
+ onComplete .invoke (null , resp );
312
+ } catch (Exception ex ) {
313
+ exceptionCallback (ex , onComplete );
314
+ }
287
315
// try {
288
316
// OAuthManager manager = this.getManager(providerName, null, true);
289
317
// OAuthCallback<Boolean> cb = new OAuthCallback<Boolean>() {
@@ -340,13 +368,35 @@ private HashMap<String,String> getConfiguration(
340
368
final String providerName
341
369
) throws Exception {
342
370
if (!_configuration .containsKey (providerName )) {
343
- throw new Exception ("Provider not configured: " + providerName );
371
+ throw new ProviderNotConfiguredException ("Provider not configured: " + providerName );
344
372
}
345
373
346
374
HashMap <String ,String > cfg = (HashMap ) _configuration .get (providerName );
347
375
return cfg ;
348
376
}
349
377
378
+ private WritableMap accessTokenResponse (
379
+ final String providerName ,
380
+ final OAuth1AccessToken accessToken ,
381
+ final String oauthVersion
382
+ ) {
383
+ WritableMap resp = Arguments .createMap ();
384
+ WritableMap response = Arguments .createMap ();
385
+
386
+ resp .putString ("status" , "ok" );
387
+ resp .putString ("provider" , providerName );
388
+ response .putString ("uuid" , accessToken .getParameter ("user_id" ));
389
+
390
+ WritableMap credentials = Arguments .createMap ();
391
+ credentials .putString ("oauth_token" , accessToken .getToken ());
392
+ credentials .putString ("oauth_secret" , accessToken .getTokenSecret ());
393
+ response .putMap ("credentials" , credentials );
394
+
395
+ resp .putMap ("response" , response );
396
+
397
+ return resp ;
398
+ }
399
+
350
400
// private OAuthManager getManager(
351
401
// final String providerName,
352
402
// @Nullable final ReadableMap params,
0 commit comments