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;
022
023import net.minidev.json.JSONObject;
024
025import com.nimbusds.jose.Algorithm;
026import com.nimbusds.jose.Header;
027import com.nimbusds.jose.JWEAlgorithm;
028import com.nimbusds.jose.JWSAlgorithm;
029import com.nimbusds.jose.util.Base64URL;
030import com.nimbusds.jose.util.JSONObjectUtils;
031
032
033/**
034 * Parser for unsecured (plain), signed and encrypted JSON Web Tokens (JWTs).
035 *
036 * @author Vladimir Dzhuvinov
037 * @author Junya Hayashi
038 * @version 2015-06-14
039 */
040public final class JWTParser {
041
042
043        /**
044         * Parses an unsecured (plain), signed or encrypted JSON Web Token
045         * (JWT) from the specified string in compact format.
046         *
047         * @param s The string to parse. Must not be {@code null}.
048         *
049         * @return The corresponding {@link PlainJWT}, {@link SignedJWT} or
050         *         {@link EncryptedJWT} instance.
051         *
052         * @throws ParseException If the string couldn't be parsed to a valid 
053         *                        unsecured, signed or encrypted JWT.
054         */
055        public static JWT parse(final String s)
056                throws ParseException {
057
058                final int firstDotPos = s.indexOf(".");
059                
060                if (firstDotPos == -1)
061                        throw new ParseException("Invalid JWT serialization: Missing dot delimiter(s)", 0);
062                        
063                Base64URL header = new Base64URL(s.substring(0, firstDotPos));
064                
065                JSONObject jsonObject;
066
067                try {
068                        jsonObject = JSONObjectUtils.parse(header.decodeToString());
069
070                } catch (ParseException e) {
071
072                        throw new ParseException("Invalid unsecured/JWS/JWE header: " + e.getMessage(), 0);
073                }
074
075                Algorithm alg = Header.parseAlgorithm(jsonObject);
076
077                if (alg.equals(Algorithm.NONE)) {
078                        return PlainJWT.parse(s);
079                } else if (alg instanceof JWSAlgorithm) {
080                        return SignedJWT.parse(s);
081                } else if (alg instanceof JWEAlgorithm) {
082                        return EncryptedJWT.parse(s);
083                } else {
084                        throw new AssertionError("Unexpected algorithm type: " + alg);
085                }
086        }
087
088
089        /**
090         * Prevents instantiation.
091         */
092        private JWTParser() {
093
094        }
095}