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