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