Search in sources :

Example 36 with CustomScript

use of org.gluu.model.custom.script.model.CustomScript in project oxTrust by GluuFederation.

the class CustomScriptWebResourceTest method updateScriptTest.

@Test
public void updateScriptTest() {
    String name = "AnotherMyScript";
    CustomScript script = getCustomScript(name);
    HttpPost request = new HttpPost(BASE_URL + ApiConstants.BASE_API_URL + ApiConstants.CONFIGURATION + ApiConstants.SCRIPTS);
    try {
        String json = mapper.writeValueAsString(script);
        HttpEntity entity = new ByteArrayEntity(json.toString().getBytes("UTF-8"), ContentType.APPLICATION_FORM_URLENCODED);
        request.setEntity(entity);
        String CONTENT_TYPE = "Content-Type";
        request.setHeader(CONTENT_TYPE, MediaType.APPLICATION_JSON);
        HttpResponse response = handle(request);
        Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
        HttpEntity result = response.getEntity();
        CustomScript myScript = mapper.readValue(EntityUtils.toString(result), CustomScript.class);
        Assert.assertEquals(myScript.getName(), name);
        myScript.setDescription(myScript.getDescription() + " Updated");
        HttpPut second = new HttpPut(BASE_URL + ApiConstants.BASE_API_URL + ApiConstants.CONFIGURATION + ApiConstants.SCRIPTS);
        json = mapper.writeValueAsString(myScript);
        entity = new ByteArrayEntity(json.toString().getBytes("UTF-8"), ContentType.APPLICATION_FORM_URLENCODED);
        second.setEntity(entity);
        second.setHeader(CONTENT_TYPE, MediaType.APPLICATION_JSON);
        response = handle(second);
        Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
    } catch (ParseException | IOException e) {
        e.printStackTrace();
        Assert.assertTrue(false);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) CustomScript(org.gluu.model.custom.script.model.CustomScript) HttpEntity(org.apache.http.HttpEntity) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) HttpResponse(org.apache.http.HttpResponse) ParseException(org.apache.http.ParseException) IOException(java.io.IOException) HttpPut(org.apache.http.client.methods.HttpPut) Test(org.junit.Test)

Example 37 with CustomScript

use of org.gluu.model.custom.script.model.CustomScript in project oxTrust by GluuFederation.

the class CustomScriptWebResourceTest method listUpdateUserCustomScriptsTest.

