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.jcip.annotations.ThreadSafe; 024 025import net.minidev.json.JSONObject; 026 027import com.nimbusds.jose.JOSEObject; 028import com.nimbusds.jose.Payload; 029import com.nimbusds.jose.PlainHeader; 030import com.nimbusds.jose.PlainObject; 031import com.nimbusds.jose.util.Base64URL; 032 033 034/** 035 * Unsecured (plain) JSON Web Token (JWT). 036 * 037 * @author Vladimir Dzhuvinov 038 * @version 2015-08-19 039 */ 040@ThreadSafe 041public class PlainJWT extends PlainObject implements JWT { 042 043 044 private static final long serialVersionUID = 1L; 045 046 047 /** 048 * Creates a new unsecured (plain) JSON Web Token (JWT) with a default 049 * {@link com.nimbusds.jose.PlainHeader} and the specified claims 050 * set. 051 * 052 * @param claimsSet The JWT claims set. Must not be {@code null}. 053 */ 054 public PlainJWT(final JWTClaimsSet claimsSet) { 055 056 super(new Payload(claimsSet.toJSONObject())); 057 } 058 059 060 /** 061 * Creates a new unsecured (plain) JSON Web Token (JWT) with the 062 * specified header and claims set. 063 * 064 * @param header The unsecured header. Must not be {@code null}. 065 * @param claimsSet The JWT claims set. Must not be {@code null}. 066 */ 067 public PlainJWT(final PlainHeader header, final JWTClaimsSet claimsSet) { 068 069 super(header, new Payload(claimsSet.toJSONObject())); 070 } 071 072 073 /** 074 * Creates a new unsecured (plain) JSON Web Token (JWT) with the 075 * specified Base64URL-encoded parts. 076 * 077 * @param firstPart The first part, corresponding to the unsecured 078 * header. Must not be {@code null}. 079 * @param secondPart The second part, corresponding to the claims set 080 * (payload). Must not be {@code null}. 081 * 082 * @throws ParseException If parsing of the serialised parts failed. 083 */ 084 public PlainJWT(final Base64URL firstPart, final Base64URL secondPart) 085 throws ParseException { 086 087 super(firstPart, secondPart); 088 } 089 090 091 @Override 092 public JWTClaimsSet getJWTClaimsSet() 093 throws ParseException { 094 095 JSONObject json = getPayload().toJSONObject(); 096 097 if (json == null) { 098 099 throw new ParseException("Payload of unsecured JOSE object is not a valid JSON object", 0); 100 } 101 102 return JWTClaimsSet.parse(json); 103 } 104 105 106 /** 107 * Parses an unsecured (plain) JSON Web Token (JWT) from the specified 108 * string in compact format. 109 * 110 * @param s The string to parse. Must not be {@code null}. 111 * 112 * @return The unsecured JWT. 113 * 114 * @throws ParseException If the string couldn't be parsed to a valid 115 * unsecured JWT. 116 */ 117 public static PlainJWT parse(final String s) 118 throws ParseException { 119 120 Base64URL[] parts = JOSEObject.split(s); 121 122 if (! parts[2].toString().isEmpty()) { 123 124 throw new ParseException("Unexpected third Base64URL part in the unsecured JWT object", 0); 125 } 126 127 return new PlainJWT(parts[0], parts[1]); 128 } 129}