1 /* 2 * Copyright 2019, Google LLC 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * 15 * * Neither the name of Google LLC nor the names of its 16 * contributors may be used to endorse or promote products derived from 17 * this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 package com.google.auth.oauth2; 33 34 import com.google.api.client.json.JsonFactory; 35 import com.google.api.client.json.webtoken.JsonWebSignature; 36 import com.google.common.base.MoreObjects; 37 import java.io.IOException; 38 import java.io.ObjectInputStream; 39 import java.io.ObjectOutputStream; 40 import java.io.Serializable; 41 import java.util.Date; 42 import java.util.Objects; 43 44 /** Represents a temporary IdToken and its JsonWebSignature object */ 45 public class IdToken extends AccessToken implements Serializable { 46 47 private static final long serialVersionUID = -8514239465808977353L; 48 49 private transient JsonWebSignature jsonWebSignature; 50 51 /** 52 * @param tokenValue String representation of the ID token. 53 * @param jsonWebSignature JsonWebSignature as object 54 */ IdToken(String tokenValue, JsonWebSignature jsonWebSignature)55 private IdToken(String tokenValue, JsonWebSignature jsonWebSignature) { 56 super(tokenValue, new Date(jsonWebSignature.getPayload().getExpirationTimeSeconds() * 1000)); 57 this.jsonWebSignature = jsonWebSignature; 58 } 59 60 /** 61 * Creates an IdToken given the encoded Json Web Signature. 62 * 63 * @param tokenValue String representation of the ID token. 64 * @throws IOException if JWT token parsing fails 65 * @return returns com.google.auth.oauth2.IdToken 66 */ create(String tokenValue)67 public static IdToken create(String tokenValue) throws IOException { 68 return create(tokenValue, OAuth2Utils.JSON_FACTORY); 69 } 70 71 /** 72 * Creates an IdToken given the encoded Json Web Signature and JSON Factory 73 * 74 * @param jsonFactory JsonFactory to use for parsing the provided token. 75 * @param tokenValue String representation of the ID token. 76 * @throws IOException if JWT token parsing fails 77 * @return returns com.google.auth.oauth2.IdToken 78 */ create(String tokenValue, JsonFactory jsonFactory)79 public static IdToken create(String tokenValue, JsonFactory jsonFactory) throws IOException { 80 return new IdToken(tokenValue, JsonWebSignature.parse(jsonFactory, tokenValue)); 81 } 82 83 /** 84 * The JsonWebSignature as object 85 * 86 * @return returns com.google.api.client.json.webtoken.JsonWebSignature 87 */ getJsonWebSignature()88 JsonWebSignature getJsonWebSignature() { 89 return jsonWebSignature; 90 } 91 92 @Override hashCode()93 public int hashCode() { 94 return Objects.hash( 95 super.getTokenValue(), jsonWebSignature.getHeader(), jsonWebSignature.getPayload()); 96 } 97 98 @Override toString()99 public String toString() { 100 return MoreObjects.toStringHelper(this) 101 .add("tokenValue", super.getTokenValue()) 102 .add("JsonWebSignature", jsonWebSignature) 103 .toString(); 104 } 105 106 @Override equals(Object obj)107 public boolean equals(Object obj) { 108 if (!(obj instanceof IdToken)) { 109 return false; 110 } 111 IdToken other = (IdToken) obj; 112 return Objects.equals(super.getTokenValue(), other.getTokenValue()) 113 && Objects.equals(this.jsonWebSignature.getHeader(), other.jsonWebSignature.getHeader()) 114 && Objects.equals(this.jsonWebSignature.getPayload(), other.jsonWebSignature.getPayload()); 115 } 116 writeObject(ObjectOutputStream oos)117 private void writeObject(ObjectOutputStream oos) throws IOException { 118 oos.writeObject(this.getTokenValue()); 119 } 120 readObject(ObjectInputStream ois)121 private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException { 122 String signature = (String) ois.readObject(); 123 this.jsonWebSignature = JsonWebSignature.parse(OAuth2Utils.JSON_FACTORY, signature); 124 } 125 } 126