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

de.otto.edison.authentication.Credentials Maven / Gradle / Ivy

There is a newer version: 3.3.3
Show newest version
package de.otto.edison.authentication;

import org.springframework.util.ObjectUtils;

import jakarta.servlet.http.HttpServletRequest;

import java.util.Base64;
import java.util.Optional;

/**
 * LDAP credentials (username, password) parsed from HTTP request
 */
public record Credentials(String username, String password) {

    /**
     * Read username and password from the request's {@code Authorization} header and create a {@code Credentials}
     * object. Requires authorization header to be base64 encoded.
     *
     * @param request incoming http request
     * @return {@code Optional} with parsed {@code Credentials} if {@code Authorization} header and credentials
     * are present, {@code Optional.empty} otherwise.
     */
    public static Optional readFrom(HttpServletRequest request) {
        String authorizationHeader = request.getHeader("Authorization");
        if (!ObjectUtils.isEmpty(authorizationHeader) && authorizationHeader.contains("Basic")) {
            String credentials = authorizationHeader.substring(6, authorizationHeader.length());
            Optional decodedCredentials = base64Decode(credentials);
            String[] decodedCredentialParts = decodedCredentials
                    .map(s1 -> s1.split(":", 2))
                    .orElse(new String[0]);
            if (decodedCredentialParts.length >= 2
                    && !decodedCredentialParts[0].isEmpty()
                    && !decodedCredentialParts[1].isEmpty()) {

                return Optional.of(new Credentials(decodedCredentialParts[0], decodedCredentialParts[1]));
            }
        }
        return Optional.empty();
    }

    private static Optional base64Decode(String input) {
        try {
            return Optional.of(new String(Base64.getDecoder().decode(input.getBytes())));
        } catch (IllegalArgumentException e) {
            return Optional.empty();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy