8
8
import android .support .annotation .Nullable ;
9
9
10
10
import com .facebook .react .bridge .ActivityEventListener ;
11
- import com .facebook .react .bridge .Arguments ;
12
11
import com .facebook .react .bridge .ReactApplicationContext ;
13
12
import com .facebook .react .bridge .ReactContextBaseJavaModule ;
14
13
import com .facebook .react .bridge .ReactMethod ;
15
14
import com .facebook .react .bridge .Promise ;
16
15
import com .facebook .react .bridge .ReadableArray ;
17
16
import com .facebook .react .bridge .ReadableMap ;
18
- import com .facebook .react .bridge .WritableArray ;
19
17
import com .facebook .react .bridge .WritableMap ;
20
- import com .rnappauth .utils .MapUtils ;
18
+ import com .rnappauth .utils .MapUtil ;
21
19
import com .rnappauth .utils .UnsafeConnectionBuilder ;
20
+ import com .rnappauth .utils .TokenResponseFactory ;
22
21
23
22
import net .openid .appauth .AppAuthConfiguration ;
24
23
import net .openid .appauth .AuthorizationException ;
34
33
import net .openid .appauth .connectivity .ConnectionBuilder ;
35
34
import net .openid .appauth .connectivity .DefaultConnectionBuilder ;
36
35
37
- import java .text .SimpleDateFormat ;
38
- import java .util .Date ;
39
36
import java .util .HashMap ;
40
- import java .util .Iterator ;
41
- import java .util .Locale ;
42
37
import java .util .Map ;
43
- import java .util .TimeZone ;
44
38
45
39
public class RNAppAuthModule extends ReactContextBaseJavaModule implements ActivityEventListener {
46
40
@@ -70,7 +64,7 @@ public void authorize(
70
64
) {
71
65
final ConnectionBuilder builder = createConnectionBuilder (dangerouslyAllowInsecureHttpRequests );
72
66
final AppAuthConfiguration appAuthConfiguration = this .createAppAuthConfiguration (builder );
73
- final HashMap <String , String > additionalParametersMap = MapUtils .readableMapToHashMap (additionalParameters );
67
+ final HashMap <String , String > additionalParametersMap = MapUtil .readableMapToHashMap (additionalParameters );
74
68
75
69
if (clientSecret != null ) {
76
70
additionalParametersMap .put ("client_secret" , clientSecret );
@@ -94,7 +88,7 @@ public void authorize(
94
88
additionalParametersMap
95
89
);
96
90
} catch (Exception e ) {
97
- promise .reject ("RNAppAuth Error" , " Failed to authenticate" , e );
91
+ promise .reject ("Failed to authenticate" , e . getMessage () );
98
92
}
99
93
} else {
100
94
final Uri issuerUri = Uri .parse (issuer );
@@ -105,7 +99,7 @@ public void onFetchConfigurationCompleted(
105
99
@ Nullable AuthorizationServiceConfiguration fetchedConfiguration ,
106
100
@ Nullable AuthorizationException ex ) {
107
101
if (ex != null ) {
108
- promise .reject ("RNAppAuth Error" , " Failed to fetch configuration" , ex );
102
+ promise .reject ("Failed to fetch configuration" , ex . errorDescription );
109
103
return ;
110
104
}
111
105
@@ -143,7 +137,7 @@ public void refresh(
143
137
) {
144
138
final ConnectionBuilder builder = createConnectionBuilder (dangerouslyAllowInsecureHttpRequests );
145
139
final AppAuthConfiguration appAuthConfiguration = createAppAuthConfiguration (builder );
146
- final HashMap <String , String > additionalParametersMap = MapUtils .readableMapToHashMap (additionalParameters );
140
+ final HashMap <String , String > additionalParametersMap = MapUtil .readableMapToHashMap (additionalParameters );
147
141
148
142
if (clientSecret != null ) {
149
143
additionalParametersMap .put ("client_secret" , clientSecret );
@@ -168,7 +162,7 @@ public void refresh(
168
162
promise
169
163
);
170
164
} catch (Exception e ) {
171
- promise .reject ("RNAppAuth Error" , " Failed to refresh token" , e );
165
+ promise .reject ("Failed to refresh token" , e . getMessage () );
172
166
}
173
167
} else {
174
168
final Uri issuerUri = Uri .parse (issuer );
@@ -180,7 +174,7 @@ public void onFetchConfigurationCompleted(
180
174
@ Nullable AuthorizationServiceConfiguration fetchedConfiguration ,
181
175
@ Nullable AuthorizationException ex ) {
182
176
if (ex != null ) {
183
- promise .reject ("RNAppAuth Error" , " Failed to fetch configuration" , ex );
177
+ promise .reject ("Failed to fetch configuration" , ex . errorDescription );
184
178
return ;
185
179
}
186
180
@@ -211,7 +205,7 @@ public void onActivityResult(Activity activity, int requestCode, int resultCode,
211
205
final AuthorizationResponse response = AuthorizationResponse .fromIntent (data );
212
206
AuthorizationException exception = AuthorizationException .fromIntent (data );
213
207
if (exception != null ) {
214
- promise .reject ("RNAppAuth Error" , " Failed to authenticate" , exception );
208
+ promise .reject ("Failed to authenticate" , exception . errorDescription );
215
209
return ;
216
210
}
217
211
@@ -230,10 +224,10 @@ public void onActivityResult(Activity activity, int requestCode, int resultCode,
230
224
public void onTokenRequestCompleted (
231
225
TokenResponse resp , AuthorizationException ex ) {
232
226
if (resp != null ) {
233
- WritableMap map = tokenResponseToMap (resp , response );
227
+ WritableMap map = TokenResponseFactory . tokenResponseToMap (resp , response );
234
228
authorizePromise .resolve (map );
235
229
} else {
236
- promise .reject ("RNAppAuth Error" , " Failed exchange token" , ex );
230
+ promise .reject ("Failed exchange token" , ex . errorDescription );
237
231
}
238
232
}
239
233
};
@@ -363,10 +357,10 @@ private void refreshWithConfiguration(
363
357
@ Override
364
358
public void onTokenRequestCompleted (@ Nullable TokenResponse response , @ Nullable AuthorizationException ex ) {
365
359
if (response != null ) {
366
- WritableMap map = tokenResponseToMap (response );
360
+ WritableMap map = TokenResponseFactory . tokenResponseToMap (response );
367
361
promise .resolve (map );
368
362
} else {
369
- promise .reject ("RNAppAuth Error" , " Failed refresh token" );
363
+ promise .reject ("Failed to refresh token" , ex . errorDescription );
370
364
}
371
365
}
372
366
};
@@ -395,103 +389,6 @@ private String arrayToString(ReadableArray array) {
395
389
return strBuilder .toString ();
396
390
}
397
391
398
- /*
399
- * Read raw token response into a React Native map to be passed down the bridge
400
- */
401
- private WritableMap tokenResponseToMap (TokenResponse response ) {
402
- WritableMap map = Arguments .createMap ();
403
-
404
- map .putString ("accessToken" , response .accessToken );
405
-
406
- if (response .accessTokenExpirationTime != null ) {
407
- Date expirationDate = new Date (response .accessTokenExpirationTime );
408
- SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss'Z'" , Locale .US );
409
- formatter .setTimeZone (TimeZone .getTimeZone ("UTC" ));
410
- String expirationDateString = formatter .format (expirationDate );
411
- map .putString ("accessTokenExpirationDate" , expirationDateString );
412
- }
413
-
414
- WritableMap additionalParametersMap = Arguments .createMap ();
415
-
416
- if (!response .additionalParameters .isEmpty ()) {
417
-
418
- Iterator <String > iterator = response .additionalParameters .keySet ().iterator ();
419
-
420
- while (iterator .hasNext ()) {
421
- String key = iterator .next ();
422
- additionalParametersMap .putString (key , response .additionalParameters .get (key ));
423
- }
424
- }
425
-
426
- map .putMap ("additionalParameters" , additionalParametersMap );
427
- map .putString ("idToken" , response .idToken );
428
- map .putString ("refreshToken" , response .refreshToken );
429
- map .putString ("tokenType" , response .tokenType );
430
-
431
- return map ;
432
- }
433
-
434
- /*
435
- * Read raw token response into a React Native map to be passed down the bridge
436
- */
437
- private WritableMap tokenResponseToMap (TokenResponse response , AuthorizationResponse authResponse ) {
438
- WritableMap map = Arguments .createMap ();
439
-
440
- map .putString ("accessToken" , response .accessToken );
441
-
442
- if (response .accessTokenExpirationTime != null ) {
443
- Date expirationDate = new Date (response .accessTokenExpirationTime );
444
- SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss'Z'" , Locale .US );
445
- formatter .setTimeZone (TimeZone .getTimeZone ("UTC" ));
446
- String expirationDateString = formatter .format (expirationDate );
447
- map .putString ("accessTokenExpirationDate" , expirationDateString );
448
- }
449
-
450
- WritableMap authorizeAdditionalParameters = Arguments .createMap ();
451
-
452
- if (!authResponse .additionalParameters .isEmpty ()) {
453
-
454
- Iterator <String > iterator = authResponse .additionalParameters .keySet ().iterator ();
455
-
456
- while (iterator .hasNext ()) {
457
- String key = iterator .next ();
458
- authorizeAdditionalParameters .putString (key , authResponse .additionalParameters .get (key ));
459
- }
460
- }
461
-
462
- WritableMap tokenAdditionalParameters = Arguments .createMap ();
463
-
464
- if (!response .additionalParameters .isEmpty ()) {
465
-
466
- Iterator <String > iterator = response .additionalParameters .keySet ().iterator ();
467
-
468
- while (iterator .hasNext ()) {
469
- String key = iterator .next ();
470
- tokenAdditionalParameters .putString (key , response .additionalParameters .get (key ));
471
- }
472
- }
473
-
474
- map .putMap ("authorizeAdditionalParameters" , authorizeAdditionalParameters );
475
- map .putMap ("tokenAdditionalParameters" , tokenAdditionalParameters );
476
- map .putString ("idToken" , response .idToken );
477
- map .putString ("refreshToken" , response .refreshToken );
478
- map .putString ("tokenType" , response .tokenType );
479
-
480
- if (!authResponse .scope .isEmpty ()) {
481
- WritableArray scopes = Arguments .createArray ();
482
- String [] scopesArray = authResponse .scope .split (" " );
483
-
484
- for ( int i = 0 ; i < scopesArray .length - 1 ; i ++)
485
- {
486
- scopes .pushString (scopesArray [i ]);
487
- }
488
-
489
- map .putArray ("scopes" , scopes );
490
- }
491
-
492
- return map ;
493
- }
494
-
495
392
/*
496
393
* Create an App Auth configuration using the provided connection builder
497
394
*/
0 commit comments