Skip to content

Commit

Permalink
Merge pull request #6 from taboola/auth-and-log-fixes
Browse files Browse the repository at this point in the history
Auth and log fixes
  • Loading branch information
vladimanaev authored Apr 10, 2018
2 parents 98fd8bb + 62f5fe5 commit a18d22b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private static void validate(Object obj, Map<Class<?>, BiFunction<Object, Field,
}

} catch (IllegalAccessException e) {
logger.error("Failed to pull field data from field [{}] for annotation [{}]", field.getName(), annotation);
logger.error("Failed to pull field data from field [{}] for annotation [{}]", field.getName(), annotation.toString());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ public BackstageEndpointsRetrofitFactory(CommunicationFactory communicationFacto
@Override
public <A> A createAuthEndpoint(Class<A> 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> E createEndpoint(Class<E> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ public interface AuthenticationService {
*/
BackstageAuthentication reAuthenticate(BackstageAuthentication auth) throws BackstageAPIUnauthorizedException, BackstageAPIConnectivityException, BackstageAPIRequestException;


/**
* Creates {@link BackstageAuthentication} based on received access token string.
* <p>Keep in mind after such authentication it is NOT possible to use {@link #reAuthenticate(BackstageAuthentication) reAuthenticate} method.</p>
*
* @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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit a18d22b

Please sign in to comment.