|
| 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.annotations.Beta; |
| 37 | +import com.google.common.base.MoreObjects; |
| 38 | +import java.io.IOException; |
| 39 | +import java.io.ObjectInputStream; |
| 40 | +import java.io.ObjectOutputStream; |
| 41 | +import java.io.Serializable; |
| 42 | +import java.util.Date; |
| 43 | +import java.util.Objects; |
| 44 | + |
| 45 | +/** Represents a temporary IdToken and its JsonWebSignature object */ |
| 46 | +@Beta |
| 47 | +public class IdToken extends AccessToken implements Serializable { |
| 48 | + |
| 49 | + private static final long serialVersionUID = -8514239465808977353L; |
| 50 | + |
| 51 | + private transient JsonWebSignature jsonWebSignature; |
| 52 | + |
| 53 | + /** |
| 54 | + * @param tokenValue String representation of the ID token. |
| 55 | + * @param jsonWebSignature JsonWebSignature as object |
| 56 | + */ |
| 57 | + private IdToken(String tokenValue, JsonWebSignature jsonWebSignature) { |
| 58 | + super(tokenValue, new Date(jsonWebSignature.getPayload().getExpirationTimeSeconds() * 1000)); |
| 59 | + this.jsonWebSignature = jsonWebSignature; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Creates an IdToken given the encoded Json Web Signature. |
| 64 | + * |
| 65 | + * @param tokenValue String representation of the ID token. |
| 66 | + * @return returns com.google.auth.oauth2.IdToken |
| 67 | + */ |
| 68 | + public static IdToken create(String tokenValue) throws IOException { |
| 69 | + return create(tokenValue, OAuth2Utils.JSON_FACTORY); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Creates an IdToken given the encoded Json Web Signature and JSON Factory |
| 74 | + * |
| 75 | + * @param jsonFactory JsonFactory to use for parsing the provided token. |
| 76 | + * @param tokenValue String representation of the ID token. |
| 77 | + * @return returns com.google.auth.oauth2.IdToken |
| 78 | + */ |
| 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 | + */ |
| 88 | + public JsonWebSignature getJsonWebSignature() { |
| 89 | + return jsonWebSignature; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public int hashCode() { |
| 94 | + return Objects.hash( |
| 95 | + super.getTokenValue(), jsonWebSignature.getHeader(), jsonWebSignature.getPayload()); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public String toString() { |
| 100 | + return MoreObjects.toStringHelper(this) |
| 101 | + .add("tokenValue", super.getTokenValue()) |
| 102 | + .add("JsonWebSignature", jsonWebSignature) |
| 103 | + .toString(); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 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 | + |
| 117 | + private void writeObject(ObjectOutputStream oos) throws IOException { |
| 118 | + oos.writeObject(this.getTokenValue()); |
| 119 | + } |
| 120 | + |
| 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 | +} |
0 commit comments