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.Payload;
012import com.nimbusds.jose.PlainHeader;
013import com.nimbusds.jose.PlainObject;
014import com.nimbusds.jose.util.Base64URL;
015
016
017/**
018 * Unsecured (plain) JSON Web Token (JWT).
019 *
020 * @author Vladimir Dzhuvinov
021 * @version 2014-08-21
022 */
023@ThreadSafe
024public class PlainJWT extends PlainObject implements JWT {
025
026
027        /**
028         * Creates a new unsecured (plain) JSON Web Token (JWT) with a default
029         * {@link com.nimbusds.jose.PlainHeader} and the specified claims 
030         * set.
031         *
032         * @param claimsSet The JWT claims set. Must not be {@code null}.
033         */
034        public PlainJWT(final ReadOnlyJWTClaimsSet claimsSet) {
035
036                super(new Payload(claimsSet.toJSONObject()));
037        }
038
039
040        /**
041         * Creates a new unsecured (plain) JSON Web Token (JWT) with the
042         * specified header and claims set.
043         *
044         * @param header    The unsecured header. Must not be {@code null}.
045         * @param claimsSet The JWT claims set. Must not be {@code null}.
046         */
047        public PlainJWT(final PlainHeader header, final ReadOnlyJWTClaimsSet claimsSet) {
048
049                super(header, new Payload(claimsSet.toJSONObject()));
050        }
051
052
053        /**
054         * Creates a new unsecured (plain) JSON Web Token (JWT) with the
055         * specified Base64URL-encoded parts.
056         *
057         * @param firstPart  The first part, corresponding to the unsecured
058         *                   header. Must not be {@code null}.
059         * @param secondPart The second part, corresponding to the claims set 
060         *                   (payload). Must not be {@code null}.
061         *
062         * @throws ParseException If parsing of the serialised parts failed.
063         */
064        public PlainJWT(final Base64URL firstPart, final Base64URL secondPart)
065                throws ParseException {
066
067                super(firstPart, secondPart);
068        }
069
070
071        @Override
072        public ReadOnlyJWTClaimsSet getJWTClaimsSet()
073                throws ParseException {
074
075                JSONObject json = getPayload().toJSONObject();
076
077                if (json == null) {
078                        
079                        throw new ParseException("Payload of unsecured JOSE object is not a valid JSON object", 0);
080                }
081
082                return JWTClaimsSet.parse(json);
083        }
084
085
086        /**
087         * Parses an unsecured (plain) JSON Web Token (JWT) from the specified
088         * string in compact format.
089         *
090         * @param s The string to parse. Must not be {@code null}.
091         *
092         * @return The unsecured JWT.
093         *
094         * @throws ParseException If the string couldn't be parsed to a valid 
095         *                        unsecured JWT.
096         */
097        public static PlainJWT parse(final String s)
098                throws ParseException {
099
100                Base64URL[] parts = JOSEObject.split(s);
101
102                if (! parts[2].toString().isEmpty()) {
103
104                        throw new ParseException("Unexpected third Base64URL part in the unsecured JWT object", 0);
105                }
106
107                return new PlainJWT(parts[0], parts[1]);
108        }
109}