Skip to content

Commit 654cc2a

Browse files
authored
[HWORKS-992] Remove obsolete code for renewing JWT tokens (#1491)
[HWORKS-992] Remove testing for renewal tokens
1 parent 9e32b74 commit 654cc2a

File tree

5 files changed

+5
-179
lines changed

5 files changed

+5
-179
lines changed

hopsworks-IT/src/test/ruby/spec/jwt_spec.rb

-3
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@
4444
expect_status_details(200)
4545
expect(headers["authorization"]).not_to be_nil
4646
expect(headers["authorization"]).not_to be_empty
47-
renew_tokens = json_body[:renewTokens]
48-
expect(renew_tokens.length).to eql(5)
4947
end
5048

5149
describe "#logged in as service user" do
@@ -57,7 +55,6 @@
5755

5856
post "#{ENV['HOPSWORKS_API']}/auth/service",
5957
URI.encode_www_form({ email: "agent@hops.io", password: "admin"}), { content_type: 'application/x-www-form-urlencoded'}
60-
@renew_tokens = json_body[:renewTokens]
6158
@master_token = headers["authorization"].split[1].strip
6259
end
6360

hopsworks-api/src/main/java/io/hops/hopsworks/api/user/AuthService.java

+4-54
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,11 @@
5252
import io.hops.hopsworks.common.user.AuthController;
5353
import io.hops.hopsworks.common.user.QrCode;
5454
import io.hops.hopsworks.common.user.UsersController;
55-
import io.hops.hopsworks.common.util.DateUtils;
5655
import io.hops.hopsworks.common.util.Settings;
5756
import io.hops.hopsworks.exceptions.HopsSecurityException;
5857
import io.hops.hopsworks.exceptions.UserException;
5958
import io.hops.hopsworks.jwt.Constants;
6059
import io.hops.hopsworks.jwt.JWTController;
61-
import io.hops.hopsworks.jwt.JsonWebToken;
6260
import io.hops.hopsworks.jwt.annotation.JWTRequired;
6361
import io.hops.hopsworks.jwt.exception.DuplicateSigningKeyException;
6462
import io.hops.hopsworks.jwt.exception.InvalidationException;
@@ -93,12 +91,6 @@
9391
import javax.ws.rs.core.SecurityContext;
9492
import java.security.GeneralSecurityException;
9593
import java.security.NoSuchAlgorithmException;
96-
import java.time.LocalDateTime;
97-
import java.time.temporal.ChronoUnit;
98-
import java.util.Collection;
99-
import java.util.HashMap;
100-
import java.util.List;
101-
import java.util.Map;
10294
import java.util.logging.Level;
10395
import java.util.logging.Logger;
10496

@@ -230,18 +222,14 @@ public Response serviceLogin(@FormParam("email") String email, @FormParam("passw
230222
if (!needLogin(request, null, user)) {
231223
return Response.ok().build();
232224
}
225+
233226
if (!userController.isUserInRole(user, "AGENT")) {
234227
throw new HopsSecurityException(RESTCodes.SecurityErrorCode.REST_ACCESS_CONTROL, Level.FINE,
235228
"Users are not allowed to access this endpoint, use auth/login instead",
236229
"User " + user.getUsername() + " tried to login but they don't have AGENT role");
237230
}
238231
request.getSession();
239232

240-
Collection roles = user.getBbcGroupCollection();
241-
if (roles == null || roles.isEmpty()) {
242-
throw new UserException(RESTCodes.UserErrorCode.NO_ROLE_FOUND, Level.FINE);
243-
}
244-
245233
statusValidator.checkStatus(user.getStatus());
246234
String saltedPassword = authController.preLoginCheck(user, password, null);
247235

@@ -251,47 +239,9 @@ public Response serviceLogin(@FormParam("email") String email, @FormParam("passw
251239
authController.registerAuthenticationFailure(user);
252240
throw new UserException(RESTCodes.UserErrorCode.AUTHENTICATION_FAILURE, Level.FINE, null, ex.getMessage(), ex);
253241
}
254-
255-
// First generate the one-time tokens for renewal of master token
256-
String renewalKeyName = jwtController.getServiceOneTimeJWTSigningKeyname(user.getUsername(),
257-
request.getRemoteHost());
258-
LocalDateTime masterExpiration = DateUtils.getNow().plus(settings.getServiceJWTLifetimeMS(), ChronoUnit.MILLIS);
259-
LocalDateTime notBefore = jwtController.computeNotBefore4ServiceRenewalTokens(masterExpiration);
260-
LocalDateTime expiresAt = notBefore.plus(settings.getServiceJWTLifetimeMS(), ChronoUnit.MILLIS);
261-
List<String> userRoles = userUtilities.getUserRoles(user);
262-
263-
JsonWebToken renewalJWTSpec = new JsonWebToken();
264-
renewalJWTSpec.setSubject(user.getUsername());
265-
renewalJWTSpec.setIssuer(settings.getJWTIssuer());
266-
renewalJWTSpec.setAudience(JWTHelper.SERVICE_RENEW_JWT_AUDIENCE);
267-
renewalJWTSpec.setKeyId(renewalKeyName);
268-
renewalJWTSpec.setNotBefore(DateUtils.localDateTime2Date(notBefore));
269-
renewalJWTSpec.setExpiresAt(DateUtils.localDateTime2Date(expiresAt));
270-
271-
Map<String, Object> claims = new HashMap<>(4);
272-
claims.put(Constants.RENEWABLE, false);
273-
claims.put(Constants.EXPIRY_LEEWAY, 3600);
274-
claims.put(Constants.ROLES, userRoles.toArray(new String[1]));
275-
276-
String[] oneTimeRenewalTokens = jwtController.generateOneTimeTokens4ServiceJWTRenewal(renewalJWTSpec, claims,
277-
settings.getJWTSigningKeyName());
278-
279-
// Then generate the master service token
280-
try {
281-
String signingKeyID = jwtController.getSignKeyID(oneTimeRenewalTokens[0]);
282-
claims.clear();
283-
// The rest of JWT claims will be added by JWTHelper
284-
claims.put(Constants.RENEWABLE, false);
285-
claims.put(Constants.SERVICE_JWT_RENEWAL_KEY_ID, signingKeyID);
286-
String token = jWTHelper.createToken(user, settings.getJWTIssuer(), claims);
287-
288-
ServiceJWTDTO renewTokensResponse = new ServiceJWTDTO();
289-
renewTokensResponse.setRenewTokens(oneTimeRenewalTokens);
290-
return Response.ok().header(AUTHORIZATION, Constants.BEARER + token).entity(renewTokensResponse).build();
291-
} catch (Exception ex) {
292-
jwtController.deleteSigningKey(renewalKeyName);
293-
throw ex;
294-
}
242+
243+
String token = jWTHelper.createToken(user, settings.getJWTIssuer(), null);
244+
return Response.ok().header(AUTHORIZATION, Constants.BEARER + token).build();
295245
}
296246

297247
@GET

hopsworks-api/src/main/java/io/hops/hopsworks/api/user/ServiceJWTDTO.java

-52
This file was deleted.

hopsworks-jwt/src/main/java/io/hops/hopsworks/jwt/Constants.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ public class Constants {
2222
public static final String EXPIRY_LEEWAY = "expLeeway";
2323
public static final String ROLES = "roles";
2424
public static final String WWW_AUTHENTICATE_VALUE="Bearer realm=\"Cauth Realm\"";
25-
public static final String SERVICE_JWT_RENEWAL_KEY_ID = "renewal_key_id";
26-
25+
2726
public static final int DEFAULT_EXPIRY_LEEWAY = 60; //60 secs for exp
2827
public static final boolean DEFAULT_RENEWABLE = false;
2928

hopsworks-jwt/src/main/java/io/hops/hopsworks/jwt/JWTController.java

-68
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@
3737
import javax.ejb.TransactionAttribute;
3838
import javax.ejb.TransactionAttributeType;
3939
import java.security.NoSuchAlgorithmException;
40-
import java.time.LocalDateTime;
41-
import java.time.ZoneId;
42-
import java.time.temporal.ChronoUnit;
4340
import java.util.Calendar;
4441
import java.util.Collection;
4542
import java.util.Date;
@@ -55,7 +52,6 @@
5552
import static io.hops.hopsworks.jwt.Constants.DEFAULT_EXPIRY_LEEWAY;
5653
import static io.hops.hopsworks.jwt.Constants.DEFAULT_RENEWABLE;
5754
import static io.hops.hopsworks.jwt.Constants.EXPIRY_LEEWAY;
58-
import static io.hops.hopsworks.jwt.Constants.ONE_TIME_JWT_SIGNING_KEY_NAME;
5955
import static io.hops.hopsworks.jwt.Constants.RENEWABLE;
6056
import static io.hops.hopsworks.jwt.Constants.ROLES;
6157

@@ -463,70 +459,6 @@ public String renewToken(String token, Date newExp, Date notBefore, boolean inva
463459
return renewedToken;
464460
}
465461

466-
public String getSignKeyID(String token) {
467-
DecodedJWT jwt = decodeToken(token);
468-
return jwt.getKeyId();
469-
}
470-
471-
public String[] generateOneTimeTokens4ServiceJWTRenewal(JsonWebToken jwtSpecs, Map<String, Object> claims,
472-
String defaultJWTSigningKeyName)
473-
throws NoSuchAlgorithmException, SigningKeyNotFoundException {
474-
String[] renewalTokens = new String[5];
475-
SignatureAlgorithm algorithm = SignatureAlgorithm.valueOf(Constants.ONE_TIME_JWT_SIGNATURE_ALGORITHM);
476-
String[] audienceArray = jwtSpecs.getAudience().toArray(new String[1]);
477-
try {
478-
renewalTokens[0] = createToken(jwtSpecs.getKeyId(), true, jwtSpecs.getIssuer(),
479-
audienceArray, jwtSpecs.getExpiresAt(), jwtSpecs.getNotBefore(), jwtSpecs.getSubject(), claims,
480-
algorithm);
481-
} catch (DuplicateSigningKeyException ex) {
482-
LOGGER.log(Level.FINE, "Signing key already exist for service JWT key " + jwtSpecs.getKeyId()
483-
+ ". Removing old one");
484-
if (defaultJWTSigningKeyName != null) {
485-
if (!defaultJWTSigningKeyName.equals(jwtSpecs.getKeyId())
486-
&& !ONE_TIME_JWT_SIGNING_KEY_NAME.equals(jwtSpecs.getKeyId())) {
487-
deleteSigningKey(jwtSpecs.getKeyId());
488-
}
489-
}
490-
try {
491-
renewalTokens[0] = createToken(jwtSpecs.getKeyId(), true, jwtSpecs.getIssuer(),
492-
audienceArray, jwtSpecs.getExpiresAt(), jwtSpecs.getNotBefore(), jwtSpecs.getSubject(), claims,
493-
algorithm);
494-
} catch (DuplicateSigningKeyException dskex) {
495-
// This should never happen, we handle it above
496-
}
497-
}
498-
for (int i = 1; i < renewalTokens.length; i++) {
499-
try {
500-
renewalTokens[i] = createToken(jwtSpecs.getKeyId(), false, jwtSpecs.getIssuer(),
501-
audienceArray, jwtSpecs.getExpiresAt(), jwtSpecs.getNotBefore(), jwtSpecs.getSubject(), claims,
502-
algorithm);
503-
} catch (DuplicateSigningKeyException dskex) {
504-
// This should never happen, we do not create new signing key here
505-
}
506-
}
507-
return renewalTokens;
508-
}
509-
510-
private Date localDateTime2Date(LocalDateTime localDateTime) {
511-
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
512-
}
513-
514-
public LocalDateTime computeNotBefore4ServiceRenewalTokens(LocalDateTime masterExpiration) {
515-
LocalDateTime notBefore = null;
516-
if (masterExpiration.minus(3L, ChronoUnit.MINUTES).isBefore(LocalDateTime.now())) {
517-
notBefore = masterExpiration.minus(3L, ChronoUnit.MILLIS);
518-
} else {
519-
notBefore = masterExpiration.minus(3L, ChronoUnit.MINUTES);
520-
}
521-
return notBefore;
522-
}
523-
524-
private static final String SERVICE_ONE_TIME_SIGNING_KEYNAME = "%s_%s__%d";
525-
public String getServiceOneTimeJWTSigningKeyname(String username, String remoteHost) {
526-
long now = System.currentTimeMillis();
527-
return String.format(SERVICE_ONE_TIME_SIGNING_KEYNAME, username, remoteHost, now);
528-
}
529-
530462
public Map<String, Object> addDefaultClaimsIfMissing(Map<String, Object> userClaims, boolean isRenewable, int leeway,
531463
String[] roles) {
532464
if (userClaims == null) {

0 commit comments

Comments
 (0)