diff --git a/pom.xml b/pom.xml index b7fd2a16..2a36c546 100644 --- a/pom.xml +++ b/pom.xml @@ -56,7 +56,7 @@ com.auth0 mvc-auth-commons - [1.0, 2.0) + 1.9.2 com.fasterxml.jackson.core diff --git a/src/main/java/com/kingsrook/qqq/backend/core/actions/ActionHelper.java b/src/main/java/com/kingsrook/qqq/backend/core/actions/ActionHelper.java index ae4e8f8a..d5ab7318 100644 --- a/src/main/java/com/kingsrook/qqq/backend/core/actions/ActionHelper.java +++ b/src/main/java/com/kingsrook/qqq/backend/core/actions/ActionHelper.java @@ -22,6 +22,7 @@ package com.kingsrook.qqq.backend.core.actions; +import com.kingsrook.qqq.backend.core.exceptions.QAuthenticationException; import com.kingsrook.qqq.backend.core.exceptions.QException; import com.kingsrook.qqq.backend.core.model.actions.AbstractActionInput; import com.kingsrook.qqq.backend.core.modules.authentication.QAuthenticationModuleDispatcher; @@ -43,7 +44,7 @@ public class ActionHelper QAuthenticationModuleInterface authenticationModule = qAuthenticationModuleDispatcher.getQModule(request.getAuthenticationMetaData()); if(!authenticationModule.isSessionValid(request.getSession())) { - throw new QException("Invalid session in request"); + throw new QAuthenticationException("Invalid session in request"); } } diff --git a/src/main/java/com/kingsrook/qqq/backend/core/exceptions/QAuthenticationException.java b/src/main/java/com/kingsrook/qqq/backend/core/exceptions/QAuthenticationException.java index 58478273..c2237629 100644 --- a/src/main/java/com/kingsrook/qqq/backend/core/exceptions/QAuthenticationException.java +++ b/src/main/java/com/kingsrook/qqq/backend/core/exceptions/QAuthenticationException.java @@ -23,7 +23,7 @@ package com.kingsrook.qqq.backend.core.exceptions; /******************************************************************************* - * Exception thrown while doing module-dispatch + * Exception thrown doing authentication * *******************************************************************************/ public class QAuthenticationException extends QException diff --git a/src/main/java/com/kingsrook/qqq/backend/core/modules/authentication/Auth0AuthenticationModule.java b/src/main/java/com/kingsrook/qqq/backend/core/modules/authentication/Auth0AuthenticationModule.java index 9078a80b..db644312 100644 --- a/src/main/java/com/kingsrook/qqq/backend/core/modules/authentication/Auth0AuthenticationModule.java +++ b/src/main/java/com/kingsrook/qqq/backend/core/modules/authentication/Auth0AuthenticationModule.java @@ -27,6 +27,7 @@ import java.time.Duration; import java.time.Instant; import java.util.Base64; import java.util.Map; +import java.util.Optional; import com.auth0.jwk.Jwk; import com.auth0.jwk.JwkException; import com.auth0.jwk.JwkProvider; @@ -60,7 +61,7 @@ public class Auth0AuthenticationModule implements QAuthenticationModuleInterface private static final int ID_TOKEN_VALIDATION_INTERVAL_SECONDS = 300; - public static final String AUTH0_ID_TOKEN_KEY = "qqq.idToken"; + public static final String AUTH0_ID_TOKEN_KEY = "sessionId"; public static final String TOKEN_NOT_PROVIDED_ERROR = "Id Token was not provided"; public static final String COULD_NOT_DECODE_ERROR = "Unable to decode id token"; @@ -82,9 +83,6 @@ public class Auth0AuthenticationModule implements QAuthenticationModuleInterface String idToken = context.get(AUTH0_ID_TOKEN_KEY); if(idToken == null) { - //////////////////////////////// - // could not decode the token // - //////////////////////////////// logger.warn(TOKEN_NOT_PROVIDED_ERROR); throw (new QAuthenticationException(TOKEN_NOT_PROVIDED_ERROR)); } @@ -166,9 +164,10 @@ public class Auth0AuthenticationModule implements QAuthenticationModuleInterface StateProviderInterface spi = getStateProvider(); Auth0StateKey key = new Auth0StateKey(session.getIdReference()); - if(spi.get(Instant.class, key).isPresent()) + Optional lastTimeCheckedOptional = spi.get(Instant.class, key); + if(lastTimeCheckedOptional.isPresent()) { - Instant lastTimeChecked = spi.get(Instant.class, key).get(); + Instant lastTimeChecked = lastTimeCheckedOptional.get(); /////////////////////////////////////////////////////////////////////////////////////////////////// // returns negative int if less than compared duration, 0 if equal, positive int if greater than // @@ -250,8 +249,15 @@ public class Auth0AuthenticationModule implements QAuthenticationModuleInterface JSONObject payload = new JSONObject(payloadString); QUser qUser = new QUser(); - qUser.setIdReference(payload.getString("email")); qUser.setFullName(payload.getString("name")); + if(payload.has("email")) + { + qUser.setIdReference(payload.getString("email")); + } + else + { + qUser.setIdReference(payload.getString("nickname")); + } QSession qSession = new QSession(); qSession.setIdReference(idToken); diff --git a/src/test/java/com/kingsrook/qqq/backend/core/modules/authentication/Auth0AuthenticationModuleTest.java b/src/test/java/com/kingsrook/qqq/backend/core/modules/authentication/Auth0AuthenticationModuleTest.java index 219eba62..80e10109 100644 --- a/src/test/java/com/kingsrook/qqq/backend/core/modules/authentication/Auth0AuthenticationModuleTest.java +++ b/src/test/java/com/kingsrook/qqq/backend/core/modules/authentication/Auth0AuthenticationModuleTest.java @@ -37,7 +37,8 @@ import static com.kingsrook.qqq.backend.core.modules.authentication.Auth0Authent import static com.kingsrook.qqq.backend.core.modules.authentication.Auth0AuthenticationModule.EXPIRED_TOKEN_ERROR; import static com.kingsrook.qqq.backend.core.modules.authentication.Auth0AuthenticationModule.INVALID_TOKEN_ERROR; import static com.kingsrook.qqq.backend.core.modules.authentication.Auth0AuthenticationModule.TOKEN_NOT_PROVIDED_ERROR; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.fail; @@ -73,8 +74,8 @@ public class Auth0AuthenticationModuleTest Auth0AuthenticationModule auth0AuthenticationModule = new Auth0AuthenticationModule(); auth0AuthenticationModule.setNow(now); QSession session = auth0AuthenticationModule.createSession(getQInstance(), context); - assertTrue(session.getUser().getIdReference().equals("tim.chamberlain@kingsrook.com")); - assertTrue(session.getUser().getFullName().equals("Tim Chamberlain")); + assertEquals("tim.chamberlain@kingsrook.com", session.getUser().getIdReference(), "Id should be Tim's email."); + assertEquals("Tim Chamberlain", session.getUser().getFullName(), "Full name should be Tim's full name (well without the middle name)."); } @@ -93,14 +94,12 @@ public class Auth0AuthenticationModuleTest { Auth0AuthenticationModule auth0AuthenticationModule = new Auth0AuthenticationModule(); auth0AuthenticationModule.createSession(getQInstance(), context); + fail("Should never get here"); } catch(QAuthenticationException qae) { - assertTrue(qae.getMessage().contains(INVALID_TOKEN_ERROR)); - return; + assertThat(qae.getMessage()).contains(INVALID_TOKEN_ERROR); } - - fail("Should never get here"); } @@ -119,14 +118,12 @@ public class Auth0AuthenticationModuleTest { Auth0AuthenticationModule auth0AuthenticationModule = new Auth0AuthenticationModule(); auth0AuthenticationModule.createSession(getQInstance(), context); + fail("Should never get here"); } catch(QAuthenticationException qae) { - assertTrue(qae.getMessage().contains(COULD_NOT_DECODE_ERROR)); - return; + assertThat(qae.getMessage()).contains(COULD_NOT_DECODE_ERROR); } - - fail("Should never get here"); } @@ -145,14 +142,12 @@ public class Auth0AuthenticationModuleTest { Auth0AuthenticationModule auth0AuthenticationModule = new Auth0AuthenticationModule(); auth0AuthenticationModule.createSession(getQInstance(), context); + fail("Should never get here"); } catch(QAuthenticationException qae) { - assertTrue(qae.getMessage().contains(EXPIRED_TOKEN_ERROR)); - return; + assertThat(qae.getMessage()).contains(EXPIRED_TOKEN_ERROR); } - - fail("Should never get here"); } @@ -168,14 +163,12 @@ public class Auth0AuthenticationModuleTest { Auth0AuthenticationModule auth0AuthenticationModule = new Auth0AuthenticationModule(); auth0AuthenticationModule.createSession(getQInstance(), new HashMap<>()); + fail("Should never get here"); } catch(QAuthenticationException qae) { - assertTrue(qae.getMessage().contains(TOKEN_NOT_PROVIDED_ERROR)); - return; + assertThat(qae.getMessage()).contains(TOKEN_NOT_PROVIDED_ERROR); } - - fail("Should never get here"); } @@ -194,14 +187,12 @@ public class Auth0AuthenticationModuleTest { Auth0AuthenticationModule auth0AuthenticationModule = new Auth0AuthenticationModule(); auth0AuthenticationModule.createSession(getQInstance(), context); + fail("Should never get here"); } catch(QAuthenticationException qae) { - assertTrue(qae.getMessage().contains(TOKEN_NOT_PROVIDED_ERROR)); - return; + assertThat(qae.getMessage()).contains(TOKEN_NOT_PROVIDED_ERROR); } - - fail("Should never get here"); }