|
13 | 13 | import static io.jooby.StatusCode.SEE_OTHER_CODE;
|
14 | 14 | import static io.jooby.StatusCode.TEMPORARY_REDIRECT_CODE;
|
15 | 15 | import static io.jooby.StatusCode.UNAUTHORIZED_CODE;
|
| 16 | +import static io.jooby.internal.pac4j.Pac4jSession.BIN; |
| 17 | +import static io.jooby.internal.pac4j.Pac4jSession.PAC4J; |
16 | 18 |
|
17 |
| -import java.io.ByteArrayInputStream; |
18 |
| -import java.io.ByteArrayOutputStream; |
19 |
| -import java.io.IOException; |
20 |
| -import java.io.ObjectInputStream; |
21 |
| -import java.io.ObjectOutputStream; |
22 |
| -import java.util.Base64; |
| 19 | +import java.io.*; |
23 | 20 | import java.util.Optional;
|
24 | 21 |
|
25 | 22 | import org.pac4j.core.context.WebContext;
|
|
35 | 32 | import org.pac4j.core.exception.http.UnauthorizedAction;
|
36 | 33 | import org.pac4j.core.exception.http.WithContentAction;
|
37 | 34 | import org.pac4j.core.exception.http.WithLocationAction;
|
| 35 | +import org.pac4j.core.util.serializer.Serializer; |
38 | 36 |
|
39 | 37 | import io.jooby.Context;
|
40 | 38 | import io.jooby.Session;
|
41 |
| -import io.jooby.SneakyThrows; |
42 | 39 | import io.jooby.Value;
|
43 | 40 | import io.jooby.pac4j.Pac4jContext;
|
44 | 41 |
|
45 | 42 | public class SessionStoreImpl implements org.pac4j.core.context.session.SessionStore {
|
46 | 43 |
|
47 |
| - private static final String PAC4J = "p4j~"; |
48 |
| - |
49 |
| - private static final String BIN = "b64~"; |
50 |
| - |
51 | 44 | private Session getSession(WebContext context) {
|
52 | 45 | return context(context).session();
|
53 | 46 | }
|
@@ -75,20 +68,17 @@ public Optional<String> getSessionId(WebContext context, boolean createSession)
|
75 | 68 |
|
76 | 69 | @Override
|
77 | 70 | public Optional<Object> get(WebContext context, String key) {
|
78 |
| - Optional sessionValue = |
79 |
| - getSessionOrEmpty(context) |
80 |
| - .map(session -> session.get(key)) |
81 |
| - .map(SessionStoreImpl::strToObject) |
82 |
| - .orElseGet(Optional::empty); |
83 |
| - return sessionValue; |
| 71 | + return getSessionOrEmpty(context) |
| 72 | + .map(session -> session.get(key)) |
| 73 | + .flatMap(value -> strToObject(context(context).require(Serializer.class), value)); |
84 | 74 | }
|
85 | 75 |
|
86 | 76 | @Override
|
87 | 77 | public void set(WebContext context, String key, Object value) {
|
88 |
| - if (value == null || value.toString().length() == 0) { |
| 78 | + if (value == null || value.toString().isEmpty()) { |
89 | 79 | getSessionOrEmpty(context).ifPresent(session -> session.remove(key));
|
90 | 80 | } else {
|
91 |
| - String encoded = objToStr(value); |
| 81 | + String encoded = objToStr(context(context).require(Serializer.class), value); |
92 | 82 | getSession(context).put(key, encoded);
|
93 | 83 | }
|
94 | 84 | }
|
@@ -116,42 +106,31 @@ public Optional<SessionStore> buildFromTrackableSession(
|
116 | 106 |
|
117 | 107 | @Override
|
118 | 108 | public boolean renewSession(WebContext context) {
|
119 |
| - getSessionOrEmpty(context).ifPresent(session -> session.renewId()); |
120 |
| - return true; |
| 109 | + var session = getSessionOrEmpty(context); |
| 110 | + session.ifPresent(Session::renewId); |
| 111 | + return session.isPresent(); |
121 | 112 | }
|
122 | 113 |
|
123 |
| - static Optional<Object> strToObject(final Value node) { |
| 114 | + static Optional<Object> strToObject(Serializer serializer, Value node) { |
124 | 115 | if (node.isMissing()) {
|
125 | 116 | return Optional.empty();
|
126 | 117 | }
|
127 | 118 | String value = node.value();
|
128 | 119 | if (value.startsWith(BIN)) {
|
129 |
| - try { |
130 |
| - byte[] bytes = Base64.getDecoder().decode(value.substring(BIN.length())); |
131 |
| - return Optional.of(new ObjectInputStream(new ByteArrayInputStream(bytes)).readObject()); |
132 |
| - } catch (Exception x) { |
133 |
| - throw SneakyThrows.propagate(x); |
134 |
| - } |
| 120 | + return Optional.of(serializer.deserializeFromString(value.substring(BIN.length()))); |
135 | 121 | } else if (value.startsWith(PAC4J)) {
|
136 | 122 | return Optional.of(strToAction(value.substring(PAC4J.length())));
|
137 | 123 | }
|
138 | 124 | return Optional.of(value);
|
139 | 125 | }
|
140 | 126 |
|
141 |
| - static String objToStr(final Object value) { |
| 127 | + static String objToStr(Serializer serializer, Object value) { |
142 | 128 | if (value instanceof CharSequence || value instanceof Number || value instanceof Boolean) {
|
143 | 129 | return value.toString();
|
144 | 130 | } else if (value instanceof HttpAction) {
|
145 | 131 | return actionToStr((HttpAction) value);
|
146 |
| - } |
147 |
| - try { |
148 |
| - ByteArrayOutputStream bytes = new ByteArrayOutputStream(); |
149 |
| - ObjectOutputStream stream = new ObjectOutputStream(bytes); |
150 |
| - stream.writeObject(value); |
151 |
| - stream.flush(); |
152 |
| - return BIN + Base64.getEncoder().encodeToString(bytes.toByteArray()); |
153 |
| - } catch (IOException x) { |
154 |
| - throw SneakyThrows.propagate(x); |
| 132 | + } else { |
| 133 | + return BIN + serializer.serializeToString(value); |
155 | 134 | }
|
156 | 135 | }
|
157 | 136 |
|
|
0 commit comments