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.JWEHeader;
028import com.nimbusds.jose.JWEObject;
029import com.nimbusds.jose.Payload;
030import com.nimbusds.jose.util.Base64URL;
031
032
033/**
034 * Encrypted JSON Web Token (JWT). This class is thread-safe.
035 *
036 * @author Vladimir Dzhuvinov
037 * @version 2015-08-19
038 */
039@ThreadSafe
040public class EncryptedJWT extends JWEObject implements JWT {
041
042
043        private static final long serialVersionUID = 1L;
044
045
046        /**
047         * Creates a new to-be-encrypted JSON Web Token (JWT) with the specified
048         * header and claims set. The initial state will be 
049         * {@link com.nimbusds.jose.JWEObject.State#UNENCRYPTED unencrypted}.
050         *
051         * @param header    The JWE header. Must not be {@code null}.
052         * @param claimsSet The JWT claims set. Must not be {@code null}.
053         */
054        public EncryptedJWT(final JWEHeader header, final JWTClaimsSet claimsSet) {
055
056                super(header, new Payload(claimsSet.toJSONObject()));
057        }
058
059
060        /**
061         * Creates a new encrypted JSON Web Token (JWT) with the specified 
062         * serialised parts. The state will be 
063         * {@link com.nimbusds.jose.JWEObject.State#ENCRYPTED encrypted}.
064         *
065         * @param firstPart  The first part, corresponding to the JWE header. 
066         *                   Must not be {@code null}.
067         * @param secondPart The second part, corresponding to the encrypted 
068         *                   key. Empty or {@code null} if none.
069         * @param thirdPart  The third part, corresponding to the initialisation
070         *                   vectory. Empty or {@code null} if none.
071         * @param fourthPart The fourth part, corresponding to the cipher text.
072         *                   Must not be {@code null}.
073         * @param fifthPart  The fifth part, corresponding to the integrity
074         *                   value. Empty of {@code null} if none.
075         *
076         * @throws ParseException If parsing of the serialised parts failed.
077         */
078        public EncryptedJWT(final Base64URL firstPart, 
079                            final Base64URL secondPart, 
080                            final Base64URL thirdPart,
081                            final Base64URL fourthPart,
082                            final Base64URL fifthPart)
083                throws ParseException {
084
085                super(firstPart, secondPart, thirdPart, fourthPart, fifthPart);
086        }
087
088
089        @Override
090        public JWTClaimsSet getJWTClaimsSet()
091                throws ParseException {
092
093                Payload payload = getPayload();
094
095                if (payload == null) {
096                        return null;
097                }
098
099                Map<String, Object> json = payload.toJSONObject();
100
101                if (json == null) {
102                        throw new ParseException("Payload of JWE object is not a valid JSON object", 0);
103                }
104
105                return JWTClaimsSet.parse(json);
106        }
107
108
109        /**
110         * Parses an encrypted JSON Web Token (JWT) from the specified string in
111         * compact format. 
112         *
113         * @param s The string to parse. Must not be {@code null}.
114         *
115         * @return The encrypted JWT.
116         *
117         * @throws ParseException If the string couldn't be parsed to a valid 
118         *                        encrypted JWT.
119         */
120        public static EncryptedJWT parse(final String s)
121                throws ParseException {
122
123                Base64URL[] parts = JOSEObject.split(s);
124
125                if (parts.length != 5) {
126                        throw new ParseException("Unexpected number of Base64URL parts, must be five", 0);
127                }
128
129                return new EncryptedJWT(parts[0], parts[1], parts[2], parts[3], parts[4]);
130        }
131}