Skip to content
This repository has been archived by the owner on Feb 9, 2021. It is now read-only.

Latest commit

 

History

History
59 lines (36 loc) · 1.65 KB

README.md

File metadata and controls

59 lines (36 loc) · 1.65 KB

JWToken Java

An implementation of JSON Web Tokens.

Introduction

JSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object. This information can be verified and trusted because it is digitally signed. JWTs can be signed using a secret (with the HMAC algorithm).

Usage

JWToken.sign(claim, secret, algorithm)

return Token

claim is a Claim object containing the claim.

secret is a string containing the secret for HMAC algorithms.

algorithm is the algoritm used for the Hmac

Available Algorithms

The library implements JWT Verification and Signing using the following algorithms:

JWS Algorithm Description
HS256 HMAC256 HMAC with SHA-256
HS384 HMAC384 HMAC with SHA-384
HS512 HMAC512 HMAC with SHA-512

The specs defines many more algorithms for signing. You can find them all in RFC 7518.

Example

Claim claim = new Claim("iss", "sub", "aud", new Date(new Date().getTime() + 1000), new Date());

String secret = "s3cr3t";

Token token = JWToken.sign(claim, Algorithm.HS512(secret));
System.out.println(token.getToken());

JWToken.verify(token, secret)

return Claim

token is a Token object.

secret is a string containing the secret for HMAC algorithms.

Example

Token token = new Token("xxxxx.yyyyyy.zzzzzz");
String secret = "s3cr3t";
String decodedPayload = JWToken.verify(token, secret);
System.out.println(decodedPayload.toJson());