Skip to content

Commit 95786c4

Browse files
committed
pac4j: javadoc + renewSessionId
1 parent 4c3be9b commit 95786c4

File tree

10 files changed

+216
-39
lines changed

10 files changed

+216
-39
lines changed

jooby/src/main/java/io/jooby/Session.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import io.jooby.internal.SessionImpl;
99

1010
import javax.annotation.Nonnull;
11+
import javax.annotation.Nullable;
1112
import java.time.Instant;
1213
import java.util.Map;
1314

@@ -22,11 +23,19 @@ public interface Session {
2223
String NAME = "session";
2324

2425
/**
25-
* Session ID.
26+
* Session ID or <code>null</code> for stateless (usually signed) sessions.
2627
*
27-
* @return Session ID.
28+
* @return Session ID or <code>null</code> for stateless (usually signed) sessions.
2829
*/
29-
@Nonnull String getId();
30+
@Nullable String getId();
31+
32+
/**
33+
* Set Session ID.
34+
*
35+
* @param id Session ID or <code>null</code>
36+
* @return Session.
37+
*/
38+
@Nonnull Session setId(@Nullable String id);
3039

3140
/**
3241
* Get a session attribute.
@@ -209,26 +218,32 @@ public interface Session {
209218
*/
210219
void destroy();
211220

221+
/**
222+
* Assign a new ID to the existing session.
223+
* @return This session.
224+
*/
225+
Session renewId();
226+
212227
/**
213228
* Creates a new session.
214229
*
215230
* @param ctx Web context.
216-
* @param id Session ID.
231+
* @param id Session ID or <code>null</code>.
217232
* @return A new session.
218233
*/
219-
static @Nonnull Session create(@Nonnull Context ctx, @Nonnull String id) {
234+
static @Nonnull Session create(@Nonnull Context ctx, @Nullable String id) {
220235
return new SessionImpl(ctx, id);
221236
}
222237

223238
/**
224239
* Creates a new session.
225240
*
226241
* @param ctx Web context.
227-
* @param id Session ID.
242+
* @param id Session ID or <code>null</code>.
228243
* @param data Session attributes.
229244
* @return A new session.
230245
*/
231-
static @Nonnull Session create(@Nonnull Context ctx, @Nonnull String id,
246+
static @Nonnull Session create(@Nonnull Context ctx, @Nullable String id,
232247
@Nonnull Map<String, String> data) {
233248
return new SessionImpl(ctx, id, data);
234249
}

jooby/src/main/java/io/jooby/SessionStore.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ public interface SessionStore {
7676
*/
7777
void saveSession(@Nonnull Context ctx, @Nonnull Session session);
7878

79+
/**
80+
* Renew Session ID. This operation might or might not be implemented by a Session Store.
81+
*
82+
* @param ctx Web Context.
83+
* @param session Session.
84+
*/
85+
void renewSessionId(@Nonnull Context ctx, @Nonnull Session session);
86+
7987
/**
8088
* Creates a cookie based session and store data in memory. Session data is not keep after
8189
* restart.

jooby/src/main/java/io/jooby/internal/MemorySessionStore.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ public MemorySessionStore(SessionToken token) {
8080
new SessionData(session.getCreationTime(), Instant.now(), session.toMap()));
8181
}
8282

83+
@Override public void renewSessionId(@Nonnull Context ctx, @Nonnull Session session) {
84+
String oldId = session.getId();
85+
session.setId(token.newToken());
86+
sessions.remove(oldId);
87+
}
88+
8389
private Session restore(Context ctx, String sessionId, SessionData data) {
8490
return Session.create(ctx, sessionId, data.hash)
8591
.setLastAccessedTime(data.lastAccessedTime)

jooby/src/main/java/io/jooby/internal/SessionImpl.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import io.jooby.ValueNode;
1313

1414
import javax.annotation.Nonnull;
15+
import javax.annotation.Nullable;
1516
import java.time.Instant;
1617
import java.util.Map;
1718
import java.util.concurrent.ConcurrentHashMap;
@@ -60,10 +61,15 @@ public SessionImpl(Context ctx, String id, Map<String, String> attributes) {
6061
return this;
6162
}
6263

63-
@Override public @Nonnull String getId() {
64+
@Override public @Nullable String getId() {
6465
return id;
6566
}
6667

68+
@Nonnull @Override public Session setId(@Nullable String id) {
69+
this.id = id;
70+
return this;
71+
}
72+
6773
@Override public @Nonnull Value get(@Nonnull String name) {
6874
return Value.create(ctx, name, attributes.get(name));
6975
}
@@ -114,6 +120,12 @@ public SessionImpl(Context ctx, String id, Map<String, String> attributes) {
114120
store(ctx).deleteSession(ctx, this);
115121
}
116122

123+
@Override public Session renewId() {
124+
store(ctx).renewSessionId(ctx, this);
125+
updateState();
126+
return this;
127+
}
128+
117129
private void updateState() {
118130
modify = true;
119131
lastAccessedTime = Instant.now();

jooby/src/main/java/io/jooby/internal/SignedSessionStore.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
public class SignedSessionStore implements SessionStore {
2121

22-
private static final String NO_ID = "<new-session>";
23-
2422
private final Function<String, Map<String, String>> decoder;
2523

2624
private final Function<Map<String, String>, String> encoder;
@@ -35,7 +33,7 @@ public SignedSessionStore(SessionToken token, Function<String, Map<String, Strin
3533
}
3634

3735
@Nonnull @Override public Session newSession(@Nonnull Context ctx) {
38-
return Session.create(ctx, NO_ID).setNew(true);
36+
return Session.create(ctx, null).setNew(true);
3937
}
4038

4139
@Nullable @Override public Session findSession(@Nonnull Context ctx) {
@@ -61,4 +59,8 @@ public SignedSessionStore(SessionToken token, Function<String, Map<String, Strin
6159
@Override public void saveSession(@Nonnull Context ctx, @Nonnull Session session) {
6260
// NOOP
6361
}
62+
63+
@Override public void renewSessionId(@Nonnull Context ctx, @Nonnull Session session) {
64+
token.saveToken(ctx, encoder.apply(session.toMap()));
65+
}
6466
}

modules/jooby-jwt/src/main/java/io/jooby/jwt/JwtSessionStore.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ public JwtSessionStore(@Nonnull Key key, @Nonnull SessionToken token) {
101101
store.saveSession(ctx, session);
102102
}
103103

104+
@Override public void renewSessionId(@Nonnull Context ctx, @Nonnull Session session) {
105+
store.renewSessionId(ctx, session);
106+
}
107+
104108
static SneakyThrows.Function<String, Map<String, String>> decoder(Key key) {
105109
return value -> {
106110
try {

modules/jooby-pac4j/src/main/java/io/jooby/internal/pac4j/SessionStoreImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ private Optional<Session> getSessionOrEmpty(Pac4jContext context) {
7272
}
7373

7474
@Override public boolean renewSession(Pac4jContext context) {
75+
getSessionOrEmpty(context).ifPresent(session -> session.renewId());
7576
return true;
7677
}
7778

modules/jooby-pac4j/src/main/java/io/jooby/pac4j/Pac4jContext.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,29 @@
99
import io.jooby.internal.pac4j.WebContextImpl;
1010
import org.pac4j.core.context.WebContext;
1111

12+
import javax.annotation.Nonnull;
13+
14+
/**
15+
* Pac4j web context.
16+
*
17+
* @author edgar
18+
* @since 2.0.0
19+
*/
1220
public interface Pac4jContext extends WebContext {
13-
Context getContext();
21+
/**
22+
* Get underlying context.
23+
*
24+
* @return The underlying context.
25+
*/
26+
@Nonnull Context getContext();
1427

15-
static Pac4jContext create(Context ctx) {
28+
/**
29+
* Wrap a Web context as pac4j context.
30+
*
31+
* @param ctx Web context.
32+
* @return Pac4j web context.
33+
*/
34+
static @Nonnull Pac4jContext create(@Nonnull Context ctx) {
1635
return new WebContextImpl(ctx);
1736
}
1837
}

0 commit comments

Comments
 (0)