@Test
public void listUpdateUserCustomScriptsTest() {
    String type = "update_user";
    HttpUriRequest request = new HttpGet(BASE_URL + ApiConstants.BASE_API_URL + ApiConstants.CONFIGURATION + ApiConstants.SCRIPTS + ApiConstants.TYPE_PATH + "/" + type);
    HttpResponse response = handle(request);
    Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
    HttpEntity entity = response.getEntity();
    try {
        String content = EntityUtils.toString(entity);
        CustomScript[] scripts = mapper.readValue(content, CustomScript[].class);
        Assert.assertTrue(scripts.length >= 1);
    } catch (ParseException | IOException e) {
        e.printStackTrace();
        Assert.assertTrue(false);
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpEntity(org.apache.http.HttpEntity) CustomScript(org.gluu.model.custom.script.model.CustomScript) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) ParseException(org.apache.http.ParseException) IOException(java.io.IOException) Test(org.junit.Test)

Example 38 with CustomScript

use of org.gluu.model.custom.script.model.CustomScript in project oxTrust by GluuFederation.

the class CustomScriptWebResourceTest method getScriptByInumTest.

@Test
public void getScriptByInumTest() {
    String inum = "5018-AF9C";
    HttpUriRequest request = new HttpGet(BASE_URL + ApiConstants.BASE_API_URL + ApiConstants.CONFIGURATION + ApiConstants.SCRIPTS + "/" + inum);
    HttpResponse response = handle(request);
    Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
    HttpEntity entity = response.getEntity();
    try {
        String content = EntityUtils.toString(entity);
        CustomScript script = mapper.readValue(content, CustomScript.class);
        Assert.assertNotNull(script);
    } catch (ParseException | IOException e) {
        e.printStackTrace();
        Assert.assertTrue(false);
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpEntity(org.apache.http.HttpEntity) CustomScript(org.gluu.model.custom.script.model.CustomScript) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) ParseException(org.apache.http.ParseException) IOException(java.io.IOException) Test(org.junit.Test)

Example 39 with CustomScript

use of org.gluu.model.custom.script.model.CustomScript in project oxTrust by GluuFederation.

the class ManageCustomScriptAction method save.

public String save() {
    try {
        List<CustomScript> oldCustomScripts = customScriptService.findCustomScripts(Arrays.asList(getScriptType()), "dn", "inum");
        List<String> updatedInums = new ArrayList<String>();
        for (Entry<CustomScriptType, List<CustomScript>> customScriptsByType : this.customScriptsByTypes.entrySet()) {
            List<CustomScript> customScripts = customScriptsByType.getValue();
            for (CustomScript customScript : customScripts) {
                String configId = customScript.getName();
                if (StringHelper.equalsIgnoreCase(configId, OxConstants.SCRIPT_TYPE_INTERNAL_RESERVED_NAME)) {
                    facesMessages.add(FacesMessage.SEVERITY_ERROR, "'%s' is reserved script name", configId);
                    return OxTrustConstants.RESULT_FAILURE;
                }
                boolean nameValidation = NAME_PATTERN.matcher(customScript.getName()).matches();
                if (!nameValidation) {
                    facesMessages.add(FacesMessage.SEVERITY_ERROR, "'%s' is invalid script name. Only alphabetic, numeric and underscore characters are allowed in Script Name", configId);
                    return OxTrustConstants.RESULT_FAILURE;
                }
                customScript.setRevision(customScript.getRevision() + 1);
                boolean update = true;
                String dn = customScript.getDn();
                String customScriptId = customScript.getInum();
                if (StringHelper.isEmpty(dn)) {
                    customScriptId = INumGenerator.generate(2);
                    dn = customScriptService.buildDn(customScriptId);
                    customScript.setDn(dn);
                    customScript.setInum(customScriptId);
                    update = false;
                }
                customScript.setDn(dn);
                customScript.setInum(customScriptId);
                if (ScriptLocationType.LDAP == customScript.getLocationType()) {
                    customScript.removeModuleProperty(CustomScript.LOCATION_PATH_MODEL_PROPERTY);
                }
                if ((customScript.getConfigurationProperties() != null) && (customScript.getConfigurationProperties().size() == 0)) {
                    customScript.setConfigurationProperties(null);
                }
                if ((customScript.getConfigurationProperties() != null) && (customScript.getModuleProperties().size() == 0)) {
                    customScript.setModuleProperties(null);
                }
                if (ScriptLocationType.FILE == customScript.getLocationType()) {
                    customScript.setScript(null);
                }
                updatedInums.add(customScriptId);
                if (update) {
                    customScriptService.update(customScript);
                } else {
                    customScriptService.add(customScript);
                }
            }
        }
        // Remove removed scripts
        for (CustomScript oldCustomScript : oldCustomScripts) {
            if (!updatedInums.contains(oldCustomScript.getInum())) {
                customScriptService.remove(oldCustomScript);
            }
        }
    } catch (Exception ex) {
        log.error("Failed to update custom scripts", ex);
        facesMessages.add(FacesMessage.SEVERITY_ERROR, "Failed to update custom script configuration");
        return OxTrustConstants.RESULT_FAILURE;
    }
    facesMessages.add(FacesMessage.SEVERITY_INFO, "Custom script configuration updated successfully");
    return OxTrustConstants.RESULT_SUCCESS;
}
Also used : CustomScript(org.gluu.model.custom.script.model.CustomScript) AuthenticationCustomScript(org.gluu.model.custom.script.model.auth.AuthenticationCustomScript) CustomScriptType(org.gluu.model.custom.script.CustomScriptType) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 40 with CustomScript

use of org.gluu.model.custom.script.model.CustomScript in project oxAuth by GluuFederation.

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(org.gluu.model.custom.script.model.CustomScript) AuthenticationCustomScript(org.gluu.model.custom.script.model.auth.AuthenticationCustomScript) CustomScriptType(org.gluu.model.custom.script.CustomScriptType) BaseExternalType(org.gluu.model.custom.script.type.BaseExternalType) ArrayList(java.util.ArrayList) CustomScriptConfiguration(org.gluu.model.custom.script.conf.CustomScriptConfiguration)

Aggregations

CustomScript (org.gluu.model.custom.script.model.CustomScript)42 ArrayList (java.util.ArrayList)20 AuthenticationCustomScript (org.gluu.model.custom.script.model.auth.AuthenticationCustomScript)11 CustomScriptType (org.gluu.model.custom.script.CustomScriptType)8 IOException (java.io.IOException)7 SimpleCustomProperty (org.gluu.model.SimpleCustomProperty)6 HttpEntity (org.apache.http.HttpEntity)5 HttpResponse (org.apache.http.HttpResponse)5 ParseException (org.apache.http.ParseException)5 SimpleExtendedCustomProperty (org.gluu.model.SimpleExtendedCustomProperty)5 Test (org.junit.Test)5 List (java.util.List)4 CustomScriptConfiguration (org.gluu.model.custom.script.conf.CustomScriptConfiguration)4 HttpGet (org.apache.http.client.methods.HttpGet)3 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)3 SelectableEntity (org.gluu.model.SelectableEntity)3 BasePersistenceException (org.gluu.persist.exception.BasePersistenceException)3 Filter (org.gluu.search.filter.Filter)3 Operation (io.swagger.v3.oas.annotations.Operation)2 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)2