Skip to content

Commit 34e2c3a

Browse files
authored
Merge pull request #105 from ochedru/upgrade_jjwt
Upgrade to jjwt 0.12.3
2 parents 0ea847f + cd7d6cc commit 34e2c3a

File tree

6 files changed

+63
-66
lines changed

6 files changed

+63
-66
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Add the dropwizard-jwt-cookie-authentication library as a dependency to your `po
2626
<dependency>
2727
<groupId>org.dhatim</groupId>
2828
<artifactId>dropwizard-jwt-cookie-authentication</artifactId>
29-
<version>5.0.1</version>
29+
<version>5.1.0</version>
3030
</dependency>
3131
```
3232

@@ -167,7 +167,7 @@ JwtCookieAuthBundle jwtCookieAuthBundle = new JwtCookieAuthBundle<>(
167167
MyJwtCookiePrincipal::toClaims,
168168
MyJwtCookiePrincipal::new);
169169

170-
Key key = JwtCookieAuthBundle.generateKey(configuration.getJwtCookieAuth().getSecretSeed());
170+
SecretKey key = JwtCookieAuthBundle.generateKey(configuration.getJwtCookieAuth().getSecretSeed());
171171

172172
environment.jersey().register(
173173
new PolymorphicAuthDynamicFeature<>(

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
<maven.compiler.source>11</maven.compiler.source>
3636
<maven.compiler.target>11</maven.compiler.target>
3737
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
38-
<jjwt.version>0.11.5</jjwt.version>
38+
<jjwt.version>0.12.3</jjwt.version>
3939
<enforcer.fail>false</enforcer.fail>
4040
</properties>
4141
<distributionManagement>

src/main/java/org/dhatim/dropwizard/jwt/cookie/authentication/DefaultJwtCookiePrincipal.java

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
22
* Copyright 2023 Dhatim
3-
*
3+
* <p>
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
55
* use this file except in compliance with the License. You may obtain a copy of
66
* the License at
7-
*
7+
* <p>
88
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
9+
* <p>
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
1212
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
@@ -17,7 +17,9 @@
1717

1818
import com.fasterxml.jackson.annotation.JsonProperty;
1919
import io.jsonwebtoken.Claims;
20+
import io.jsonwebtoken.ClaimsBuilder;
2021
import io.jsonwebtoken.Jwts;
22+
2123
import java.util.Collection;
2224
import java.util.Collections;
2325
import java.util.Optional;
@@ -30,25 +32,26 @@ public class DefaultJwtCookiePrincipal implements JwtCookiePrincipal {
3032
private final static String PERSISTENT = "pst"; // long-term token == rememberme
3133
private final static String ROLES = "rls";
3234

33-
protected final Claims claims;
35+
protected final ClaimsBuilder claimsBuilder;
3436

3537
/**
3638
* Builds a new instance of DefaultJwtCookiePrincipal
3739
*
38-
* @param name the principal name
40+
* @param name the principal name
3941
* @param persistent if the cookie must be persistent
40-
* @param roles the roles the principal is in
41-
* @param claims custom data associated with the principal
42+
* @param roles the roles the principal is in
43+
* @param claims custom data associated with the principal
4244
*/
4345
public DefaultJwtCookiePrincipal(
4446
@JsonProperty("name") String name,
4547
@JsonProperty("persistent") boolean persistent,
4648
@JsonProperty("roles") Collection<String> roles,
4749
@JsonProperty("claims") Claims claims) {
48-
this.claims = Optional.ofNullable(claims).orElseGet(Jwts::claims);
49-
this.claims.setSubject(name);
50-
this.claims.put(PERSISTENT, persistent);
51-
this.claims.put(ROLES, roles);
50+
this.claimsBuilder = Jwts.claims();
51+
if (claims != null) {
52+
claimsBuilder.add(claims);
53+
}
54+
claimsBuilder.subject(name).add(PERSISTENT, persistent).add(ROLES, roles);
5255
}
5356

5457
/**
@@ -66,7 +69,10 @@ public DefaultJwtCookiePrincipal(String name) {
6669
* @param claims the JWT claims
6770
*/
6871
public DefaultJwtCookiePrincipal(Claims claims) {
69-
this.claims = claims;
72+
this.claimsBuilder = Jwts.claims();
73+
if (claims != null) {
74+
claimsBuilder.add(claims);
75+
}
7076
}
7177

7278
/**
@@ -75,7 +81,7 @@ public DefaultJwtCookiePrincipal(Claims claims) {
7581
* @return the claims
7682
*/
7783
public Claims getClaims() {
78-
return claims;
84+
return claimsBuilder.build();
7985
}
8086

8187
/**
@@ -95,7 +101,7 @@ public boolean isInRole(String role) {
95101
* @return the roles
96102
*/
97103
public Collection<String> getRoles() {
98-
return Optional.ofNullable(claims.get(ROLES))
104+
return Optional.ofNullable(getClaims().get(ROLES))
99105
.map(Collection.class::cast)
100106
.orElse(Collections.emptyList());
101107
}
@@ -106,7 +112,7 @@ public Collection<String> getRoles() {
106112
* @param roles the roles
107113
*/
108114
public void setRoles(Collection<String> roles) {
109-
claims.put(ROLES, roles);
115+
claimsBuilder.add(ROLES, roles);
110116
}
111117

112118
/**
@@ -116,18 +122,7 @@ public void setRoles(Collection<String> roles) {
116122
*/
117123
@Override
118124
public boolean isPersistent() {
119-
return claims.get(PERSISTENT) == Boolean.TRUE;
120-
}
121-
122-
/**
123-
* Set if the cookie must be persistent
124-
*
125-
* @param persistent if the cookie must be persistent
126-
* @deprecated Typo in method name. Replaced by {@link #setPersistent(boolean)}
127-
*/
128-
@Deprecated
129-
public void setPresistent(boolean persistent) {
130-
setPersistent(persistent);
125+
return getClaims().get(PERSISTENT) == Boolean.TRUE;
131126
}
132127

133128
/**
@@ -136,7 +131,7 @@ public void setPresistent(boolean persistent) {
136131
* @param persistent if the cookie must be persistent
137132
*/
138133
public void setPersistent(boolean persistent) {
139-
claims.put(PERSISTENT, persistent);
134+
claimsBuilder.add(PERSISTENT, persistent);
140135
}
141136

142137
/**
@@ -146,7 +141,7 @@ public void setPersistent(boolean persistent) {
146141
*/
147142
@Override
148143
public String getName() {
149-
return (String) claims.getSubject();
144+
return getClaims().getSubject();
150145
}
151146

152147
/**
@@ -155,7 +150,7 @@ public String getName() {
155150
* @param name the name
156151
*/
157152
public void setName(String name) {
158-
claims.setSubject(name);
153+
claimsBuilder.subject(name);
159154
}
160155

161156
}

src/main/java/org/dhatim/dropwizard/jwt/cookie/authentication/JwtCookieAuthBundle.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
import org.glassfish.jersey.server.filter.RolesAllowedDynamicFeature;
3232

3333
import javax.crypto.KeyGenerator;
34+
import javax.crypto.SecretKey;
3435
import javax.crypto.spec.SecretKeySpec;
3536
import java.nio.charset.StandardCharsets;
36-
import java.security.Key;
3737
import java.security.NoSuchAlgorithmException;
3838
import java.time.Duration;
3939
import java.util.Optional;
@@ -55,7 +55,7 @@ public class JwtCookieAuthBundle<C extends Configuration, P extends JwtCookiePri
5555
private final Function<P, Claims> serializer;
5656
private final Function<Claims, P> deserializer;
5757
private Function<C, JwtCookieAuthConfiguration> configurationSupplier;
58-
private BiFunction<C, Environment, Key> keySuppplier;
58+
private BiFunction<C, Environment, SecretKey> keySuppplier;
5959
private UnauthorizedHandler unauthorizedHandler;
6060

6161
/**
@@ -92,7 +92,7 @@ public JwtCookieAuthBundle(Class<P> principalType, Function<P, Claims> serialize
9292
* @param keySupplier a bi-function which will return the signing key from the configuration and environment
9393
* @return this
9494
*/
95-
public JwtCookieAuthBundle<C, P> withKeyProvider(BiFunction<C, Environment, Key> keySupplier) {
95+
public JwtCookieAuthBundle<C, P> withKeyProvider(BiFunction<C, Environment, SecretKey> keySupplier) {
9696
this.keySuppplier = keySupplier;
9797
return this;
9898
}
@@ -131,7 +131,7 @@ public void run(C configuration, Environment environment) throws Exception {
131131
JwtCookieAuthConfiguration conf = configurationSupplier.apply(configuration);
132132

133133
//build the key from the key factory if it was provided
134-
Key key = Optional
134+
SecretKey key = Optional
135135
.ofNullable(keySuppplier)
136136
.map(k -> k.apply(configuration, environment))
137137
.orElseGet(() -> generateKey(conf.getSecretSeed()));
@@ -151,7 +151,7 @@ public void run(C configuration, Environment environment) throws Exception {
151151
* @param cookieName the name of the cookie holding the JWT
152152
* @return the request filter
153153
*/
154-
public AuthFilter<String, P> getAuthRequestFilter(Key key, String cookieName) {
154+
public AuthFilter<String, P> getAuthRequestFilter(SecretKey key, String cookieName) {
155155
return new JwtCookieAuthRequestFilter.Builder()
156156
.setCookieName(cookieName)
157157
.setAuthenticator(new JwtCookiePrincipalAuthenticator(key, deserializer))
@@ -168,7 +168,7 @@ public AuthFilter<String, P> getAuthRequestFilter(Key key, String cookieName) {
168168
* @param key the key used to validate the JWT
169169
* @return the request filter
170170
*/
171-
public AuthFilter<String, P> getAuthRequestFilter(Key key) {
171+
public AuthFilter<String, P> getAuthRequestFilter(SecretKey key) {
172172
return getAuthRequestFilter(key, JWT_COOKIE_DEFAULT_NAME);
173173
}
174174

@@ -179,7 +179,7 @@ public AuthFilter<String, P> getAuthRequestFilter(Key key) {
179179
* @param configuration cookie configuration (secure, httpOnly, expiration...)
180180
* @return the response filter
181181
*/
182-
public ContainerResponseFilter getAuthResponseFilter(Key key, JwtCookieAuthConfiguration configuration) {
182+
public ContainerResponseFilter getAuthResponseFilter(SecretKey key, JwtCookieAuthConfiguration configuration) {
183183
return new JwtCookieAuthResponseFilter<>(
184184
principalType,
185185
serializer,
@@ -201,11 +201,11 @@ public ContainerResponseFilter getAuthResponseFilter(Key key, JwtCookieAuthConfi
201201
* If null, a random key is returned.
202202
* @return a HMAC SHA256 Key
203203
*/
204-
public static Key generateKey(String secretSeed) {
204+
public static SecretKey generateKey(String secretSeed) {
205205
// make a key from the seed if it was provided
206206
return Optional.ofNullable(secretSeed)
207207
.map(seed -> Hashing.sha256().newHasher().putString(seed, StandardCharsets.UTF_8).hash().asBytes())
208-
.map(k -> (Key) new SecretKeySpec(k, SignatureAlgorithm.HS256.getJcaName()))
208+
.map(k -> (SecretKey) new SecretKeySpec(k, SignatureAlgorithm.HS256.getJcaName()))
209209
//else generate a random key
210210
.orElseGet(getHmacSha256KeyGenerator()::generateKey);
211211
}

src/main/java/org/dhatim/dropwizard/jwt/cookie/authentication/JwtCookiePrincipal.java

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,41 @@
1616
package org.dhatim.dropwizard.jwt.cookie.authentication;
1717

1818
import jakarta.ws.rs.container.ContainerRequestContext;
19-
import org.checkerframework.checker.nullness.qual.Nullable;
2019

2120
import java.security.Principal;
2221

2322
/**
2423
* A principal persisted in JWT cookies
2524
*/
26-
public interface JwtCookiePrincipal extends Principal{
25+
public interface JwtCookiePrincipal extends Principal {
2726

2827
/**
2928
* Indicates if the cookie will be persistent (aka 'remember me')
29+
*
3030
* @return if the cookie must be persistent
3131
*/
32-
boolean isPersistent();
33-
34-
/**
35-
* Indicates if this principal has the given role
36-
* @param role the role
37-
* @return true if the principal is in the given role, false otherwise
38-
*/
39-
boolean isInRole(String role);
40-
41-
/**
42-
* Add this principal in the request context.
43-
* It will serialized in a JWT cookie and can be reused in subsequent queries
44-
* @param context the request context
45-
*/
46-
default void addInContext(ContainerRequestContext context){
32+
boolean isPersistent();
33+
34+
/**
35+
* Indicates if this principal has the given role
36+
*
37+
* @param role the role
38+
* @return true if the principal is in the given role, false otherwise
39+
*/
40+
boolean isInRole(String role);
41+
42+
/**
43+
* Add this principal in the request context.
44+
* It will serialized in a JWT cookie and can be reused in subsequent queries
45+
*
46+
* @param context the request context
47+
*/
48+
default void addInContext(ContainerRequestContext context) {
4749
context.setSecurityContext(new JwtCookieSecurityContext(this, context.getSecurityContext().isSecure()));
4850
}
4951

50-
public static void removeFromContext(ContainerRequestContext context){
51-
context.setSecurityContext(new JwtCookieSecurityContext(null, context.getSecurityContext().isSecure()));
52-
}
52+
public static void removeFromContext(ContainerRequestContext context) {
53+
context.setSecurityContext(new JwtCookieSecurityContext(null, context.getSecurityContext().isSecure()));
54+
}
5355

5456
}

src/main/java/org/dhatim/dropwizard/jwt/cookie/authentication/JwtCookiePrincipalAuthenticator.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,24 @@
2222
import io.jsonwebtoken.Jwts;
2323
import io.jsonwebtoken.security.SecurityException;
2424

25-
import java.security.Key;
25+
import javax.crypto.SecretKey;
2626
import java.util.Optional;
2727
import java.util.function.Function;
2828

2929
class JwtCookiePrincipalAuthenticator<P extends JwtCookiePrincipal> implements Authenticator<String, P> {
3030

31-
private final Key key;
31+
private final SecretKey key;
3232
private final Function<Claims, P> deserializer;
3333

34-
public JwtCookiePrincipalAuthenticator(Key key, Function<Claims, P> deserializer) {
34+
public JwtCookiePrincipalAuthenticator(SecretKey key, Function<Claims, P> deserializer) {
3535
this.key = key;
3636
this.deserializer = deserializer;
3737
}
3838

3939
@Override
4040
public Optional<P> authenticate(String credentials) throws AuthenticationException {
4141
try {
42-
return Optional.of(deserializer.apply(Jwts.parserBuilder().setSigningKey(key).build().parseClaimsJws(credentials).getBody()));
42+
return Optional.of(deserializer.apply(Jwts.parser().verifyWith(key).build().parseClaimsJws(credentials).getBody()));
4343
} catch (ExpiredJwtException | SecurityException e) {
4444
return Optional.empty();
4545
}

0 commit comments

Comments
 (0)