Credential vault :- Is the place where a particular user can store his credentials and retrieve it when ever required securely. For example when you are accessing your employee portal you want to store your other portal credentials and when ever you click on a link. Which redirects to that portal and you want to login automatically.
Types of credential vault :-
A) User managed vault
B) Admin managed vault
Types of credentials :-
A) Active credential
B) Passive credential
Active credentials :- User wont be able to retrieve credentials but he will be able to pass it to back end service by form or basic authentication
Passive credentials :- User will be able to access his password and use it for other purposes
Technical overview :-
CredentialVaultService
Vault adapter
CredentialSlotConfig
Sample portlet applications
This article provides code examples for four sample portlet applications. You can install the applications to understand different ways to create vault slots, and to set and get credentials into vault slots.
The credential vault provided by WebSphere Portal defines four types of vault slots:
Portlet private slot: Stores user credentials that are not shared among portlets. The Private Slot Portlet application demonstrates the private slot.
Shared slot: Stores user credentials that are shared among the user's portlets. The Shared Slot Portlet application demonstrates the shared slot.
Administrative slot: Allows each user to store a secret for an administrator-defined resource (for example, Lotus Notes). The Administrative Slot Portlet application demonstrates the administrative slot.
System slot: Stores system credentials where the actual secret is shared among all users and portlets. The System Slot Portlet application demonstrates system slot.
Portlets belonging to all of these sample applications, except for System Slot Portlet Application, support both edit and view modes. A user can select edit mode to store credentials into the vault and view mode to see credentials that have been set.
Example :-
Portlet code :-
package com.ibm.credentialvault;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import com.ibm.portal.ObjectID;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletRequest;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import com.ibm.portal.portlet.service.PortletServiceHome;
import com.ibm.portal.portlet.service.credentialvault.CredentialSlotConfig;
import com.ibm.portal.portlet.service.credentialvault.CredentialTypes;
import com.ibm.portal.portlet.service.credentialvault.CredentialVaultException;
import com.ibm.portal.portlet.service.credentialvault.CredentialVaultService;
import com.ibm.portal.portlet.service.credentialvault.credentials.HttpFormBasedAuthCredential;
import com.ibm.portal.portlet.service.credentialvault.credentials.UserPasswordPassiveCredential;
/**
* A sample portlet
*/
public class CredentialVaultPortlet extends javax.portlet.GenericPortlet {
private CredentialVaultService cvs;
private static String sharedSlotResourceName ="loginslot";
/**
* @see javax.portlet.Portlet#init()
*/
public void init() throws PortletException{
super.init();
try{
javax.naming.Context ctx = new javax.naming.InitialContext();
PortletServiceHome cvsHome = (PortletServiceHome)ctx.lookup("portletservice/com.ibm.portal.portlet.service.credentialvault.CredentialVaultService" );
cvs = (CredentialVaultService)cvsHome.getPortletService (CredentialVaultService. class );
}catch(Exception e){
}
}
/**
* Serve up the <code>view</code> mode.
*
* @see javax.portlet.GenericPortlet#doView(javax.portlet.RenderRequest, javax.portlet.RenderResponse)
*/
public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
// Set the MIME type for the render response
response.setContentType(request.getResponseContentType());
//
// Passive credentials
//
String slot = getSharedSlotId(request);
if (slot != null)
{
try{
UserPasswordPassiveCredential credential =(UserPasswordPassiveCredential) cvs.getCredential
(slot, "UserPasswordPassive", new HashMap<String,String>(), request);
System.out.println("UserId :-"+ credential.getUserId());
System.out.println("Pwd :-" +String.valueOf(credential.getPassword()));
}
catch(Exception e){
return ;
}
}
// Invoke the JSP to render, replace with the actual jsp name
PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher("/_CredentialVault/jsp/html/CredentialVaultPortletView.jsp");
rd.include(request,response);
// or write to the response directly
//response.getWriter().println("CredentialVault#doView()");
}
private void getActiveCredentilas(RenderRequest request ){
String slot = getSharedSlotId(request);
if (slot != null)
{
HttpFormBasedAuthCredential credential;
try {
credential = (HttpFormBasedAuthCredential) cvs.getCredential(slot,CredentialTypes.HTTP_FORM_BASED_AUTH, buildCredentialConfigurationMap(), request);
//login to the resource
credential.login();
HttpURLConnection infoConn = credential.getAuthenticatedConnection("http://localhost:10039/Login/TestServlet");
InputStream is = infoConn.getInputStream();
//String info = readStringFromStream(is);
//credential.logout();
//request.setAttribute("result",info);
} catch (CredentialVaultException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private Map buildCredentialConfigurationMap()
{
Map credentialDataMap = new HashMap();
LinkedList list = new LinkedList();
list.add("action=j_security_check");
credentialDataMap.put(HttpFormBasedAuthCredential.KEY_FORM_DATA, list);
credentialDataMap.put(HttpFormBasedAuthCredential.KEY_USE_AUTH_COOKIES, Boolean.TRUE);
credentialDataMap.put(HttpFormBasedAuthCredential.KEY_LOGIN_URL, "http://localhost:10039/Login/j_security_check");
credentialDataMap.put(HttpFormBasedAuthCredential.KEY_LOGOUT_URL, "http://localhost:10039/Login/j_security_check");
credentialDataMap.put(HttpFormBasedAuthCredential.KEY_PASSWORD_ATTRIBUTE_NAME, "j_password");
credentialDataMap.put(HttpFormBasedAuthCredential.KEY_USERID_ATTRIBUTE_NAME, "j_username");
return credentialDataMap;
}
/**
* Serve up the <code>edit</code> mode.
*
* @see javax.portlet.GenericPortlet#doEdit(javax.portlet.RenderRequest, javax.portlet.RenderResponse)
*/
public void doEdit(RenderRequest request, RenderResponse response) throws PortletException, IOException {
// Set the MIME type for the render response
response.setContentType(request.getResponseContentType());
// Invoke the JSP to render
PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher("EditCredentials.jsp");
rd.include(request,response);
}
/**
* Serve up the <code>help</code> mode.
*
* @see javax.portlet.GenericPortlet#doHelp(javax.portlet.RenderRequest, javax.portlet.RenderResponse)
*/
protected void doHelp(RenderRequest request, RenderResponse response) throws PortletException, IOException {
// TODO: auto-generated method stub
}
/**
* Process an action request.
*
* @see javax.portlet.Portlet#processAction(javax.portlet.ActionRequest, javax.portlet.ActionResponse)
*/
public void processAction(ActionRequest request, ActionResponse response) throws PortletException, java.io.IOException {
try {
System.out.println("Entering ino processAction");
// Set userId/password text in the credential vault
String slotName = getSharedSlotId(request);
if(slotName == null)
slotName=createSlot(request);
System.out.println("slotName="+slotName);
String userID = request.getParameter("username");
String password = request.getParameter("password");
// save only if both parameters are set
if(slotName != null && userID!=null && password!=null && !userID.trim().equals("") && !password.trim().equals("")) {
cvs.setCredentialSecretUserPassword(slotName, userID,password.toCharArray(),request);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
private String createSlot(ActionRequest request) throws Exception
{
String resourceName = sharedSlotResourceName; //For shared slot resource name should be slot name
ObjectID segmentID = cvs.getDefaultUserCredentialSegmentId();
HashMap<String, String> descriptionsMap = new HashMap<String, String>();
HashMap<String, String> keywordsMap = new HashMap<String, String>();
int secretType = CredentialVaultService.SECRET_TYPE_USERID_STRING_PASSWORD_STRING;
boolean bActive = true; //Active
boolean bPrivate = true; //Portlet Private Slot
CredentialSlotConfig slot=null;
try
{
//Creating slot
slot= cvs.createCredentialSlot(resourceName, segmentID,descriptionsMap, keywordsMap, secretType, bActive, bPrivate, request);
System.out.println("New active slot created: " + slot);
}
catch(Exception e)
{
e.printStackTrace();
}
return slot.getSlotId();
}
private String getSharedSlotId(PortletRequest request)
{
String slotId=null;
try
{
for(Iterator it = cvs.getAccessibleSlots(request); it.hasNext();)
{
CredentialSlotConfig config = (CredentialSlotConfig)it.next();
if(config.getResourceName().startsWith(sharedSlotResourceName))
{
slotId = config.getSlotId();
return slotId;
}
}
}
catch(CredentialVaultException e)
{
System.out.println("Exception while retrieveing slot id " + e);
}
return slotId;
}
}
Edit Page JSP :-
<%@page language="java"
contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" session="false"%>
<%@taglib uri="http://java.sun.com/portlet" prefix="portlet"%>
<%@taglib
uri="http://www.ibm.com/xmlns/prod/websphere/portal/v6.1/portlet-client-model"
prefix="portlet-client-model"%><portlet-client-model:init>
<portlet-client-model:require module="ibm.portal.xml.*" />
<portlet-client-model:require module="ibm.portal.portlet.*" />
</portlet-client-model:init>
<portlet:defineObjects />
<form name="f1" method="post" action="<portlet:actionURL/>" id="f1">
<table>
<tr>
<td class="f1_label">User Name :</td><td><input type="text" name="username" value="" />
</td>
</tr>
<tr>
<td class="f1_label">Password :</td><td><input type="password" name="password" value="" />
</td>
</tr>
<tr>
<td>
<input type="submit" name="login" value="Store Credentials" style="font-size:18px; " />
</td>
</tr>
</table>
</form>
<p>Place content here.</p>