Package de.kosmos_lab.web.server
Class JWT
- java.lang.Object
-
- de.kosmos_lab.web.server.JWT
-
public class JWT extends Object
JWT is used to verify and sign JWT
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classJWT.Algorithmstatic classJWT.JWTVerifyFailedThe Class JWTVerifyFailed.
-
Field Summary
Fields Modifier and Type Field Description static longDEFAULT_LIFETIMEprotected byte[]keyThe key usedlonglifetimedefault lifetime of a JWT
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Stringencode(String data)Encode the payloadstatic StringextractPayload(String data)extract the payload from a given data stringStringsign(byte[] head, byte[] payload)Sign the payloadStringsign(org.json.JSONObject object)Sign a json payloadstatic Stringtrim(String data)Trims the data, removes trailing =org.json.JSONObjectverify(String data)Verify.
-
-
-
Field Detail
-
DEFAULT_LIFETIME
public static final long DEFAULT_LIFETIME
- See Also:
- Constant Field Values
-
lifetime
public final long lifetime
default lifetime of a JWT
-
key
protected byte[] key
The key used
-
-
Constructor Detail
-
JWT
public JWT(String key, long lifetime)
Instantiates a new jwt with HS256- Parameters:
key- the key to use
-
JWT
public JWT(String key)
-
JWT
public JWT(JWT.Algorithm algorithm, String key, long lifetime)
Instantiates a new jwt.- Parameters:
algorithm- the algorithm to usekey- the key to use
-
-
Method Detail
-
trim
public static String trim(String data)
Trims the data, removes trailing =- Parameters:
data- data to trim- Returns:
- trimmed version of data
-
encode
public String encode(String data) throws InvalidKeyException, NoSuchAlgorithmException, IllegalStateException
Encode the payload- Parameters:
data- the data- Returns:
- the string
- Throws:
InvalidKeyException- the invalid key exceptionNoSuchAlgorithmException- the no such algorithm exceptionIllegalStateException- the illegal state exception
-
sign
public String sign(byte[] head, byte[] payload) throws UnsupportedEncodingException, InvalidKeyException, NoSuchAlgorithmException, IllegalStateException
Sign the payload- Parameters:
head- the headerpayload- the payload- Returns:
- the signed JWT string
- Throws:
UnsupportedEncodingException- the unsupported encoding exceptionInvalidKeyException- the invalid key exceptionNoSuchAlgorithmException- the no such algorithm exceptionIllegalStateException- the illegal state exception
-
sign
public String sign(org.json.JSONObject object) throws UnsupportedEncodingException, InvalidKeyException, NoSuchAlgorithmException, IllegalStateException
Sign a json payload- Parameters:
object- the JSON object- Returns:
- signed JWT string
- Throws:
UnsupportedEncodingException- the unsupported encoding exceptionInvalidKeyException- the invalid key exceptionNoSuchAlgorithmException- the no such algorithm exceptionIllegalStateException- the illegal state exception
-
verify
public org.json.JSONObject verify(String data) throws InvalidKeyException, NoSuchAlgorithmException, IllegalStateException, UnsupportedEncodingException, JWT.JWTVerifyFailed
Verify.- Parameters:
data- the data to be verified- Returns:
- an JSONObject with the payload
- Throws:
InvalidKeyException- if key is invalidNoSuchAlgorithmException- if algorithm is not supportedIllegalStateException- if state was illegalUnsupportedEncodingException- if the encoding is not supportedJWT.JWTVerifyFailed- the JWT verify failed
-
extractPayload
public static String extractPayload(String data) throws JWT.JWTVerifyFailed
extract the payload from a given data string- Parameters:
data-- Returns:
- Throws:
JWT.JWTVerifyFailed- will be thrown if it does not appear like a valid JWT
-
-