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