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