use of javax.servlet.http.HttpServletResponse.SC_UNAUTHORIZED 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());
}
}
use of javax.servlet.http.HttpServletResponse.SC_UNAUTHORIZED in project common-java-modules by navikt.
the class CsrfDoubleSubmitCookieFilter method doFilter.
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
String path = request.getRequestURI().substring(request.getContextPath().length());
if (stream(ignoredUrls).noneMatch(path::startsWith)) {
if (ALLOWED_METHODS.contains(request.getMethod())) {
if (request.getCookies() == null || stream(request.getCookies()).noneMatch(cookie -> cookie.getName().equals(CSRF_COOKIE_NAVN))) {
response.addCookie(createCsrfProtectionCookie(request));
}
} else if (!cookieMatcherHeader(request)) {
LOG.warn("Feil i CSRF-sjekk. " + "Bruker du dette filteret må du i frontend sørge for å sende med NAV_CSRF_PROTECTION-cookien som en header med navn NAV_CSRF_PROTECTION og verdien til cookien. " + "Er headeren satt? " + isNotBlank(request.getHeader(CSRF_COOKIE_NAVN)));
response.sendError(SC_UNAUTHORIZED, "Mangler NAV_CSRF_PROTECTION-cookie!! Du må inkludere cookie-verdien i en header med navn NAV_CSRF_PROTECTION");
return;
}
}
filterChain.doFilter(request, response);
}
Aggregations