@@ -468,6 +468,128 @@ public void update(@Nullable RegistrationResponse regResponse) {
468468 mAuthorizationException = null ;
469469 }
470470
471+ /**
472+ * Ensures that a non-expired access token is available before invoking the provided action.
473+ * @return
474+ */
475+ @ NonNull
476+ public Tokens getSynchronousFreshToken (
477+ @ NonNull AuthorizationService service ) throws AuthorizationException {
478+ return getSynchronousFreshToken (
479+ service ,
480+ NoClientAuthentication .INSTANCE ,
481+ Collections .<String , String >emptyMap (),
482+ SystemClock .INSTANCE );
483+ }
484+
485+ /**
486+ * Ensures that a non-expired access token is available before invoking the provided action.
487+ * @return
488+ */
489+ @ NonNull
490+ public Tokens getSynchronousFreshToken (
491+ @ NonNull AuthorizationService service ,
492+ @ NonNull ClientAuthentication clientAuth ) throws AuthorizationException {
493+ return getSynchronousFreshToken (
494+ service ,
495+ clientAuth ,
496+ Collections .<String , String >emptyMap (),
497+ SystemClock .INSTANCE );
498+ }
499+
500+ /**
501+ * Ensures that a non-expired access token is available before invoking the provided action.
502+ * If a token refresh is required, the provided additional parameters will be included in this
503+ * refresh request.
504+ * @return
505+ */
506+ @ NonNull
507+ public Tokens getSynchronousFreshToken (
508+ @ NonNull AuthorizationService service ,
509+ @ NonNull Map <String , String > refreshTokenAdditionalParams ) throws ClientAuthentication .UnsupportedAuthenticationMethod , AuthorizationException {
510+ return getSynchronousFreshToken (
511+ service ,
512+ getClientAuthentication (),
513+ refreshTokenAdditionalParams ,
514+ SystemClock .INSTANCE );
515+ }
516+
517+ /**
518+ * Ensures that a non-expired access token is available before invoking the provided action.
519+ * If a token refresh is required, the provided additional parameters will be included in this
520+ * refresh request.
521+ * @return
522+ */
523+ @ NonNull
524+ public Tokens getSynchronousFreshToken (
525+ @ NonNull AuthorizationService service ,
526+ @ NonNull ClientAuthentication clientAuth ,
527+ @ NonNull Map <String , String > refreshTokenAdditionalParams ) throws AuthorizationException {
528+ return getSynchronousFreshToken (
529+ service ,
530+ clientAuth ,
531+ refreshTokenAdditionalParams ,
532+ SystemClock .INSTANCE );
533+ }
534+
535+ @ VisibleForTesting
536+ @ NonNull
537+ Tokens getSynchronousFreshToken (
538+ @ NonNull final AuthorizationService service ,
539+ @ NonNull final ClientAuthentication clientAuth ,
540+ @ NonNull final Map <String , String > refreshTokenAdditionalParams ,
541+ @ NonNull final Clock clock ) throws AuthorizationException {
542+ checkNotNull (service , "service cannot be null" );
543+ checkNotNull (clientAuth , "client authentication cannot be null" );
544+ checkNotNull (refreshTokenAdditionalParams ,
545+ "additional params cannot be null" );
546+ checkNotNull (clock , "clock cannot be null" );
547+
548+ if (!getNeedsTokenRefresh (clock )) {
549+ String accessToken = getAccessToken ();
550+ String idToken = getIdToken ();
551+ if (accessToken != null && idToken != null ) {
552+ return new Tokens (accessToken , idToken );
553+ }
554+ }
555+
556+ if (mRefreshToken == null ) {
557+ throw AuthorizationException .fromTemplate (
558+ AuthorizationRequestErrors .CLIENT_ERROR ,
559+ new IllegalStateException ("No refresh token available and token have expired" ));
560+ }
561+
562+ TokenResponse response = null ;
563+ AuthorizationException exception = null ;
564+ try {
565+ response = service .performSynchronousTokenRequest (createTokenRefreshRequest (refreshTokenAdditionalParams ), clientAuth );
566+ } catch (AuthorizationException e ) {
567+ exception = e ;
568+ }
569+
570+ update (response , exception );
571+
572+ if (response != null ) {
573+ String accessToken = getAccessToken ();
574+ String idToken = getIdToken ();
575+ if (accessToken != null && idToken != null ) {
576+ return new Tokens (accessToken , idToken );
577+ } else {
578+ exception = AuthorizationException .fromTemplate (
579+ AuthorizationException .GeneralErrors .JSON_DESERIALIZATION_ERROR ,
580+ new Exception ("" ));
581+ }
582+ }
583+
584+ if (exception != null ) {
585+ throw exception ;
586+ } else {
587+ throw AuthorizationException .fromTemplate (
588+ AuthorizationException .GeneralErrors .JSON_DESERIALIZATION_ERROR ,
589+ new Exception ("" ));
590+ }
591+ }
592+
471593 /**
472594 * Ensures that a non-expired access token is available before invoking the provided action.
473595 */
@@ -764,6 +886,29 @@ void execute(
764886 @ Nullable AuthorizationException ex );
765887 }
766888
889+ public static class Tokens {
890+ /**
891+ * Result of the synchronous function that return accessToken
892+ */
893+ private @ NonNull final String accessToken ;
894+ private @ NonNull final String idToken ;
895+
896+ public Tokens (@ NonNull String accessToken , @ NonNull String idToken ) {
897+ this .accessToken = accessToken ;
898+ this .idToken = idToken ;
899+ }
900+
901+ @ NonNull
902+ public String getAccessToken () {
903+ return accessToken ;
904+ }
905+
906+ @ NonNull
907+ public String getIdToken () {
908+ return idToken ;
909+ }
910+ }
911+
767912 /**
768913 * Creates the required client authentication for the token endpoint based on information
769914 * in the most recent registration response (if it is set).
0 commit comments