use of com.auth0.android.jwt.JWT in project restheart by SoftInstigate.
the class JwtAuthenticationMechanism method init.
@InjectConfiguration
public void init(Map<String, Object> args) throws ConfigurationException {
// get configuration arguments
base64Encoded = argValue(args, "base64Encoded");
algorithm = argValue(args, "algorithm");
key = argValue(args, "key");
usernameClaim = argValue(args, "usernameClaim");
rolesClaim = argValue(args, "rolesClaim");
fixedRoles = argValue(args, "fixedRoles");
issuer = argValue(args, "issuer");
audience = argValue(args, "audience");
Algorithm _algorithm;
try {
_algorithm = getAlgorithm(algorithm, key);
} catch (CertificateException | UnsupportedEncodingException ex) {
throw new ConfigurationException("wrong JWT configuration, " + "cannot setup algorithm", ex);
}
Verification v = JWT.require(_algorithm);
if (audience != null) {
v.withAudience(audience);
}
if (issuer != null) {
v.withIssuer(issuer);
}
if (rolesClaim != null && fixedRoles != null) {
throw new ConfigurationException("wrong JWT configuration, " + "cannot set both 'rolesClaim' and 'fixedRoles'");
}
if (rolesClaim == null && fixedRoles == null) {
throw new ConfigurationException("wrong JWT configuration, " + "need to set either 'rolesClaim' or 'fixedRoles'");
}
this.jwtVerifier = v.build();
}
use of com.auth0.android.jwt.JWT in project bookmark by FleyX.
the class JwtUtil method encode.
/**
* Description: 生成一个jwt字符串
*
* @param map data携带数据
* @param secret 秘钥
* @param timeOut 超时时间(单位s)
* @return java.lang.String
* @author fanxb
* @date 2019/3/4 17:26
*/
public static String encode(Map<String, String> map, String secret, long timeOut) {
Algorithm algorithm = Algorithm.HMAC256(secret);
JWTCreator.Builder builder = JWT.create().withExpiresAt(new Date(System.currentTimeMillis() + timeOut));
// 设置负载
map.forEach(builder::withClaim);
return builder.sign(algorithm);
}
use of com.auth0.android.jwt.JWT in project ARLAS-server by gisaia.
the class AuthorizationFilter method filter.
@Override
public void filter(ContainerRequestContext ctx) {
Transaction transaction = ElasticApm.currentTransaction();
boolean isPublic = ctx.getUriInfo().getPath().concat(":").concat(ctx.getMethod()).matches(authConf.getPublicRegex());
String header = ctx.getHeaderString(HttpHeaders.AUTHORIZATION);
if (header == null || (header != null && !header.toLowerCase().startsWith("bearer "))) {
if (isPublic || ctx.getMethod() == "OPTIONS") {
return;
} else {
ctx.abortWith(Response.status(Response.Status.UNAUTHORIZED).build());
}
}
try {
// header presence and format already checked before in AuthenticationFilter
DecodedJWT jwt = jwtVerifier.verify(header.substring(7));
// remove it in case it's been set manually
ctx.getHeaders().remove(authConf.headerUser);
String userId = jwt.getSubject();
if (!StringUtil.isNullOrEmpty(userId)) {
ctx.getHeaders().putSingle(authConf.headerUser, userId);
transaction.setUser(userId, "", "");
}
// remove it in case it's been set manually
ctx.getHeaders().remove(authConf.headerGroup);
Claim jwtClaimRoles = jwt.getClaim(authConf.claimRoles);
if (!jwtClaimRoles.isNull()) {
List<String> groups = jwtClaimRoles.asList(String.class).stream().filter(r -> r.toLowerCase().startsWith("group")).collect(Collectors.toList());
ctx.setProperty("groups", groups);
ctx.getHeaders().put(authConf.headerGroup, groups);
}
Claim jwtClaimPermissions = jwt.getClaim(authConf.claimPermissions);
if (!jwtClaimPermissions.isNull()) {
ArlasClaims arlasClaims = new ArlasClaims(jwtClaimPermissions.asList(String.class));
ctx.setProperty("claims", arlasClaims);
if (arlasClaims.isAllowed(ctx.getMethod(), ctx.getUriInfo().getPath())) {
arlasClaims.injectHeaders(ctx.getHeaders(), transaction);
return;
}
}
if (isPublic) {
return;
} else {
ctx.abortWith(Response.status(Response.Status.FORBIDDEN).build());
}
} catch (JWTVerificationException e) {
LOGGER.warn("JWT verification failed.", e);
if (!isPublic) {
ctx.abortWith(Response.status(Response.Status.UNAUTHORIZED).build());
}
return;
}
ctx.abortWith(Response.status(Response.Status.FORBIDDEN).build());
}
use of com.auth0.android.jwt.JWT in project drug-formulary-ri by HL7-DaVinci.
the class IntrospectionEndpoint method handleIntrospection.
public static ResponseEntity<String> handleIntrospection(String token) {
JSONObject response = new JSONObject();
String baseUrl = AuthUtils.getFhirBaseUrl();
try {
Algorithm algorithm = Algorithm.RSA256(OauthEndpointController.getPublicKey(), null);
JWTVerifier verifier = JWT.require(algorithm).withIssuer(baseUrl).withAudience(baseUrl).build();
DecodedJWT jwt = verifier.verify(token);
response.put("active", true);
response.put("aud", jwt.getAudience().get(0));
response.put("iss", jwt.getIssuer());
// Display in sec not ms
response.put("exp", jwt.getExpiresAt().getTime() / 1000);
// Display in sec not ms
response.put("iat", jwt.getIssuedAt().getTime() / 1000);
response.put("patient_id", jwt.getClaim("patient_id").asString());
} catch (JWTVerificationException exception) {
response.put("active", false);
}
return new ResponseEntity<>(response.toString(), HttpStatus.OK);
}
use of com.auth0.android.jwt.JWT in project drug-formulary-ri by HL7-DaVinci.
the class PatientAuthorizationInterceptor method verify.
/**
* Helper method to verify and decode the access token
*
* @param token - the access token
* @param fhirBaseUrl - the base url of this FHIR server
* @return the base interface Patient ID datatype if the jwt token is verified
* and contains a patient ID in it claim, otherwise null.
* @throws SignatureVerificationException
* @throws TokenExpiredException
* @throws JWTVerificationException
*/
private IIdType verify(String token, String fhirBaseUrl) throws SignatureVerificationException, TokenExpiredException, JWTVerificationException {
Algorithm algorithm = Algorithm.RSA256(OauthEndpointController.getPublicKey(), null);
logger.fine("Verifying JWT token iss and aud is " + fhirBaseUrl);
JWTVerifier verifier = JWT.require(algorithm).withIssuer(fhirBaseUrl).withAudience(fhirBaseUrl).build();
DecodedJWT jwt = verifier.verify(token);
String patientId = jwt.getClaim("patient_id").asString();
if (patientId != null)
return new IdType("Patient", patientId);
return null;
}
Aggregations