/*
 * 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.guacamole.auth.radius;

import com.google.inject.AbstractModule;
import java.security.Provider;
import java.security.Security;
import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.auth.radius.conf.ConfigurationService;
import org.apache.guacamole.auth.radius.conf.RadiusAuthenticationProtocol;
import org.apache.guacamole.auth.radius.conf.RadiusGuacamoleProperties;
import org.apache.guacamole.environment.Environment;
import org.apache.guacamole.environment.LocalEnvironment;
import org.apache.guacamole.net.auth.AuthenticationProvider;

/**
 * Guice module which configures RADIUS-specific injections.
 */
public class RadiusAuthenticationProviderModule extends AbstractModule {

    /**
     * Guacamole server environment.
     */
    private final Environment environment;

    /**
     * A reference to the RadiusAuthenticationProvider on behalf of which this
     * module has configured injection.
     */
    private final AuthenticationProvider authProvider;

    /**
     * Creates a new RADIUS authentication provider module which configures
     * injection for the RadiusAuthenticationProvider.
     *
     * @param authProvider
     *     The AuthenticationProvider for which injection is being configured.
     *
     * @throws GuacamoleException
     *     If an error occurs while retrieving the Guacamole server
     *     environment.
     */
    @SuppressWarnings("deprecation")
    public RadiusAuthenticationProviderModule(AuthenticationProvider authProvider)
            throws GuacamoleException {

        // Get local environment
        this.environment = new LocalEnvironment();
        
        // Check for MD4 requirement
        RadiusAuthenticationProtocol authProtocol = environment.getProperty(RadiusGuacamoleProperties.RADIUS_AUTH_PROTOCOL);
        RadiusAuthenticationProtocol innerProtocol = environment.getProperty(RadiusGuacamoleProperties.RADIUS_EAP_TTLS_INNER_PROTOCOL);
        if (authProtocol == RadiusAuthenticationProtocol.MSCHAPv1 
                    || authProtocol == RadiusAuthenticationProtocol.MSCHAPv2
                    || innerProtocol == RadiusAuthenticationProtocol.MSCHAPv1 
                    || innerProtocol == RadiusAuthenticationProtocol.MSCHAPv2) {
            
            Security.addProvider(new Provider("MD4", 0.00, "MD4 for MSCHAPv1/2 Support") {
                {
                    this.put("MessageDigest.MD4", org.bouncycastle.jce.provider.JDKMessageDigest.MD4.class.getName());
                }
            });
            
        }

        // Store associated auth provider
        this.authProvider = authProvider;

    }

    @Override
    protected void configure() {

        // Bind core implementations of guacamole-ext classes
        bind(AuthenticationProvider.class).toInstance(authProvider);
        bind(Environment.class).toInstance(environment);

        // Bind RADIUS-specific services
        bind(ConfigurationService.class);
        bind(RadiusConnectionService.class);

    }

}

@@ -886,16 +886,106 @@
 Module;%0A
+import java.lang.reflect.Constructor;%0Aimport java.lang.reflect.InvocationTargetException;%0A
 import j
@@ -1007,16 +1007,16 @@
 ovider;%0A
-
 import j
@@ -1082,24 +1082,78 @@
 eException;%0A
+import org.apache.guacamole.GuacamoleServerException;%0A
 import org.a
@@ -2355,45 +2355,8 @@
  */%0A
-    @SuppressWarnings(%22deprecation%22)%0A
     
@@ -3199,49 +3199,362 @@
     
-Security.addProvider(new Provider
+try %7B%0A                Provider md4Provider;%0A                Constructor providerConstructor = Provider.class%0A                        .getConstructor(String.class, String.class, String.class);%0A                if (providerConstructor != null)%0A                    md4Provider = (Provider) providerConstructor%0A                            .newInstance
 (%22MD4%22, 
 0.00
@@ -3553,12 +3553,14 @@
 4%22, 
+%22
 0.00
+%22
 , %22M
@@ -3590,181 +3590,778 @@
 rt%22)
- %7B%0A                %7B%0A                    this.put(%22MessageDigest.MD4%22, org.bouncycastle.jce.provider.JDKMessageDigest.MD4.class.getName());%0A                %7D%0A            %7D);
+;%0A                else%0A                    md4Provider = (Provider) Provider.class%0A                            .getConstructor(String.class, Double.class, String.class)%0A                            .newInstance(%22MD4%22, 0.00, %22MD4 for MSCHAPv1/2 Support%22);%0A%0A                assert(md4Provider != null);%0A                md4Provider.put(%22MessageDigest.MD4%22, org.bouncycastle.jce.provider.JDKMessageDigest.MD4.class.getName());%0A                Security.addProvider(md4Provider);%0A            %7D%0A            catch (IllegalAccessException%0A                    %7C InstantiationException%0A                    %7C InvocationTargetException%0A                    %7C NoSuchMethodException e) %7B%0A                throw new GuacamoleServerException(%22Unable to load MD4 support.%22, e);%0A            %7D
 %0A   

Deal with deprecated Provider constructor.