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