本文整理汇总了Java中com.nimbusds.jose.crypto.MACSigner类的典型用法代码示例。如果您正苦于以下问题:Java MACSigner类的具体用法?Java MACSigner怎么用?Java MACSigner使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MACSigner类属于com.nimbusds.jose.crypto包,在下文中一共展示了MACSigner类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: determineOptimalAlgorithm
import com.nimbusds.jose.crypto.MACSigner; //导入依赖的package包/类
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: obtainJWSSigner
import com.nimbusds.jose.crypto.MACSigner; //导入依赖的package包/类
/**
* obtainJWSSigner
* Obtain JWT Signer based upon our SecretKey
*
* @param secretKey Reference
* @return JWSSigner Signer to be used for Signing and Validating Signatures.
*/
private JWSSigner obtainJWSSigner(SecretKey secretKey) {
if (secretKey == null) {
LOGGER.error("{}Unable to use specified Secret Key, very Bad!", LOGGING_HEADER);
return null;
}
try {
/**
* Create HMMAC Signer.
*/
return new MACSigner(secretKey.getEncoded());
} catch (KeyLengthException kle) {
LOGGER.error("{}Signing Secret Key Failed with Key Length Exception, very Bad, {}",
LOGGING_HEADER, kle.getMessage());
return null;
}
}
示例3: setUp
import com.nimbusds.jose.crypto.MACSigner; //导入依赖的package包/类
@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);
}
示例4: sign
import com.nimbusds.jose.crypto.MACSigner; //导入依赖的package包/类
@Override
public String sign(String payload) {
JWSHeader header = new JWSHeader(JWSAlgorithm.HS256);
Payload payloadObj = new Payload(payload);
JWSObject jwsObject = new JWSObject(header, payloadObj);
try {
if (signer == null) {
this.signer = new MACSigner(secret);
}
jwsObject.sign(signer);
return jwsObject.serialize();
} catch (JOSEException e) {
Logger.error("TokenSerializerImpl", e);
}
return "";
}
示例5: sign
import com.nimbusds.jose.crypto.MACSigner; //导入依赖的package包/类
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);
}
}
示例6: createToken
import com.nimbusds.jose.crypto.MACSigner; //导入依赖的package包/类
public static String createToken( String headerJson, String claimJson, String sharedKey )
{
try
{
JWSHeader header = JWSHeader.parse( headerJson );
JWSSigner signer = new MACSigner( sharedKey.getBytes() );
JWTClaimsSet claimsSet = JWTClaimsSet.parse( claimJson );
SignedJWT signedJWT = new SignedJWT( header, claimsSet );
signedJWT.sign( signer );
return signedJWT.serialize();
}
catch ( Exception e )
{
LOG.error( "Error creating token", e.getMessage() );
return "";
}
}
示例7: createToken
import com.nimbusds.jose.crypto.MACSigner; //导入依赖的package包/类
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;
}
}
示例8: validToken
import com.nimbusds.jose.crypto.MACSigner; //导入依赖的package包/类
@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));
}
示例9: invalidTokenNotBeforeTime
import com.nimbusds.jose.crypto.MACSigner; //导入依赖的package包/类
@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));
}
示例10: invalidTokenExpirationTime
import com.nimbusds.jose.crypto.MACSigner; //导入依赖的package包/类
@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));
}
示例11: generateJWT
import com.nimbusds.jose.crypto.MACSigner; //导入依赖的package包/类
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();
}
示例12: getJwt
import com.nimbusds.jose.crypto.MACSigner; //导入依赖的package包/类
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;
}
示例13: generateJWToken
import com.nimbusds.jose.crypto.MACSigner; //导入依赖的package包/类
/**
* 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;
}
示例14: getSigner
import com.nimbusds.jose.crypto.MACSigner; //导入依赖的package包/类
/**
* Returns correct implementation of signer based on algorithm type.
*
* @param jwsAlgorithm
* JWS algorithm
* @return signer for algorithm and private key
* @throws JOSEException
* if algorithm cannot be supported
*/
private JWSSigner getSigner(Algorithm jwsAlgorithm) throws JOSEException {
if (JWSAlgorithm.Family.EC.contains(jwsAlgorithm)) {
return new ECDSASigner((ECPrivateKey) credential.getPrivateKey());
}
if (JWSAlgorithm.Family.RSA.contains(jwsAlgorithm)) {
return new RSASSASigner(credential.getPrivateKey());
}
if (JWSAlgorithm.Family.HMAC_SHA.contains(jwsAlgorithm)) {
return new MACSigner(credential.getSecretKey());
}
throw new JOSEException("Unsupported algorithm " + jwsAlgorithm.getName());
}
示例15: testHS256
import com.nimbusds.jose.crypto.MACSigner; //导入依赖的package包/类
@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);
}