Pages

Subscribe:

Ads 468x60px

Featured Posts

Thursday, December 18, 2014

Extending SCIM User Schema Of WSO2 Identity Server

In this post we are going to extend the SCIM User Schema Of WSO2 Identity Server and add custom fields.
More details about Extending SCIM User Schema Of WSO2 Identity Server and SCIM User Provisioning With WSO2 Identity Server using these links. [1], [2]. In this sample we are going to add a custom field called dateOfBirth to the schema. Follow the following steps to enable the custom field.
 
1. Enable User Schema Extension by setting the user-schema-extension-enabled value true in /repositiry/conf/provisioning-config.xml
2. Set your Custom User Schema as follows in the /repository/conf/scim-schema-extension.config
=================================================================================
[
{
"attributeURI":"urn:scim:schemas:extension:wso2:1.0:wso2Extension.dateOfBirth",
"attributeName":"dateOfBirth",
"dataType":"string",
"multiValued":"false",
"multiValuedAttributeChildName":"null",
"description":"Date of Birth",
"schemaURI":"urn:scim:schemas:extension:wso2:1.0",
"readOnly":"true",
"required":"true",
"caseExact":"true",
"subAttributes":"null"
},
{
"attributeURI":"urn:scim:schemas:extension:wso2:1.0",
"attributeName":"wso2Extension",
"dataType":"null",
"multiValued":"false",
"multiValuedAttributeChildName":"null",
"description":"SCIM wso2 User Schema Extension",
"schemaURI":"urn:scim:schemas:extension:wso2:1.0",
"readOnly":"false",
"required":"false",
"caseExact":"false",
"subAttributes":"dateOfBirth"
}
]
==================================================================================

3. Start the server and login to the management console.
4. Go to
Home     > Configure     > Claim Management > urn:scim:schemas:core:1.0 > Add New Claim Mapping

File in the Followings
Display Name - dateOfBirth
Description- Date of Birth
Claim Uri - urn:scim:schemas:extension:wso2:1.0:wso2Extension.dateOfBirth
Mapped Attribute (s) - nickName

Here we have mapped to the nickName which is already available.

5. Now you are done with the configurations.

Now execute the following curl commands to add and get the user.

Adding a user
curl -v -k --user admin:admin --data "{"schemas":[],"wso2Extension":{"dateOfBirth":"1987-12-12"},"name":{"familyName":"gunasinghe","givenName":"hasinitg"},"userName":"hasinitg","password":"hasinitg","emails":[{"primary":true,"value":"hasini_home.com","type":"home"},{"value":"hasini_work.com","type":"work"}]}" --header "Content-Type:application/json" https://localhost:9443/wso2/scim/Users

Getting a user using the following command
curl -v -k --user admin:admin https://localhost:9443/wso2/scim/Users/[User ID Value]

Will result the following.

{"id":"92851fef-31a8-4c54-b631-616e58a85cc8","wso2Extension":{"dateOfBirth":"1987-12-12"},"schemas":["urn:scim:schemas:core:1.0","urn:scim:schemas:extension:wso2:1.0"],"nickName":"2012-12-12","name":{"familyName":"gunasinghe","givenName":"hasinitg"},"userName":"hasinitg","emails":[{"value":"hasini_work.com","type":"work"},{"value":"hasini_home.com","type":"home"}],"meta":{"lastModified":"2014-12-02T15:29:27","created":"2014-12-02T15:29:27","location":"https://localhost:9443/wso2/scim/Users/92851fef-31a8-4c54-b631-616e58a85cc8"}}


[1]. http://sureshatt.blogspot.com/2013/06/scim-user-provisioning-with-wso2.html
[2]. http://sureshatt.blogspot.com/2013/07/extending-scim-user-schema-of-wso2.html

Wednesday, November 5, 2014

[WSO2] Sample Web Application to Demonstrate Insertion, Retrieval and Deletion of a resource to Registry

Here is a sample web application to test Insertion, Retrieval and Deletion of a  resource to Registry.
Here is the sample servlet code. Github Link

