Browse Source

implemented test for session service

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
sqlite
Stephan Richter 2 months ago
parent
commit
5d7c638b86
  1. 2
      de.srsoftware.oidc.api/src/main/java/de/srsoftware/oidc/api/SessionService.java
  2. 11
      de.srsoftware.oidc.datastore.file/src/main/java/de/srsoftware/oidc/datastore/file/FileStore.java
  3. 53
      de.srsoftware.oidc.datastore.file/src/test/java/de/srsoftware/oidc/datastore/file/SessionServiceTest.java
  4. 6
      de.srsoftware.oidc.datastore.sqlite/src/main/java/de/srsoftware/oidc/datastore/sqlite/SqliteSessionService.java

2
de.srsoftware.oidc.api/src/main/java/de/srsoftware/oidc/api/SessionService.java

@ -3,7 +3,6 @@ package de.srsoftware.oidc.api; @@ -3,7 +3,6 @@ package de.srsoftware.oidc.api;
import de.srsoftware.oidc.api.data.Session;
import de.srsoftware.oidc.api.data.User;
import java.time.Duration;
import java.util.Optional;
public interface SessionService {
@ -11,5 +10,4 @@ public interface SessionService { @@ -11,5 +10,4 @@ public interface SessionService {
SessionService dropSession(String sessionId);
Session extend(Session session, User user);
Optional<Session> retrieve(String sessionId);
SessionService setDuration(Duration duration);
}

11
de.srsoftware.oidc.datastore.file/src/main/java/de/srsoftware/oidc/datastore/file/FileStore.java

@ -5,6 +5,7 @@ import static de.srsoftware.oidc.api.data.User.*; @@ -5,6 +5,7 @@ import static de.srsoftware.oidc.api.data.User.*;
import static de.srsoftware.utils.Optionals.nullable;
import static de.srsoftware.utils.Strings.uuid;
import static java.lang.System.Logger.Level.*;
import static java.time.temporal.ChronoUnit.SECONDS;
import static java.util.Optional.empty;
import de.srsoftware.oidc.api.*;
@ -17,7 +18,6 @@ import java.io.FileNotFoundException; @@ -17,7 +18,6 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.*;
@ -221,7 +221,7 @@ public class FileStore implements AuthorizationService, ClientService, SessionSe @@ -221,7 +221,7 @@ public class FileStore implements AuthorizationService, ClientService, SessionSe
@Override
public Session createSession(User user) {
var now = Instant.now();
var endOfSession = now.plus(user.sessionDuration());
var endOfSession = now.plus(user.sessionDuration()).truncatedTo(SECONDS);
return save(new Session(user.uuid(), endOfSession, uuid()));
}
@ -247,7 +247,7 @@ public class FileStore implements AuthorizationService, ClientService, SessionSe @@ -247,7 +247,7 @@ public class FileStore implements AuthorizationService, ClientService, SessionSe
try {
var session = sessions().getJSONObject(sessionId);
var userId = session.getString(USER);
var expiration = Instant.ofEpochSecond(session.getLong(EXPIRATION));
var expiration = Instant.ofEpochSecond(session.getLong(EXPIRATION)).truncatedTo(SECONDS);
if (expiration.isAfter(Instant.now())) return Optional.of(new Session(userId, expiration, sessionId));
dropSession(sessionId);
} catch (Exception ignored) {
@ -261,11 +261,6 @@ public class FileStore implements AuthorizationService, ClientService, SessionSe @@ -261,11 +261,6 @@ public class FileStore implements AuthorizationService, ClientService, SessionSe
return session;
}
@Override
public SessionService setDuration(Duration duration) {
return null;
}
/** client service methods **/
@Override

53
de.srsoftware.oidc.datastore.file/src/test/java/de/srsoftware/oidc/datastore/file/SessionServiceTest.java

@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
package de.srsoftware.oidc.datastore.file;
import static de.srsoftware.utils.Strings.uuid;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import de.srsoftware.oidc.api.SessionService;
@ -45,7 +46,7 @@ public class SessionServiceTest { @@ -45,7 +46,7 @@ public class SessionServiceTest {
}
@Test
public void testCreate() {
public void testCreateAndLoad() {
var uuid = uuid();
var pass = hasher().hash(PASSWORD, uuid);
var user = new User(USERNAME, pass, REALNAME, EMAIL, uuid).sessionDuration(Duration.ofMinutes(5));
@ -55,5 +56,55 @@ public class SessionServiceTest { @@ -55,5 +56,55 @@ public class SessionServiceTest {
var expiration = session.expiration();
assertTrue(expiration.isAfter(now.plus(5, ChronoUnit.MINUTES).minusSeconds(1)));
assertTrue(expiration.isBefore(now.plus(5, ChronoUnit.MINUTES).plusSeconds(1)));
var loaded = sessionService.retrieve(session.id());
assertTrue(loaded.isPresent());
assertEquals(session, loaded.get());
}
@Test
public void testCreateAndExtend() {
var uuid = uuid();
var pass = hasher().hash(PASSWORD, uuid);
var user = new User(USERNAME, pass, REALNAME, EMAIL, uuid).sessionDuration(Duration.ofMinutes(5));
var session = sessionService.createSession(user);
Instant now = Instant.now();
sessionService.extend(session, user.sessionDuration(Duration.ofMinutes(10)));
var loaded = sessionService.retrieve(session.id());
assertTrue(loaded.isPresent());
assertEquals(session.id(), loaded.get().id());
var expiration = loaded.get().expiration();
assertTrue(expiration.isAfter(now.plus(10, ChronoUnit.MINUTES).minusSeconds(1)));
assertTrue(expiration.isBefore(now.plus(10, ChronoUnit.MINUTES).plusSeconds(1)));
}
@Test
public void textCreateAndDrop() {
var uuid = uuid();
var pass = hasher().hash(PASSWORD, uuid);
var user = new User(USERNAME, pass, REALNAME, EMAIL, uuid).sessionDuration(Duration.ofMinutes(5));
var session = sessionService.createSession(user);
assertTrue(sessionService.retrieve(session.id()).isPresent());
sessionService.dropSession(session.id());
var loaded = sessionService.retrieve(session.id());
assertTrue(sessionService.retrieve(session.id()).isEmpty());
}
@Test
public void testExpiration() throws InterruptedException {
var uuid = uuid();
var pass = hasher().hash(PASSWORD, uuid);
var user = new User(USERNAME, pass, REALNAME, EMAIL, uuid).sessionDuration(Duration.ofSeconds(2));
var session = sessionService.createSession(user);
assertTrue(sessionService.retrieve(session.id()).isPresent());
Thread.sleep(2500);
assertTrue(sessionService.retrieve(session.id()).isEmpty());
}
}

6
de.srsoftware.oidc.datastore.sqlite/src/main/java/de/srsoftware/oidc/datastore/sqlite/SqliteSessionService.java

@ -5,7 +5,6 @@ import de.srsoftware.oidc.api.SessionService; @@ -5,7 +5,6 @@ import de.srsoftware.oidc.api.SessionService;
import de.srsoftware.oidc.api.data.Session;
import de.srsoftware.oidc.api.data.User;
import java.sql.Connection;
import java.time.Duration;
import java.util.Optional;
public class SqliteSessionService implements SessionService {
@ -31,9 +30,4 @@ public class SqliteSessionService implements SessionService { @@ -31,9 +30,4 @@ public class SqliteSessionService implements SessionService {
public Optional<Session> retrieve(String sessionId) {
return Optional.empty();
}
@Override
public SessionService setDuration(Duration duration) {
return null;
}
}

Loading…
Cancel
Save