Package org.olat.shibboleth

Source Code of org.olat.shibboleth.ShibbolethModule

/**
* OLAT - Online Learning and Training<br>
* http://www.olat.org
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Copyright (c) since 2004 at Multimedia- & E-Learning Services (MELS),<br>
* University of Zurich, Switzerland.
* <p>
*/

package org.olat.shibboleth;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.olat.core.configuration.OLATModule;
import org.olat.core.logging.Tracing;
import org.olat.core.util.WebappHelper;
import org.olat.shibboleth.util.AttributeTranslator;
import org.olat.shibboleth.util.OLATUserfieldsMapper;
import org.olat.shibboleth.util.UniqueIdentifierMapper;

import com.anthonyeden.lib.config.Configuration;


/**
* Initial Date:  16.07.2004
*
* @author Mike Stock
*
* Comment: 
*
*/
public class ShibbolethModule implements OLATModule {
  /**
   * Path identifier for shibboleth registration workflows.
   */
  static final String PATH_REGISTER_SHIBBOLETH = "shibregister";
 
  private static final String CONF_ENABLE = "EnableShibbolethLogins";   
  private static final String CONF_ATTRIBUTETRANSLATIONS = "AttributeTranslations";
  private static final String CONF_UNIQUEIDENTIFIERS = "UniqueIdentifiers";
  private static final String CONF_OLATUSERMAPPING = "OLATUserMapping";
  private static final String CONF_USELANGUAGEINREQ ="UseLanguageInRequest";
  private static final String CONF_LANGUAGEPARAMNAM ="LanguageParamName";
  private static final String CONF_LOG_OPERATORS = "operators";

  private static boolean enableShibbolethLogins = false;
 
  private static String providerId;
 
  private static AttributeTranslator attributeTranslator;
  private static UniqueIdentifierMapper uidMapper;

  private static boolean useLanguageInReq = false;

  private static String languageParamName;
 
  /**
   * List of operator keys
   */
  private static List<String> operators = new ArrayList<String>();
 
  public static final String MULTIVALUE_SEPARATOR = ";";
 
  /**
   * @see org.olat.core.configuration.OLATModule#init(com.anthonyeden.lib.config.Configuration)
   */
  public void init(Configuration moduleConfig) {
    // check if shibboleth is enabled
    String sEnableShibbolethLogins = moduleConfig.getChildValue(CONF_ENABLE);
    if (sEnableShibbolethLogins.equalsIgnoreCase("true") || sEnableShibbolethLogins.equalsIgnoreCase("yes")) {
      enableShibbolethLogins = true;
      Tracing.logInfo("Shibboleth logins enabled.", ShibbolethModule.class);
    } else {
      Tracing.logInfo("Shibboleth logins disabled.", ShibbolethModule.class);
      return;
    }
   
    setGlobalProperties(moduleConfig);
       
    // initialize AttributeTranslator
    attributeTranslator = new AttributeTranslator(moduleConfig.getChild(CONF_ATTRIBUTETRANSLATIONS));
   
    // initialize uidMapper
    uidMapper = new UniqueIdentifierMapper(moduleConfig.getChild(CONF_UNIQUEIDENTIFIERS));
   
    //CredentialsManager(for server certificate check) no more needed
       
    // initialize user mapping
    OLATUserfieldsMapper.setMapping(moduleConfig.getChild(CONF_OLATUSERMAPPING));
   
    // Set operators
    Configuration operatorConfiguration = moduleConfig.getChild(CONF_LOG_OPERATORS);
    if (operatorConfiguration != null) {
      List operatorList = operatorConfiguration.getChildren();
      for (Iterator iter = operatorList.iterator(); iter.hasNext();) {
        Configuration operator = (Configuration) iter.next();
        operators.add(operator.getValue());
      }
    }
  }

  /**
   * @see org.olat.core.configuration.OLATModule#destroy()
   */
  public void destroy() {
    //nothing to do
  }

  private void setGlobalProperties(Configuration config) {
       
    // use language parameter
    String sUseLanguageInReq = config.getChildValue(CONF_USELANGUAGEINREQ);
    if (sUseLanguageInReq != null && (sUseLanguageInReq.equalsIgnoreCase("true") || sUseLanguageInReq.equalsIgnoreCase("yes"))) {
      useLanguageInReq = true;
      languageParamName = config.getChildValue(CONF_LANGUAGEPARAMNAM);
      if(languageParamName==null || languageParamName.length()==0) languageParamName = null;
      if(languageParamName!=null) Tracing.logInfo("Language code is sent as parameter in the AAI request", ShibbolethModule.class);
      else Tracing.logInfo("Language code was enabled to be sent, but no parameter name defined! please fix in the configuration.", ShibbolethModule.class);
    }else {
      useLanguageInReq = false;
      languageParamName = null;
      Tracing.logInfo("Language code is not sent with AAI request.", ShibbolethModule.class);
    }
  }

 
  public static String getSanitizedFileLocation(String location) {
    if (location == null || location.length() == 0)
      return null;
    // try as URL
    try {
      new URL(location);
      return location;
    } catch (MalformedURLException e) {
      // ok, we'll try files
    }
    // try as absolute file
    File fAbsFile = new File(location);
    if (fAbsFile.exists()) {
      try {
        return fAbsFile.toURL().toExternalForm();
      } catch (MalformedURLException e2) {
        return null;
      }
    }
    // assemble as relative file
    if (!location.startsWith(location)) location = "/" + location;
    location = WebappHelper.getContextRoot() + location;
    fAbsFile = new File(location);
    if (fAbsFile.exists()) {
      try {
        return fAbsFile.toURL().toExternalForm();
      } catch (MalformedURLException e2) {
        return null;
      }
    }
    return null;
  }
 
  // Getters and Setters //
  /**
   * @return True if shibboleth logins are allowed.
   */
  public static boolean isEnableShibbolethLogins() {
    return enableShibbolethLogins;
  }

  /**
   * @return true if the language should be sent in the aai request
   */
  public static boolean useLanguageInReq() {
    return useLanguageInReq;
  }

  /**
   * @return the get request parameter name to be used sending the language code.
   */
  public static String getLanguageParamName() {
    return languageParamName;
  }

  public static AttributeTranslator getAttributeTranslator() {
    return attributeTranslator;
  }

  public static UniqueIdentifierMapper getUniqueIdentifierMapper() {
    return uidMapper;
  }

  public static String[] getRegisteredOperatorKeys() {
    return null;
  }
 
  public static List<String> getOperatorKeys() {
    return operators;
  }

}
TOP

Related Classes of org.olat.shibboleth.ShibbolethModule

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.