001 package com.nimbusds.jwt;
002
003
004 import java.text.ParseException;
005
006 import net.minidev.json.JSONObject;
007
008 import net.jcip.annotations.ThreadSafe;
009
010 import com.nimbusds.jose.JOSEObject;
011 import com.nimbusds.jose.Payload;
012 import com.nimbusds.jose.JWEHeader;
013 import com.nimbusds.jose.JWEObject;
014
015 import com.nimbusds.jose.util.Base64URL;
016
017
018 /**
019 * Encrypted JSON Web Token (JWT). This class is thread-safe.
020 *
021 * @author Vladimir Dzhuvinov
022 * @version $version$ (2013-01-15)
023 */
024 @ThreadSafe
025 public class EncryptedJWT extends JWEObject implements JWT {
026
027
028 /**
029 * Creates a new to-be-encrypted JSON Web Token (JWT) with the specified
030 * header and claims set. The initial state will be
031 * {@link com.nimbusds.jose.JWEObject.State#UNENCRYPTED unencrypted}.
032 *
033 * @param header The JWE header. Must not be {@code null}.
034 * @param claimsSet The JWT claims set. Must not be {@code null}.
035 */
036 public EncryptedJWT(final JWEHeader header, ReadOnlyJWTClaimsSet claimsSet) {
037
038 super(header, new Payload(claimsSet.toJSONObject()));
039 }
040
041
042 /**
043 * Creates a new encrypted JSON Web Token (JWT) with the specified
044 * serialised parts. The state will be
045 * {@link com.nimbusds.jose.JWEObject.State#ENCRYPTED encrypted}.
046 *
047 * @param firstPart The first part, corresponding to the JWE header.
048 * Must not be {@code null}.
049 * @param secondPart The second part, corresponding to the encrypted
050 * key. Empty or {@code null} if none.
051 * @param thirdPart The third part, corresponding to the initialisation
052 * vectory. Empty or {@code null} if none.
053 * @param fourthPart The fourth part, corresponding to the cipher text.
054 * Must not be {@code null}.
055 * @param fifthPart The fifth part, corresponding to the integrity
056 * value. Empty of {@code null} if none.
057 *
058 * @throws ParseException If parsing of the serialised parts failed.
059 */
060 public EncryptedJWT(final Base64URL firstPart,
061 final Base64URL secondPart,
062 final Base64URL thirdPart,
063 final Base64URL fourthPart,
064 final Base64URL fifthPart)
065 throws ParseException {
066
067 super(firstPart, secondPart, thirdPart, fourthPart, fifthPart);
068 }
069
070
071 @Override
072 public ReadOnlyJWTClaimsSet getJWTClaimsSet()
073 throws ParseException {
074
075 Payload payload = getPayload();
076
077 if (payload == null)
078 return null;
079
080 JSONObject json = payload.toJSONObject();
081
082 if (json == null)
083 throw new ParseException("Payload of JWE object is not a valid JSON object", 0);
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 return new EncryptedJWT(parts[0], parts[1], parts[2], parts[3], parts[4]);
109 }
110 }