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.JWSHeader;
011import com.nimbusds.jose.JWSObject;
012import com.nimbusds.jose.Payload;
013import com.nimbusds.jose.util.Base64URL;
014
015
016/**
017 * Signed JSON Web Token (JWT).
018 *
019 * @author Vladimir Dzhuvinov
020 * @version $version$ (2013-01-15)
021 */
022@ThreadSafe
023public class SignedJWT extends JWSObject implements JWT {
024
025
026        /**
027         * Creates a new to-be-signed JSON Web Token (JWT) with the specified
028         * header and claims set. The initial state will be 
029         * {@link com.nimbusds.jose.JWSObject.State#UNSIGNED unsigned}.
030         *
031         * @param header    The JWS header. Must not be {@code null}.
032         * @param claimsSet The JWT claims set. Must not be {@code null}.
033         */
034        public SignedJWT(final JWSHeader header, final ReadOnlyJWTClaimsSet claimsSet) {
035
036                super(header, new Payload(claimsSet.toJSONObject()));
037        }
038
039
040        /**
041         * Creates a new signed JSON Web Token (JWT) with the specified 
042         * serialised parts. The state will be 
043         * {@link com.nimbusds.jose.JWSObject.State#SIGNED signed}.
044         *
045         * @param firstPart  The first part, corresponding to the JWS header. 
046         *                   Must not be {@code null}.
047         * @param secondPart The second part, corresponding to the claims set
048         *                   (payload). Must not be {@code null}.
049         * @param thirdPart  The third part, corresponding to the signature.
050         *                   Must not be {@code null}.
051         *
052         * @throws ParseException If parsing of the serialised parts failed.
053         */
054        public SignedJWT(final Base64URL firstPart, final Base64URL secondPart, final Base64URL thirdPart)      
055                        throws ParseException {
056
057                super(firstPart, secondPart, thirdPart);
058        }
059
060
061        @Override
062        public ReadOnlyJWTClaimsSet getJWTClaimsSet()   
063                        throws ParseException {
064
065                JSONObject json = getPayload().toJSONObject();
066
067                if (json == null) {
068                        throw new ParseException("Payload of JWS object is not a valid JSON object", 0);
069                }
070
071                return JWTClaimsSet.parse(json);
072        }
073
074
075        /**
076         * Parses a signed JSON Web Token (JWT) from the specified string in 
077         * compact format. 
078         *
079         * @param s The string to parse. Must not be {@code null}.
080         *
081         * @return The signed JWT.
082         *
083         * @throws ParseException If the string couldn't be parsed to a valid 
084         *                        signed JWT.
085         */
086        public static SignedJWT parse(final String s)
087                        throws ParseException {
088
089                Base64URL[] parts = JOSEObject.split(s);
090
091                if (parts.length != 3) {
092                        throw new ParseException("Unexpected number of Base64URL parts, must be three", 0);
093                }
094
095                return new SignedJWT(parts[0], parts[1], parts[2]);
096        }
097}