001/*
002 * nimbus-jose-jwt
003 *
004 * Copyright 2012-2016, Connect2id Ltd.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
007 * this file except in compliance with the License. You may obtain a copy of the
008 * License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software distributed
013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
015 * specific language governing permissions and limitations under the License.
016 */
017
018package com.nimbusds.jwt;
019
020
021import java.text.ParseException;
022import java.util.Map;
023
024import net.jcip.annotations.ThreadSafe;
025
026import com.nimbusds.jose.JOSEObject;
027import com.nimbusds.jose.JWSHeader;
028import com.nimbusds.jose.JWSObject;
029import com.nimbusds.jose.Payload;
030import com.nimbusds.jose.util.Base64URL;
031
032
033/**
034 * Signed JSON Web Token (JWT).
035 *
036 * @author Vladimir Dzhuvinov
037 * @version 2015-08-19
038 */
039@ThreadSafe
040public class SignedJWT extends JWSObject implements JWT {
041
042
043        private static final long serialVersionUID = 1L;
044
045
046        /**
047         * Creates a new to-be-signed JSON Web Token (JWT) with the specified
048         * header and claims set. The initial state will be 
049         * {@link com.nimbusds.jose.JWSObject.State#UNSIGNED unsigned}.
050         *
051         * @param header    The JWS header. Must not be {@code null}.
052         * @param claimsSet The JWT claims set. Must not be {@code null}.
053         */
054        public SignedJWT(final JWSHeader header, final JWTClaimsSet claimsSet) {
055
056                super(header, new Payload(claimsSet.toJSONObject()));
057        }
058
059
060        /**
061         * Creates a new signed JSON Web Token (JWT) with the specified 
062         * serialised parts. The state will be 
063         * {@link com.nimbusds.jose.JWSObject.State#SIGNED signed}.
064         *
065         * @param firstPart  The first part, corresponding to the JWS header. 
066         *                   Must not be {@code null}.
067         * @param secondPart The second part, corresponding to the claims set
068         *                   (payload). Must not be {@code null}.
069         * @param thirdPart  The third part, corresponding to the signature.
070         *                   Must not be {@code null}.
071         *
072         * @throws ParseException If parsing of the serialised parts failed.
073         */
074        public SignedJWT(final Base64URL firstPart, final Base64URL secondPart, final Base64URL thirdPart)      
075                throws ParseException {
076
077                super(firstPart, secondPart, thirdPart);
078        }
079
080
081        @Override
082        public JWTClaimsSet getJWTClaimsSet()
083                throws ParseException {
084
085                Map<String, Object> json = getPayload().toJSONObject();
086
087                if (json == null) {
088                        throw new ParseException("Payload of JWS object is not a valid JSON object", 0);
089                }
090
091                return JWTClaimsSet.parse(json);
092        }
093
094
095        /**
096         * Parses a signed JSON Web Token (JWT) from the specified string in 
097         * compact format. 
098         *
099         * @param s The string to parse. Must not be {@code null}.
100         *
101         * @return The signed JWT.
102         *
103         * @throws ParseException If the string couldn't be parsed to a valid 
104         *                        signed JWT.
105         */
106        public static SignedJWT parse(final String s)
107                throws ParseException {
108
109                Base64URL[] parts = JOSEObject.split(s);
110
111                if (parts.length != 3) {
112                        throw new ParseException("Unexpected number of Base64URL parts, must be three", 0);
113                }
114
115                return new SignedJWT(parts[0], parts[1], parts[2]);
116        }
117}