@@ -843,15 +843,20 @@ public TokenRequest exchangeToken(String subjectToken, String subjectTokenType)
843843 }
844844
845845 /**
846- * Creates a request to exchange an Auth0 refresh token for a federated identity provider's access token
846+ * Creates a request to exchange a subject token for a federated identity provider's access token
847847 * using the Token Vault grant
848848 * {@code urn:auth0:params:oauth:grant-type:token-exchange:federated-connection-access-token}.
849+ * This is the generic entry point: the caller supplies both the {@code subjectToken} and its
850+ * {@code subjectTokenType}, so no assumption is made about which kind of token is being exchanged.
851+ * For the common cases, prefer {@link #getTokenForConnectionWithRefreshToken(String, String, String)}
852+ * or {@link #getTokenForConnectionWithAccessToken(String, String, String)}.
849853 * The connection must have the token vault enabled, and client authentication
850854 * (client secret or client assertion) is required as this must be a private client.
851855 * <pre>
852856 * {@code
853857 * try {
854- * TokenHolder result = authAPI.getTokenForConnection("google-oauth2", refreshToken, "google-user-id")
858+ * TokenHolder result = authAPI.getTokenForConnection(
859+ * "google-oauth2", subjectToken, "urn:ietf:params:oauth:token-type:refresh_token", "google-user-id")
855860 * .execute()
856861 * .getBody();
857862 * String federatedAccessToken = result.getAccessToken();
@@ -864,22 +869,27 @@ public TokenRequest exchangeToken(String subjectToken, String subjectTokenType)
864869 * @see <a href="https://auth0.com/docs/secure/tokens/token-vault">Token Vault documentation</a>
865870 * @param connection the name of the federated connection to obtain an access token for
866871 * (for example {@code google-oauth2}). Must not be null.
867- * @param refreshToken a valid Auth0 refresh token to exchange. Must not be null.
872+ * @param subjectToken the Auth0 token to exchange. Must not be null.
873+ * @param subjectTokenType the identifier for the type of {@code subjectToken}, for example
874+ * {@code urn:ietf:params:oauth:token-type:refresh_token} or
875+ * {@code urn:ietf:params:oauth:token-type:access_token}. Must not be null.
868876 * @param loginHint the user's ID within the identity provider specified by the connection
869877 * (for example, the Google user ID when the connection is {@code google-oauth2}).
870878 * May be null, in which case no {@code login_hint} is sent.
871879 * @return a Request to configure and execute.
872880 */
873- public TokenRequest getTokenForConnection (String connection , String refreshToken , String loginHint ) {
881+ public TokenRequest getTokenForConnection (
882+ String connection , String subjectToken , String subjectTokenType , String loginHint ) {
874883 Asserts .assertNotNull (connection , "connection" );
875- Asserts .assertNotNull (refreshToken , "refresh token" );
884+ Asserts .assertNotNull (subjectToken , "subject token" );
885+ Asserts .assertNotNull (subjectTokenType , "subject token type" );
876886
877887 TokenRequest request = new TokenRequest (client , getTokenUrl ());
878888 request .addParameter (KEY_CLIENT_ID , clientId );
879889 request .addParameter (
880890 KEY_GRANT_TYPE , "urn:auth0:params:oauth:grant-type:token-exchange:federated-connection-access-token" );
881- request .addParameter (KEY_SUBJECT_TOKEN , refreshToken );
882- request .addParameter (KEY_SUBJECT_TOKEN_TYPE , "urn:ietf:params:oauth:token-type:refresh_token" );
891+ request .addParameter (KEY_SUBJECT_TOKEN , subjectToken );
892+ request .addParameter (KEY_SUBJECT_TOKEN_TYPE , subjectTokenType );
883893 request .addParameter (
884894 KEY_REQUESTED_TOKEN_TYPE , "http://auth0.com/oauth/token-type/federated-connection-access-token" );
885895 request .addParameter (KEY_CONNECTION , connection );
@@ -890,6 +900,97 @@ public TokenRequest getTokenForConnection(String connection, String refreshToken
890900 return request ;
891901 }
892902
903+ /**
904+ * Convenience method to exchange an Auth0 refresh token for a federated identity provider's access token
905+ * using the Token Vault grant. Delegates to
906+ * {@link #getTokenForConnection(String, String, String, String)} with the subject token type
907+ * {@code urn:ietf:params:oauth:token-type:refresh_token}.
908+ * The connection must have the token vault enabled, and client authentication
909+ * (client secret or client assertion) is required as this must be a private client.
910+ * <pre>
911+ * {@code
912+ * try {
913+ * TokenHolder result = authAPI.getTokenForConnectionWithRefreshToken("google-oauth2", refreshToken, "google-user-id")
914+ * .execute()
915+ * .getBody();
916+ * String federatedAccessToken = result.getAccessToken();
917+ * } catch (Auth0Exception e) {
918+ * //Something happened
919+ * }
920+ * }
921+ * </pre>
922+ *
923+ * @see <a href="https://auth0.com/docs/secure/tokens/token-vault">Token Vault documentation</a>
924+ * @param connection the name of the federated connection to obtain an access token for
925+ * (for example {@code google-oauth2}). Must not be null.
926+ * @param refreshToken a valid Auth0 refresh token to exchange. Must not be null.
927+ * @param loginHint the user's ID within the identity provider specified by the connection
928+ * (for example, the Google user ID when the connection is {@code google-oauth2}).
929+ * May be null, in which case no {@code login_hint} is sent.
930+ * @return a Request to configure and execute.
931+ */
932+ public TokenRequest getTokenForConnectionWithRefreshToken (
933+ String connection , String refreshToken , String loginHint ) {
934+ Asserts .assertNotNull (refreshToken , "refresh token" );
935+ return getTokenForConnection (
936+ connection , refreshToken , "urn:ietf:params:oauth:token-type:refresh_token" , loginHint );
937+ }
938+
939+ /**
940+ * Convenience method to exchange an Auth0 access token for a federated identity provider's access token
941+ * using the Token Vault grant. Delegates to
942+ * {@link #getTokenForConnection(String, String, String, String)} with the subject token type
943+ * {@code urn:ietf:params:oauth:token-type:access_token}.
944+ * The connection must have the token vault enabled, and client authentication
945+ * (client secret or client assertion) is required as this must be a private client.
946+ * <pre>
947+ * {@code
948+ * try {
949+ * TokenHolder result = authAPI.getTokenForConnectionWithAccessToken("google-oauth2", accessToken, "google-user-id")
950+ * .execute()
951+ * .getBody();
952+ * String federatedAccessToken = result.getAccessToken();
953+ * } catch (Auth0Exception e) {
954+ * //Something happened
955+ * }
956+ * }
957+ * </pre>
958+ *
959+ * @see <a href="https://auth0.com/docs/secure/tokens/token-vault">Token Vault documentation</a>
960+ * @param connection the name of the federated connection to obtain an access token for
961+ * (for example {@code google-oauth2}). Must not be null.
962+ * @param accessToken a valid Auth0 access token to exchange. Must not be null.
963+ * @param loginHint the user's ID within the identity provider specified by the connection
964+ * (for example, the Google user ID when the connection is {@code google-oauth2}).
965+ * May be null, in which case no {@code login_hint} is sent.
966+ * @return a Request to configure and execute.
967+ */
968+ public TokenRequest getTokenForConnectionWithAccessToken (String connection , String accessToken , String loginHint ) {
969+ Asserts .assertNotNull (accessToken , "access token" );
970+ return getTokenForConnection (
971+ connection , accessToken , "urn:ietf:params:oauth:token-type:access_token" , loginHint );
972+ }
973+
974+ /**
975+ * Creates a request to exchange an Auth0 refresh token for a federated identity provider's access token
976+ * using the Token Vault grant.
977+ *
978+ * @deprecated Use {@link #getTokenForConnectionWithRefreshToken(String, String, String)} to exchange a
979+ * refresh token, or {@link #getTokenForConnectionWithAccessToken(String, String, String)} to
980+ * exchange an access token. For full control over the subject token type, use
981+ * {@link #getTokenForConnection(String, String, String, String)}.
982+ * @param connection the name of the federated connection to obtain an access token for
983+ * (for example {@code google-oauth2}). Must not be null.
984+ * @param refreshToken a valid Auth0 refresh token to exchange. Must not be null.
985+ * @param loginHint the user's ID within the identity provider specified by the connection.
986+ * May be null, in which case no {@code login_hint} is sent.
987+ * @return a Request to configure and execute.
988+ */
989+ @ Deprecated
990+ public TokenRequest getTokenForConnection (String connection , String refreshToken , String loginHint ) {
991+ return getTokenForConnectionWithRefreshToken (connection , refreshToken , loginHint );
992+ }
993+
893994 /**
894995 * Creates a request to revoke an existing Refresh Token.
895996 * Confidential clients (Regular Web Apps) <strong>must</strong> have a client secret configured on this {@code AuthAPI} instance.
0 commit comments