diff --git a/src/main/java/org/rundeck/api/ApiCall.java b/src/main/java/org/rundeck/api/ApiCall.java index 23e05aa..3ffa5c2 100644 --- a/src/main/java/org/rundeck/api/ApiCall.java +++ b/src/main/java/org/rundeck/api/ApiCall.java @@ -106,11 +106,29 @@ class ApiCall { } /** - * Test the credentials (login/password) on the RunDeck instance + * Test the authentication on the RunDeck instance. Will delegate to either {@link #testLoginAuth()} (in case of + * login-based auth) or {@link #testTokenAuth()} (in case of token-based auth). + * + * @throws RundeckApiLoginException if the login fails (in case of login-based authentication) + * @throws RundeckApiTokenException if the token is invalid (in case of token-based authentication) + * @see #testLoginAuth() + * @see #testTokenAuth() + */ + public void testAuth() throws RundeckApiLoginException, RundeckApiTokenException { + if (client.getToken() != null) { + testTokenAuth(); + } else { + testLoginAuth(); + } + } + + /** + * Test the login-based authentication on the RunDeck instance * * @throws RundeckApiLoginException if the login fails + * @see #testAuth() */ - public void testCredentials() throws RundeckApiLoginException { + public void testLoginAuth() throws RundeckApiLoginException { HttpClient httpClient = instantiateHttpClient(); try { login(httpClient); @@ -119,6 +137,22 @@ class ApiCall { } } + /** + * Test the token-based authentication on the RunDeck instance + * + * @throws RundeckApiTokenException if the token is invalid + * @see #testAuth() + */ + public void testTokenAuth() throws RundeckApiTokenException { + try { + execute(new HttpGet(client.getUrl() + RundeckClient.API_ENDPOINT + "/system/info")); + } catch (RundeckApiTokenException e) { + throw e; + } catch (RundeckApiException e) { + throw new RundeckApiTokenException("Failed to verify token", e); + } + } + /** * Execute an HTTP GET request to the RunDeck instance, on the given path. We will login first, and then execute the * API call. At the end, the given parser will be used to convert the response to a more useful result object. diff --git a/src/main/java/org/rundeck/api/RundeckClient.java b/src/main/java/org/rundeck/api/RundeckClient.java index 98befb1..ec919de 100644 --- a/src/main/java/org/rundeck/api/RundeckClient.java +++ b/src/main/java/org/rundeck/api/RundeckClient.java @@ -32,7 +32,6 @@ import org.rundeck.api.RundeckApiException.RundeckApiLoginException; import org.rundeck.api.RundeckApiException.RundeckApiTokenException; import org.rundeck.api.domain.RundeckAbort; import org.rundeck.api.domain.RundeckExecution; -import org.rundeck.api.domain.RundeckExecution.ExecutionStatus; import org.rundeck.api.domain.RundeckHistory; import org.rundeck.api.domain.RundeckJob; import org.rundeck.api.domain.RundeckJobsImportMethod; @@ -40,6 +39,7 @@ import org.rundeck.api.domain.RundeckJobsImportResult; import org.rundeck.api.domain.RundeckNode; import org.rundeck.api.domain.RundeckProject; import org.rundeck.api.domain.RundeckSystemInfo; +import org.rundeck.api.domain.RundeckExecution.ExecutionStatus; import org.rundeck.api.parser.AbortParser; import org.rundeck.api.parser.ExecutionParser; import org.rundeck.api.parser.HistoryParser; @@ -158,12 +158,22 @@ public class RundeckClient implements Serializable { } /** - * Test your credentials (login/password) on the RunDeck instance + * Test the authentication on the RunDeck instance. * - * @throws RundeckApiLoginException if the login fails + * @throws RundeckApiLoginException if the login fails (in case of login-based authentication) + * @throws RundeckApiTokenException if the token is invalid (in case of token-based authentication) */ - public void testCredentials() throws RundeckApiLoginException { - new ApiCall(this).testCredentials(); + public void testAuth() throws RundeckApiLoginException, RundeckApiTokenException { + new ApiCall(this).testAuth(); + } + + /** + * @deprecated Use {@link #testAuth()} + * @see #testAuth() + */ + @Deprecated + public void testCredentials() throws RundeckApiLoginException, RundeckApiTokenException { + testAuth(); } /*