use of org.pac4j.core.context.session.SessionStore in project pac4j by pac4j.
the class DefaultCsrfTokenGeneratorTests method test.
@Test
public void test() {
final WebContext context = MockWebContext.create();
final SessionStore sessionStore = new MockSessionStore();
final var token = generator.get(context, sessionStore);
assertNotNull(token);
final var token2 = (String) sessionStore.get(context, Pac4jConstants.CSRF_TOKEN).orElse(null);
assertEquals(token, token2);
final long expirationDate = (Long) sessionStore.get(context, Pac4jConstants.CSRF_TOKEN_EXPIRATION_DATE).orElse(null);
final var nowPlusTtl = new Date().getTime() + 1000 * generator.getTtlInSeconds();
assertTrue(expirationDate > nowPlusTtl - 1000);
assertTrue(expirationDate < nowPlusTtl + 1000);
generator.get(context, sessionStore);
final var token3 = (String) sessionStore.get(context, Pac4jConstants.PREVIOUS_CSRF_TOKEN).orElse(null);
assertEquals(token, token3);
}
use of org.pac4j.core.context.session.SessionStore in project pac4j by pac4j.
the class DefaultAuthorizationCheckerTests method testCsrfCheckPost.
@Test
public void testCsrfCheckPost() {
final var context = MockWebContext.create().setRequestMethod(HttpConstants.HTTP_METHOD.POST.name());
final var generator = new DefaultCsrfTokenGenerator();
final SessionStore sessionStore = new MockSessionStore();
generator.get(context, sessionStore);
assertFalse(checker.isAuthorized(context, sessionStore, profiles, DefaultAuthorizers.CSRF_CHECK, new HashMap<>(), new ArrayList<>()));
}
use of org.pac4j.core.context.session.SessionStore in project pac4j by pac4j.
the class CsrfAuthorizerTests method testParameterNoExpirationDate.
@Test
public void testParameterNoExpirationDate() {
final WebContext context = MockWebContext.create().addRequestParameter(Pac4jConstants.CSRF_TOKEN, VALUE);
final SessionStore sessionStore = new MockSessionStore();
sessionStore.set(context, Pac4jConstants.CSRF_TOKEN, VALUE);
Assert.assertFalse(authorizer.isAuthorized(context, sessionStore, null));
}
use of org.pac4j.core.context.session.SessionStore in project pac4j by pac4j.
the class CsrfAuthorizerTests method testParameterOk.
@Test
public void testParameterOk() {
final WebContext context = MockWebContext.create().addRequestParameter(Pac4jConstants.CSRF_TOKEN, VALUE);
final SessionStore sessionStore = new MockSessionStore();
sessionStore.set(context, Pac4jConstants.CSRF_TOKEN, VALUE);
sessionStore.set(context, Pac4jConstants.CSRF_TOKEN_EXPIRATION_DATE, expirationDate);
Assert.assertTrue(authorizer.isAuthorized(context, sessionStore, null));
}
use of org.pac4j.core.context.session.SessionStore in project pac4j by pac4j.
the class CsrfAuthorizerTests method testParameterOkNewName.
@Test
public void testParameterOkNewName() {
final WebContext context = MockWebContext.create().addRequestParameter(NAME, VALUE);
final SessionStore sessionStore = new MockSessionStore();
sessionStore.set(context, Pac4jConstants.CSRF_TOKEN, VALUE);
sessionStore.set(context, Pac4jConstants.CSRF_TOKEN_EXPIRATION_DATE, expirationDate);
authorizer.setParameterName(NAME);
Assert.assertTrue(authorizer.isAuthorized(context, sessionStore, null));
}
Aggregations