Search in sources :

Example 1 with CustomScript

use of io.jans.model.custom.script.model.CustomScript in project jans by JanssenProject.

the class ExternalAuthenticationService method getAuthModesByAcrValues.

public List<String> getAuthModesByAcrValues(List<String> acrValues) {
    List<String> authModes = new ArrayList<String>();
    for (String acrValue : acrValues) {
        if (StringHelper.isNotEmpty(acrValue)) {
            String customScriptName = StringHelper.toLowerCase(scriptName(acrValue));
            if (customScriptConfigurationsNameMap.containsKey(customScriptName)) {
                CustomScriptConfiguration customScriptConfiguration = customScriptConfigurationsNameMap.get(customScriptName);
                CustomScript customScript = customScriptConfiguration.getCustomScript();
                // Handle internal authentication method
                if (customScript.isInternal()) {
                    authModes.add(scriptName(acrValue));
                    continue;
                }
                CustomScriptType customScriptType = customScriptConfiguration.getCustomScript().getScriptType();
                BaseExternalType defaultImplementation = customScriptType.getDefaultImplementation();
                BaseExternalType pythonImplementation = customScriptConfiguration.getExternalType();
                if ((pythonImplementation != null) && (defaultImplementation != pythonImplementation)) {
                    authModes.add(scriptName(acrValue));
                }
            }
        }
    }
    return authModes;
}
Also used : CustomScript(io.jans.model.custom.script.model.CustomScript) AuthenticationCustomScript(io.jans.model.custom.script.model.auth.AuthenticationCustomScript) CustomScriptType(io.jans.model.custom.script.CustomScriptType) BaseExternalType(io.jans.model.custom.script.type.BaseExternalType) ArrayList(java.util.ArrayList) CustomScriptConfiguration(io.jans.model.custom.script.conf.CustomScriptConfiguration)

Example 2 with CustomScript

use of io.jans.model.custom.script.model.CustomScript in project jans by JanssenProject.

the class CustomScriptManager method reloadCustomScriptConfigurations.

private ReloadResult reloadCustomScriptConfigurations(Map<String, CustomScriptConfiguration> customScriptConfigurations, List<CustomScript> newCustomScripts) {
    Map<String, CustomScriptConfiguration> newCustomScriptConfigurations;
    boolean modified = false;
    if (customScriptConfigurations == null) {
        newCustomScriptConfigurations = new HashMap<String, CustomScriptConfiguration>();
        modified = true;
    } else {
        // Clone old map to avoid reload not changed scripts because it's time and CPU
        // consuming process
        newCustomScriptConfigurations = new HashMap<String, CustomScriptConfiguration>(customScriptConfigurations);
    }
    List<String> newSupportedCustomScriptInums = new ArrayList<String>();
    for (CustomScript newCustomScript : newCustomScripts) {
        if (!newCustomScript.isEnabled()) {
            continue;
        }
        if (ScriptLocationType.FILE == newCustomScript.getLocationType()) {
            // Replace script revision with file modification time. This should allow to
            // reload script automatically after changing location_type
            long fileModifiactionTime = getFileModificationTime(newCustomScript.getLocationPath());
            newCustomScript.setRevision(fileModifiactionTime);
        }
        String newSupportedCustomScriptInum = StringHelper.toLowerCase(newCustomScript.getInum());
        newSupportedCustomScriptInums.add(newSupportedCustomScriptInum);
        CustomScriptConfiguration prevCustomScriptConfiguration = newCustomScriptConfigurations.get(newSupportedCustomScriptInum);
        if (prevCustomScriptConfiguration == null || prevCustomScriptConfiguration.getCustomScript().getRevision() != newCustomScript.getRevision()) {
            // Destroy old version properly before creating new one
            if (prevCustomScriptConfiguration != null) {
                destroyCustomScript(prevCustomScriptConfiguration);
            }
            // Load script entry with all attributes
            CustomScript loadedCustomScript = customScriptService.getCustomScriptByDn(newCustomScript.getScriptType().getCustomScriptModel(), newCustomScript.getDn());
            // Prepare configuration attributes
            Map<String, SimpleCustomProperty> newConfigurationAttributes = new HashMap<String, SimpleCustomProperty>();
            List<SimpleExtendedCustomProperty> simpleCustomProperties = loadedCustomScript.getConfigurationProperties();
            if (simpleCustomProperties == null) {
                simpleCustomProperties = new ArrayList<SimpleExtendedCustomProperty>(0);
            }
            for (SimpleCustomProperty simpleCustomProperty : simpleCustomProperties) {
                newConfigurationAttributes.put(simpleCustomProperty.getValue1(), simpleCustomProperty);
            }
            if (ScriptLocationType.FILE == loadedCustomScript.getLocationType()) {
                // Replace script revision with file modification time. This should allow to
                // reload script automatically after changing location_type
                long fileModifiactionTime = getFileModificationTime(loadedCustomScript.getLocationPath());
                loadedCustomScript.setRevision(fileModifiactionTime);
                if (fileModifiactionTime != 0) {
                    String scriptFromFile = loadFromFile(loadedCustomScript.getLocationPath());
                    if (StringHelper.isNotEmpty(scriptFromFile)) {
                        loadedCustomScript.setScript(scriptFromFile);
                    }
                }
            }
            // Load script
            BaseExternalType newCustomScriptExternalType = createExternalType(loadedCustomScript, newConfigurationAttributes);
            CustomScriptConfiguration newCustomScriptConfiguration = new CustomScriptConfiguration(loadedCustomScript, newCustomScriptExternalType, newConfigurationAttributes);
            // Store configuration and script
            newCustomScriptConfigurations.put(newSupportedCustomScriptInum, newCustomScriptConfiguration);
            modified = true;
        }
    }
    // Remove old external scripts configurations
    for (Iterator<Entry<String, CustomScriptConfiguration>> it = newCustomScriptConfigurations.entrySet().iterator(); it.hasNext(); ) {
        Entry<String, CustomScriptConfiguration> externalAuthenticatorConfigurationEntry = it.next();
        String prevSupportedCustomScriptInum = externalAuthenticatorConfigurationEntry.getKey();
        if (!newSupportedCustomScriptInums.contains(prevSupportedCustomScriptInum)) {
            // Destroy old authentication method
            destroyCustomScript(externalAuthenticatorConfigurationEntry.getValue());
            it.remove();
            modified = true;
        }
    }
    return new ReloadResult(newCustomScriptConfigurations, modified);
}
Also used : CustomScript(io.jans.model.custom.script.model.CustomScript) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SimpleExtendedCustomProperty(io.jans.model.SimpleExtendedCustomProperty) Entry(java.util.Map.Entry) BaseExternalType(io.jans.model.custom.script.type.BaseExternalType) SimpleCustomProperty(io.jans.model.SimpleCustomProperty) CustomScriptConfiguration(io.jans.model.custom.script.conf.CustomScriptConfiguration)

