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

org.wildfly.security.http.util.SetMechanismInformationMechanismFactory Maven / Gradle / Ivy

There is a newer version: 2.4.1.Final
Show newest version
/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2016 Red Hat, Inc., and individual contributors
 * as indicated by the @author tags.
 *
 * 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 org.wildfly.security.http.util;

import static org.wildfly.security.http.HttpConstants.HOST;
import static org.wildfly.security.http.util.ElytronMessages.log;

import java.util.Map;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;

import org.wildfly.security.auth.callback.MechanismInformationCallback;
import org.wildfly.security.auth.server.MechanismInformation;
import org.wildfly.security.http.HttpAuthenticationException;
import org.wildfly.security.http.HttpServerAuthenticationMechanism;
import org.wildfly.security.http.HttpServerAuthenticationMechanismFactory;
import org.wildfly.security.http.HttpServerRequest;

/**
 * A wrapper {@link HttpServerAuthenticationMechanismFactory} to ensure that mechanism information for the current
 * authentication request is set before the first authentication callbacks.
 *
 * The host name and protocol are derived from the current request being authenticated.
 *
 * @author Darran Lofthouse
 */
public class SetMechanismInformationMechanismFactory implements HttpServerAuthenticationMechanismFactory {

    private HttpServerAuthenticationMechanismFactory delegate;

    /**
     * Construct a wrapping mechanism factory instance.
     *
     * @param delegate the wrapped mechanism factory
     */
    public SetMechanismInformationMechanismFactory(final HttpServerAuthenticationMechanismFactory delegate) {
        this.delegate = delegate;
    }

    @Override
    public String[] getMechanismNames(Map properties) {
        return delegate.getMechanismNames(properties);
    }

    @Override
    public HttpServerAuthenticationMechanism createAuthenticationMechanism(final String mechanismName, Map properties,
            final CallbackHandler callbackHandler) throws HttpAuthenticationException {
        final HttpServerAuthenticationMechanism mechanism = delegate.createAuthenticationMechanism(mechanismName, properties, callbackHandler);
        return mechanism != null ? new HttpServerAuthenticationMechanism() {

            @Override
            public String getMechanismName() {
                return mechanism.getMechanismName();
            }

            @Override
            public void evaluateRequest(HttpServerRequest request) throws HttpAuthenticationException {
                String host = request.getFirstRequestHeaderValue(HOST);
                String resolvedHostName = null;
                if (host != null) {
                  if (host.startsWith("[")) {
                      int close = host.indexOf(']');
                      if (close > 0) {
                          resolvedHostName = host.substring(0, close + 1);
                      }
                  } else {
                      int colon = host.lastIndexOf(':');
                      resolvedHostName = colon > 0 ? host.substring(0, colon) : host;
                  }
                }

                try {
                    final String mechanismName = getMechanismName();
                    final String hostName = resolvedHostName;
                    final String protocol = request.getRequestURI().getScheme();
                    callbackHandler.handle(new Callback[] { new MechanismInformationCallback(new MechanismInformation() {

                        @Override
                        public String getProtocol() {
                            return protocol;
                        }

                        @Override
                        public String getMechanismType() {
                            return "HTTP";
                        }

                        @Override
                        public String getMechanismName() {
                            return mechanismName;
                        }

                        @Override
                        public String getHostName() {
                            return hostName;
                        }
                    })});

                } catch (Throwable e) {
                    throw log.unableToLocateMechanismConfiguration(e).toHttpAuthenticationException();
                }

                mechanism.evaluateRequest(request);
            }

            @Override
            public void dispose() {
                mechanism.dispose();
            }

        } : null;
    }



}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy