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