Example 3 with CustomScript

use of io.jans.model.custom.script.model.CustomScript in project jans by JanssenProject.

the class CustomScriptManager method saveScriptErrorImpl.

protected void saveScriptErrorImpl(CustomScript customScript, Exception exception, boolean overwrite) {
    // Load entry from DN
    String customScriptDn = customScript.getDn();
    Class<? extends CustomScript> scriptType = customScript.getScriptType().getCustomScriptModel();
    CustomScript loadedCustomScript = customScriptService.getCustomScriptByDn(scriptType, customScriptDn);
    // Check if there is error value already
    ScriptError currError = loadedCustomScript.getScriptError();
    if (!overwrite && (currError != null)) {
        return;
    }
    // Save error into script entry
    StringBuilder builder = new StringBuilder();
    builder.append(ExceptionUtils.getStackTrace(exception));
    String message = exception.getMessage();
    if (message != null && !StringUtils.isEmpty(message)) {
        builder.append("\n==================Further details============================\n");
        builder.append(message);
    }
    loadedCustomScript.setScriptError(new ScriptError(new Date(), builder.toString()));
    customScriptService.update(loadedCustomScript);
}
Also used : ScriptError(io.jans.model.custom.script.model.ScriptError) CustomScript(io.jans.model.custom.script.model.CustomScript) Date(java.util.Date)

Example 4 with CustomScript

use of io.jans.model.custom.script.model.CustomScript in project jans by JanssenProject.

the class AbstractCustomScriptService method findCustomScripts.

public List<CustomScript> findCustomScripts(List<CustomScriptType> customScriptTypes, String... returnAttributes) {
    String baseDn = baseDn();
    if ((customScriptTypes == null) || (customScriptTypes.size() == 0)) {
        return findAllCustomScripts(returnAttributes);
    }
    List<Filter> customScriptTypeFilters = new ArrayList<Filter>();
    for (CustomScriptType customScriptType : customScriptTypes) {
        Filter customScriptTypeFilter = Filter.createEqualityFilter("jansScrTyp", customScriptType.getValue());
        customScriptTypeFilters.add(customScriptTypeFilter);
    }
    Filter filter = Filter.createORFilter(customScriptTypeFilters);
    List<CustomScript> result = persistenceEntryManager.findEntries(baseDn, CustomScript.class, filter, returnAttributes);
    return result;
}
Also used : CustomScript(io.jans.model.custom.script.model.CustomScript) Filter(io.jans.orm.search.filter.Filter) CustomScriptType(io.jans.model.custom.script.CustomScriptType) ArrayList(java.util.ArrayList)

Example 5 with CustomScript

use of io.jans.model.custom.script.model.CustomScript in project jans by JanssenProject.

the class AbstractCustomScriptService method getScriptByDisplayName.

public CustomScript getScriptByDisplayName(String name) {
    Filter searchFilter = Filter.createEqualityFilter("displayName", name);
    List<CustomScript> result = persistenceEntryManager.findEntries(baseDn(), CustomScript.class, searchFilter, 1);
    if (result != null && !result.isEmpty()) {
        return result.get(0);
    }
    return null;
}
Also used : CustomScript(io.jans.model.custom.script.model.CustomScript) Filter(io.jans.orm.search.filter.Filter)

Aggregations

CustomScript (io.jans.model.custom.script.model.CustomScript)12 CustomScriptConfiguration (io.jans.model.custom.script.conf.CustomScriptConfiguration)4 ArrayList (java.util.ArrayList)4 ProtectedApi (io.jans.configapi.core.rest.ProtectedApi)3 AuthenticationCustomScript (io.jans.model.custom.script.model.auth.AuthenticationCustomScript)3 Filter (io.jans.orm.search.filter.Filter)3 CustomScriptType (io.jans.model.custom.script.CustomScriptType)2 ScriptError (io.jans.model.custom.script.model.ScriptError)2 BaseExternalType (io.jans.model.custom.script.type.BaseExternalType)2 HashMap (java.util.HashMap)2 SimpleCustomProperty (io.jans.model.SimpleCustomProperty)1 SimpleExtendedCustomProperty (io.jans.model.SimpleExtendedCustomProperty)1 Date (java.util.Date)1 Entry (java.util.Map.Entry)1