diff --git a/src/main/java/com/taboola/backstage/internal/FieldsValidator.java b/src/main/java/com/taboola/backstage/internal/FieldsValidator.java index cca1260..4d1d72b 100644 --- a/src/main/java/com/taboola/backstage/internal/FieldsValidator.java +++ b/src/main/java/com/taboola/backstage/internal/FieldsValidator.java @@ -60,7 +60,7 @@ private static void validate(Object obj, Map, BiFunction A createAuthEndpoint(Class clazz) { Objects.requireNonNull(clazz, "clazz"); - logger.debug("creating authentication endpoint using retrofit for class [{}]", clazz); + logger.debug("creating authentication endpoint using retrofit for class [{}]", clazz::toString); return communicationFactory.createRetrofitAuthEndpoint(clazz); } @Override public E createEndpoint(Class clazz) { Objects.requireNonNull(clazz, "clazz"); - logger.debug("creating endpoint using retrofit for class [{}]", clazz); + logger.debug("creating endpoint using retrofit for class [{}]", clazz::toString); return communicationFactory.createRetrofitEndpoint(clazz); } } diff --git a/src/main/java/com/taboola/backstage/services/AuthenticationService.java b/src/main/java/com/taboola/backstage/services/AuthenticationService.java index 337044f..8913dd8 100644 --- a/src/main/java/com/taboola/backstage/services/AuthenticationService.java +++ b/src/main/java/com/taboola/backstage/services/AuthenticationService.java @@ -59,6 +59,16 @@ public interface AuthenticationService { */ BackstageAuthentication reAuthenticate(BackstageAuthentication auth) throws BackstageAPIUnauthorizedException, BackstageAPIConnectivityException, BackstageAPIRequestException; + + /** + * Creates {@link BackstageAuthentication} based on received access token string. + *

Keep in mind after such authentication it is NOT possible to use {@link #reAuthenticate(BackstageAuthentication) reAuthenticate} method.

+ * + * @param accessToken supplied by authentication {@link com.taboola.backstage.model.auth.Token Token} + * @return {@link BackstageAuthentication} object which is being required by all services + */ + BackstageAuthentication authenticate(String accessToken); + /** * Specific {@link com.taboola.backstage.model.auth.Token Token} metadata * diff --git a/src/main/java/com/taboola/backstage/services/AuthenticationServiceImpl.java b/src/main/java/com/taboola/backstage/services/AuthenticationServiceImpl.java index cb08406..7619ef1 100644 --- a/src/main/java/com/taboola/backstage/services/AuthenticationServiceImpl.java +++ b/src/main/java/com/taboola/backstage/services/AuthenticationServiceImpl.java @@ -6,6 +6,8 @@ import com.taboola.backstage.model.auth.*; import com.taboola.backstage.internal.BackstageAuthenticationEndpoint; +import java.util.Objects; + /** * Created by vladi * Date: 10/14/2017 @@ -35,8 +37,17 @@ public BackstageAuthentication passwordAuthentication(String clientId, String cl @Override public BackstageAuthentication reAuthenticate(BackstageAuthentication auth) throws BackstageAPIUnauthorizedException, BackstageAPIConnectivityException, BackstageAPIRequestException { AuthenticationDetails details = auth.getDetails(); + if(details == null) { + throw new IllegalStateException("Can't reAuthenticate without authentication details"); + } + + GrantType grantType = details.getGrantType(); + if(grantType == null) { + throw new IllegalStateException("Unknown grant type"); + } + //In order to save some usability pain paying here with down cast... - switch (details.getGrantType()) { + switch (grantType) { case CLIENT_CREDENTIALS: if(details instanceof ClientCredentialAuthenticationDetails) { ClientCredentialAuthenticationDetails clientCredentialDetails = (ClientCredentialAuthenticationDetails) details; @@ -58,6 +69,14 @@ public BackstageAuthentication reAuthenticate(BackstageAuthentication auth) thro } } + @Override + public BackstageAuthentication authenticate(String accessToken) { + Objects.requireNonNull(accessToken, "accessToken"); + Token token = new Token(); + token.setAccessToken(accessToken); + return new BackstageAuthentication(null, token); + } + @Override public TokenDetails getTokenDetails(BackstageAuthentication auth) throws BackstageAPIUnauthorizedException, BackstageAPIConnectivityException, BackstageAPIRequestException { return endpoint.getTokenDetails(auth.getToken().getAccessTokenForHeader()); diff --git a/src/test/java/com/taboola/backstage/services/AuthenticationServiceImplTest.java b/src/test/java/com/taboola/backstage/services/AuthenticationServiceImplTest.java index 4bb8dd4..019a7db 100644 --- a/src/test/java/com/taboola/backstage/services/AuthenticationServiceImplTest.java +++ b/src/test/java/com/taboola/backstage/services/AuthenticationServiceImplTest.java @@ -148,4 +148,22 @@ public GrantType getGrantType() { testInstance.reAuthenticate(invalidInstance); } + + @Test(expected = IllegalStateException.class) + public void testInvalidGrantType_expectingException() { + BackstageAuthentication invalidInstance = new BackstageAuthentication(null, new Token()); + testInstance.reAuthenticate(invalidInstance); + } + + @Test(expected = IllegalStateException.class) + public void testNullAuthenticationDetails_expectingException() { + BackstageAuthentication invalidInstance = new BackstageAuthentication(new AuthenticationDetails(GrantType.CLIENT_CREDENTIALS) { + @Override + public GrantType getGrantType() { + return null; + } + }, new Token()); + + testInstance.reAuthenticate(invalidInstance); + } }