Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add new variable to create JWT that allows changing steps.
- Loading branch information
Showing
6 changed files
with
89 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ | |
/goobi-jar/ | ||
/goobi-war/ | ||
/target/ | ||
/module-war/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package de.sub.goobi.helper; | ||
|
||
import java.util.Date; | ||
|
||
import javax.naming.ConfigurationException; | ||
|
||
import org.goobi.beans.Step; | ||
import org.joda.time.DateTime; | ||
|
||
import com.auth0.jwt.JWT; | ||
import com.auth0.jwt.JWTVerifier; | ||
import com.auth0.jwt.algorithms.Algorithm; | ||
import com.auth0.jwt.exceptions.JWTVerificationException; | ||
import com.auth0.jwt.interfaces.DecodedJWT; | ||
|
||
import de.sub.goobi.config.ConfigurationHelper; | ||
import lombok.extern.log4j.Log4j; | ||
|
||
@Log4j | ||
public class JwtHelper { | ||
public static String createChangeStepToken(Step step) throws ConfigurationException { | ||
String secret = ConfigurationHelper.getInstance().getJwtSecret(); | ||
if (secret == null) { | ||
throw new ConfigurationException( | ||
"Could not get JWT secret from configuration. Please configure the key 'jwtSecret' in the file goobi_config.properties"); | ||
} | ||
Algorithm algorithm = Algorithm.HMAC256("secret"); | ||
Date expiryDate = new DateTime().plusHours(37).toDate(); | ||
String token = JWT.create() | ||
.withIssuer("Goobi") | ||
.withClaim("stepId", step.getId()) | ||
.withClaim("changeStepAllowed", true) | ||
.withExpiresAt(expiryDate) | ||
.sign(algorithm); | ||
return token; | ||
} | ||
|
||
public static boolean verifyChangeStepToken(String token, Integer stepId) throws ConfigurationException { | ||
String secret = ConfigurationHelper.getInstance().getJwtSecret(); | ||
if (secret == null) { | ||
throw new ConfigurationException( | ||
"Could not get JWT secret from configuration. Please configure the key 'jwtSecret' in the file goobi_config.properties"); | ||
} | ||
try { | ||
Algorithm algorithm = Algorithm.HMAC256("secret"); | ||
JWTVerifier verifier = JWT.require(algorithm) | ||
.withIssuer("Goobi") | ||
.build(); | ||
DecodedJWT jwt = verifier.verify(token); | ||
Integer claimId = jwt.getClaim("stepId").asInt(); | ||
if (claimId == null || !stepId.equals(claimId)) { | ||
log.debug("token rejected: step IDs do not match"); | ||
return false; | ||
} | ||
Boolean changeStepAllowed = jwt.getClaim("changeStepAllowed").asBoolean(); | ||
if (changeStepAllowed == null || !changeStepAllowed) { | ||
log.debug("token rejected: changing the step not allowed"); | ||
return false; | ||
} | ||
} catch (JWTVerificationException exception) { | ||
//Invalid signature/claims | ||
return false; | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.