本文整理匯總了Java中com.nimbusds.jose.JWSAlgorithm.HS256屬性的典型用法代碼示例。如果您正苦於以下問題:Java JWSAlgorithm.HS256屬性的具體用法?Java JWSAlgorithm.HS256怎麽用?Java JWSAlgorithm.HS256使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類com.nimbusds.jose.JWSAlgorithm
的用法示例。
在下文中一共展示了JWSAlgorithm.HS256屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: determineOptimalAlgorithm
public JWSAlgorithm determineOptimalAlgorithm(byte[] secret) {
JWSAlgorithm result = null;
Set<JWSAlgorithm> algorithms = MACSigner.getCompatibleAlgorithms(ByteUtils.bitLength(secret));
if (algorithms.contains(JWSAlgorithm.HS512)) {
result = JWSAlgorithm.HS512;
}
if (result == null && algorithms.contains(JWSAlgorithm.HS384)) {
result = JWSAlgorithm.HS384;
}
if (result == null && algorithms.contains(JWSAlgorithm.HS256)) {
result = JWSAlgorithm.HS256;
}
if (result == null) {
throw new ConfigurationException("Secret is too short for any JWS HMAC algorithm.");
}
return result;
}
示例2: setUp
@Before
public void setUp() throws Exception {
secretKey = TOKEN.getBytes();
sessionIdentifier = spy(new SessionIdentifier(TOKEN, secretKey, authenticationData));
signer = new MACSigner(secretKey);
whenNew(MACSigner.class).withArguments(secretKey).thenReturn(signer);
JWTClaimsSet.Builder builder = new JWTClaimsSet.Builder();
builder.issuer(USER);
builder.claim("mode", MODE);
JWTClaimsSet buildClaim = builder.build();
when(authenticationData.buildClaimSet()).thenReturn(buildClaim);
jwsHeader = new JWSHeader(JWSAlgorithm.HS256);
signedJWT = spy(new SignedJWT(jwsHeader, buildClaim));
whenNew(SignedJWT.class).withAnyArguments().thenReturn(signedJWT);
}
示例3: sign
public String sign(String algorithm, String kid, String keyStr, String dataToSign) {
try {
Key key = getKey(algorithm, keyStr);
JWSHeader.Builder jwsBuilder = new JWSHeader.Builder("HS256".equals(algorithm) ? JWSAlgorithm.HS256 : JWSAlgorithm.RS256);
jwsBuilder.keyID(kid);
JWSHeader signingHeader = jwsBuilder.build();
JWSSigner signer = "HS256".equals(algorithm) ? new MACSigner(key.getEncoded()) : new RSASSASigner((RSAPrivateKey) key);
JWSObject jwsObject = new JWSObject(signingHeader, new Payload(dataToSign));
jwsObject.sign(signer);
checkObject(jwsObject);
String parts[] = jwsObject.serialize().split("\\.");
return "{\"protected\":\"" + parts[0] + "\", \"payload\":\"" + parts[1] + "\", \"signature\":\"" + parts[2] + "\"}";
} catch (Exception e) {
throw new CryptoException("Exception signing data: " + e.getMessage(), e);
}
}
示例4: mapSignatureAlgorithm
/**
* This method map signature algorithm define in identity.xml to nimbus
* signature algorithm
* format, Strings are defined inline hence there are not being used any
* where
*
* @param signatureAlgorithm
* @return
* @throws IdentityOAuth2Exception
*/
protected JWSAlgorithm mapSignatureAlgorithm(String signatureAlgorithm) throws IdentityOAuth2Exception {
if (NONE.equals(signatureAlgorithm)) {
return new JWSAlgorithm(JWSAlgorithm.NONE.getName());
} else if (SHA256_WITH_RSA.equals(signatureAlgorithm)) {
return JWSAlgorithm.RS256;
} else if (SHA384_WITH_RSA.equals(signatureAlgorithm)) {
return JWSAlgorithm.RS384;
} else if (SHA512_WITH_RSA.equals(signatureAlgorithm)) {
return JWSAlgorithm.RS512;
} else if (SHA256_WITH_HMAC.equals(signatureAlgorithm)) {
return JWSAlgorithm.HS256;
} else if (SHA384_WITH_HMAC.equals(signatureAlgorithm)) {
return JWSAlgorithm.HS384;
} else if (SHA512_WITH_HMAC.equals(signatureAlgorithm)) {
return JWSAlgorithm.HS512;
} else if (SHA256_WITH_EC.equals(signatureAlgorithm)) {
return JWSAlgorithm.ES256;
} else if (SHA384_WITH_EC.equals(signatureAlgorithm)) {
return JWSAlgorithm.ES384;
} else if (SHA512_WITH_EC.equals(signatureAlgorithm)) {
return JWSAlgorithm.ES512;
}
throw new IdentityOAuth2Exception("Unsupported Signature Algorithm in identity.xml");
}
示例5: createToken
default String createToken(Object userId) {
try {
JWTClaimsSet.Builder builder = new JWTClaimsSet.Builder();
builder.issuer(getIssuer());
builder.subject(userId.toString());
builder.issueTime(new Date());
builder.notBeforeTime(new Date());
builder.expirationTime(new Date(new Date().getTime() + getExpirationDate()));
builder.jwtID(UUID.randomUUID().toString());
JWTClaimsSet claimsSet = builder.build();
JWSHeader header = new JWSHeader(JWSAlgorithm.HS256);
Payload payload = new Payload(claimsSet.toJSONObject());
JWSObject jwsObject = new JWSObject(header, payload);
JWSSigner signer = new MACSigner(getSharedKey());
jwsObject.sign(signer);
return jwsObject.serialize();
} catch (JOSEException ex) {
return null;
}
}
示例6: validToken
@Test
public void validToken() throws JOSEException, ParseException {
JWTClaimsSet jwtClaims = getJWTClaimsSet("issuer", "subject", new Date(), new Date(), new Date(new Date().getTime() + 100000));
JWSHeader header = new JWSHeader(JWSAlgorithm.HS256);
Payload payload = new Payload(jwtClaims.toJSONObject());
JWSObject jwsObject = new JWSObject(header, payload);
JWSSigner signer = new MACSigner(sharedKey);
jwsObject.sign(signer);
String token = jwsObject.serialize();
SignedJWT signed = SignedJWT.parse(token);
JWSVerifier verifier = new MACVerifierExtended(sharedKey, signed.getJWTClaimsSet());
signed.verify(verifier);
Assert.assertTrue("Must be valid", signed.verify(verifier));
}
示例7: invalidTokenNotBeforeTime
@Test
public void invalidTokenNotBeforeTime() throws JOSEException, ParseException {
JWTClaimsSet jwtClaims = getJWTClaimsSet("issuer", "subject", new Date(), new Date(new Date().getTime() + 100000), new Date(new Date().getTime() + 200000));
JWSHeader header = new JWSHeader(JWSAlgorithm.HS256);
Payload payload = new Payload(jwtClaims.toJSONObject());
JWSObject jwsObject = new JWSObject(header, payload);
JWSSigner signer = new MACSigner(sharedKey);
jwsObject.sign(signer);
String token = jwsObject.serialize();
SignedJWT signed = SignedJWT.parse(token);
JWSVerifier verifier = new MACVerifierExtended(sharedKey, signed.getJWTClaimsSet());
signed.verify(verifier);
Assert.assertFalse("Must be invalid", signed.verify(verifier));
}
示例8: invalidTokenExpirationTime
@Test
public void invalidTokenExpirationTime() throws JOSEException, ParseException {
JWTClaimsSet jwtClaims = getJWTClaimsSet("issuer", "subject", new Date(), new Date(), new Date());
JWSHeader header = new JWSHeader(JWSAlgorithm.HS256);
Payload payload = new Payload(jwtClaims.toJSONObject());
JWSObject jwsObject = new JWSObject(header, payload);
JWSSigner signer = new MACSigner(sharedKey);
jwsObject.sign(signer);
String token = jwsObject.serialize();
SignedJWT signed = SignedJWT.parse(token);
JWSVerifier verifier = new MACVerifierExtended(sharedKey, signed.getJWTClaimsSet());
signed.verify(verifier);
Assert.assertFalse("Must be invalid", signed.verify(verifier));
}
示例9: generateJWT
private String generateJWT(final String username) throws JOSEException {
// Create HMAC signer
final JWSSigner signer = new MACSigner(secret);
// Prepare JWT with claims set
final JWTClaimsSet claimsSet = new JWTClaimsSet.Builder().subject(username).expirationTime(new Date(new Date().getTime() + 60 * 1000)).claim("http://localhost:8080/", true).build();
final SignedJWT signedJWT = new SignedJWT(new JWSHeader(JWSAlgorithm.HS256), claimsSet);
// Apply the HMAC protection
signedJWT.sign(signer);
// Serialize to compact form, produces something like
// eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE0NDMzODA1NDAsInN1YiI6ImNpZGlhbiIsImh0dHA6XC9cL2xvY2FsaG9zdDo4MDgwXC8iOnRydWV9.EkPxd0EfujgLrk35DX1XmvnmyJsFO8dqbnzsgg78coM
return signedJWT.serialize();
}
示例10: getJwt
private static String getJwt(String subject, String issuer, String secret,
int expiresInSeconds) throws JOSEException {
JWSSigner signer = new MACSigner(secret.getBytes());
JWTClaimsSet claimsSet = new JWTClaimsSet();
claimsSet.setSubjectClaim(subject);
claimsSet.setIssuedAtClaim(new Date().getTime());
claimsSet.setIssuerClaim(issuer);
claimsSet.setExpirationTimeClaim(new Date().getTime()
+ (expiresInSeconds * 1000));
SignedJWT signedJWT = new SignedJWT(new JWSHeader(JWSAlgorithm.HS256),
claimsSet);
signedJWT.sign(signer);
String jwt = signedJWT.serialize();
return jwt;
}
示例11: mapSignatureAlgorithm
protected JWSAlgorithm mapSignatureAlgorithm(String signatureAlgorithm)
throws IdentityOAuth2Exception {
if ("SHA256withRSA".equals(signatureAlgorithm)) {
return JWSAlgorithm.RS256;
} else if ("SHA384withRSA".equals(signatureAlgorithm)) {
return JWSAlgorithm.RS384;
} else if ("SHA512withRSA".equals(signatureAlgorithm)) {
return JWSAlgorithm.RS512;
} else if ("SHA256withHMAC".equals(signatureAlgorithm)) {
return JWSAlgorithm.HS256;
} else if ("SHA384withHMAC".equals(signatureAlgorithm)) {
return JWSAlgorithm.HS384;
} else if ("SHA512withHMAC".equals(signatureAlgorithm)) {
return JWSAlgorithm.HS512;
} else if ("SHA256withEC".equals(signatureAlgorithm)) {
return JWSAlgorithm.ES256;
} else if ("SHA384withEC".equals(signatureAlgorithm)) {
return JWSAlgorithm.ES384;
} else if ("SHA512withEC".equals(signatureAlgorithm)) {
return JWSAlgorithm.ES512;
}
log.error("Unsupported Signature Algorithm in identity.xml");
throw new IdentityOAuth2Exception("Unsupported Signature Algorithm in identity.xml");
}
示例12: generateJWToken
/**
* Generates a new JWT token.
* @param user a User object belonging to the app
* @param app the app object
* @return a new JWT or null
*/
public static SignedJWT generateJWToken(User user, App app) {
if (app != null) {
try {
Date now = new Date();
JWTClaimsSet.Builder claimsSet = new JWTClaimsSet.Builder();
String userSecret = "";
claimsSet.issueTime(now);
claimsSet.expirationTime(new Date(now.getTime() + (app.getTokenValiditySec() * 1000)));
claimsSet.notBeforeTime(now);
claimsSet.claim("refresh", getNextRefresh(app.getTokenValiditySec()));
claimsSet.claim(Config._APPID, app.getId());
if (user != null) {
claimsSet.subject(user.getId());
userSecret = user.getTokenSecret();
}
JWSSigner signer = new MACSigner(app.getSecret() + userSecret);
SignedJWT signedJWT = new SignedJWT(new JWSHeader(JWSAlgorithm.HS256), claimsSet.build());
signedJWT.sign(signer);
return signedJWT;
} catch (JOSEException e) {
logger.warn("Unable to sign JWT: {}.", e.getMessage());
}
}
return null;
}
示例13: testHS256
@Test(groups = TCKConstants.TEST_GROUP_DEBUG,
description = "Validate how to use the HS256 signature alg")
public void testHS256() throws Exception {
JWTClaimsSet claimsSet = JWTClaimsSet.parse("{\"sub\":\"jdoe\"}");
SignedJWT signedJWT = new SignedJWT(new JWSHeader(JWSAlgorithm.HS256), claimsSet);
SecureRandom random = new SecureRandom();
BigInteger secret = BigInteger.probablePrime(256, random);
JWSSigner signer = new MACSigner(secret.toByteArray());
signedJWT.sign(signer);
}
示例14: generateToken
@Override
public String generateToken(AuthenticationData authenticationData, byte[] secretKey) throws TokenException {
try {
JWSSigner signer = new MACSigner(secretKey);
SignedJWT signedJWT = new SignedJWT(new JWSHeader(JWSAlgorithm.HS256),
authenticationData.buildClaimSet());
signedJWT.sign(signer);
return signedJWT.serialize();
} catch (JOSEException e) {
throw new TokenException(e);
}
}
示例15: generateToken
@Override
public String generateToken(AuthenticationDto authenticationDto, byte[] secretKey) throws JOSEException {
JWSSigner signer = new MACSigner(secretKey);
SignedJWT signedJWT = new SignedJWT(new JWSHeader(JWSAlgorithm.HS256), generateClaimsSet(authenticationDto));
signedJWT.sign(signer);
return signedJWT.serialize();
}