package edu.kit.ipd.crowdcontrol.workerservice;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

/**
 * @author LeanderK
 * @version 1.0
 */
public class JWTHelper {
    public String generateJWT(int workerId) {
        return Jwts.builder()
                .setSubject(String.valueOf(workerId))
                .signWith(SignatureAlgorithm.ES512, "a")
                .compact();
    }
}

@@ -118,264 +118,1352 @@
 hm;%0A
-%0A/**%0A * @author LeanderK%0A * @version 1.0%0A */%0Apublic class JWTHelper %7B%0A    public String generateJWT(int workerId) %7B%0A        return Jwts.builder()%0A                .setSubject(String.valueOf(workerId))%0A                .signWith(SignatureAlgorithm.ES512, %22a
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Aimport java.util.Objects;%0A%0A/**%0A * this class creates and reads the JWT (Json Web Token)%0A * @author LeanderK%0A * @version 1.0%0A */%0Apublic class JWTHelper %7B%0A    private static final Logger logger = LoggerFactory.getLogger(JWTHelper.class);%0A    private final String secret;%0A%0A    /**%0A     * creates the JWT-Helper%0A     * @param secret the base64 encoded secret to use%0A     */%0A    public JWTHelper(String secret) %7B%0A        this.secret = secret;%0A    %7D%0A%0A    /**%0A     * creates the JWT%0A     * @param workerId the workerID%0A     * @return the JWT string%0A     */%0A    public String generateJWT(int workerId) %7B%0A        Objects.requireNonNull(workerId);%0A        logger.debug(%22encoding JWT for workerID%22, workerId);%0A        return Jwts.builder()%0A                .setSubject(String.valueOf(workerId))%0A                .signWith(SignatureAlgorithm.ES512, secret)%0A                .compact();%0A    %7D%0A%0A    /**%0A     * returns the workerID form the JWT%0A     * @param jwt the jwt to parse%0A     * @return the workerID%0A     */%0A    public int getWorkerID(String jwt) %7B%0A        String subject = Jwts.parser().setSigningKey(secret).parseClaimsJws(jwt).getBody().getSubject();%0A        if (subject == null) %7B%0A            logger.error(%22subject is null%22);%0A            throw new UnauthorizedException(%22subject is missing
 %22)
+;
 %0A   
@@ -1467,35 +1467,310 @@
         
-        .compact();
+%7D%0A        try %7B%0A            return Integer.parseInt(subject);%0A        %7D catch (NumberFormatException e) %7B%0A            logger.error(String.format(%22subject %25s is not a number%22, subject));%0A            throw new UnauthorizedException(String.format(%22subject %25s is not a number%22, subject));%0A        %7D
 %0A    %7D%0A%7D

finished jwthelper