6
6
package io .jooby .internal .pac4j ;
7
7
8
8
import io .jooby .Session ;
9
- import io .jooby .SneakyThrows ;
10
9
import io .jooby .Value ;
11
10
import io .jooby .pac4j .Pac4jContext ;
12
11
import org .pac4j .core .context .session .SessionStore ;
21
20
import org .pac4j .core .exception .http .UnauthorizedAction ;
22
21
import org .pac4j .core .exception .http .WithContentAction ;
23
22
import org .pac4j .core .exception .http .WithLocationAction ;
23
+ import org .pac4j .core .util .JavaSerializationHelper ;
24
24
25
- import java .io .ByteArrayInputStream ;
26
- import java .io .ByteArrayOutputStream ;
27
- import java .io .IOException ;
28
- import java .io .ObjectInputStream ;
29
- import java .io .ObjectOutputStream ;
30
- import java .util .Base64 ;
25
+ import java .io .*;
31
26
import java .util .Optional ;
32
27
33
28
import static io .jooby .StatusCode .BAD_REQUEST_CODE ;
42
37
public class SessionStoreImpl
43
38
implements org .pac4j .core .context .session .SessionStore <Pac4jContext > {
44
39
45
- private static final String PAC4J = "p4j~" ;
46
-
47
- private static final String BIN = "b64~" ;
48
-
49
40
private Session getSession (Pac4jContext context ) {
50
- return context .getContext ().session ();
41
+ return session (context .getContext ().session ());
42
+ }
43
+
44
+ private Session session (Session session ) {
45
+ if (session instanceof Pac4jSession ) {
46
+ return ((Pac4jSession ) session ).getSession ();
47
+ }
48
+ return session ;
51
49
}
52
50
53
51
private Optional <Session > getSessionOrEmpty (Pac4jContext context ) {
54
- return Optional .ofNullable (context .getContext ().sessionOrNull ());
52
+ return Optional .ofNullable (session ( context .getContext ().sessionOrNull () ));
55
53
}
56
54
57
55
@ Override public String getOrCreateSessionId (Pac4jContext context ) {
@@ -94,7 +92,7 @@ private Optional<Session> getSessionOrEmpty(Pac4jContext context) {
94
92
}
95
93
96
94
@ Override public boolean renewSession (Pac4jContext context ) {
97
- // getSessionOrEmpty(context).ifPresent(session -> session. renewId() );
95
+ getSessionOrEmpty (context ).ifPresent (Session :: renewId );
98
96
return true ;
99
97
}
100
98
@@ -103,15 +101,11 @@ static Optional<Object> strToObject(final Value node) {
103
101
return Optional .empty ();
104
102
}
105
103
String value = node .value ();
106
- if (value .startsWith (BIN )) {
107
- try {
108
- byte [] bytes = Base64 .getDecoder ().decode (value .substring (BIN .length ()));
109
- return Optional .of (new ObjectInputStream (new ByteArrayInputStream (bytes )).readObject ());
110
- } catch (Exception x ) {
111
- throw SneakyThrows .propagate (x );
112
- }
113
- } else if (value .startsWith (PAC4J )) {
114
- return Optional .of (strToAction (value .substring (PAC4J .length ())));
104
+ if (value .startsWith (Pac4jSession .BIN )) {
105
+ JavaSerializationHelper helper = new JavaSerializationHelper ();
106
+ return Optional .of (helper .deserializeFromBase64 (value .substring (Pac4jSession .BIN .length ())));
107
+ } else if (value .startsWith (Pac4jSession .PAC4J )) {
108
+ return Optional .of (strToAction (value .substring (Pac4jSession .PAC4J .length ())));
115
109
}
116
110
return Optional .of (value );
117
111
}
@@ -121,21 +115,17 @@ static String objToStr(final Object value) {
121
115
return value .toString ();
122
116
} else if (value instanceof HttpAction ) {
123
117
return actionToStr ((HttpAction ) value );
124
- }
125
- try {
126
- ByteArrayOutputStream bytes = new ByteArrayOutputStream ();
127
- ObjectOutputStream stream = new ObjectOutputStream (bytes );
128
- stream .writeObject (value );
129
- stream .flush ();
130
- return BIN + Base64 .getEncoder ().encodeToString (bytes .toByteArray ());
131
- } catch (IOException x ) {
132
- throw SneakyThrows .propagate (x );
118
+ } else if (value instanceof Serializable ) {
119
+ JavaSerializationHelper helper = new JavaSerializationHelper ();
120
+ return Pac4jSession .BIN + helper .serializeToBase64 ((Serializable ) value );
121
+ } else {
122
+ throw new UnsupportedOperationException ("Unsupported type: " + value .getClass ().getName ());
133
123
}
134
124
}
135
125
136
126
private static String actionToStr (HttpAction action ) {
137
127
StringBuilder buffer = new StringBuilder ();
138
- buffer .append (PAC4J ).append (action .getCode ());
128
+ buffer .append (Pac4jSession . PAC4J ).append (action .getCode ());
139
129
if (action instanceof WithContentAction ) {
140
130
buffer .append (":" ).append (((WithContentAction ) action ).getContent ());
141
131
} else if (action instanceof WithLocationAction ) {
0 commit comments