package org.wso2.carbon.test;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.RegistryType;
import org.wso2.carbon.registry.api.Registry;
import org.wso2.carbon.registry.api.Resource;
/**
 * Servlet implementation class RegistryTest
 */
@WebServlet("/RegistryTest")
public class RegistryTest extends HttpServlet {
    private static final long serialVersionUID = 1L;
    /**
     * Default constructor.
     */
    public RegistryTest() {
        // TODO Auto-generated constructor stub
    }
    protected void processRequest(HttpServletRequest request,
                                  HttpServletResponse response) throws ServletException, IOException {
        try {
            PrintWriter out = response.getWriter();
            System.out.print("URL = " + request.getRequestURL());
            System.out.print(" :: action = " + request.getParameter("action"));
            System.out.print(" :: path = " + request.getParameter("path"));
            System.out.println(" :: resource = "+ request.getParameter("resource"));
            String resourcePath = request.getParameter("path");
            String resourceValue = request.getParameter("resource");
            String action = request.getParameter("action");
            CarbonContext cCtx = CarbonContext.getThreadLocalCarbonContext();
            Registry registry = cCtx.getRegistry(RegistryType.SYSTEM_CONFIGURATION);
            if (resourcePath != null && action != null) {
                if (action.equalsIgnoreCase("add")) {
                    if( resourceValue != null){
                        Resource resource = registry.newResource();
                        resource.setContent(resourceValue);
                        registry.put(resourcePath, resource);
                        out.println("Resource added successfully!!");
                        out.println("Registry path :: " + resourcePath);
                        out.println("Registry value :: " + resourceValue);
                    }else{
                        out.println("ERROR :: Resource Value Empty!!!");
                    }
                } else if (action.equals("get")) {
                    if (registry.resourceExists(resourcePath)) {
                        Resource resource = registry.get(resourcePath);
                        String content = new String((byte[]) resource.getContent());
                        response.addHeader("resource-content", content);
                        out.println("Resource Found in Registry returned!!!");
                        out.println("Registry path :: " + resourcePath);
                        out.println("Registry value :: " + content);
                    } else {
                        out.println("ERROR :: Resource Not Found in Registry!!!");
                        out.println("Registry path :: " + resourcePath);
                    }
                } else if (action.equalsIgnoreCase("delete")) {
                    if (registry.resourceExists(resourcePath)) {
                        Resource resource = registry.get(resourcePath);
                        String content = new String((byte[]) resource.getContent());
                        registry.delete(resourcePath);
                        out.println("Resource Found and deleted!!!");
                        out.println("Registry path :: " + resourcePath);
                        out.println("Registry value :: " + content);
                    } else {
                        out.println("ERROR :: Resource Not Found in Registry!!!");
                        out.println("Registry path :: " + resourcePath);
                    }
                }
            } else {
                out.println("ERROR :: Resource Error!!!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     * response)
     */
    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     * response)
     */
    protected void doPost(HttpServletRequest request,
                          HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }
}

To test the application host it to WSO2 Application Server. Execute the following commands.

Adding resource

curl --data "action=add&path=&resource=" -v http://localhost:9763/CarbonTest-1.0.0/RegistryTest
e.g

curl --data "action=add&amp;path=/foo/bar&amp;resource=AAAAAAAAA" -v http://localhost:9763/CarbonTest-1.0.0/RegistryTest<br />

Getting resource

curl --data "action=get&amp;path=" -v http://localhost:9763/CarbonTest-1.0.0/RegistryTest
e.g
curl --data "action=get&amp;path=/foo/bar" -v http://localhost:9763/CarbonTest-1.0.0/RegistryTest<br />

Deleting resource

curl --data "action=delete&amp;path=" -v http://localhost:9763/CarbonTest-1.0.0/RegistryTest
e.g

curl --data "action=delete&amp;path=/foo/bar" -v http://localhost:9763/CarbonTest-1.0.0/RegistryTest<br />