Class JsonWebSignature
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 ClassesModifier and TypeClassDescriptionstatic class
Header as specified in Reserved Header Parameter Names.static final class
JWS parser.Nested classes/interfaces inherited from class com.google.api.client.json.webtoken.JsonWebToken
JsonWebToken.Payload
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final byte[]
Bytes of the signature.private final byte[]
Bytes of the signature content. -
Constructor Summary
ConstructorsConstructorDescriptionJsonWebSignature
(JsonWebSignature.Header header, JsonWebToken.Payload payload, byte[] signatureBytes, byte[] signedContentBytes) -
Method Summary
Modifier and TypeMethodDescriptionprivate static X509TrustManager
Returns the header.final byte[]
Returns the bytes of the signature.final byte[]
Returns the bytes of the signature content.static JsonWebSignature
parse
(JsonFactory jsonFactory, String tokenString) Parses the given JWS token string and returns the parsedJsonWebSignature
.static JsonWebSignature.Parser
parser
(JsonFactory jsonFactory) Returns a new instance of a JWS parser.static String
signUsingRsaSha256
(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.final X509Certificate
Beta
Verifies the signature of the content using the certificate chain embedded in the signature.final boolean
verifySignature
(PublicKey publicKey) Verifies the signature of the content.final X509Certificate
verifySignature
(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
-
Field Details
-
signatureBytes
private final byte[] signatureBytesBytes of the signature. -
signedContentBytes
private final byte[] signedContentBytesBytes of the signature content.
-
-
Constructor Details
-
JsonWebSignature
public JsonWebSignature(JsonWebSignature.Header header, JsonWebToken.Payload payload, byte[] signatureBytes, byte[] signedContentBytes) - Parameters:
header
- headerpayload
- payloadsignatureBytes
- bytes of the signaturesignedContentBytes
- bytes of the signed content
-
-
Method Details
-
getHeader
Description copied from class:JsonWebToken
Returns the header.Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.
- Overrides:
getHeader
in classJsonWebToken
-
verifySignature
Verifies the signature of the content.Currently only
"RS256"
and"ES256"
algorithms are 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:
GeneralSecurityException
-
verifySignature
@Beta public final X509Certificate verifySignature(X509TrustManager trustManager) throws GeneralSecurityException Beta
Verifies the signature of the content using the certificate chain embedded in the signature.Currently only
"RS256"
and"ES256"
algorithms are 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:
GeneralSecurityException
- Since:
- 1.19.1
-
verifySignature
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:
GeneralSecurityException
- Since:
- 1.19.1.
-
getDefaultX509TrustManager
-
getSignatureBytes
public final byte[] getSignatureBytes()Returns the bytes of the signature. -
getSignedContentBytes
public final byte[] getSignedContentBytes()Returns the bytes of the signature content. -
parse
public static JsonWebSignature parse(JsonFactory jsonFactory, String tokenString) throws IOException Parses the given JWS token string and returns the parsedJsonWebSignature
.- Parameters:
jsonFactory
- JSON factorytokenString
- JWS token string- Returns:
- parsed JWS
- Throws:
IOException
-
parser
Returns a new instance of a JWS parser. -
signUsingRsaSha256
public static String signUsingRsaSha256(PrivateKey privateKey, JsonFactory jsonFactory, JsonWebSignature.Header header, JsonWebToken.Payload payload) throws GeneralSecurityException, IOException 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.- Parameters:
privateKey
- private keyjsonFactory
- JSON factoryheader
- JWS headerpayload
- JWS payload- Returns:
- signed JWS string
- Throws:
GeneralSecurityException
IOException
- Since:
- 1.14 (since 1.7 as com.google.api.client.auth.jsontoken.RsaSHA256Signer)
-