001package com.nimbusds.jwt; 002 003 004import java.text.ParseException; 005 006import net.jcip.annotations.ThreadSafe; 007 008import net.minidev.json.JSONObject; 009 010import com.nimbusds.jose.JOSEObject; 011import com.nimbusds.jose.Payload; 012import com.nimbusds.jose.PlainHeader; 013import com.nimbusds.jose.PlainObject; 014import com.nimbusds.jose.util.Base64URL; 015 016 017/** 018 * Unsecured (plain) JSON Web Token (JWT). 019 * 020 * @author Vladimir Dzhuvinov 021 * @version 2015-08-19 022 */ 023@ThreadSafe 024public class PlainJWT extends PlainObject implements JWT { 025 026 027 private static final long serialVersionUID = 1L; 028 029 030 /** 031 * Creates a new unsecured (plain) JSON Web Token (JWT) with a default 032 * {@link com.nimbusds.jose.PlainHeader} and the specified claims 033 * set. 034 * 035 * @param claimsSet The JWT claims set. Must not be {@code null}. 036 */ 037 public PlainJWT(final JWTClaimsSet claimsSet) { 038 039 super(new Payload(claimsSet.toJSONObject())); 040 } 041 042 043 /** 044 * Creates a new unsecured (plain) JSON Web Token (JWT) with the 045 * specified header and claims set. 046 * 047 * @param header The unsecured header. Must not be {@code null}. 048 * @param claimsSet The JWT claims set. Must not be {@code null}. 049 */ 050 public PlainJWT(final PlainHeader header, final JWTClaimsSet claimsSet) { 051 052 super(header, new Payload(claimsSet.toJSONObject())); 053 } 054 055 056 /** 057 * Creates a new unsecured (plain) JSON Web Token (JWT) with the 058 * specified Base64URL-encoded parts. 059 * 060 * @param firstPart The first part, corresponding to the unsecured 061 * header. Must not be {@code null}. 062 * @param secondPart The second part, corresponding to the claims set 063 * (payload). Must not be {@code null}. 064 * 065 * @throws ParseException If parsing of the serialised parts failed. 066 */ 067 public PlainJWT(final Base64URL firstPart, final Base64URL secondPart) 068 throws ParseException { 069 070 super(firstPart, secondPart); 071 } 072 073 074 @Override 075 public JWTClaimsSet getJWTClaimsSet() 076 throws ParseException { 077 078 JSONObject json = getPayload().toJSONObject(); 079 080 if (json == null) { 081 082 throw new ParseException("Payload of unsecured JOSE object is not a valid JSON object", 0); 083 } 084 085 return JWTClaimsSet.parse(json); 086 } 087 088 089 /** 090 * Parses an unsecured (plain) JSON Web Token (JWT) from the specified 091 * string in compact format. 092 * 093 * @param s The string to parse. Must not be {@code null}. 094 * 095 * @return The unsecured JWT. 096 * 097 * @throws ParseException If the string couldn't be parsed to a valid 098 * unsecured JWT. 099 */ 100 public static PlainJWT parse(final String s) 101 throws ParseException { 102 103 Base64URL[] parts = JOSEObject.split(s); 104 105 if (! parts[2].toString().isEmpty()) { 106 107 throw new ParseException("Unexpected third Base64URL part in the unsecured JWT object", 0); 108 } 109 110 return new PlainJWT(parts[0], parts[1]); 111 } 112}