/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation.
* Copyright (c) 2009, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.admingui.common.handlers;
import com.sun.jsftemplating.annotation.Handler;
import com.sun.jsftemplating.annotation.HandlerInput;
import com.sun.jsftemplating.annotation.HandlerOutput;
import com.sun.jsftemplating.layout.descriptors.handler.HandlerContext;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import org.glassfish.admingui.common.util.GuiUtil;
import org.glassfish.admingui.common.util.RestResponse;
import org.glassfish.admingui.common.util.RestUtil;
/**
*
* @author anilam
*/
public class SecurityHandler {
/**
* This handler returns the a Map for storing the attributes for realm creation.
* @param handlerCtx The HandlerContext.
*/
@Handler(id="getRealmAttrForCreate",
output={
@HandlerOutput(name="attrMap", type=Map.class),
@HandlerOutput(name="classnameOption", type=String.class),
@HandlerOutput(name="realmClasses", type=List.class),
@HandlerOutput(name="properties", type=List.class)})
public static void getRealmAttrForCreate(HandlerContext handlerCtx) {
handlerCtx.setOutputValue("realmClasses", realmClassList);
handlerCtx.setOutputValue("classnameOption", "predefine");
Map attrMap = new HashMap();
attrMap.put("predefinedClassname", Boolean.TRUE);
handlerCtx.setOutputValue("attrMap", attrMap);
handlerCtx.setOutputValue("properties", new ArrayList());
}
/**
*
This handler returns the a Map for storing the attributes for editing a realm.
* This can be used by either the node agent realm or the realm in configuration-Security-realm
* @param handlerCtx The HandlerContext.
*/
@Handler(id="getRealmAttrForEdit",
input={
@HandlerInput(name="endpoint", type=String.class)},
output={
@HandlerOutput(name="attrMap", type=Map.class),
@HandlerOutput(name="classnameOption", type=String.class),
@HandlerOutput(name="realmClasses", type=List.class),
@HandlerOutput(name="properties", type=List.class)})
public static void getRealmAttrForEdit(HandlerContext handlerCtx) {
String endpoint = (String) handlerCtx.getInputValue("endpoint");
HashMap realmMap = (HashMap) RestUtil.getEntityAttrs(endpoint, "entity");
HashMap responseMap = (HashMap) RestUtil.restRequest(endpoint + "/property.json", null, "GET", null, false);
HashMap propsMap = (HashMap) ((Map) responseMap.get("data")).get("extraProperties");
ArrayList propList = (ArrayList) propsMap.get("properties");
HashMap origProps = new HashMap();
for (HashMap prop : propList) {
origProps.put(prop.get("name"), prop.get("value"));
}
Map attrMap = new HashMap();
attrMap.put("Name", (String) realmMap.get("name"));
attrMap.put("fileJaax", "fileRealm");
attrMap.put("ldapJaax", "ldapRealm" );
attrMap.put("solarisJaax", "solarisRealm");
attrMap.put("jdbcJaax", "jdbcRealm");
String classname = (String) realmMap.get("classname");
if (realmClassList.contains(classname)){
handlerCtx.setOutputValue("classnameOption", "predefine");
attrMap.put("predefinedClassname", Boolean.TRUE);
attrMap.put("classname", classname);
List props = getChildrenMapForTableList(propList, "property", skipRealmPropsList);
handlerCtx.setOutputValue("properties", props);
if(classname.indexOf("FileRealm")!= -1){
attrMap.put("file", origProps.get("file"));
attrMap.put("fileJaax", origProps.get("jaas-context"));
attrMap.put("fileAsGroups", origProps.get("assign-groups"));
}else
if(classname.indexOf("LDAPRealm")!= -1){
attrMap.put("ldapJaax", origProps.get("jaas-context"));
attrMap.put("ldapAsGroups", origProps.get("assign-groups"));
attrMap.put("directory", origProps.get("directory"));
attrMap.put("baseDn", origProps.get("base-dn"));
}else
if(classname.indexOf("SolarisRealm")!= -1){
attrMap.put("solarisJaax", origProps.get("jaas-context"));
attrMap.put("solarisAsGroups", origProps.get("assign-groups"));
}else
if(classname.indexOf("PamRealm")!= -1){
attrMap.put("pamJaax", origProps.get("jaas-context"));
}else
if(classname.indexOf("JDBCRealm")!= -1){
attrMap.put("jdbcJaax", origProps.get("jaas-context"));
attrMap.put("jdbcAsGroups", origProps.get("assign-groups"));
attrMap.put("datasourceJndi", origProps.get("datasource-jndi"));
attrMap.put("userTable", origProps.get("user-table"));
attrMap.put("userNameColumn", origProps.get("user-name-column"));
attrMap.put("passwordColumn", origProps.get("password-column"));
attrMap.put("groupTable", origProps.get("group-table"));
attrMap.put("groupTableUserName", origProps.get("group-table-user-name-column"));
attrMap.put("groupNameColumn", origProps.get("group-name-column"));
attrMap.put("dbUser", origProps.get("db-user"));
attrMap.put("dbPassword", origProps.get("db-password"));
attrMap.put("digestAlgorithm", origProps.get("digest-algorithm"));
attrMap.put("pswdEncAlgorithm", origProps.get("digestrealm-password-enc-algorithm"));
attrMap.put("encoding", origProps.get("encoding"));
attrMap.put("charset", origProps.get("charset"));
}else
if(classname.indexOf("CertificateRealm")!= -1){
attrMap.put("certAsGroups", origProps.get("assign-groups"));
}
}else{
//Custom realm class
handlerCtx.setOutputValue("classnameOption", "input");
attrMap.put("predefinedClassname", Boolean.FALSE);
attrMap.put("classnameInput", classname);
attrMap.put("classname", classname);
List props = getChildrenMapForTableList(propList, "property", null);
handlerCtx.setOutputValue("properties", props);
}
handlerCtx.setOutputValue("attrMap", attrMap);
handlerCtx.setOutputValue("realmClasses", realmClassList);
}
public static List getChildrenMapForTableList(List propList, String childType, List skipList){
boolean hasSkip = true;
if (skipList == null ){
hasSkip = false;
}
List result = new ArrayList();
if (propList != null) {
for(HashMap oneMap: propList){
HashMap oneRow = new HashMap();
String name = (String) oneMap.get("name");
if (hasSkip && skipList.contains(name)){
continue;
}
oneRow.put("selected", false);
oneRow.put("name", name);
oneRow.put("value", oneMap.get("value"));
oneRow.put("description", oneMap.get("description"));
result.add(oneRow);
}
}
return result;
}
public static List getListfromMap(HashMap props) {
List result = new ArrayList();
Iterator it = props.entrySet().iterator();
while(it.hasNext()) {
Map.Entry m =(Map.Entry)it.next();
HashMap oneRow = new HashMap();
oneRow.put("selected", false);
oneRow.put("Name", m.getKey());
oneRow.put("Value", m.getValue());
oneRow.put("Description", "");
result.add(oneRow);
}
return result;
}
@Handler(id="saveRealm",
input={
@HandlerInput(name="endpoint", type=String.class),
@HandlerInput(name="classnameOption", type=String.class),
@HandlerInput(name="attrMap", type=Map.class),
@HandlerInput(name="edit", type=Boolean.class, required=true),
@HandlerInput(name="contentType", type=String.class, required=false),
@HandlerInput(name="propList", type=List.class)
},
output={
@HandlerOutput(name="newPropList", type=List.class)
})
public static void saveRealm(HandlerContext handlerCtx) {
String option = (String) handlerCtx.getInputValue("classnameOption");
List