Class JsonWebSignature
- java.lang.Object
-
- com.google.api.client.json.webtoken.JsonWebToken
-
- com.google.api.client.json.webtoken.JsonWebSignature
-
public class JsonWebSignature extends JsonWebToken
JSON Web Signature (JWS).Sample usage:
public static void printPayload(JsonFactory jsonFactory, String tokenString) throws IOException { JsonWebSignature jws = JsonWebSignature.parse(jsonFactory, tokenString); System.out.println(jws.getPayload()); }Implementation is not thread-safe.
- Since:
- 1.14 (since 1.7 as com.google.api.client.auth.jsontoken.JsonWebSignature)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classJsonWebSignature.HeaderHeader as specified in Reserved Header Parameter Names.static classJsonWebSignature.ParserJWS parser.-
Nested classes/interfaces inherited from class com.google.api.client.json.webtoken.JsonWebToken
JsonWebToken.Payload
-
-
Field Summary
Fields Modifier and Type Field Description private byte[]signatureBytesBytes of the signature.private byte[]signedContentBytesBytes of the signature content.
-
Constructor Summary
Constructors Constructor Description JsonWebSignature(JsonWebSignature.Header header, JsonWebToken.Payload payload, byte[] signatureBytes, byte[] signedContentBytes)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static javax.net.ssl.X509TrustManagergetDefaultX509TrustManager()JsonWebSignature.HeadergetHeader()Returns the header.byte[]getSignatureBytes()Returns the modifiable array of bytes of the signature.byte[]getSignedContentBytes()Returns the modifiable array of bytes of the signature content.static JsonWebSignatureparse(JsonFactory jsonFactory, java.lang.String tokenString)Parses the given JWS token string and returns the parsedJsonWebSignature.static JsonWebSignature.Parserparser(JsonFactory jsonFactory)Returns a new instance of a JWS parser.static java.lang.StringsignUsingRsaSha256(java.security.PrivateKey privateKey, JsonFactory jsonFactory, JsonWebSignature.Header header, JsonWebToken.Payload payload)Signs a given JWS header and payload based on the given private key using RSA and SHA-256 as described in JWS using RSA SHA-256.java.security.cert.X509CertificateverifySignature()Beta
Verifies the signature of the content using the certificate chain embedded in the signature.booleanverifySignature(java.security.PublicKey publicKey)Verifies the signature of the content.java.security.cert.X509CertificateverifySignature(javax.net.ssl.X509TrustManager trustManager)Beta
Verifies the signature of the content using the certificate chain embedded in the signature.-
Methods inherited from class com.google.api.client.json.webtoken.JsonWebToken
getPayload, toString
-
-
-
-
Constructor Detail
-
JsonWebSignature
public JsonWebSignature(JsonWebSignature.Header header, JsonWebToken.Payload payload, byte[] signatureBytes, byte[] signedContentBytes)
- Parameters:
header- headerpayload- payloadsignatureBytes- bytes of the signaturesignedContentBytes- bytes of the signature content
-
-
Method Detail
-
getHeader
public JsonWebSignature.Header getHeader()
Description copied from class:JsonWebTokenReturns the header.Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.
- Overrides:
getHeaderin classJsonWebToken
-
verifySignature
public final boolean verifySignature(java.security.PublicKey publicKey) throws java.security.GeneralSecurityExceptionVerifies the signature of the content.Currently only
"RS256"algorithm is verified, but others may be added in the future. For any other algorithm it returnsfalse.- Parameters:
publicKey- public key- Returns:
- whether the algorithm is recognized and it is verified
- Throws:
java.security.GeneralSecurityException
-
verifySignature
@Beta public final java.security.cert.X509Certificate verifySignature(javax.net.ssl.X509TrustManager trustManager) throws java.security.GeneralSecurityException
Beta
Verifies the signature of the content using the certificate chain embedded in the signature.Currently only
"RS256"algorithm is verified, but others may be added in the future. For any other algorithm it returnsnull.The leaf certificate of the certificate chain must be an SSL server certificate.
- Parameters:
trustManager- Trust manager used to verify the X509 certificate chain embedded in this message.- Returns:
- The signature certificate if the signature could be verified, null otherwise.
- Throws:
java.security.GeneralSecurityException- Since:
- 1.19.1.
-
verifySignature
@Beta public final java.security.cert.X509Certificate verifySignature() throws java.security.GeneralSecurityException
Beta
Verifies the signature of the content using the certificate chain embedded in the signature.Currently only
"RS256"algorithm is verified, but others may be added in the future. For any other algorithm it returnsnull.The certificate chain is verified using the system default trust manager.
The leaf certificate of the certificate chain must be an SSL server certificate.
- Returns:
- The signature certificate if the signature could be verified, null otherwise.
- Throws:
java.security.GeneralSecurityException- Since:
- 1.19.1.
-
getDefaultX509TrustManager
private static javax.net.ssl.X509TrustManager getDefaultX509TrustManager()
-
getSignatureBytes
public final byte[] getSignatureBytes()
Returns the modifiable array of bytes of the signature.
-
getSignedContentBytes
public final byte[] getSignedContentBytes()
Returns the modifiable array of bytes of the signature content.
-
parse
public static JsonWebSignature parse(JsonFactory jsonFactory, java.lang.String tokenString) throws java.io.IOException
Parses the given JWS token string and returns the parsedJsonWebSignature.- Parameters:
jsonFactory- JSON factorytokenString- JWS token string- Returns:
- parsed JWS
- Throws:
java.io.IOException
-
parser
public static JsonWebSignature.Parser parser(JsonFactory jsonFactory)
Returns a new instance of a JWS parser.
-
signUsingRsaSha256
public static java.lang.String signUsingRsaSha256(java.security.PrivateKey privateKey, JsonFactory jsonFactory, JsonWebSignature.Header header, JsonWebToken.Payload payload) throws java.security.GeneralSecurityException, java.io.IOExceptionSigns a given JWS header and payload based on the given private key using RSA and SHA-256 as described in JWS using RSA SHA-256.- Parameters:
privateKey- private keyjsonFactory- JSON factoryheader- JWS headerpayload- JWS payload- Returns:
- signed JWS string
- Throws:
java.security.GeneralSecurityExceptionjava.io.IOException- Since:
- 1.14 (since 1.7 as com.google.api.client.auth.jsontoken.RsaSHA256Signer)
-
-