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