diff --git a/jjwt/src/main/java/io/jsonwebtoken/jjwtfun/DemoApplication.java b/jjwt/src/main/java/io/jsonwebtoken/jjwtfun/JJWTFunApplication.java similarity index 71% rename from jjwt/src/main/java/io/jsonwebtoken/jjwtfun/DemoApplication.java rename to jjwt/src/main/java/io/jsonwebtoken/jjwtfun/JJWTFunApplication.java index 2d09c182b6..7189617d55 100644 --- a/jjwt/src/main/java/io/jsonwebtoken/jjwtfun/DemoApplication.java +++ b/jjwt/src/main/java/io/jsonwebtoken/jjwtfun/JJWTFunApplication.java @@ -4,9 +4,9 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication -public class DemoApplication { +public class JJWTFunApplication { public static void main(String[] args) { - SpringApplication.run(DemoApplication.class, args); + SpringApplication.run(JJWTFunApplication.class, args); } } diff --git a/jjwt/src/main/java/io/jsonwebtoken/jjwtfun/controller/DynamicJWTController.java b/jjwt/src/main/java/io/jsonwebtoken/jjwtfun/controller/DynamicJWTController.java index e1d98bb199..fff7e1d6a2 100644 --- a/jjwt/src/main/java/io/jsonwebtoken/jjwtfun/controller/DynamicJWTController.java +++ b/jjwt/src/main/java/io/jsonwebtoken/jjwtfun/controller/DynamicJWTController.java @@ -1,39 +1,27 @@ package io.jsonwebtoken.jjwtfun.controller; +import io.jsonwebtoken.JwtBuilder; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import java.io.UnsupportedEncodingException; -import java.util.AbstractMap.SimpleEntry; -import java.util.Collections; +import java.time.Instant; +import java.util.Date; import java.util.Map; -import java.util.stream.Collectors; -import java.util.stream.Stream; + +import static org.springframework.web.bind.annotation.RequestMethod.POST; @RestController public class DynamicJWTController { @Value("#{ @environment['jjwtfun.secret'] ?: 'secret' }") String secret; - Map map = Collections.unmodifiableMap(Stream.of( - new SimpleEntry<>("iss", ""), - new SimpleEntry<>("a", ""), - new SimpleEntry<>("b", ""), - new SimpleEntry<>("c", ""), - new SimpleEntry<>("d", ""), - new SimpleEntry<>("e", ""), - new SimpleEntry<>("f", ""), - new SimpleEntry<>("g", ""), - new SimpleEntry<>("h", "")) - .collect(Collectors.toMap((e) -> e.getKey(), (e) -> e.getValue()))); - - @RequestMapping(value = "/dynamic-builder", method = RequestMethod.POST) - public String dynamicBuilder(@RequestBody Map claims) throws UnsupportedEncodingException { + @RequestMapping(value = "/dynamic-builder-general", method = POST) + public String dynamicBuilderGeneric(@RequestBody Map claims) throws UnsupportedEncodingException { return Jwts.builder() .setClaims(claims) .signWith( @@ -42,4 +30,41 @@ public class DynamicJWTController { ) .compact(); } + + @RequestMapping(value = "/dynamic-builder-specific", method = POST) + public String dynamicBuilderSpecific(@RequestBody Map claims) throws UnsupportedEncodingException { + JwtBuilder builder = Jwts.builder(); + + claims.forEach((key, value) -> { + switch (key) { + case "iss": + builder.setIssuer((String)value); + break; + case "sub": + builder.setSubject((String)value); + break; + case "aud": + builder.setAudience((String)value); + break; + case "exp": + builder.setExpiration(Date.from(Instant.ofEpochSecond(Long.parseLong((String)value)))); + break; + case "nbf": + builder.setNotBefore(Date.from(Instant.ofEpochSecond(Long.parseLong((String)value)))); + break; + case "iat": + builder.setIssuedAt(Date.from(Instant.ofEpochSecond(Long.parseLong((String)value)))); + break; + case "jti": + builder.setId((String)value); + break; + default: + builder.claim(key, value); + } + }); + + builder.signWith(SignatureAlgorithm.HS256, secret.getBytes("UTF-8")); + + return builder.compact(); + } } diff --git a/jjwt/src/main/java/io/jsonwebtoken/jjwtfun/controller/HomeController.java b/jjwt/src/main/java/io/jsonwebtoken/jjwtfun/controller/HomeController.java new file mode 100644 index 0000000000..d6717e383f --- /dev/null +++ b/jjwt/src/main/java/io/jsonwebtoken/jjwtfun/controller/HomeController.java @@ -0,0 +1,27 @@ +package io.jsonwebtoken.jjwtfun.controller; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpServletRequest; + +@RestController +public class HomeController { + + @RequestMapping("/") + public String home(HttpServletRequest req) { + String requestUrl = getUrl(req); + return "Available commands (assumes httpie - https://github.com/jkbrzt/httpie):\n" + + " http " + requestUrl + "/\n\tThis usage\n" + + " http " + requestUrl + "/static-builder\n\tbuild JWT from hardcoded claims\n" + + " http " + requestUrl + "/dynamic-builder-general claim-1=value-1 ... [claim-n=value-n]\n\tbuild JWT from passed in claims (using general claims map)\n" + + " http " + requestUrl + "/dynamic-builder-specific claim-1=value-1 ... [claim-n=value-n]\n\tbuild JWT from passed in claims (using specific claims methods)\n" + + " http " + requestUrl + "/parser?jwt=\n\tParse passed in JWT\n"; + } + + private String getUrl(HttpServletRequest req) { + return req.getScheme() + "://" + + req.getServerName() + + ((req.getServerPort() == 80 || req.getServerPort() == 443) ? "" : ":" + req.getServerPort()); + } +} diff --git a/jjwt/src/main/java/io/jsonwebtoken/jjwtfun/controller/FixedJWTController.java b/jjwt/src/main/java/io/jsonwebtoken/jjwtfun/controller/StaticJWTController.java similarity index 84% rename from jjwt/src/main/java/io/jsonwebtoken/jjwtfun/controller/FixedJWTController.java rename to jjwt/src/main/java/io/jsonwebtoken/jjwtfun/controller/StaticJWTController.java index 7a648063ac..6f98ffcd94 100644 --- a/jjwt/src/main/java/io/jsonwebtoken/jjwtfun/controller/FixedJWTController.java +++ b/jjwt/src/main/java/io/jsonwebtoken/jjwtfun/controller/StaticJWTController.java @@ -9,6 +9,7 @@ import io.jsonwebtoken.SignatureException; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; @@ -20,10 +21,13 @@ import java.util.Date; import java.util.HashMap; import java.util.Map; -@RestController -public class FixedJWTController { +import static org.springframework.web.bind.annotation.RequestMethod.GET; +import static org.springframework.web.bind.annotation.RequestMethod.POST; - @RequestMapping("/fixed-builder") +@RestController +public class StaticJWTController { + + @RequestMapping(value = "/static-builder", method = POST) public String fixedBuilder() throws UnsupportedEncodingException { String jws = Jwts.builder() @@ -40,7 +44,7 @@ public class FixedJWTController { return jws; } - @RequestMapping("/fixed-parser") + @RequestMapping(value = "/parser", method = GET) public Jws fixedParser(@RequestParam String jws) throws UnsupportedEncodingException { Jws claims = Jwts.parser() .setSigningKey("secret".getBytes("UTF-8")) diff --git a/jjwt/src/test/java/io/jsonwebtoken/jjwtfun/DemoApplicationTests.java b/jjwt/src/test/java/io/jsonwebtoken/jjwtfun/DemoApplicationTests.java index 7e5b9b78f1..357d91ed73 100644 --- a/jjwt/src/test/java/io/jsonwebtoken/jjwtfun/DemoApplicationTests.java +++ b/jjwt/src/test/java/io/jsonwebtoken/jjwtfun/DemoApplicationTests.java @@ -7,7 +7,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; @RunWith(SpringJUnit4ClassRunner.class) -@SpringApplicationConfiguration(classes = DemoApplication.class) +@SpringApplicationConfiguration(classes = JJWTFunApplication.class) @WebAppConfiguration public class DemoApplicationTests {