|
| 1 | +package io.jooby.jwt; |
| 2 | + |
| 3 | +import com.typesafe.config.Config; |
| 4 | +import io.jooby.Cookie; |
| 5 | +import io.jooby.Extension; |
| 6 | +import io.jooby.Jooby; |
| 7 | +import io.jooby.SessionStore; |
| 8 | +import io.jooby.SessionToken; |
| 9 | +import io.jooby.SneakyThrows; |
| 10 | +import io.jsonwebtoken.Claims; |
| 11 | +import io.jsonwebtoken.Jws; |
| 12 | +import io.jsonwebtoken.JwtBuilder; |
| 13 | +import io.jsonwebtoken.JwtException; |
| 14 | +import io.jsonwebtoken.Jwts; |
| 15 | +import io.jsonwebtoken.security.Keys; |
| 16 | + |
| 17 | +import javax.annotation.Nonnull; |
| 18 | +import java.security.Key; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.Optional; |
| 22 | + |
| 23 | +/** |
| 24 | + * A HTTP cookie session store using JSON Web Token. Usage: |
| 25 | + * <pre>{@code |
| 26 | + * { |
| 27 | + * install(new JwtSession()); |
| 28 | + * } |
| 29 | + * }</pre> |
| 30 | + * |
| 31 | + * It uses <code>HMAC-SHA-256</code> for signing the cookie. Secret key and cookie option can be |
| 32 | + * specify programmatically or in your application configuration file. |
| 33 | + * |
| 34 | + * @author edgar |
| 35 | + * @since 2.2.0 |
| 36 | + */ |
| 37 | +public class JwtSession implements Extension { |
| 38 | + |
| 39 | + private final Key key; |
| 40 | + |
| 41 | + private final Cookie cookie; |
| 42 | + |
| 43 | + /** |
| 44 | + * Creates a JSON Web Token session store. The <code>session.secret</code> property must be |
| 45 | + * defined in your application configuration. |
| 46 | + * |
| 47 | + * Cookie details are created from <code>session.cookie</code> when present, otherwise uses |
| 48 | + * {@link SessionToken#SID}. |
| 49 | + */ |
| 50 | + public JwtSession() { |
| 51 | + this.key = null; |
| 52 | + this.cookie = null; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Creates a JSON Web Token session store. It uses the provided key unless the |
| 57 | + * <code>session.secret</code> property is present in your application configuration. |
| 58 | + * |
| 59 | + * Cookie details are created from <code>session.cookie</code> when present, otherwise uses |
| 60 | + * {@link SessionToken#SID}. |
| 61 | + * |
| 62 | + * @param key Key to use. Override it by <code>session.secret</code> property. |
| 63 | + */ |
| 64 | + public JwtSession(@Nonnull String key) { |
| 65 | + this(key, SessionToken.SID); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Creates a JSON Web Token session store. It uses the provided key unless the |
| 70 | + * <code>session.secret</code> property is present in your application configuration. |
| 71 | + * |
| 72 | + * Cookie details are created from <code>session.cookie</code> when present, otherwise uses |
| 73 | + * the provided cookie. |
| 74 | + * |
| 75 | + * See {@link Cookie#create(String, Config)}. |
| 76 | + * |
| 77 | + * @param key Key to use. Override it by <code>session.secret</code> property. |
| 78 | + * @param cookie Cookie to use. Override it by <code>session.cookie</code> property. |
| 79 | + */ |
| 80 | + public JwtSession(@Nonnull String key, @Nonnull Cookie cookie) { |
| 81 | + this(Keys.hmacShaKeyFor(key.getBytes()), cookie); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Creates a JSON Web Token session store. It uses the provided key unless the |
| 86 | + * <code>session.secret</code> property is present in your application configuration. |
| 87 | + * |
| 88 | + * Cookie details are created from <code>session.cookie</code> when present, otherwise uses |
| 89 | + * the provided cookie. |
| 90 | + * |
| 91 | + * See {@link Cookie#create(String, Config)}. |
| 92 | + * |
| 93 | + * @param key Key to use. Override it by <code>session.secret</code> property. |
| 94 | + * @param cookie Cookie to use. Override it by <code>session.cookie</code> property. |
| 95 | + */ |
| 96 | + public JwtSession(@Nonnull Key key, @Nonnull Cookie cookie) { |
| 97 | + this.key = key; |
| 98 | + this.cookie = cookie; |
| 99 | + } |
| 100 | + |
| 101 | + @Override public void install(@Nonnull Jooby application) throws Exception { |
| 102 | + Config config = application.getConfig(); |
| 103 | + Key key = config.hasPath("session.secret") |
| 104 | + ? Keys.hmacShaKeyFor(config.getString("session.secret").getBytes()) |
| 105 | + : this.key; |
| 106 | + if (key == null) { |
| 107 | + throw new IllegalStateException("No secret session secret key"); |
| 108 | + } |
| 109 | + Cookie cookie = Cookie.create("session.cookie", config) |
| 110 | + .orElse(Optional.ofNullable(this.cookie).orElse(SessionToken.SID)); |
| 111 | + |
| 112 | + application.setSessionStore(SessionStore.cookie(cookie, decoder(key), encoder(key))); |
| 113 | + } |
| 114 | + |
| 115 | + static SneakyThrows.Function<String, Map<String, String>> decoder(Key key) { |
| 116 | + return value -> { |
| 117 | + try { |
| 118 | + Jws<Claims> claims = Jwts.parser().setSigningKey(key).parseClaimsJws(value); |
| 119 | + Map<String, String> attributes = new HashMap<>(); |
| 120 | + for (Map.Entry<String, Object> entry : claims.getBody().entrySet()) { |
| 121 | + attributes.put(entry.getKey(), entry.getValue().toString()); |
| 122 | + } |
| 123 | + return attributes; |
| 124 | + } catch (JwtException x) { |
| 125 | + return null; |
| 126 | + } |
| 127 | + }; |
| 128 | + } |
| 129 | + |
| 130 | + static SneakyThrows.Function<Map<String, String>, String> encoder(Key key) { |
| 131 | + return attributes -> { |
| 132 | + JwtBuilder builder = Jwts.builder().signWith(key); |
| 133 | + for (Map.Entry<String, String> entry : attributes.entrySet()) { |
| 134 | + builder.claim(entry.getKey(), entry.getValue()); |
| 135 | + } |
| 136 | + return builder.compact(); |
| 137 | + }; |
| 138 | + } |
| 139 | +} |
0 commit comments