org.bitbucket.gkutiel.in.my.mind.feature.Jwt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of in-my-mind Show documentation
Show all versions of in-my-mind Show documentation
An opinionated web framework on top of in-core
The newest version!
package org.bitbucket.gkutiel.in.my.mind.feature;
import static io.jsonwebtoken.Jwts.builder;
import static io.jsonwebtoken.Jwts.parser;
import static io.jsonwebtoken.SignatureAlgorithm.HS256;
import java.security.Key;
import com.google.gson.Gson;
import io.jsonwebtoken.impl.crypto.MacProvider;
public interface Jwt {
static final Key key = MacProvider.generateKey();
static final Gson gson = new Gson();
public default T decode(final String jwt, final Class type) {
final String val = parser().setSigningKey(key).parseClaimsJws(jwt).getBody().getSubject();
return gson.fromJson(val, type);
}
public default String encode(final T obj) {
final String sub = obj instanceof String ? (String) obj : gson.toJson(obj);
return builder().setSubject(sub).signWith(HS256, key).compact();
}
}