Skip to content

Commit

Permalink
Added authentication method to allow an easy way to generate Backstag…
Browse files Browse the repository at this point in the history
…eAuthentication
  • Loading branch information
vladimanaev committed Apr 9, 2018
1 parent 98fd8bb commit 999c290
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
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.</
*
* @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 999c290

Please sign in to comment.