88import android .support .annotation .Nullable ;
99
1010import com .facebook .react .bridge .ActivityEventListener ;
11- import com .facebook .react .bridge .Arguments ;
1211import com .facebook .react .bridge .ReactApplicationContext ;
1312import com .facebook .react .bridge .ReactContextBaseJavaModule ;
1413import com .facebook .react .bridge .ReactMethod ;
1514import com .facebook .react .bridge .Promise ;
1615import com .facebook .react .bridge .ReadableArray ;
1716import com .facebook .react .bridge .ReadableMap ;
18- import com .facebook .react .bridge .WritableArray ;
1917import com .facebook .react .bridge .WritableMap ;
20- import com .rnappauth .utils .MapUtils ;
18+ import com .rnappauth .utils .MapUtil ;
2119import com .rnappauth .utils .UnsafeConnectionBuilder ;
20+ import com .rnappauth .utils .TokenResponseFactory ;
2221
2322import net .openid .appauth .AppAuthConfiguration ;
2423import net .openid .appauth .AuthorizationException ;
3433import net .openid .appauth .connectivity .ConnectionBuilder ;
3534import net .openid .appauth .connectivity .DefaultConnectionBuilder ;
3635
37- import java .text .SimpleDateFormat ;
38- import java .util .Date ;
3936import java .util .HashMap ;
40- import java .util .Iterator ;
41- import java .util .Locale ;
4237import java .util .Map ;
43- import java .util .TimeZone ;
4438
4539public class RNAppAuthModule extends ReactContextBaseJavaModule implements ActivityEventListener {
4640
@@ -70,7 +64,7 @@ public void authorize(
7064 ) {
7165 final ConnectionBuilder builder = createConnectionBuilder (dangerouslyAllowInsecureHttpRequests );
7266 final AppAuthConfiguration appAuthConfiguration = this .createAppAuthConfiguration (builder );
73- final HashMap <String , String > additionalParametersMap = MapUtils .readableMapToHashMap (additionalParameters );
67+ final HashMap <String , String > additionalParametersMap = MapUtil .readableMapToHashMap (additionalParameters );
7468
7569 if (clientSecret != null ) {
7670 additionalParametersMap .put ("client_secret" , clientSecret );
@@ -94,7 +88,7 @@ public void authorize(
9488 additionalParametersMap
9589 );
9690 } catch (Exception e ) {
97- promise .reject ("RNAppAuth Error" , " Failed to authenticate" , e );
91+ promise .reject ("Failed to authenticate" , e . getMessage () );
9892 }
9993 } else {
10094 final Uri issuerUri = Uri .parse (issuer );
@@ -105,7 +99,7 @@ public void onFetchConfigurationCompleted(
10599 @ Nullable AuthorizationServiceConfiguration fetchedConfiguration ,
106100 @ Nullable AuthorizationException ex ) {
107101 if (ex != null ) {
108- promise .reject ("RNAppAuth Error" , " Failed to fetch configuration" , ex );
102+ promise .reject ("Failed to fetch configuration" , ex . errorDescription );
109103 return ;
110104 }
111105
@@ -143,7 +137,7 @@ public void refresh(
143137 ) {
144138 final ConnectionBuilder builder = createConnectionBuilder (dangerouslyAllowInsecureHttpRequests );
145139 final AppAuthConfiguration appAuthConfiguration = createAppAuthConfiguration (builder );
146- final HashMap <String , String > additionalParametersMap = MapUtils .readableMapToHashMap (additionalParameters );
140+ final HashMap <String , String > additionalParametersMap = MapUtil .readableMapToHashMap (additionalParameters );
147141
148142 if (clientSecret != null ) {
149143 additionalParametersMap .put ("client_secret" , clientSecret );
@@ -168,7 +162,7 @@ public void refresh(
168162 promise
169163 );
170164 } catch (Exception e ) {
171- promise .reject ("RNAppAuth Error" , " Failed to refresh token" , e );
165+ promise .reject ("Failed to refresh token" , e . getMessage () );
172166 }
173167 } else {
174168 final Uri issuerUri = Uri .parse (issuer );
@@ -180,7 +174,7 @@ public void onFetchConfigurationCompleted(
180174 @ Nullable AuthorizationServiceConfiguration fetchedConfiguration ,
181175 @ Nullable AuthorizationException ex ) {
182176 if (ex != null ) {
183- promise .reject ("RNAppAuth Error" , " Failed to fetch configuration" , ex );
177+ promise .reject ("Failed to fetch configuration" , ex . errorDescription );
184178 return ;
185179 }
186180
@@ -211,7 +205,7 @@ public void onActivityResult(Activity activity, int requestCode, int resultCode,
211205 final AuthorizationResponse response = AuthorizationResponse .fromIntent (data );
212206 AuthorizationException exception = AuthorizationException .fromIntent (data );
213207 if (exception != null ) {
214- promise .reject ("RNAppAuth Error" , " Failed to authenticate" , exception );
208+ promise .reject ("Failed to authenticate" , exception . errorDescription );
215209 return ;
216210 }
217211
@@ -230,10 +224,10 @@ public void onActivityResult(Activity activity, int requestCode, int resultCode,
230224 public void onTokenRequestCompleted (
231225 TokenResponse resp , AuthorizationException ex ) {
232226 if (resp != null ) {
233- WritableMap map = tokenResponseToMap (resp , response );
227+ WritableMap map = TokenResponseFactory . tokenResponseToMap (resp , response );
234228 authorizePromise .resolve (map );
235229 } else {
236- promise .reject ("RNAppAuth Error" , " Failed exchange token" , ex );
230+ promise .reject ("Failed exchange token" , ex . errorDescription );
237231 }
238232 }
239233 };
@@ -363,10 +357,10 @@ private void refreshWithConfiguration(
363357 @ Override
364358 public void onTokenRequestCompleted (@ Nullable TokenResponse response , @ Nullable AuthorizationException ex ) {
365359 if (response != null ) {
366- WritableMap map = tokenResponseToMap (response );
360+ WritableMap map = TokenResponseFactory . tokenResponseToMap (response );
367361 promise .resolve (map );
368362 } else {
369- promise .reject ("RNAppAuth Error" , " Failed refresh token" );
363+ promise .reject ("Failed to refresh token" , ex . errorDescription );
370364 }
371365 }
372366 };
@@ -395,103 +389,6 @@ private String arrayToString(ReadableArray array) {
395389 return strBuilder .toString ();
396390 }
397391
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-
495392 /*
496393 * Create an App Auth configuration using the provided connection builder
497394 */
0 commit comments