本文整理汇总了Java中com.auth0.jwt.exceptions.JWTDecodeException类的典型用法代码示例。如果您正苦于以下问题:Java JWTDecodeException类的具体用法?Java JWTDecodeException怎么用?Java JWTDecodeException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JWTDecodeException类属于com.auth0.jwt.exceptions包,在下文中一共展示了JWTDecodeException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: recoverFrom
import com.auth0.jwt.exceptions.JWTDecodeException; //导入依赖的package包/类
@Override
public Optional<Person> recoverFrom(String token) {
try {
JWTVerifier verifier = JWT.require(getAlgorithm())
.withIssuer(config.getString(ServerVariable.JWT_ISSUER))
.build();
verifier.verify(token);
JWT decode = JWT.decode(token);
String email = decode.getClaim(EMAIL_CLAIM).asString();
return repository.findByEmail(email);
} catch (UnsupportedEncodingException | SignatureVerificationException | JWTDecodeException e) {
return Optional.empty();
}
}
示例2: idToken
import com.auth0.jwt.exceptions.JWTDecodeException; //导入依赖的package包/类
public IdToken idToken(String id_token) {
try {
DecodedJWT jwt = JWT.decode(id_token);
return new IdToken(
jwt.getClaim("iss").asString(),
jwt.getClaim("sub").asString(),
jwt.getClaim("aud").asString(),
jwt.getClaim("ext").asLong(),
jwt.getClaim("iat").asLong(),
jwt.getClaim("nonce").asString(),
jwt.getClaim("name").asString(),
jwt.getClaim("picture").asString());
} catch (JWTDecodeException e) {
throw new RuntimeException(e);
}
}
示例3: asMap
import com.auth0.jwt.exceptions.JWTDecodeException; //导入依赖的package包/类
@Override
public Map<String, Object> asMap() throws JWTDecodeException {
if (!data.isObject()) {
return null;
}
try {
TypeReference<Map<String, Object>> mapType = new TypeReference<Map<String, Object>>() {
};
ObjectMapper thisMapper = getObjectMapper();
JsonParser thisParser = thisMapper.treeAsTokens(data);
return thisParser.readValueAs(mapType);
} catch (IOException e) {
throw new JWTDecodeException("Couldn't map the Claim value to Map", e);
}
}
示例4: deserialize
import com.auth0.jwt.exceptions.JWTDecodeException; //导入依赖的package包/类
@Override
public Payload deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
Map<String, JsonNode> tree = p.getCodec().readValue(p, new TypeReference<Map<String, JsonNode>>() {
});
if (tree == null) {
throw new JWTDecodeException("Parsing the Payload's JSON resulted on a Null map");
}
List<String> issuer = getStringOrArray(tree, PublicClaims.ISSUER);
List<String> subject = getStringOrArray(tree, PublicClaims.SUBJECT);
List<String> audience = getStringOrArray(tree, PublicClaims.AUDIENCE);
Date expiresAt = getDateFromSeconds(tree, PublicClaims.EXPIRES_AT);
Date notBefore = getDateFromSeconds(tree, PublicClaims.NOT_BEFORE);
Date issuedAt = getDateFromSeconds(tree, PublicClaims.ISSUED_AT);
String jwtId = getString(tree, PublicClaims.JWT_ID);
return new PayloadImpl(issuer, subject, audience, expiresAt, notBefore, issuedAt, jwtId, tree);
}
示例5: getStringOrArray
import com.auth0.jwt.exceptions.JWTDecodeException; //导入依赖的package包/类
List<String> getStringOrArray(Map<String, JsonNode> tree, String claimName) throws JWTDecodeException {
JsonNode node = tree.get(claimName);
if (node == null || node.isNull() || !(node.isArray() || node.isTextual())) {
return null;
}
if (node.isTextual() && !node.asText().isEmpty()) {
return Collections.singletonList(node.asText());
}
ObjectMapper mapper = new ObjectMapper();
List<String> list = new ArrayList<>(node.size());
for (int i = 0; i < node.size(); i++) {
try {
list.add(mapper.treeToValue(node.get(i), String.class));
} catch (JsonProcessingException e) {
throw new JWTDecodeException("Couldn't map the Claim's array contents to String", e);
}
}
return list;
}
示例6: asArray
import com.auth0.jwt.exceptions.JWTDecodeException; //导入依赖的package包/类
@Override
@SuppressWarnings("unchecked")
public <T> T[] asArray(Class<T> tClazz) throws JWTDecodeException {
if (!data.isArray()) {
return null;
}
T[] arr = (T[]) Array.newInstance(tClazz, data.size());
for (int i = 0; i < data.size(); i++) {
try {
arr[i] = getObjectMapper().treeToValue(data.get(i), tClazz);
} catch (JsonProcessingException e) {
throw new JWTDecodeException("Couldn't map the Claim's array contents to " + tClazz.getSimpleName(), e);
}
}
return arr;
}
示例7: asList
import com.auth0.jwt.exceptions.JWTDecodeException; //导入依赖的package包/类
@Override
public <T> List<T> asList(Class<T> tClazz) throws JWTDecodeException {
if (!data.isArray()) {
return null;
}
List<T> list = new ArrayList<>();
for (int i = 0; i < data.size(); i++) {
try {
list.add(getObjectMapper().treeToValue(data.get(i), tClazz));
} catch (JsonProcessingException e) {
throw new JWTDecodeException("Couldn't map the Claim's array contents to " + tClazz.getSimpleName(), e);
}
}
return list;
}
示例8: deserialize
import com.auth0.jwt.exceptions.JWTDecodeException; //导入依赖的package包/类
@Override
public Payload deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
Map<String, JsonNode> tree = p.getCodec().readValue(p, new TypeReference<Map<String, JsonNode>>() {
});
if (tree == null) {
throw new JWTDecodeException("Parsing the Payload's JSON resulted on a Null map");
}
String issuer = getString(tree, PublicClaims.ISSUER);
String subject = getString(tree, PublicClaims.SUBJECT);
List<String> audience = getStringOrArray(tree, PublicClaims.AUDIENCE);
Date expiresAt = getDateFromSeconds(tree, PublicClaims.EXPIRES_AT);
Date notBefore = getDateFromSeconds(tree, PublicClaims.NOT_BEFORE);
Date issuedAt = getDateFromSeconds(tree, PublicClaims.ISSUED_AT);
String jwtId = getString(tree, PublicClaims.JWT_ID);
return new PayloadImpl(issuer, subject, audience, expiresAt, notBefore, issuedAt, jwtId, tree);
}
示例9: shouldThrowIfAnExtraordinaryExceptionHappensWhenParsingAsGenericMap
import com.auth0.jwt.exceptions.JWTDecodeException; //导入依赖的package包/类
@Test
public void shouldThrowIfAnExtraordinaryExceptionHappensWhenParsingAsGenericMap() throws Exception {
JsonNode value = mock(ObjectNode.class);
when(value.getNodeType()).thenReturn(JsonNodeType.OBJECT);
JsonNodeClaim claim = (JsonNodeClaim) claimFromNode(value);
JsonNodeClaim spiedClaim = spy(claim);
ObjectMapper mockedMapper = mock(ObjectMapper.class);
when(spiedClaim.getObjectMapper()).thenReturn(mockedMapper);
JsonParser mockedParser = mock(JsonParser.class);
when(mockedMapper.treeAsTokens(value)).thenReturn(mockedParser);
when(mockedParser.readValueAs(ArgumentMatchers.any(TypeReference.class))).thenThrow(IOException.class);
exception.expect(JWTDecodeException.class);
spiedClaim.asMap();
}
示例10: isValidBearerToken
import com.auth0.jwt.exceptions.JWTDecodeException; //导入依赖的package包/类
/**
* This method is used to validate the Bearer token. It validates the source and the expiration and if the token is about to expire in 30 seconds, set as invalid token
* @param accessToken
* @param profile
* @param clientId
* @return
* @throws IOException
*/
private static boolean isValidBearerToken(String accessToken, ServerProfile profile, String clientId) throws IOException{
boolean isValid = false;
try {
JWT jwt = JWT.decode(accessToken);
String jwtClientId = jwt.getClaim("client_id").asString();
String jwtEmailId = jwt.getClaim("email").asString();
long jwtExpiresAt = jwt.getExpiresAt().getTime()/1000;
long difference = jwtExpiresAt - (System.currentTimeMillis()/1000);
if(jwt!= null && jwtClientId!=null && jwtClientId.equals(clientId)
&& jwtEmailId!=null && jwtEmailId.equalsIgnoreCase(profile.getCredential_user())
&& profile.getTokenUrl().contains(jwt.getIssuer())
&& difference >= 30){
isValid = true;
}
} catch (JWTDecodeException exception){
throw new IOException(exception.getMessage());
}
return isValid;
}
示例11: loadFromSaaS
import com.auth0.jwt.exceptions.JWTDecodeException; //导入依赖的package包/类
public static GitAccount loadFromSaaS(String authHeader) {
String jwtToken = authHeader;
int idx = authHeader.indexOf(' ');
jwtToken = jwtToken.substring(idx + 1, jwtToken.length());
try {
JWT.decode(jwtToken);
} catch (JWTDecodeException e) {
throw new KeyCloakFailureException("KeyCloak returned an invalid token. Are you sure you are logged in?", e);
}
String authToken = TokenHelper.getMandatoryTokenFor(KeycloakEndpoint.GET_GITHUB_TOKEN, authHeader);
return new GitAccount(null, authToken, null, null);
}
示例12: create
import com.auth0.jwt.exceptions.JWTDecodeException; //导入依赖的package包/类
public static Verifier create(final String token) {
final Verifier verifier = new Verifier();
verifier.token = token;
try {
verifier.jwt = JWT.decode(token);
if (verifier.jwt.getClaim("uid").isNull()) {
return null;
}
if (verifier.jwt.getClaim("url").isNull()) {
return null;
}
if (verifier.jwt.getClaim("name").isNull()) {
return null;
}
if (verifier.jwt.getClaim("roles").isNull()) {
return null;
}
if (verifier.jwt.getExpiresAt() == null) {
return null;
}
if (verifier.jwt.getIssuedAt() == null) {
return null;
}
} catch (JWTDecodeException exception) {
log.error("Error decoding token: " + token, exception);
return null;
}
return verifier;
}
示例13: getUid
import com.auth0.jwt.exceptions.JWTDecodeException; //导入依赖的package包/类
public static int getUid(String token) {
try {
DecodedJWT decodedJWT = JWT.decode(token);
return decodedJWT.getClaim("uid").asInt();
} catch (JWTDecodeException e) {
return 0;
}
}
示例14: getUsername
import com.auth0.jwt.exceptions.JWTDecodeException; //导入依赖的package包/类
/**
* Extracts the username from the decoded JWT.
*/
@Override
public String getUsername(String sessionId) {
try {
DecodedJWT jwt = JWT.decode(sessionId);
Map<String, Claim> claims = jwt.getClaims(); //Key is the Claim name
Claim claim = claims.get("usr");
return claim.asString();
} catch (JWTDecodeException exception){
exception.printStackTrace();
return null;
}
}
示例15: getDatabase
import com.auth0.jwt.exceptions.JWTDecodeException; //导入依赖的package包/类
/**
* Extracts the Database from the decoded JWT.
*/
@Override
public String getDatabase(String sessionId) {
try {
DecodedJWT jwt = JWT.decode(sessionId);
Map<String, Claim> claims = jwt.getClaims(); //Key is the Claim name
Claim claim = claims.get("dbn");
return claim.asString();
} catch (JWTDecodeException exception){
System.err.println(exception);
return null;
}
}