SPRINT-18: fixed unit test on auth0 reuse

This commit is contained in:
Tim Chamberlain
2023-01-04 22:20:38 -06:00
parent 2874b98b66
commit de05e4ae58
4 changed files with 44 additions and 10 deletions

View File

@ -167,6 +167,13 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@ -193,7 +193,7 @@ public class Auth0AuthenticationModule implements QAuthenticationModuleInterface
/*******************************************************************************
**
*******************************************************************************/
private static String getIdTokenFromBase64BasicAuthCredentials(AuthAPI auth, String base64Credentials) throws Auth0Exception
private String getIdTokenFromBase64BasicAuthCredentials(AuthAPI auth, String base64Credentials) throws Auth0Exception
{
////////////////////////////////////////////////////////////////////////////////
// look for a fresh idToken in the state provider for this set of credentials //
@ -211,9 +211,25 @@ public class Auth0AuthenticationModule implements QAuthenticationModuleInterface
}
}
//////////////////////////////////////////////////////////////////////////////
// not found in cache, make request to auth0 and cache the returned idToken //
//////////////////////////////////////////////////////////////////////////////
byte[] credDecoded = Base64.getDecoder().decode(base64Credentials);
String credentials = new String(credDecoded, StandardCharsets.UTF_8);
String idToken = getIdTokenFromAuth0(auth, credentials);
stateProvider.put(idTokenStateKey, idToken);
stateProvider.put(timestampStateKey, Instant.now());
return (idToken);
}
/*******************************************************************************
**
*******************************************************************************/
protected String getIdTokenFromAuth0(AuthAPI auth, String credentials) throws Auth0Exception
{
/////////////////////////////////////
// call auth0 with a login request //
/////////////////////////////////////
@ -221,12 +237,7 @@ public class Auth0AuthenticationModule implements QAuthenticationModuleInterface
.setScope("openid email nickname")
.execute();
String idToken = result.getIdToken();
stateProvider.put(idTokenStateKey, idToken);
stateProvider.put(timestampStateKey, Instant.now());
return idToken;
return (result.getIdToken());
}

View File

@ -27,6 +27,7 @@ import java.time.temporal.ChronoUnit;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import com.auth0.exception.Auth0Exception;
import com.kingsrook.qqq.backend.core.exceptions.QAuthenticationException;
import com.kingsrook.qqq.backend.core.instances.QMetaDataVariableInterpreter;
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
@ -47,6 +48,10 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
/*******************************************************************************
@ -247,13 +252,16 @@ public class Auth0AuthenticationModuleTest
**
*******************************************************************************/
@Test
void testBasicAuthSuccess() throws QAuthenticationException
void testBasicAuthSuccess() throws QAuthenticationException, Auth0Exception
{
Map<String, String> context = new HashMap<>();
context.put(BASIC_AUTH_KEY, encodeBasicAuth("darin.kelkhoff@gmail.com", "6-EQ!XzBJ!F*LRVDK6VZY__92!"));
Auth0AuthenticationModule auth0AuthenticationModule = new Auth0AuthenticationModule();
auth0AuthenticationModule.createSession(getQInstance(), context);
Auth0AuthenticationModule auth0Spy = spy(Auth0AuthenticationModule.class);
auth0Spy.createSession(getQInstance(), context);
auth0Spy.createSession(getQInstance(), context);
auth0Spy.createSession(getQInstance(), context);
verify(auth0Spy, times(1)).getIdTokenFromAuth0(any(), any());
}

View File

@ -0,0 +1,8 @@
#!/bin/bash
############################################################################
## resolve-pom-conflicts.sh
## Tries to automatically resove pom conflicts by putting SNAPSHOT back
############################################################################
gsed "/Updated upstream/,/=======/d" pom.xml | gsed "/Stashed/d" > /tmp/temp-pom.xml
mv /tmp/temp-pom.xml pom.xml