本文整理汇总了Java中org.jose4j.jws.JsonWebSignature类的典型用法代码示例。如果您正苦于以下问题:Java JsonWebSignature类的具体用法?Java JsonWebSignature怎么用?Java JsonWebSignature使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JsonWebSignature类属于org.jose4j.jws包,在下文中一共展示了JsonWebSignature类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: verifyJwsSignature
import org.jose4j.jws.JsonWebSignature; //导入依赖的package包/类
/**
* Verify jws signature byte [ ].
*
* @param value the value
* @param signingKey the signing key
* @return the byte [ ]
*/
public static byte[] verifyJwsSignature(final Key signingKey, final byte[] value) {
try {
final String asString = new String(value, StandardCharsets.UTF_8);
final JsonWebSignature jws = new JsonWebSignature();
jws.setCompactSerialization(asString);
jws.setKey(signingKey);
final boolean verified = jws.verifySignature();
if (verified) {
final String payload = jws.getPayload();
LOGGER.trace("Successfully decoded value. Result in Base64-encoding is [{}]", payload);
return EncodingUtils.decodeBase64(payload);
}
return null;
} catch (final Exception e) {
throw Throwables.propagate(e);
}
}
示例2: generateJWTAssertion
import org.jose4j.jws.JsonWebSignature; //导入依赖的package包/类
public static String generateJWTAssertion(String email, String privateKeyBase64,
float expiryInSeconds) {
PrivateKey privateKey = getPrivateKey(privateKeyBase64);
final JwtClaims claims = new JwtClaims();
claims.setSubject(email);
claims.setAudience("https://api.metamind.io/v1/oauth2/token");
claims.setExpirationTimeMinutesInTheFuture(expiryInSeconds / 60);
claims.setIssuedAtToNow();
// Generate the payload
final JsonWebSignature jws = new JsonWebSignature();
jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.RSA_USING_SHA256);
jws.setPayload(claims.toJson());
jws.setKeyIdHeaderValue(UUID.randomUUID().toString());
// Sign using the private key
jws.setKey(privateKey);
try {
return jws.getCompactSerialization();
} catch (JoseException e) {
return null;
}
}
示例3: createSignedTokenFromClaims
import org.jose4j.jws.JsonWebSignature; //导入依赖的package包/类
/**
* Create a RSA256 signed token from given claims and RSA jwk.
*
* @param JwtClaims claims
* @param RsaJsonWebKey rsaJsonWebKey
* @return String
* @throws JoseException
*/
private String createSignedTokenFromClaims(JwtClaims claims, RsaJsonWebKey rsaJsonWebKey) throws JoseException {
// A JWT is a JWS and/or a JWE with JSON claims as the payload.
// In this example it is a JWS so we create a JsonWebSignature object.
JsonWebSignature jws = new JsonWebSignature();
// The payload of the JWS is JSON content of the JWT Claims
jws.setPayload(claims.toJson());
// The JWT is signed using the private key
jws.setKey(rsaJsonWebKey.getPrivateKey());
// Set the signature algorithm on the JWT/JWS that will integrity protect the claims
jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.RSA_USING_SHA256);
return jws.getCompactSerialization();
}
示例4: createUnsupportedSignedTokenFromClaims
import org.jose4j.jws.JsonWebSignature; //导入依赖的package包/类
/**
* Create a RSA384 signed token from given claims and RSA jwk.
*
* @param JwtClaims claims
* @param RsaJsonWebKey rsaJsonWebKey
* @return String
* @throws JoseException
*/
private String createUnsupportedSignedTokenFromClaims(JwtClaims claims, RsaJsonWebKey rsaJsonWebKey) throws JoseException {
// A JWT is a JWS and/or a JWE with JSON claims as the payload.
// In this example it is a JWS so we create a JsonWebSignature object.
JsonWebSignature jws = new JsonWebSignature();
// The payload of the JWS is JSON content of the JWT Claims
jws.setPayload(claims.toJson());
// The JWT is signed using the private key
jws.setKey(rsaJsonWebKey.getPrivateKey());
// Set the signature algorithm on the JWT/JWS that will integrity protect the claims
jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.RSA_USING_SHA384);
return jws.getCompactSerialization();
}
示例5: verifySignature
import org.jose4j.jws.JsonWebSignature; //导入依赖的package包/类
/**
* Verify signature.
*
* @param value the value
* @return the value associated with the signature, which may have to
* be decoded, or null.
*/
protected byte[] verifySignature(@NotNull final byte[] value) {
try {
final String asString = new String(value);
final JsonWebSignature jws = new JsonWebSignature();
jws.setCompactSerialization(asString);
jws.setKey(this.signingKey);
final boolean verified = jws.verifySignature();
if (verified) {
final String payload = jws.getPayload();
logger.debug("Successfully decoded value. Result in Base64-encoding is [{}]", payload);
return CompressionUtils.decodeBase64(payload);
}
return null;
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
示例6: verifySignature
import org.jose4j.jws.JsonWebSignature; //导入依赖的package包/类
/**
* Verify signature.
*
* @param value the value
* @return the value associated with the signature, which may have to
* be decoded, or null.
*/
private String verifySignature(@NotNull final String value) {
try {
final JsonWebSignature jws = new JsonWebSignature();
jws.setCompactSerialization(value);
jws.setKey(this.secretKeySigningKey);
final boolean verified = jws.verifySignature();
if (verified) {
logger.debug("Signature successfully verified. Payload is [{}]", jws.getPayload());
return jws.getPayload();
}
return null;
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
示例7: encode
import org.jose4j.jws.JsonWebSignature; //导入依赖的package包/类
/**
* Sign id token claim string.
*
* @param svc the service
* @param claims the claims
* @return the string
* @throws JoseException the jose exception
*/
public String encode(final OidcRegisteredService svc, final JwtClaims claims) throws JoseException {
try {
LOGGER.debug("Attempting to produce id token generated for service [{}]", svc);
final JsonWebSignature jws = new JsonWebSignature();
final String jsonClaims = claims.toJson();
jws.setPayload(jsonClaims);
LOGGER.debug("Generated claims to put into id token are [{}]", jsonClaims);
jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.NONE);
jws.setAlgorithmConstraints(AlgorithmConstraints.NO_CONSTRAINTS);
String innerJwt = svc.isSignIdToken() ? signIdToken(svc, jws) : jws.getCompactSerialization();
if (svc.isEncryptIdToken() && StringUtils.isNotBlank(svc.getIdTokenEncryptionAlg())
&& StringUtils.isNotBlank(svc.getIdTokenEncryptionEncoding())) {
innerJwt = encryptIdToken(svc, jws, innerJwt);
}
return innerJwt;
} catch (final Exception e) {
LOGGER.error(e.getMessage(), e);
throw Throwables.propagate(e);
}
}
示例8: encryptIdToken
import org.jose4j.jws.JsonWebSignature; //导入依赖的package包/类
private String encryptIdToken(final OidcRegisteredService svc, final JsonWebSignature jws, final String innerJwt) throws Exception {
LOGGER.debug("Service [{}] is set to encrypt id tokens", svc);
final JsonWebEncryption jwe = new JsonWebEncryption();
jwe.setAlgorithmHeaderValue(svc.getIdTokenEncryptionAlg());
jwe.setEncryptionMethodHeaderParameter(svc.getIdTokenEncryptionEncoding());
final Optional<RsaJsonWebKey> jwks = this.serviceJsonWebKeystoreCache.get(svc);
if (!jwks.isPresent()) {
throw new IllegalArgumentException("Service " + svc.getServiceId()
+ " with client id " + svc.getClientId()
+ " is configured to encrypt id tokens, yet no JSON web key is available");
}
final RsaJsonWebKey jsonWebKey = jwks.get();
LOGGER.debug("Found JSON web key to encrypt the id token: [{}]", jsonWebKey);
if (jsonWebKey.getPublicKey() == null) {
throw new IllegalArgumentException("JSON web key used to sign the id token has no associated public key");
}
jwe.setKey(jsonWebKey.getPublicKey());
jwe.setKeyIdHeaderValue(jws.getKeyIdHeaderValue());
jwe.setContentTypeHeaderValue("JWT");
jwe.setPayload(innerJwt);
return jwe.getCompactSerialization();
}
示例9: prepareJsonWebSignatureForIdTokenSigning
import org.jose4j.jws.JsonWebSignature; //导入依赖的package包/类
private void prepareJsonWebSignatureForIdTokenSigning(final OidcRegisteredService svc, final JsonWebSignature jws,
final RsaJsonWebKey jsonWebKey) {
LOGGER.debug("Service [{}] is set to sign id tokens", svc);
jws.setKey(jsonWebKey.getPrivateKey());
jws.setAlgorithmConstraints(AlgorithmConstraints.DISALLOW_NONE);
if (StringUtils.isBlank(jsonWebKey.getKeyId())) {
jws.setKeyIdHeaderValue(UUID.randomUUID().toString());
} else {
jws.setKeyIdHeaderValue(jsonWebKey.getKeyId());
}
LOGGER.debug("Signing id token with key id header value [{}]", jws.getKeyIdHeaderValue());
jws.setAlgorithmHeaderValue(getJsonWebKeySigningAlgorithm());
LOGGER.debug("Signing id token with algorithm [{}]", jws.getAlgorithmHeaderValue());
}
示例10: sign
import org.jose4j.jws.JsonWebSignature; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public String sign(final JwtClaims claims) {
try {
final RsaJsonWebKey aSigningKey = cachedDataProvider.getASigningKey();
final JsonWebSignature jws = new JsonWebSignature();
jws.setPayload(claims.toJson());
jws.setKeyIdHeaderValue(aSigningKey.getKeyId());
jws.setKey(aSigningKey.getPrivateKey());
jws.setAlgorithmHeaderValue(aSigningKey.getAlgorithm());
jws.sign();
return jws.getCompactSerialization();
} catch (final JoseException e) {
throw new InternalServerErrorException(e);
}
}
示例11: generateToken
import org.jose4j.jws.JsonWebSignature; //导入依赖的package包/类
public String generateToken(String subject) {
final JwtClaims claims = new JwtClaims();
claims.setSubject(subject);
claims.setExpirationTimeMinutesInTheFuture(TOKEN_EXPIRATION_IN_MINUTES);
final JsonWebSignature jws = new JsonWebSignature();
jws.setPayload(claims.toJson());
jws.setAlgorithmHeaderValue(HMAC_SHA256);
jws.setKey(new HmacKey(tokenSecret));
jws.setDoKeyValidation(false); //relaxes hmac key length restrictions
try {
return jws.getCompactSerialization();
} catch (JoseException e) {
throw new RuntimeException(e);
}
}
示例12: verify
import org.jose4j.jws.JsonWebSignature; //导入依赖的package包/类
@Override
public boolean verify(String authToken, String issuer) {
Preconditions.checkNotNull(authToken);
Preconditions.checkNotNull(issuer);
try {
JsonWebKeySet jwks = this.jwksSupplier.supply(issuer);
JsonWebSignature jws = new JsonWebSignature();
jws.setCompactSerialization(authToken);
for (JsonWebKey jwk : this.jwkSelector.selectList(jws, jwks.getJsonWebKeys())) {
jws.setKey(jwk.getKey());
if (jws.verifySignature()) {
return true;
}
}
} catch (JoseException exception) {
throw new UnauthenticatedException("Cannot verify the signature", exception);
}
return false;
}
示例13: generateJwt
import org.jose4j.jws.JsonWebSignature; //导入依赖的package包/类
private static String generateJwt(RsaJsonWebKey jwk, Optional<String> keyId)
throws JoseException {
JwtClaims claims = new JwtClaims();
claims.setIssuer("Issuer");
claims.setAudience("Audience");
JsonWebSignature jws = new JsonWebSignature();
jws.setPayload(claims.toJson());
jws.setKey(jwk.getPrivateKey());
jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.RSA_USING_SHA256);
if (keyId.isPresent()) {
jws.setKeyIdHeaderValue(keyId.get());
}
return jws.getCompactSerialization();
}
示例14: createToken
import org.jose4j.jws.JsonWebSignature; //导入依赖的package包/类
private static String createToken(Key key, JsonObject jsonClaims) {
JwtClaims claims = new JwtClaims();
claims.setSubject(jsonClaims.toString());
claims.setIssuedAtToNow();
claims.setExpirationTime(NumericDate.fromSeconds(NumericDate.now().getValue() + JWT_TOKEN_EXPIRES_TIME));
JsonWebSignature jws = new JsonWebSignature();
jws.setDoKeyValidation(false);
jws.setPayload(claims.toJson());
jws.setKey(key);
jws.setAlgorithmHeaderValue(ALG);
try {
return jws.getCompactSerialization();
} catch (JoseException ex) {
LOGGER.log(Level.SEVERE, null, ex);
}
return null;
}
示例15: verifySignature
import org.jose4j.jws.JsonWebSignature; //导入依赖的package包/类
/**
* Verify signature.
*
* @param value the value
* @return the value associated with the signature, which may have to
* be decoded, or null.
*/
protected byte[] verifySignature(@NotNull final byte[] value) {
try {
final String asString = new String(value);
final JsonWebSignature jws = new JsonWebSignature();
jws.setCompactSerialization(asString);
jws.setKey(this.signingKey);
final boolean verified = jws.verifySignature();
if (verified) {
final String payload = jws.getPayload();
logger.debug("Successfully decoded value. Result in Base64-encoding is [{}]", payload);
return CompressionUtils.decodeBase64ToByteArray(payload);
}
return null;
} catch (final Exception e) {
throw new RuntimeException(e);
}
}