use of org.dcache.macaroons.CaveatType.BEFORE in project dcache by dCache.
the class MacaroonRequestHandler method buildMacaroon.
private String buildMacaroon(String target, Request request) throws ErrorResponseException {
checkValidRequest(request.isSecure(), "Not secure transport");
if (Subjects.isNobody(getSubject())) {
throw new ErrorResponseException(SC_UNAUTHORIZED, "Authentication required");
}
MacaroonContext context = buildContext(target, request);
MacaroonRequest macaroonRequest = parseJSON(request);
try {
List<Caveat> caveats = new ArrayList<>();
List<Caveat> beforeCaveats = new ArrayList<>();
for (String serialisedCaveat : macaroonRequest.getCaveats()) {
Caveat caveat = new Caveat(serialisedCaveat);
(caveat.hasType(BEFORE) ? beforeCaveats : caveats).add(caveat);
}
macaroonRequest.getValidity().map(Duration::parse).map(Instant.now()::plus).map(i -> new Caveat(BEFORE, i)).ifPresent(beforeCaveats::add);
Instant expiry = calculateExpiry(context, beforeCaveats);
MacaroonProcessor.MacaroonBuildResult result = _processor.buildMacaroon(expiry, context, caveats);
request.setAttribute(MACAROON_ID_ATTRIBUTE, result.getId());
return result.getMacaroon();
} catch (DateTimeParseException e) {
throw new ErrorResponseException(SC_BAD_REQUEST, "Bad validity value: " + e.getMessage());
} catch (InvalidCaveatException e) {
throw new ErrorResponseException(SC_BAD_REQUEST, "Bad requested caveat: " + e.getMessage());
} catch (InternalErrorException e) {
throw new ErrorResponseException(SC_INTERNAL_SERVER_ERROR, "Internal error: " + e.getMessage());
}
}
Aggregations