use of com.auth0.android.jwt.JWT in project balcaovirtual by trf2-jus-br.
the class AutenticarPost method verify.
public static Map<String, Object> verify(String jwt) throws SwaggerAuthorizationException {
final JWTVerifier verifier = new JWTVerifier(Utils.getJwtPassword());
Map<String, Object> map;
try {
map = verifier.verify(jwt);
} catch (InvalidKeyException | NoSuchAlgorithmException | IllegalStateException | SignatureException | IOException | JWTVerifyException e) {
throw new SwaggerAuthorizationException(e);
}
return map;
}
use of com.auth0.android.jwt.JWT in project iet-hf-2022-k-k-k-k-k-k by BME-MIT-IET.
the class TokenController method post.
@ApiOperation("AuthChecking")
@PostMapping("/hasRightForPage")
public ResponseEntity<PageAuthorizationResponse> post(@RequestHeader HttpHeaders headers, @RequestBody PageAuthorizationRequest body) {
String authHeader = headers.getFirst(HttpHeaders.AUTHORIZATION);
String route = body.getRoute();
if (PageAuthorizationChecker.noRightPages().contains(route)) {
return new ResponseEntity<>(PageAuthorizationResponse.hasRight, HttpStatus.OK);
}
if (authHeader == null) {
return new ResponseEntity<>(PageAuthorizationResponse.tokenExpired, HttpStatus.OK);
} else if (authHeader.startsWith("Bearer") && authHeader.length() < 7) {
return new ResponseEntity<>(PageAuthorizationResponse.tokenExpired, HttpStatus.OK);
}
try {
DecodedJWT jwt = JwtUtil.getDecodedJWT(authHeader);
if (jwt.getExpiresAt().before(new Date())) {
return new ResponseEntity<>(PageAuthorizationResponse.tokenExpired, HttpStatus.OK);
}
Collection<Role> rolesOfUser = this.personService.getByUsername(JwtUtil.getUsernameFromJwt(authHeader)).getRoles();
if (PageAuthorizationChecker.hasRightForPage(route, rolesOfUser)) {
return new ResponseEntity<>(PageAuthorizationResponse.hasRight, HttpStatus.OK);
} else {
return new ResponseEntity<>(PageAuthorizationResponse.noRight, HttpStatus.OK);
}
} catch (Exception e) {
return new ResponseEntity<>(PageAuthorizationResponse.tokenExpired, HttpStatus.OK);
}
}
use of com.auth0.android.jwt.JWT in project tanafaso-backend by tanafaso.
the class ApiAuthenticationController method validateAppleAuthCode.
private boolean validateAppleAuthCode(AppleAuthenticationRequest request) {
Map<String, Object> appleApiRequestHeader = new HashMap<>();
appleApiRequestHeader.put("alg", "ES256");
appleApiRequestHeader.put("kid", appleSignInKeyId);
appleApiRequestHeader.put("typ", "JWT");
InputStreamReader appleAuthPrivateKeyInputStreamReader;
try {
appleAuthPrivateKeyInputStreamReader = new InputStreamReader(new ClassPathResource(appleAuthPrivateKeyFile).getInputStream());
} catch (IOException e) {
logger.error("Couldn't read the apple authorization private key file.", e);
return false;
}
ECPrivateKey privateKey;
try {
PemObject pemObject;
pemObject = new PemReader(appleAuthPrivateKeyInputStreamReader).readPemObject();
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(pemObject.getContent());
KeyFactory factory;
factory = KeyFactory.getInstance("EC");
privateKey = (ECPrivateKey) factory.generatePrivate(spec);
} catch (Exception e) {
logger.error("Could not convert Apple private key into an EC key.", e);
return false;
}
String signedJwt = JWT.create().withHeader(appleApiRequestHeader).withIssuer(appleTeamId).withIssuedAt(new Date(System.currentTimeMillis())).withExpiresAt(new Date(System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(10))).withAudience("https://appleid.apple.com").withSubject("com.tanafaso.azkar").sign(Algorithm.ECDSA256(privateKey));
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.add("client_id", "com.tanafaso.azkar");
map.add("client_secret", signedJwt);
map.add("code", request.getAuthCode());
map.add("grant_type", "authorization_code");
HttpEntity<MultiValueMap<String, String>> appleApiRequestHttpEntity = new HttpEntity<>(map, headers);
logger.info("Sending to Apple auth code verification API.");
ResponseEntity<AppleIdToken> appleIdToken = restTemplate.postForEntity("https://appleid.apple.com/auth/token", appleApiRequestHttpEntity, AppleIdToken.class);
if (appleIdToken.getStatusCode() == HttpStatus.OK) {
DecodedJWT decodedJwt = JWT.decode(appleIdToken.getBody().getIdToken());
boolean emailIsVerified = decodedJwt.getClaim("email_verified").asString().equals("true");
String potentiallyVerifiedEmail = decodedJwt.getClaim("email").asString().toLowerCase();
if (emailIsVerified && potentiallyVerifiedEmail.equals(request.getEmail())) {
return true;
}
logger.info("Failed to verify user signing in with apple: email={}, firstName={}, " + "lastName={}, emailIsVerified={}, appleApiReturnedEmail={}", request.getEmail(), request.getFirstName(), request.getLastName(), emailIsVerified, potentiallyVerifiedEmail);
return false;
}
logger.info("Failed to verify user signing in with apple as apple API returned status code: " + "{} for email={}, firstName={}, lastName={}", appleIdToken.getStatusCode().toString(), request.getEmail(), request.getFirstName(), request.getLastName());
return false;
}
use of com.auth0.android.jwt.JWT in project brapi-Java-TestServer by plantbreeding.
the class BrapiTestServerJWTAuthFilter method validateOAuthToken.
private String validateOAuthToken(HttpServletRequest request) {
try {
String token = request.getHeader("Authorization");
if (token != null) {
token = token.replaceFirst("Bearer ", "");
RSAPublicKey pubKey = getPublicKey(oidcDiscoveryUrl);
Algorithm algorithm = Algorithm.RSA256(pubKey, null);
JWTVerifier verifier = JWT.require(algorithm).withIssuer("https://auth.brapi.org/auth/realms/brapi").build();
DecodedJWT jwt = verifier.verify(token);
return jwt.getClaim("email").asString();
}
return null;
} catch (Exception e) {
return null;
}
}
use of com.auth0.android.jwt.JWT in project vars-annotation by mbari-media-management.
the class BasicJWTAuthInterceptor method isExpired.
private boolean isExpired(Authorization a) {
try {
DecodedJWT jwt = JWT.decode(a.getAccessToken());
Instant iat = jwt.getExpiresAt().toInstant();
return iat.isBefore(Instant.now());
} catch (Exception e) {
return true;
}
}
Aggregations