Java Code Examples for io.jsonwebtoken.security.Keys#secretKeyFor()

The following examples show how to use io.jsonwebtoken.security.Keys#secretKeyFor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LTI13JJWTTest.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
@Test
public void testOne() throws NoSuchAlgorithmException, NoSuchProviderException {
	Key key = Keys.secretKeyFor(SignatureAlgorithm.HS256);

	String jws = Jwts.builder().setSubject("Joe").signWith(key).compact();
	assertEquals(83, jws.length());
	Matcher m = base64url_pattern.matcher(jws);
	boolean good = m.find();
	if (!good) {
		System.out.println("Bad JWS:\n" + jws);
	}
	assertTrue(good);

	String subject = Jwts.parser().setAllowedClockSkewSeconds(60).setSigningKey(key).parseClaimsJws(jws).getBody().getSubject();
	assertEquals("Joe", subject);
}
 
Example 2
Source File: LTI13JJWTTest.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
@Test
public void testOne() throws NoSuchAlgorithmException, NoSuchProviderException {
	Key key = Keys.secretKeyFor(SignatureAlgorithm.HS256);

	String jws = Jwts.builder().setSubject("Joe").signWith(key).compact();
	assertEquals(83, jws.length());
	Matcher m = base64url_pattern.matcher(jws);
	boolean good = m.find();
	if (!good) {
		System.out.println("Bad JWS:\n" + jws);
	}
	assertTrue(good);

	String subject = Jwts.parser().setAllowedClockSkewSeconds(60).setSigningKey(key).parseClaimsJws(jws).getBody().getSubject();
	assertEquals("Joe", subject);
}
 
Example 3
Source File: OpenCPSKeyGenerator.java    From opencps-v2 with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Key generateKey() {
	if (_key != null) {
		return _key;   		
	}
	_key = Keys.secretKeyFor(SignatureAlgorithm.HS512);
	
	return _key;
}
 
Example 4
Source File: LTI13JJWTTest.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Test
public void testThree() throws NoSuchAlgorithmException, NoSuchProviderException {
	Key key = Keys.secretKeyFor(SignatureAlgorithm.HS256);
	String jws = Jwts.builder()
			.setIssuer("me").setSubject("Bob").setAudience("you")
			.signWith(key).compact();

	String x = getBody(jws, key);
	assertEquals("{iss=me, sub=Bob, aud=you}", x);
}
 
Example 5
Source File: LTI13JJWTTest.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Test
public void testFour() throws NoSuchAlgorithmException, NoSuchProviderException {
	String nonce = UUID.randomUUID().toString();
	String client_id = "12345";
	String subject = "Bob";
	String locale = Locale.getDefault().getLanguage().replace('_', '-');

	Matcher m = uuid_pattern.matcher(nonce);
	boolean good = m.find();
	if (!good) {
		System.out.println("Bad UUID:\n" + nonce);
	}
	assertTrue(good);

	Date now = new Date();
	Key key = Keys.secretKeyFor(SignatureAlgorithm.HS256);
	String jws = Jwts.builder()
			.setIssuer(LTI_13_ISSUER)
			.setSubject(subject)
			.setAudience(client_id)
			.setIssuedAt(now)
			.setExpiration(new Date(now.getTime() + 600000L)) // Milliseconds
			.claim(LTI13ConstantsUtil.KEY_NONCE, nonce)
			.claim(LTI13ConstantsUtil.KEY_LOCALE, locale)
			.signWith(key)
			.compact();

	String body = getBody(jws, key);
	good = body.contains(LTI_13_ISSUER);
	if (!good) {
		System.out.println("Bad body: " + body);
	}
	assertTrue(good);
}
 
Example 6
Source File: LTI13JJWTTest.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Test
public void testThree() throws NoSuchAlgorithmException, NoSuchProviderException {
	Key key = Keys.secretKeyFor(SignatureAlgorithm.HS256);
	String jws = Jwts.builder()
			.setIssuer("me").setSubject("Bob").setAudience("you")
			.signWith(key).compact();

	String x = getBody(jws, key);
	assertEquals("{iss=me, sub=Bob, aud=you}", x);
}
 
Example 7
Source File: LTI13JJWTTest.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Test
public void testFour() throws NoSuchAlgorithmException, NoSuchProviderException {
	String nonce = UUID.randomUUID().toString();
	String client_id = "12345";
	String subject = "Bob";
	String locale = Locale.getDefault().getLanguage().replace('_', '-');

	Matcher m = uuid_pattern.matcher(nonce);
	boolean good = m.find();
	if (!good) {
		System.out.println("Bad UUID:\n" + nonce);
	}
	assertTrue(good);

	Date now = new Date();
	Key key = Keys.secretKeyFor(SignatureAlgorithm.HS256);
	String jws = Jwts.builder()
			.setIssuer(LTI_13_ISSUER)
			.setSubject(subject)
			.setAudience(client_id)
			.setIssuedAt(now)
			.setExpiration(new Date(now.getTime() + 600000L)) // Milliseconds
			.claim(LTI13ConstantsUtil.KEY_NONCE, nonce)
			.claim(LTI13ConstantsUtil.KEY_LOCALE, locale)
			.signWith(key)
			.compact();

	String body = getBody(jws, key);
	good = body.contains(LTI_13_ISSUER);
	if (!good) {
		System.out.println("Bad body: " + body);
	}
	assertTrue(good);
}
 
Example 8
Source File: AuthTokenUtils.java    From pulsar with Apache License 2.0 4 votes vote down vote up
public static SecretKey createSecretKey(SignatureAlgorithm signatureAlgorithm) {
    return Keys.secretKeyFor(signatureAlgorithm);
}
 
Example 9
Source File: TokenUtil.java    From JwtPermission with Apache License 2.0 2 votes vote down vote up
/**
 * 生成密钥Key
 *
 * @return Key
 */
public static Key genKey() {
    return Keys.secretKeyFor(SignatureAlgorithm.HS256);
}
 
Example 10
Source File: JwtHelper.java    From kisso with Apache License 2.0 2 votes vote down vote up
/**
 * 获取对应签名算法的字符串密钥
 *
 * @param signatureAlgorithm 签名算法
 * @return
 */
public static String getSecretKey(SignatureAlgorithm signatureAlgorithm) {
    Key key = Keys.secretKeyFor(signatureAlgorithm);
    return Base64.getEncoder().encodeToString(key.getEncoded());
}