All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.inverno.mod.security.http.basic.BasicAuthenticationErrorInterceptor Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2022 Jeremy Kuhn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.inverno.mod.security.http.basic;

import io.inverno.mod.http.base.ExchangeContext;
import io.inverno.mod.http.base.header.Headers;
import io.inverno.mod.http.server.ErrorExchange;
import io.inverno.mod.security.http.HttpAuthenticationErrorInterceptor;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringEscapeUtils;


/**
 * 

* An HTTP authentication error interceptor that implements RFC 7617 The 'Basic' HTTP Authentication Scheme. *

* *

* As per RFC 7617, a basic challenge with the {@code realm} parameter is sent to the requester to initiate basic HTTP authentication. *

* * @author Jeremy Kuhn * @since 1.5 * * @param the context type * @param the error exchange type */ public class BasicAuthenticationErrorInterceptor> extends HttpAuthenticationErrorInterceptor { /** * The realm parameter. * *

* RFC 7235 Section 2.2 *

*/ private static final String PARAMETER_REALM = "realm"; /** * The www-authenticate challenge format. * *

* RFC 7235 Section 2 *

*/ private static final String FORMAT_WWW_AUTHENTICATE = Headers.Authorization.AUTH_SCHEME_BASIC + " " + PARAMETER_REALM + "=\"%s\""; /** * The realm. */ private final String realm; /** *

* Creates a basic authentication error interceptor. *

* * @param realm the realm */ public BasicAuthenticationErrorInterceptor(String realm) { if(StringUtils.isBlank(realm)) { throw new IllegalArgumentException("realm is null or empty"); } this.realm = realm; } /*** *

* Returns the realm. *

* * @return the realm */ public String getRealm() { return realm; } @Override protected String createChallenge(io.inverno.mod.security.SecurityException cause) { return String.format(FORMAT_WWW_AUTHENTICATE, StringEscapeUtils.escapeJava(this.realm)); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy