本文整理匯總了Java中org.jose4j.jwt.JwtClaims.setAudience方法的典型用法代碼示例。如果您正苦於以下問題:Java JwtClaims.setAudience方法的具體用法?Java JwtClaims.setAudience怎麽用?Java JwtClaims.setAudience使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.jose4j.jwt.JwtClaims
的用法示例。
在下文中一共展示了JwtClaims.setAudience方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: generateJWTAssertion
import org.jose4j.jwt.JwtClaims; //導入方法依賴的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;
}
}
示例2: createDefaultClaims
import org.jose4j.jwt.JwtClaims; //導入方法依賴的package包/類
/**
* Create a JwtClaims object with prefilled sane defaults.
* @return JwtClaims
*/
private JwtClaims createDefaultClaims() {
JwtClaims claims = new JwtClaims();
claims.setIssuer(SSODataTest.DATA_ISSUER); // who creates the token and signs it
claims.setAudience(SSODataTest.DATA_AUDIENCE); // to whom the token is intended to be sent
claims.setExpirationTimeMinutesInTheFuture(10); // time when the token will expire (10 minutes from now)
claims.setGeneratedJwtId(); // a unique identifier for the token
claims.setIssuedAtToNow(); // when the token was issued/created (now)
claims.setNotBeforeMinutesInThePast(2); // time before which the token is not yet valid (2 minutes ago)
claims.setSubject(SSODataTest.DATA_USER_ID); // the subject/principal is whom the token is about
claims.setClaim(SSOData.KEY_INSTANCE_ID, SSODataTest.DATA_INSTANCE_ID); // additional claims/attributes about the subject can be added
return claims;
}
示例3: createToken
import org.jose4j.jwt.JwtClaims; //導入方法依賴的package包/類
@Override
public String createToken(final String to) {
try {
final String token = UUID.randomUUID().toString();
final JwtClaims claims = new JwtClaims();
claims.setJwtId(token);
claims.setIssuer(issuer);
claims.setAudience(issuer);
claims.setExpirationTimeMinutesInTheFuture(passwordManagementProperties.getReset().getExpirationMinutes());
claims.setIssuedAtToNow();
final ClientInfo holder = ClientInfoHolder.getClientInfo();
claims.setStringClaim("origin", holder.getServerIpAddress());
claims.setStringClaim("client", holder.getClientIpAddress());
claims.setSubject(to);
final String json = claims.toJson();
return this.cipherExecutor.encode(json);
} catch (final Exception e) {
LOGGER.error(e.getMessage(), e);
}
return null;
}
示例4: createJwtClaims
import org.jose4j.jwt.JwtClaims; //導入方法依賴的package包/類
private static JwtClaims createJwtClaims(
List<String> audiences,
String email,
int expiration,
String subject,
int notBefore,
String issuer) {
JwtClaims jwtClaims = new JwtClaims();
jwtClaims.setAudience(audiences);
jwtClaims.setExpirationTimeMinutesInTheFuture(expiration);
jwtClaims.setClaim("email", email);
jwtClaims.setSubject(subject);
jwtClaims.setNotBeforeMinutesInThePast(notBefore);
jwtClaims.setIssuer(issuer);
return jwtClaims;
}
示例5: generateJwt
import org.jose4j.jwt.JwtClaims; //導入方法依賴的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();
}
示例6: getDefaultJwtClaims
import org.jose4j.jwt.JwtClaims; //導入方法依賴的package包/類
/**
* Construct a default JwtClaims
*
* @return JwtClaims
*/
public static JwtClaims getDefaultJwtClaims() {
JwtConfig config = (JwtConfig) Config.getInstance().getJsonObjectConfig(JWT_CONFIG, JwtConfig.class);
JwtClaims claims = new JwtClaims();
claims.setIssuer(config.getIssuer());
claims.setAudience(config.getAudience());
claims.setExpirationTimeMinutesInTheFuture(config.getExpiredInMinutes());
claims.setGeneratedJwtId(); // a unique identifier for the token
claims.setIssuedAtToNow(); // when the token was issued/created (now)
claims.setNotBeforeMinutesInThePast(2); // time before which the token is not yet valid (2 minutes ago)
claims.setClaim("version", config.getVersion());
return claims;
}
示例7: getTestClaims
import org.jose4j.jwt.JwtClaims; //導入方法依賴的package包/類
private static JwtClaims getTestClaims() {
JwtClaims claims = new JwtClaims();
claims.setIssuer("urn:com:networknt:oauth2:v1");
claims.setAudience("urn:com.networknt");
claims.setExpirationTimeMinutesInTheFuture(10);
claims.setGeneratedJwtId(); // a unique identifier for the token
claims.setIssuedAtToNow(); // when the token was issued/created (now)
claims.setNotBeforeMinutesInThePast(2); // time before which the token is not yet valid (2 minutes ago)
claims.setClaim("version", "1.0");
claims.setClaim("user_id", "steve");
claims.setClaim("user_type", "EMPLOYEE");
claims.setClaim("client_id", "aaaaaaaa-1234-1234-1234-bbbbbbbb");
List<String> scope = Arrays.asList("api.r", "api.w");
claims.setStringListClaim("scope", scope); // multi-valued claims work too and will end up as a JSON array
return claims;
}
示例8: testSign
import org.jose4j.jwt.JwtClaims; //導入方法依賴的package包/類
@Test
public void testSign() throws Exception {
// Base64 string server public/private key
String vapidPublicKey = "BOH8nTQA5iZhl23+NCzGG9prvOZ5BE0MJXBW+GUkQIvRVTVB32JxmX0V1j6z0r7rnT7+bgi6f2g5fMPpAh5brqM=";
String vapidPrivateKey = "TRlY/7yQzvqcLpgHQTxiU5fVzAAvAw/cdSh5kLFLNqg=";
JwtClaims claims = new JwtClaims();
claims.setAudience("https://developer.services.mozilla.com/a476b8ea-c4b8-4359-832a-e2747b6ab88a");
JsonWebSignature jws = new JsonWebSignature();
jws.setPayload(claims.toJson());
jws.setKey(Utils.loadPrivateKey(vapidPrivateKey));
jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.ECDSA_USING_P256_CURVE_AND_SHA256);
System.out.println(jws.getCompactSerialization());
}
示例9: getJwtClaims
import org.jose4j.jwt.JwtClaims; //導入方法依賴的package包/類
private JwtClaims getJwtClaims() {
JwtClaims claims = new JwtClaims();
claims.setIssuer("Issuer"); // who creates the token and signs it
claims.setAudience("Audience"); // to whom the token is intended to be sent
claims.setExpirationTimeMinutesInTheFuture(10); // time when the token will expire (10 minutes from now)
claims.setGeneratedJwtId(); // a unique identifier for the token
claims.setIssuedAtToNow(); // when the token was issued/created (now)
claims.setNotBeforeMinutesInThePast(2); // time before which the token is not yet valid (2 minutes ago)
claims.setSubject("subject"); // the subject/principal is whom the token is about
claims.setClaim("email", "[email protected]"); // additional claims/attributes about the subject can be added
return claims;
}
示例10: json
import org.jose4j.jwt.JwtClaims; //導入方法依賴的package包/類
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiResponses(@ApiResponse(code = 401,
message = "Unauthorized Response",
response = ErrorResponse.class))
public Response json(final UsernamePassword usernamePassword,
@HeaderParam(HttpHeaders.AUTHORIZATION) final String authorization) {
if (!"password".equals(usernamePassword.getPassword())) {
throw ErrorResponses.unauthorized(ErrorCodes.UNAUTHORIZED_CLIENT, "invalid username/password combination", "FORM");
}
final JwtClaims claims = new JwtClaims();
claims.setSubject(usernamePassword.getUsername());
claims.setAudience(HttpAuthorizationHeaders.parseBasicAuthorization(authorization)[0]);
final Form form = new Form();
form.param("grant_type", GrantTypes.JWT_ASSERTION);
form.param("assertion", cryptoOps.sign(claims));
return Response.ok(client.target(authorizationEndpoint).request(MediaType.APPLICATION_JSON_TYPE)
.header(HttpHeaders.AUTHORIZATION, authorization)
.post(Entity.form(form), OAuthTokenResponse.class))
.build();
}
示例11: testBadCrypto
import org.jose4j.jwt.JwtClaims; //導入方法依賴的package包/類
@Test(expected = InternalServerErrorException.class)
public void testBadCrypto() throws Exception {
final JwtClaims claims = new JwtClaims();
claims.setAudience("mememe");
cachedDataProvider.getKeySet();
final HttpsJwks jwks = mock(HttpsJwks.class);
cryptoOps.toClaimsSet("XXXXXX", "mememe", jwks);
}
示例12: getSamplePayload
import org.jose4j.jwt.JwtClaims; //導入方法依賴的package包/類
private static String getSamplePayload() {
JwtClaims claims = new JwtClaims();
claims.setIssuer("issue-idp-1");
claims.setAudience("aud-1", "aud-2");
claims.setExpirationTimeMinutesInTheFuture(299);
claims.setGeneratedJwtId();
claims.setIssuedAtToNow();
claims.setNotBeforeMinutesInThePast(2);
claims.setSubject("johndoe");
claims.setClaim("email","[email protected]");
return claims.toJson();
}
示例13: generateAuthToken
import org.jose4j.jwt.JwtClaims; //導入方法依賴的package包/類
/**
* Generate an auth token with the given claims and sign the token with the
* private key in the provided {@link RsaJsonWebKey}.
*/
public static String generateAuthToken(
Optional<Collection<String>> audiences,
Optional<String> email,
NumericDate expirationTime,
Optional<String> issuer,
NumericDate notBefore,
Optional<String> subject,
RsaJsonWebKey rsaJsonWebKey) {
JwtClaims claims = new JwtClaims();
if (audiences.isPresent()) {
claims.setAudience(ImmutableList.copyOf(audiences.get()));
}
if (email.isPresent()) {
claims.setClaim("email", email.get());
}
if (issuer.isPresent()) {
claims.setIssuer(issuer.get());
}
if (subject.isPresent()) {
claims.setSubject(subject.get());
}
claims.setExpirationTime(expirationTime);
claims.setNotBefore(notBefore);
JsonWebSignature jsonWebSignature = new JsonWebSignature();
jsonWebSignature.setPayload(claims.toJson());
jsonWebSignature.setKey(rsaJsonWebKey.getPrivateKey());
jsonWebSignature.setKeyIdHeaderValue(rsaJsonWebKey.getKeyId());
jsonWebSignature.setAlgorithmHeaderValue(rsaJsonWebKey.getAlgorithm());
try {
return jsonWebSignature.getCompactSerialization();
} catch (JoseException exception) {
throw new RuntimeException("failed to generate JWT", exception);
}
}
示例14: rsaPublicKeyEncodingDecodingAndSign
import org.jose4j.jwt.JwtClaims; //導入方法依賴的package包/類
@Test
public void rsaPublicKeyEncodingDecodingAndSign() throws Exception
{
PublicJsonWebKey publicJsonWebKey = ExampleRsaJwksFromJwe.APPENDIX_A_1;
String pem = KeyPairUtil.pemEncode(publicJsonWebKey.getPublicKey());
String expectedPem = "-----BEGIN PUBLIC KEY-----\r\n" +
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoahUIoWw0K0usKNuOR6H\r\n" +
"4wkf4oBUXHTxRvgb48E+BVvxkeDNjbC4he8rUWcJoZmds2h7M70imEVhRU5djINX\r\n" +
"tqllXI4DFqcI1DgjT9LewND8MW2Krf3Spsk/ZkoFnilakGygTwpZ3uesH+PFABNI\r\n" +
"UYpOiN15dsQRkgr0vEhxN92i2asbOenSZeyaxziK72UwxrrKoExv6kc5twXTq4h+\r\n" +
"QChLOln0/mtUZwfsRaMStPs6mS6XrgxnxbWhojf663tuEQueGC+FCMfra36C9knD\r\n" +
"FGzKsNa7LZK2djYgyD3JR/MB/4NUJW/TqOQtwHYbxevoJArm+L5StowjzGy+/bq6\r\n" +
"GwIDAQAB\r\n" +
"-----END PUBLIC KEY-----";
Assert.assertThat(pem, equalTo(expectedPem));
RsaKeyUtil rsaKeyUtil = new RsaKeyUtil();
PublicKey publicKey = rsaKeyUtil.fromPemEncoded(pem);
Assert.assertThat(publicKey, equalTo(publicJsonWebKey.getPublicKey()));
JwtClaims claims = new JwtClaims();
claims.setSubject("meh");
claims.setExpirationTimeMinutesInTheFuture(20);
claims.setGeneratedJwtId();
claims.setAudience("you");
claims.setIssuer("me");
JsonWebSignature jws = new JsonWebSignature();
jws.setPayload(claims.toJson());
jws.setKey(publicJsonWebKey.getPrivateKey());
jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.RSA_USING_SHA256);
String jwt = jws.getCompactSerialization();
Logger log = LoggerFactory.getLogger(this.getClass());
log.debug("The following JWT and public key should be (and were on 11/11/15) usable and produce a valid " +
"result at jwt.io (related to http://stackoverflow.com/questions/32744172):\n" + jwt + "\n" + pem);
}
示例15: payload2token
import org.jose4j.jwt.JwtClaims; //導入方法依賴的package包/類
public String payload2token(String subject, Object payload, long minutes) throws JoseException {
JwtClaims claims = new JwtClaims();
claims.setIssuer(TOKEN_ISSUER);
claims.setAudience(TOKEN_AUDIENCE);
claims.setExpirationTimeMinutesInTheFuture(minutes);
claims.setGeneratedJwtId();
claims.setIssuedAtToNow();
claims.setNotBeforeMinutesInThePast(2);
claims.setSubject(subject);
claims.setStringClaim(CLAIM_KEY, jsonHelper.object2json(payload));
JsonWebSignature jws = new JsonWebSignature();
jws.setPayload(claims.toJson());
jws.setKey(getKey());
jws.setKeyIdHeaderValue(KEY_ID);
jws.setAlgorithmHeaderValue(getAlgorithm());
String token = jws.getCompactSerialization();
if (!tokenService.contains(token)) {
tokenService.store(token, null); //todo
}
return token;
}