本文整理匯總了Java中org.jose4j.jwt.JwtClaims.parse方法的典型用法代碼示例。如果您正苦於以下問題:Java JwtClaims.parse方法的具體用法?Java JwtClaims.parse怎麽用?Java JwtClaims.parse使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.jose4j.jwt.JwtClaims
的用法示例。
在下文中一共展示了JwtClaims.parse方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: init
import org.jose4j.jwt.JwtClaims; //導入方法依賴的package包/類
private void init(MessageContext msgContext, ExecutionContext execContext) throws Exception {
this.msgContext = msgContext;
this.execContext = execContext;
this.jws = "true".equals(this.resolveVariable(this.properties.get("jws")));
this.jwsAlgo = this.resolveVariable(this.properties.get("jws-algo"));
this.jwsKey = this.resolveVariable(this.properties.get("jws-key"));
this.jwsKeyPassword = this.resolveVariable(this.properties.get("jws-key-pass"));
this.jwe = "true".equals(this.resolveVariable(this.properties.get("jwe")));
this.jweKey = this.resolveVariable(this.properties.get("jwe-key"));
this.jweKeyPassword = this.resolveVariable(this.properties.get("jwe-key-pass"));
this.jweKeyAlgo = this.resolveVariable(this.properties.get("jwe-key-algo"));
this.jweAlgo = this.resolveVariable(this.properties.get("jwe-algo"));
this.claims = JwtClaims.parse(this.resolveVariable(this.properties.get("claims-json")));
this.issuer = this.resolveVariable(this.properties.get("iss"));
this.audience = this.resolveVariable(this.properties.get("aud"));
this.expiry = this.resolveVariable(this.properties.get("expiry"));
this.debugMessage("Properties", this.printMap(this.properties));
this.debugMessage("PropertiesValue", this.toString());
}
示例2: verifyJWE
import org.jose4j.jwt.JwtClaims; //導入方法依賴的package包/類
private void verifyJWE(Key key, String keyAlgo, String jweAlgo, String jwt) {
try {
JsonWebEncryption jwe = new JsonWebEncryption();
jwe.setAlgorithmConstraints(
new AlgorithmConstraints(
ConstraintType.WHITELIST,
ALGORITHMS.get(keyAlgo)));
jwe.setContentEncryptionAlgorithmConstraints(
new AlgorithmConstraints(
ConstraintType.WHITELIST,
ALGORITHMS.get(jweAlgo)));
jwe.setCompactSerialization(jwt);
jwe.setKey(key);
JwtClaims claims = JwtClaims.parse(jwe.getPlaintextString());
assertEquals("abc xyz", claims.getSubject());
assertEquals("[email protected]", claims.getClaimValue("email"));
} catch (Exception e) {
fail();
}
}
示例3: someBasicSubChecks
import org.jose4j.jwt.JwtClaims; //導入方法依賴的package包/類
@Test
public void someBasicSubChecks() throws InvalidJwtException
{
JwtClaims jwtClaims = JwtClaims.parse("{\"sub\":\"brian.d.campbell\"}");
JwtConsumer jwtConsumer = new JwtConsumerBuilder().build();
SimpleJwtConsumerTestHelp.goodValidate(jwtClaims, jwtConsumer);
jwtConsumer = new JwtConsumerBuilder().setRequireSubject().build();
SimpleJwtConsumerTestHelp.goodValidate(jwtClaims, jwtConsumer);
jwtClaims = JwtClaims.parse("{\"name\":\"brian.d.campbell\"}");
SimpleJwtConsumerTestHelp.expectValidationFailure(jwtClaims, jwtConsumer);
jwtConsumer = new JwtConsumerBuilder().build();
SimpleJwtConsumerTestHelp.goodValidate(jwtClaims, jwtConsumer);
jwtClaims = JwtClaims.parse("{\"sub\":724729}");
jwtConsumer = new JwtConsumerBuilder().setRequireSubject().build();
SimpleJwtConsumerTestHelp.expectValidationFailure(jwtClaims, jwtConsumer);
jwtClaims = JwtClaims.parse("{\"sub\":{\"values\":[\"one\", \"2\"]}}");
jwtConsumer = new JwtConsumerBuilder().build();
SimpleJwtConsumerTestHelp.expectValidationFailure(jwtClaims, jwtConsumer);
}
示例4: someBasicJtiChecks
import org.jose4j.jwt.JwtClaims; //導入方法依賴的package包/類
@Test
public void someBasicJtiChecks() throws InvalidJwtException
{
JwtClaims jwtClaims = JwtClaims.parse("{\"jti\":\"1Y5iLSQfNgcSGt0A4is29\"}");
JwtConsumer jwtConsumer = new JwtConsumerBuilder().build();
SimpleJwtConsumerTestHelp.goodValidate(jwtClaims, jwtConsumer);
jwtConsumer = new JwtConsumerBuilder().setRequireJwtId().build();
SimpleJwtConsumerTestHelp.goodValidate(jwtClaims, jwtConsumer);
jwtClaims = JwtClaims.parse("{\"notjti\":\"lbZ_mLS6w3xBSlvW6ULmkV-uLCk\"}");
SimpleJwtConsumerTestHelp.expectValidationFailure(jwtClaims, jwtConsumer);
jwtConsumer = new JwtConsumerBuilder().build();
SimpleJwtConsumerTestHelp.goodValidate(jwtClaims, jwtConsumer);
jwtClaims = JwtClaims.parse("{\"jti\":55581529751992}");
jwtConsumer = new JwtConsumerBuilder().setRequireJwtId().build();
SimpleJwtConsumerTestHelp.expectValidationFailure(jwtClaims, jwtConsumer);
jwtClaims = JwtClaims.parse("{\"jti\":[\"S0w3XbslvW6ULmk0\", \"5iLSQfNgcSGt7A4is\"]}");
jwtConsumer = new JwtConsumerBuilder().build();
SimpleJwtConsumerTestHelp.expectValidationFailure(jwtClaims, jwtConsumer);
}
示例5: parseToken
import org.jose4j.jwt.JwtClaims; //導入方法依賴的package包/類
@Override
public String parseToken(final String token) {
try {
final String json = this.cipherExecutor.decode(token);
final JwtClaims claims = JwtClaims.parse(json);
if (!claims.getIssuer().equals(issuer)) {
LOGGER.error("Token issuer does not match CAS");
return null;
}
if (claims.getAudience().isEmpty() || !claims.getAudience().get(0).equals(issuer)) {
LOGGER.error("Token audience does not match CAS");
return null;
}
if (StringUtils.isBlank(claims.getSubject())) {
LOGGER.error("Token has no subject identifier");
return null;
}
final ClientInfo holder = ClientInfoHolder.getClientInfo();
if (!claims.getStringClaimValue("origin").equals(holder.getServerIpAddress())) {
LOGGER.error("Token origin does not match CAS");
return null;
}
if (!claims.getStringClaimValue("client").equals(holder.getClientIpAddress())) {
LOGGER.error("Token client does not match CAS");
return null;
}
if (claims.getExpirationTime().isBefore(NumericDate.now())) {
LOGGER.error("Token has expired.");
return null;
}
return claims.getSubject();
} catch (final Exception e) {
LOGGER.error(e.getMessage(), e);
}
return null;
}
示例6: getAdditionalClaims
import org.jose4j.jwt.JwtClaims; //導入方法依賴的package包/類
public JwtClaims getAdditionalClaims() {
try {
return JwtClaims.parse(additionalClaimsJson);
} catch (final InvalidJwtException e) {
throw new InternalServerErrorException(e);
}
}
示例7: testInterceptor
import org.jose4j.jwt.JwtClaims; //導入方法依賴的package包/類
@SuppressWarnings({
"unchecked",
"rawtypes"
})
@Test
public void testInterceptor() throws Exception {
jwtInterceptor.setClaimsProcessor(new ValidatingProcessor("typ", "https://example.com/register"));
final JwtClaims jwtClaims = JwtClaims.parse("{\"typ\":\"https://example.com/register\", \"aud\":\"sample\", \"jti\": \"abc\", \"iss\":\"http://accounts.trajano.net\"}");
final String jwt = cryptoOps.sign(jwtClaims);
System.out.println(jwt);
final ResourceInfo resourceInfo = Mockito.mock(ResourceInfo.class);
Mockito.when(resourceInfo.getResourceMethod()).thenReturn(Hello.class.getMethod("hello2B"));
Mockito.when(resourceInfo.getResourceClass()).thenReturn((Class) Hello.class);
jwtInterceptor.setResourceInfo(resourceInfo);
final ContainerRequestContext containerRequestContext = Mockito.mock(ContainerRequestContext.class);
final UriInfo uriInfo = Mockito.mock(UriInfo.class);
Mockito.when(uriInfo.getRequestUri()).thenReturn(URI.create("http://trajano.net/sample"));
Mockito.when(containerRequestContext.getUriInfo()).thenReturn(uriInfo);
Mockito.when(containerRequestContext.getHeaderString("X-JWT-Assertion")).thenReturn(jwt);
Mockito.when(containerRequestContext.getHeaderString("X-JWT-Audience")).thenReturn("sample");
mdcInterceptor.filter(containerRequestContext);
jwtInterceptor.filter(containerRequestContext);
// final Request request = mock(Request.class);
// when(request.getHeader("X-JWT-Assertion")).thenReturn(jwt);
// final Response responder = mock(Response.class);
// final ServiceMethodInfo serviceMethodInfo = mock(ServiceMethodInfo.class);
// assertTrue(interceptor.preCall(request, responder, serviceMethodInfo));
}
示例8: varifyClaims
import org.jose4j.jwt.JwtClaims; //導入方法依賴的package包/類
private void varifyClaims(String claimsJSON) {
try {
JwtClaims claims = JwtClaims.parse(claimsJSON);
assertEquals("abc xyz", claims.getSubject());
assertEquals("[email protected]", claims.getClaimValue("email"));
} catch (Exception e) {
fail();
}
}
示例9: verifyJWS
import org.jose4j.jwt.JwtClaims; //導入方法依賴的package包/類
private void verifyJWS(Key key, String jwt) {
try {
JsonWebSignature jws = new JsonWebSignature();
jws.setKey(key);
jws.setCompactSerialization(jwt);
jws.setDoKeyValidation(false);
assertTrue(jws.verifySignature());
JwtClaims claims = JwtClaims.parse(jws.getPayload());
assertEquals("abc xyz", claims.getSubject());
assertEquals("[email protected]", claims.getClaimValue("email"));
} catch (Exception e) {
fail();
}
}
示例10: someBasicIssChecks
import org.jose4j.jwt.JwtClaims; //導入方法依賴的package包/類
@Test
public void someBasicIssChecks() throws InvalidJwtException
{
JwtClaims jwtClaims = JwtClaims.parse("{\"iss\":\"issuer.example.com\"}");
JwtConsumer jwtConsumer = new JwtConsumerBuilder().build();
SimpleJwtConsumerTestHelp.goodValidate(jwtClaims, jwtConsumer);
jwtConsumer = new JwtConsumerBuilder().setExpectedIssuer(null).build();
SimpleJwtConsumerTestHelp.goodValidate(jwtClaims, jwtConsumer);
jwtConsumer = new JwtConsumerBuilder().setExpectedIssuer(false, null).build();
SimpleJwtConsumerTestHelp.goodValidate(jwtClaims, jwtConsumer);
jwtConsumer = new JwtConsumerBuilder().setExpectedIssuer("issuer.example.com").build();
SimpleJwtConsumerTestHelp.goodValidate(jwtClaims, jwtConsumer);
jwtConsumer = new JwtConsumerBuilder().setExpectedIssuer(false, "issuer.example.com").build();
SimpleJwtConsumerTestHelp.goodValidate(jwtClaims, jwtConsumer);
jwtConsumer = new JwtConsumerBuilder().setExpectedIssuer("nope.example.com").build();
SimpleJwtConsumerTestHelp.expectValidationFailure(jwtClaims, jwtConsumer);
jwtClaims = JwtClaims.parse("{\"sub\":\"subject\"}");
jwtConsumer = new JwtConsumerBuilder().setExpectedIssuer("issuer.example.com").build();
SimpleJwtConsumerTestHelp.expectValidationFailure(jwtClaims, jwtConsumer);
jwtConsumer = new JwtConsumerBuilder().setExpectedIssuer(false, "issuer.example.com").build();
SimpleJwtConsumerTestHelp.goodValidate(jwtClaims, jwtConsumer);
jwtConsumer = new JwtConsumerBuilder().setExpectedIssuer(false, null).build();
SimpleJwtConsumerTestHelp.goodValidate(jwtClaims, jwtConsumer);
jwtClaims = JwtClaims.parse("{\"iss\":[\"issuer1\", \"other.one\", \"meh\"]}");
jwtConsumer = new JwtConsumerBuilder().setExpectedIssuer("issuer.example.com").build();
SimpleJwtConsumerTestHelp.expectValidationFailure(jwtClaims, jwtConsumer);
jwtClaims = JwtClaims.parse("{\"iss\":[\"issuer1\", \"nope.not\"]}");
jwtConsumer = new JwtConsumerBuilder().build();
SimpleJwtConsumerTestHelp.expectValidationFailure(jwtClaims, jwtConsumer);
}
示例11: someBasicChecks
import org.jose4j.jwt.JwtClaims; //導入方法依賴的package包/類
@Test
public void someBasicChecks() throws InvalidJwtException
{
JwtClaims jcs = JwtClaims.parse("{\"sub\":\"subject\", \"iss\":\"issuer\", \"aud\":\"audience\"}");
JwtConsumer consumer = new JwtConsumerBuilder().setExpectedAudience("audience").setExpectedIssuer("issuer").build();
SimpleJwtConsumerTestHelp.goodValidate(jcs, consumer);
consumer = new JwtConsumerBuilder()
.setExpectedAudience("nope")
.setExpectedIssuer("no way")
.setRequireSubject()
.setRequireJwtId()
.build();
SimpleJwtConsumerTestHelp.expectValidationFailure(jcs, consumer);
}
示例12: validateTamperedAuthToken
import org.jose4j.jwt.JwtClaims; //導入方法依賴的package包/類
/**
* Validation of an auth token that has been tampered with (modify the
* claims part of the token) should fail.
*/
@Test
public void validateTamperedAuthToken() throws Exception {
DateTime expirationTime = UtcTime.now().plusMinutes(10);
String legitToken = signToken(TOKEN_ISSUER, signatureKeyPair, expirationTime);
// modify the claims part in an attempt to try and reuse a token but
// issue it for a different client subject
LOG.debug("legitimate token: {}", legitToken);
// try to modify the signature part of the token
// <B64-encoded header>.<B64-encoded claims>.<B64-encoded signature>
String[] parts = legitToken.split("\\.");
String claims = new String(BaseEncoding.base64().decode(parts[1]), Charsets.UTF_8);
JwtClaims legitClaims = JwtClaims.parse(claims);
LOG.debug("legit claims: {}", legitClaims);
legitClaims.setSubject("[email protected]");
LOG.debug("tampered claims: {}", legitClaims);
parts[1] = BaseEncoding.base64().encode(legitClaims.toJson().getBytes());
String tamperedToken = Joiner.on(".").join(parts);
LOG.debug("tampered token: {}", tamperedToken);
String authzHeader = "Bearer " + tamperedToken;
try {
validator.validate(authzHeader);
fail("validation should have failed");
} catch (AuthTokenValidationException e) {
assertValidationException(e, "failed to validate Authorization token", "signature is invalid");
}
}
示例13: accessProtectedResourceWithTamperedAuthToken
import org.jose4j.jwt.JwtClaims; //導入方法依賴的package包/類
/**
* Verify that it isn't possible to access a protected resource with an auth
* token that has been tampered with (modify the claims part of the token).
*/
@Test
public void accessProtectedResourceWithTamperedAuthToken() throws Exception {
startServer(new AsymmetricKeyAuthTokenValidator(this.signatureKeyPair).withExpectedIssuer(TOKEN_ISSUER));
DateTime expirationTime = UtcTime.now().plusMinutes(10);
String legitToken = signToken(TOKEN_ISSUER, this.signatureKeyPair, expirationTime);
// modify the claims part in an attempt to try and reuse a token but
// issue it for a different client subject
LOG.debug("legitimate token: {}", legitToken);
// try to modify the signature part of the token
// <B64-encoded header>.<B64-encoded claims>.<B64-encoded signature>
String[] parts = legitToken.split("\\.");
String claims = new String(BaseEncoding.base64().decode(parts[1]), Charsets.UTF_8);
JwtClaims legitClaims = JwtClaims.parse(claims);
LOG.debug("legit claims: {}", legitClaims);
legitClaims.setSubject("[email protected]");
LOG.debug("tampered claims: {}", legitClaims);
parts[1] = BaseEncoding.base64().encode(legitClaims.toJson().getBytes());
String tamperedToken = Joiner.on(".").join(parts);
LOG.debug("tampered token: {}", tamperedToken);
Response response = getWithToken("/api/protected", tamperedToken);
assertThat(response.getStatus(), is(Status.UNAUTHORIZED.getStatusCode()));
assertThat(response.readEntity(ErrorType.class), is(error("failed to validate Authorization token")));
// verify that error header is present
assertThat(response.getHeaderString("WWW-Authenticate"), is(notNullValue()));
}
示例14: parseToken
import org.jose4j.jwt.JwtClaims; //導入方法依賴的package包/類
@Nullable
public static User parseToken(@NotNull JsonWebEncryption jwe, @NotNull String token, int tokenEnsureTime) {
try {
jwe.setCompactSerialization(token);
final JwtClaims claims = JwtClaims.parse(jwe.getPayload());
final NumericDate now = NumericDate.now();
final NumericDate expire = NumericDate.fromMilliseconds(now.getValueInMillis());
if (tokenEnsureTime > 0) {
expire.addSeconds(tokenEnsureTime);
}
if (claims.getExpirationTime() == null || claims.getExpirationTime().isBefore(expire)) {
return null;
}
if (claims.getNotBefore() == null || claims.getNotBefore().isAfter(now)) {
return null;
}
if (claims.getSubject() == null) {
return User.getAnonymous();
}
return User.create(
claims.getSubject(),
claims.getClaimValue("name", String.class),
claims.getClaimValue("email", String.class),
claims.getClaimValue("external", String.class)
);
} catch (JoseException | MalformedClaimException | InvalidJwtException e) {
log.warn("Token parsing error: " + e.getMessage());
return null;
}
}
示例15: someBasicAudChecks
import org.jose4j.jwt.JwtClaims; //導入方法依賴的package包/類
@Test
public void someBasicAudChecks() throws InvalidJwtException
{
JwtClaims jwtClaims = JwtClaims.parse("{\"aud\":\"example.com\"}");
JwtConsumer jwtConsumer = new JwtConsumerBuilder().build();
SimpleJwtConsumerTestHelp.expectValidationFailure(jwtClaims, jwtConsumer);
jwtConsumer = new JwtConsumerBuilder().setExpectedAudience("example.com").build();
SimpleJwtConsumerTestHelp.goodValidate(jwtClaims, jwtConsumer);
jwtConsumer = new JwtConsumerBuilder().setExpectedAudience("example.org", "example.com", "k8HiI26Y7").build();
SimpleJwtConsumerTestHelp.goodValidate(jwtClaims, jwtConsumer);
jwtConsumer = new JwtConsumerBuilder().setExpectedAudience("example.org").build();
SimpleJwtConsumerTestHelp.expectValidationFailure(jwtClaims, jwtConsumer);
jwtConsumer = new JwtConsumerBuilder().setExpectedAudience("example.org", "nope", "nada").build();
SimpleJwtConsumerTestHelp.expectValidationFailure(jwtClaims, jwtConsumer);
jwtClaims = JwtClaims.parse("{\"sub\":\"subject\"}");
jwtConsumer = new JwtConsumerBuilder().setExpectedAudience(false, "example.org", "www.example.org").build();
SimpleJwtConsumerTestHelp.goodValidate(jwtClaims, jwtConsumer);
jwtConsumer = new JwtConsumerBuilder().setExpectedAudience(true, "example.org", "www.example.org").build();
SimpleJwtConsumerTestHelp.expectValidationFailure(jwtClaims, jwtConsumer);
jwtConsumer = new JwtConsumerBuilder().setExpectedAudience("example.org").build();
SimpleJwtConsumerTestHelp.expectValidationFailure(jwtClaims, jwtConsumer);
jwtClaims = JwtClaims.parse("{\"aud\":[\"example.com\", \"usa.org\", \"ca.ca\"]}");
jwtConsumer = new JwtConsumerBuilder().setExpectedAudience("example.org").build();
SimpleJwtConsumerTestHelp.expectValidationFailure(jwtClaims, jwtConsumer);
jwtConsumer = new JwtConsumerBuilder().setExpectedAudience("example.org", "some.other.junk").build();
SimpleJwtConsumerTestHelp.expectValidationFailure(jwtClaims, jwtConsumer);
jwtConsumer = new JwtConsumerBuilder().setExpectedAudience("usa.org").build();
SimpleJwtConsumerTestHelp.goodValidate(jwtClaims, jwtConsumer);
jwtConsumer = new JwtConsumerBuilder().setExpectedAudience("ca.ca").build();
SimpleJwtConsumerTestHelp.goodValidate(jwtClaims, jwtConsumer);
jwtConsumer = new JwtConsumerBuilder().setExpectedAudience("ca.ca", "some.other.thing").build();
SimpleJwtConsumerTestHelp.goodValidate(jwtClaims, jwtConsumer);
jwtConsumer = new JwtConsumerBuilder().setExpectedAudience("noway", "ca.ca", "some.other.thing").build();
SimpleJwtConsumerTestHelp.goodValidate(jwtClaims, jwtConsumer);
jwtConsumer = new JwtConsumerBuilder().setExpectedAudience("usa.org", "ca.ca", "random").build();
SimpleJwtConsumerTestHelp.goodValidate(jwtClaims, jwtConsumer);
jwtConsumer = new JwtConsumerBuilder().setExpectedAudience("usa.org", "ca.ca").build();
SimpleJwtConsumerTestHelp.goodValidate(jwtClaims, jwtConsumer);
jwtConsumer = new JwtConsumerBuilder().setExpectedAudience("usa.org", "ca.ca", "example.com").build();
SimpleJwtConsumerTestHelp.goodValidate(jwtClaims, jwtConsumer);
jwtClaims = JwtClaims.parse("{\"aud\":[\"example.com\", 47, false]}");
jwtConsumer = new JwtConsumerBuilder().setExpectedAudience("example.org").build();
SimpleJwtConsumerTestHelp.expectValidationFailure(jwtClaims, jwtConsumer);
jwtClaims = JwtClaims.parse("{\"aud\":20475}");
jwtConsumer = new JwtConsumerBuilder().setExpectedAudience("example.org").build();
SimpleJwtConsumerTestHelp.expectValidationFailure(jwtClaims, jwtConsumer);
jwtClaims = JwtClaims.parse("{\"aud\":{\"aud\":\"example.org\"}}");
jwtConsumer = new JwtConsumerBuilder().setExpectedAudience("example.org").build();
SimpleJwtConsumerTestHelp.expectValidationFailure(jwtClaims, jwtConsumer);
}