11package nl .wouterh .keycloak .trusteddevice .util ;
22
3+ import jakarta .ws .rs .core .NewCookie ;
34import jakarta .ws .rs .core .UriBuilder ;
4- import java .util .Set ;
5+ import jakarta .ws .rs .core .NewCookie .SameSite ;
6+ import jakarta .ws .rs .core .Cookie ;
57import lombok .Getter ;
68import lombok .NoArgsConstructor ;
79import lombok .Setter ;
1012import nl .wouterh .keycloak .trusteddevice .credential .TrustedDeviceCredentialProviderFactory ;
1113import org .keycloak .TokenCategory ;
1214import org .keycloak .common .ClientConnection ;
13- import org .keycloak .common .util .ServerCookie ;
1415import org .keycloak .common .util .Time ;
1516import org .keycloak .credential .CredentialProvider ;
1617import org .keycloak .models .KeycloakSession ;
1718import org .keycloak .models .RealmModel ;
1819import org .keycloak .models .UserModel ;
1920import org .keycloak .representations .JsonWebToken ;
20- import org .keycloak .services .util .CookieHelper ;
2121
2222@ Getter
2323@ Setter
@@ -38,34 +38,31 @@ private static void addCookie(KeycloakSession session, RealmModel realm, String
3838
3939 ClientConnection connection = session .getContext ().getConnection ();
4040 boolean secure = realm .getSslRequired ().isRequired (connection );
41-
42- ServerCookie .SameSiteAttributeValue sameSiteValue =
43- secure ? ServerCookie .SameSiteAttributeValue .NONE : null ;
44- CookieHelper .addCookie (
45- COOKIE_NAME ,
46- value ,
47- path ,
48- null ,
49- null ,
50- maxAge ,
51- secure ,
52- true ,
53- sameSiteValue ,
54- session
55- );
41+ SameSite sameSiteValue = secure ? SameSite .NONE : null ;
42+ NewCookie newCookie = new NewCookie .Builder (COOKIE_NAME )
43+ .maxAge (maxAge )
44+ .value (value )
45+ .path (path )
46+ .secure (secure )
47+ .sameSite (sameSiteValue )
48+ .build ();
49+
50+ session .getContext ().getHttpResponse ().setCookieIfAbsent (newCookie );
5651 }
5752
5853 public static TrustedDeviceToken getCookie (KeycloakSession session ) {
59- Set < String > cookieValues = CookieHelper . getCookieValue ( session , COOKIE_NAME );
54+ Cookie cookie = session . getContext (). getRequestHeaders (). getCookies (). get ( COOKIE_NAME );
6055 long time = Time .currentTime ();
6156
62- for (String cookieValue : cookieValues ) {
63- TrustedDeviceToken decoded = session .tokens ().decode (cookieValue , TrustedDeviceToken .class );
64- if (decoded != null && (decoded .getExp () == null || decoded .getExp () > time )) {
65- return decoded ;
66- }
57+ if (cookie == null ) {
58+ return null ;
6759 }
68-
60+
61+ TrustedDeviceToken decoded = session .tokens ().decode (cookie .getValue (), TrustedDeviceToken .class );
62+ if (decoded != null && (decoded .getExp () == null || decoded .getExp () > time )) {
63+ return decoded ;
64+ }
65+
6966 return null ;
7067 }
7168
0 commit comments