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.JWEHeader; 012import com.nimbusds.jose.JWEObject; 013import com.nimbusds.jose.Payload; 014import com.nimbusds.jose.util.Base64URL; 015 016 017/** 018 * Encrypted JSON Web Token (JWT). This class is thread-safe. 019 * 020 * @author Vladimir Dzhuvinov 021 * @version 2015-08-19 022 */ 023@ThreadSafe 024public class EncryptedJWT extends JWEObject implements JWT { 025 026 027 private static final long serialVersionUID = 1L; 028 029 030 /** 031 * Creates a new to-be-encrypted JSON Web Token (JWT) with the specified 032 * header and claims set. The initial state will be 033 * {@link com.nimbusds.jose.JWEObject.State#UNENCRYPTED unencrypted}. 034 * 035 * @param header The JWE header. Must not be {@code null}. 036 * @param claimsSet The JWT claims set. Must not be {@code null}. 037 */ 038 public EncryptedJWT(final JWEHeader header, final JWTClaimsSet claimsSet) { 039 040 super(header, new Payload(claimsSet.toJSONObject())); 041 } 042 043 044 /** 045 * Creates a new encrypted JSON Web Token (JWT) with the specified 046 * serialised parts. The state will be 047 * {@link com.nimbusds.jose.JWEObject.State#ENCRYPTED encrypted}. 048 * 049 * @param firstPart The first part, corresponding to the JWE header. 050 * Must not be {@code null}. 051 * @param secondPart The second part, corresponding to the encrypted 052 * key. Empty or {@code null} if none. 053 * @param thirdPart The third part, corresponding to the initialisation 054 * vectory. Empty or {@code null} if none. 055 * @param fourthPart The fourth part, corresponding to the cipher text. 056 * Must not be {@code null}. 057 * @param fifthPart The fifth part, corresponding to the integrity 058 * value. Empty of {@code null} if none. 059 * 060 * @throws ParseException If parsing of the serialised parts failed. 061 */ 062 public EncryptedJWT(final Base64URL firstPart, 063 final Base64URL secondPart, 064 final Base64URL thirdPart, 065 final Base64URL fourthPart, 066 final Base64URL fifthPart) 067 throws ParseException { 068 069 super(firstPart, secondPart, thirdPart, fourthPart, fifthPart); 070 } 071 072 073 @Override 074 public JWTClaimsSet getJWTClaimsSet() 075 throws ParseException { 076 077 Payload payload = getPayload(); 078 079 if (payload == null) { 080 return null; 081 } 082 083 JSONObject json = payload.toJSONObject(); 084 085 if (json == null) { 086 throw new ParseException("Payload of JWE object is not a valid JSON object", 0); 087 } 088 089 return JWTClaimsSet.parse(json); 090 } 091 092 093 /** 094 * Parses an encrypted JSON Web Token (JWT) from the specified string in 095 * compact format. 096 * 097 * @param s The string to parse. Must not be {@code null}. 098 * 099 * @return The encrypted JWT. 100 * 101 * @throws ParseException If the string couldn't be parsed to a valid 102 * encrypted JWT. 103 */ 104 public static EncryptedJWT parse(final String s) 105 throws ParseException { 106 107 Base64URL[] parts = JOSEObject.split(s); 108 109 if (parts.length != 5) { 110 throw new ParseException("Unexpected number of Base64URL parts, must be five", 0); 111 } 112 113 return new EncryptedJWT(parts[0], parts[1], parts[2], parts[3], parts[4]); 114 } 115}