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

org.apache.sling.auth.core.spi.AuthenticationInfo Maven / Gradle / Ivy

There is a newer version: 6.5.21
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.sling.auth.core.spi;

import java.util.HashMap;
import java.util.Map;

import org.apache.sling.api.resource.ResourceResolverFactory;

/**
 * The AuthenticationInfo conveys any authentication credentials
 * and/or details extracted by the
 * {@link AuthenticationHandler#extractCredentials(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)}
 * method from the request.
 * 

* {@link AuthenticationHandler} implementations must return instances of this * class which may be constructed through any of the provided public * constructors. *

* Internally all values are stored in the map where some property names have * special semantics and the data type of the properties are ensured by the * {@link #put(String, Object)} method implementation. */ @SuppressWarnings("serial") public class AuthenticationInfo extends HashMap { /** * A special instance of this class which may be returned from the * {@link AuthenticationHandler#extractCredentials(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)} * method to inform the caller, that a response has been sent to the client * to request for credentials. *

* If this value is returned, the request should be considered finished and * no further actions should be taken on this request. */ public static final AuthenticationInfo DOING_AUTH = new ReadOnlyAuthenticationInfo( "DOING_AUTH"); /** * A special instance of this class which may be returned from the * {@link AuthenticationHandler#extractCredentials(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)} * method to inform the caller that credential extraction failed for some * reason. *

* If this value is returned, the handler signals that credentials would be * present in the request but the credentials are not valid for use (for * example OpenID identify failed to validate or some authentication cookie * expired). */ public static final AuthenticationInfo FAIL_AUTH = new ReadOnlyAuthenticationInfo( "FAIL_AUTH"); /** * The name of the special property providing the authentication type * provided by the {@link AuthenticationHandler}. This value must be * supplied to one of the constructors and is ultimately used as the value * of the HttpServletRequest.getAuthType method. *

* This property is always present (and cannot be removed) in this map and * is of String type. */ public static final String AUTH_TYPE = "sling.authType"; /** * Creates an instance of this class with just the authentication type. To * effectively use this instance the user Id with optional password and/or * the credentials should be set. * * @param authType The authentication type, must not be null. */ public AuthenticationInfo(final String authType) { this(authType, null, null); } /** * Creates an instance of this class authenticating with the given type and * userid. * * @param authType The authentication type, must not be null. * @param userId The name of the user to authenticate as. This may be * null for the constructor and later be set. * @throws NullPointerException if authType is * null. */ public AuthenticationInfo(final String authType, final String userId) { this(authType, userId, null); } /** * Creates an instance of this class authenticating with the given type and * userid/password connecting. * * @param authType The authentication type, must not be null. * @param userId The name of the user to authenticate as. This may be * null for the constructor and later be set. * @param password The password to authenticate with or null if * no password can be supplied. * @throws NullPointerException if authType is * null. */ public AuthenticationInfo(final String authType, final String userId, final char[] password) { super.put(AUTH_TYPE, authType); putIfNotNull(ResourceResolverFactory.USER, userId); putIfNotNull(ResourceResolverFactory.PASSWORD, password); } /** * @param authType The authentication type to set. If this is * null the current authentication type is not * replaced. */ public final void setAuthType(String authType) { putIfNotNull(AUTH_TYPE, authType); } /** * Returns the authentication type stored as the {@link #AUTH_TYPE} property * in this map. This value is expected to never be null. *

* If authentication is taking place through one of the standard ways, such * as Basic or Digest, the return value is one of the predefined constants * of the HttpServletRequest interface. Otherwise the value may * be specific to the {@link AuthenticationHandler} implementation. */ public final String getAuthType() { return (String) get(AUTH_TYPE); } /** * @param user The name of the user to authenticate as. If this is * null the current user name is not replaced. */ public final void setUser(String user) { putIfNotNull(ResourceResolverFactory.USER, user); } /** * Returns the user name stored as the {@link ResourceResolverFactory#USER} property or * null if the user is not set in this map. */ public final String getUser() { return (String) get(ResourceResolverFactory.USER); } /** * @param password The password to authenticate with. If this is * null the current password is not replaced. */ public final void setPassword(char[] password) { putIfNotNull(ResourceResolverFactory.PASSWORD, password); } /** * Returns the password stored as the {@link ResourceResolverFactory#PASSWORD} property or * null if the password is not set in this map. */ public final char[] getPassword() { return (char[]) get(ResourceResolverFactory.PASSWORD); } /** * Sets or resets a property with the given key to a new * value. Some keys have special meanings and their values are * required to have predefined as listed in the following table: *

* * * * * * * * * * * * *
{@link #AUTH_TYPE}String
{@link ResourceResolverFactory#USER}String
{@link ResourceResolverFactory#PASSWORD}char[]
*

* If the value for the special key does not match the required type an * IllegalArgumentException is thrown. * * @param key The name of the property to set * @param value The value of the property which must be of a special type if * the key designates one of the predefined * properties. * @return The value previously set for the given key. * @throws IllegalArgumentException if key designates one of * the special properties and the value does not * have the correct type for the respective key. */ @Override public Object put(final String key, final Object value) { if (AUTH_TYPE.equals(key) && !(value instanceof String)) { throw new IllegalArgumentException(AUTH_TYPE + " property must be a String"); } if (ResourceResolverFactory.USER.equals(key) && !(value instanceof String)) { throw new IllegalArgumentException(ResourceResolverFactory.USER + " property must be a String"); } if (ResourceResolverFactory.PASSWORD.equals(key) && !(value instanceof char[])) { throw new IllegalArgumentException(ResourceResolverFactory.PASSWORD + " property must be a char[]"); } return super.put(key, value); } /** * Removes the entry with the given key and returns its former * value (if existing). If the key is {@link #AUTH_TYPE} the * value is not actually removed and null is always returned. * * @param key Removes the value associated with this key. * @return The former value associated with the key. */ @Override public Object remove(Object key) { // don't remove the auth type from the map if (AUTH_TYPE.equals(key)) { return null; } return super.remove(key); } /** * Clears all properties from the map with the exception of the * {@link #AUTH_TYPE} property. */ @Override public void clear() { final String authType = getAuthType(); super.clear(); setAuthType(authType); } /** * Calls {@link #put(String, Object)} only if the value is not * null, otherwise does nothing. * * @param key The key of the property to set * @param value The value to set for the property. This may be * null in which case this method does nothing. */ private void putIfNotNull(final String key, final Object value) { if (value != null) { put(key, value); } } /** * The ReadOnlyAuthenticationInfo extends the * {@link AuthenticationInfo} class overwriting the * {@link #put(String, Object)} method such that no property is ever set. * This acts like kind of a read-only wrapper. */ private static final class ReadOnlyAuthenticationInfo extends AuthenticationInfo { /** * Creates an instance of this read-only class with the given * authentication type. * * @param authType The authentication type to set */ ReadOnlyAuthenticationInfo(final String authType) { super(authType); } /** * Does not put the property into the map */ @Override public Object put(String key, Object value) { return null; } /** * Sets/adds non of the properties */ @Override public void putAll(Map m) { } /** * Does not clear the properties */ @Override public void clear() { } /** * Removes nothing */ @Override public Object remove(Object key) { return null; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy