4646/**
4747 * API client for Auth0 Authentication API.
4848 *
49+ * <pre><code>
50+ * Auth0 auth0 = new Auth0("your_client_id", "your_domain");
51+ * AuthenticationAPIClient client = new AuthenticationAPIClient(auth0);
52+ * </code></pre>
4953 * @see <a href="https://auth0.com/docs/auth-api">Auth API docs</a>
5054 */
5155public class AuthenticationAPIClient {
@@ -72,7 +76,6 @@ public class AuthenticationAPIClient {
7276 private static final String RESOURCE_OWNER_PATH = "ro" ;
7377 private static final String TOKEN_INFO_PATH = "tokeninfo" ;
7478 private static final String OAUTH_CODE_KEY = "code" ;
75- private static final String OAUTH_CODE_VERIFIER_KEY = "code_verifier" ;
7679 private static final String REDIRECT_URI_KEY = "redirect_uri" ;
7780
7881 private final Auth0 auth0 ;
@@ -680,6 +683,52 @@ public ProfileRequest getProfileAfter(AuthenticationRequest authenticationReques
680683 return new ProfileRequest (authenticationRequest , profileRequest );
681684 }
682685
686+ /**
687+ * Fetch the token information from Auth0, using the authorization_code grant type
688+ *
689+ * For Public Client, e.g. Android apps ,you need to provide the code_verifier
690+ * used to generate the challenge sent to Auth0 {@literal /authorize} method like:
691+ *
692+ * <pre>{@code
693+ * AuthenticationAPIClient client = new AuthenticationAPIClient(new Auth0("clientId", "domain"));
694+ * client
695+ * .token("code", "redirect_uri")
696+ * .setCodeVerifier("code_verifier")
697+ * .start(new Callback<Credentials> {...});
698+ * }</pre>
699+ *
700+ * For the rest of clients, clients who can safely keep a {@literal client_secret}, you need to provide it instead like:
701+ *
702+ * <pre>{@code
703+ * AuthenticationAPIClient client = new AuthenticationAPIClient(new Auth0("clientId", "domain"));
704+ * client
705+ * .token("code", "redirect_uri")
706+ * .setClientSecret("client_secret")
707+ * .start(new Callback<Credentials> {...});
708+ * }</pre>
709+ *
710+ * @param authorizationCode the authorization code received from the /authorize call.
711+ * @param redirectUri the uri sent to /authorize as the 'redirect_uri'.
712+ * @return a request to obtain access_token by exchanging a authorization code.
713+ */
714+ @ SuppressWarnings ("WeakerAccess" )
715+ public TokenRequest token (String authorizationCode , String redirectUri ) {
716+ Map <String , Object > parameters = ParameterBuilder .newBuilder ()
717+ .setClientId (getClientId ())
718+ .setGrantType (GRANT_TYPE_AUTHORIZATION_CODE )
719+ .set (OAUTH_CODE_KEY , authorizationCode )
720+ .set (REDIRECT_URI_KEY , redirectUri )
721+ .asDictionary ();
722+
723+ HttpUrl url = HttpUrl .parse (auth0 .getDomainUrl ()).newBuilder ()
724+ .addPathSegment (OAUTH_PATH )
725+ .addPathSegment (TOKEN_PATH )
726+ .build ();
727+
728+ ParameterizableRequest <Credentials > request = factory .POST (url , client , gson , Credentials .class ).addParameters (parameters );
729+ return new TokenRequest (request );
730+ }
731+
683732 private AuthenticationRequest loginWithResourceOwner (Map <String , Object > parameters ) {
684733 HttpUrl url = HttpUrl .parse (auth0 .getDomainUrl ()).newBuilder ()
685734 .addPathSegment (OAUTH_PATH )
@@ -703,29 +752,4 @@ private ParameterizableRequest<UserProfile> profileRequest() {
703752 return factory .POST (url , client , gson , UserProfile .class );
704753 }
705754
706- /**
707- * Fetch the token information from Auth0, using the authorization_code grant type
708- *
709- * @param authorizationCode the authorization code received from the /authorize call.
710- * @param codeVerifier the code verifier used when requesting a code to /authorize.
711- * @param redirectUri the uri to redirect after a successful request.
712- * @return a request to configure and start
713- */
714- public AuthenticationRequest token (String authorizationCode , String codeVerifier , String redirectUri ) {
715- Map <String , Object > parameters = ParameterBuilder .newBuilder ()
716- .setClientId (getClientId ())
717- .setGrantType (GRANT_TYPE_AUTHORIZATION_CODE )
718- .set (OAUTH_CODE_KEY , authorizationCode )
719- .set (OAUTH_CODE_VERIFIER_KEY , codeVerifier )
720- .set (REDIRECT_URI_KEY , redirectUri )
721- .asDictionary ();
722-
723- HttpUrl url = HttpUrl .parse (auth0 .getDomainUrl ()).newBuilder ()
724- .addPathSegment (OAUTH_PATH )
725- .addPathSegment (TOKEN_PATH )
726- .build ();
727-
728- return factory .authenticationPOST (url , client , gson )
729- .addAuthenticationParameters (parameters );
730- }
731755}
0 commit comments