Friday, October 19, 2007

Implementing SHA encryption to secure data

Here I provide you with a class that will assist you to have a Base64 encoding of your data.
==============================
Base64coder.java
==============================
public class Base64Coder {

// Mapping table from 6-bit nibbles to Base64 characters.
private static char[] map1 = new char[64];
static {
int i=0;
for (char c='A'; c<='Z'; c++) map1[i++] = c;
for (char c='a'; c<='z'; c++) map1[i++] = c;
for (char c='0'; c<='9'; c++) map1[i++] = c;
map1[i++] = '+'; map1[i++] = '/'; }

// Mapping table from Base64 characters to 6-bit nibbles.
private static byte[] map2 = new byte[128];
static {
for (int i=0; i < map2.length; i++) map2[i] = -1;
for (int i=0; i < 64; i++) map2[map1[i]] = (byte)i; }

/**
* Encodes a string into Base64 format.
* No blanks or line breaks are inserted.
* @param s a String to be encoded.
* @return A String with the Base64 encoded data.
*/
public static String encodeString (String s) {
return new String(encode(s.getBytes())); }

/**
* Encodes a byte array into Base64 format.
* No blanks or line breaks are inserted.
* @param in an array containing the data bytes to be encoded.
* @return A character array with the Base64 encoded data.
*/
public static char[] encode (byte[] in) {
return encode(in,in.length); }

/**
* Encodes a byte array into Base64 format.
* No blanks or line breaks are inserted.
* @param in an array containing the data bytes to be encoded.
* @param iLen number of bytes to process in in.
* @return A character array with the Base64 encoded data.
*/
public static char[] encode (byte[] in, int iLen) {
int oDataLen = (iLen*4+2)/3; // output length without padding
int oLen = ((iLen+2)/3)*4; // output length including padding
char[] out = new char[oLen];
int ip = 0;
int op = 0;
while (ip < iLen) {
int i0 = in[ip++] & 0xff;
int i1 = ip < iLen ? in[ip++] & 0xff : 0;
int i2 = ip < iLen ? in[ip++] & 0xff : 0;
int o0 = i0 >>> 2;
int o1 = ((i0 & 3) << 4) | (i1 >>> 4);
int o2 = ((i1 & 0xf) << 2) | (i2 >>> 6);
int o3 = i2 & 0x3F;
out[op++] = map1[o0];
out[op++] = map1[o1];
out[op] = op < oDataLen ? map1[o2] : '='; op++;
out[op] = op < oDataLen ? map1[o3] : '='; op++; }
return out; }

/**
* Decodes a string from Base64 format.
* @param s a Base64 String to be decoded.
* @return A String containing the decoded data.
* @throws IllegalArgumentException if the input is not valid Base64 encoded data.
*/
public static String decodeString (String s) {
return new String(decode(s)); }

/**
* Decodes a byte array from Base64 format.
* @param s a Base64 String to be decoded.
* @return An array containing the decoded data bytes.
* @throws IllegalArgumentException if the input is not valid Base64 encoded data.
*/
public static byte[] decode (String s) {
return decode(s.toCharArray()); }

/**
* Decodes a byte array from Base64 format.
* No blanks or line breaks are allowed within the Base64 encoded data.
* @param in a character array containing the Base64 encoded data.
* @return An array containing the decoded data bytes.
* @throws IllegalArgumentException if the input is not valid Base64 encoded data.
*/
public static byte[] decode (char[] in) {
int iLen = in.length;
if (iLen%4 != 0) throw new IllegalArgumentException ("Length of Base64 encoded input string is not a multiple of 4.");
while (iLen > 0 && in[iLen-1] == '=') iLen--;
int oLen = (iLen*3) / 4;
byte[] out = new byte[oLen];
int ip = 0;
int op = 0;
while (ip < iLen) {
int i0 = in[ip++];
int i1 = in[ip++];
int i2 = ip < iLen ? in[ip++] : 'A';
int i3 = ip < iLen ? in[ip++] : 'A';
if (i0 > 127 || i1 > 127 || i2 > 127 || i3 > 127)
throw new IllegalArgumentException ("Illegal character in Base64 encoded data.");
int b0 = map2[i0];
int b1 = map2[i1];
int b2 = map2[i2];
int b3 = map2[i3];
if (b0 < 0 || b1 < 0 || b2 < 0 || b3 < 0)
throw new IllegalArgumentException ("Illegal character in Base64 encoded data.");
int o0 = ( b0 <<2) | (b1>>>4);
int o1 = ((b1 & 0xf)<<4) | (b2>>>2);
int o2 = ((b2 & 3)<<6) | b3;
out[op++] = (byte)o0;
if (op < oLen) out[op++] = (byte)o1;
if (op < oLen) out[op++] = (byte)o2; }
return out; }

// Dummy constructor.
private Base64Coder() {}

} // end class Base64Coder


===============================
DigestService.java
===============================

import java.security.MessageDigest;

public final class DigestService
{
public static String encrypt(String plaintext) throws Exception {

MessageDigest md = MessageDigest.getInstance("SHA");

md.update(plaintext.getBytes("UTF-8"));

return new String(Base64Coder.encode(md.digest()));
}
}

================================
Base64Test.java
================================

public class Base64Test {

public static void main(String[] args) {
try{
System.out.println(DigestService.encrypt("rajnishbhatia19"));
}catch(Exception ex){
ex.printStackTrace();
}
}

}

========================
Results:
========================
OA4WNjP6mo9Yw12bDET5ZutjOC4=

Thursday, September 6, 2007

Achieving IWA for Internet based web app

Here, I present you with a sample .NET application on how to authenticate users with your domain instead of application level user id and password. The benefit of doing is that all your internet based web apps can use the same mechanism and NOT store individual user ids & passwords in a seperate datastore (often times different for each app). This feature allows your administrators to control users enterprise-wide instead of worrying about controlling users at application level & making sure all company security guidelines are followed.

Environment : VS 2005, C# , IIS

Login.aspx is your application's start page in this example. If the user is not authenticated, he gets an error message, otherwise the user proceeds to default.aspx page of the app.

LoginPage.aspx:


LoginPage.aspx with Incorrect Password:


LoginPage.aspx with Correct Password:


Default.aspx after Authentication done:


You can use the following code to achieve this :

===================
LoginPage.aspx
===================
<%@ Page language="c#" Inherits="LogonUserCS.LoginPage" CodeFile="LoginPage.aspx.cs" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>LoginPage</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<FORM id="Form1" method="post" runat="server">
<TABLE id="Table1" cellSpacing="1" cellPadding="1" width="300" border="0">
<TR>
<TD><FONT face="Arial"><STRONG>Username:</STRONG></FONT></TD>
<TD>
<asp:TextBox id="txtUsername" runat="server" Width="150px">UserID</asp:TextBox><FONT face="Arial"></FONT></TD>
</TR>
<TR>
<TD><FONT face="Arial"><STRONG>Password:</STRONG></FONT></TD>
<TD>
<asp:TextBox id="txtPassword" runat="server" Width="150px" TextMode="Password">tryme</asp:TextBox><FONT face="Arial"></FONT></TD>
</TR>
<TR>
<TD colSpan="2"><FONT face="Arial">
<asp:CheckBox id="chkRemember" runat="server" Text="Remember login information"></asp:CheckBox></FONT></TD>
</TR>
</TABLE>
<P><FONT face="Arial" color="#ff0000"><STRONG>
<asp:Label id="lblError" runat="server" Visible="False">Login failed! Please try again.</asp:Label>
</STRONG></FONT></P>
<P>
<asp:Button id="cmdLogin" runat="server" Text="Login" onclick="cmdLogin_Click"></asp:Button><BR>
</P>
</FORM>
</body>
</HTML>


=======================
LoginPage.aspx.cs
=======================
using System;
using System.Collections;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;

namespace LogonUserCS
{
/// <summary>
/// Summary description for LoginPage.
/// </summary>
public partial class LoginPage : System.Web.UI.Page
{

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
}
#endregion

// Declare the logon types as constants
const long LOGON32_LOGON_INTERACTIVE = 2;
const long LOGON32_LOGON_NETWORK = 3;

// Declare the logon providers as constants
const long LOGON32_PROVIDER_DEFAULT = 0;
const long LOGON32_PROVIDER_WINNT50 = 3;
const long LOGON32_PROVIDER_WINNT40 = 2;
const long LOGON32_PROVIDER_WINNT35 = 1;

[DllImport("advapi32.dll",EntryPoint = "LogonUser")]
private static extern bool LogonUser(
string lpszUsername,
string lpszDomain,
string lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);

/// <summary>
/// Validates the user based on the supplied credentials
/// </summary>
/// <param name="Username">The username to use when valdiating the user</param>
/// <param name="Password">The password to use when validating the user</param>
/// <param name="Domain">The account domain or machine name to use when validating the user</param>
/// <returns>Returns true if the credentials are valid, and false otherwise</returns>
private bool ValidateLogin(
string Username,
string Password,
string Domain)
{
// This is the token returned by the API call
// Look forward to a future article covering
// the uses of it
IntPtr token = new IntPtr(0);
token = IntPtr.Zero;

// Call the API
if (LogonUser(
Username,
Domain,
Password,
(int)LOGON32_LOGON_NETWORK,
(int)LOGON32_PROVIDER_DEFAULT,
ref token))
{
//' Since the API returned TRUE, return TRUE to the caller
return true;
}
else
{
//' Bad credentials, return FALSE
return false;
}
}

/// <summary>
/// Called when the user clicks on the Login button
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void cmdLogin_Click(object sender, System.EventArgs e)
{
string Username = txtUsername.Text;
string Password = txtPassword.Text;
// Pull the domain out of the web.config file
string Domain = ConfigurationSettings.AppSettings["AccountDomain"];

if (ValidateLogin(Username,Password,Domain))
{
//' Since the credentials are valid,
//' redirect the user to the calling page
FormsAuthentication.RedirectFromLoginPage(Username,chkRemember.Checked);
}
else
{
//' Bad credentials, show an error message
lblError.Visible = true;
}

}
}
}

========================
default.aspx
========================
<%@ Page language="c#" Inherits="LogonUserCS.WebForm1" CodeFile="default.aspx.cs" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<FORM id="Form1" method="post" runat="server">
<FONT face="Arial">You are logged in as </FONT><FONT face="Arial"><STRONG>
<asp:Label id="lblUser" runat="server"></asp:Label></STRONG></FONT><FONT face="Arial">.</FONT>
</FORM>
</body>
</HTML>

============================
default.aspx.cs
============================

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace LogonUserCS
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public partial class WebForm1 : System.Web.UI.Page
{

protected void Page_Load(object sender, System.EventArgs e)
{
lblUser.Text = User.Identity.Name;
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{

}
#endregion
}
}

========================
web.config
========================
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation defaultLanguage="C#" debug="true">
<compilers>
<compiler language="c#" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" extension=".cs" compilerOptions="/d:DEBUG;TRACE"/></compilers></compilation>
<customErrors mode="On"/>
<authentication mode="Forms">
<forms loginUrl="/LoginPage.aspx" name="LogonUserDemo" timeout="20" path="/" protection="All"/>
</authentication>
<authorization>
<allow users="*"/>
<deny users="?"/>
</authorization>
<trace enabled="false" requestLimit="10" pageOutput="false" traceMode="SortByTime" localOnly="true"/>
<sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes" cookieless="false" timeout="20"/>
<globalization requestEncoding="utf-8" responseEncoding="utf-8"/>
<xhtmlConformance mode="Legacy"/></system.web>
<appSettings>
<add key="AccountDomain" value="DomainName"/>
</appSettings>
</configuration>

Tuesday, September 4, 2007

Implementing Sun RSA CRT Security to secure data

Often times you need to encrypt/cipher data to keep in your datastores in a non human readable format. There are various ways to achieve this and one of the ways is to implement asymmetric encryption or commonly known as key/pair implementation. Key Pair term is used because this implementation involves a pair of distributable Public Keys and a secret Private key. Data Encrypted by your Public Key can only be decrypted by your Private Key. Longer Keys provide stronger encryption but it requires more computation and hence not appropriate for large amounts of data. You may use it to secure small but important data like credentials, social-security numbers etc. The algorithm used in this secure implementation is Chinese Remainder Theorem (CRT) and it is patented by RSA.

Here, I present you a way to implement this into your apps:

=================================================
KeyGen.java
=================================================

import java.security.KeyPair;
import java.security.KeyPairGenerator;

public class KeyGen {

public static void main(String[] args) throws Exception {
String algorithm = "RSA";
KeyPairGenerator generator = KeyPairGenerator.getInstance(algorithm);
generator.initialize(1024);
KeyPair keyPair = generator.generateKeyPair();
System.out.println(keyPair.getClass().getName());
System.out.println(keyPair.getPublic().getClass().getName());
System.out.println(keyPair.getPublic());
System.out.println(keyPair.getPrivate());
}

}
=========================
Results:
=========================
java.security.KeyPair

sun.security.rsa.RSAPublicKeyImpl


Sun RSA public key, 1024 bits
modulus: 98677203744446260465542414765650691002275163600893336774030639809055241671590316795866062624226355106366106365150146141568384880010496063021240945844297495973945754466316385920919706173968448314515263558835923413988948374505246140063606334219872299586481788510932872327106443573840159515004641256381252312213
public exponent: 65537


Sun RSA private CRT key, 1024 bits
modulus: 98677203744446260465542414765650691002275163600893336774030639809055241671590316795866062624226355106366106365150146141568384880010496063021240945844297495973945754466316385920919706173968448314515263558835923413988948374505246140063606334219872299586481788510932872327106443573840159515004641256381252312213
public exponent: 65537
private exponent: 66010151355116476266381509769151653939465423309378897073565730501377708014162855770748799174644973590768519601973656972282825907877079480277007252494924174792809949607545280699010499959716736392703906869830507078213365818919271289454064367852372369106548543273821152999734305850287488981430290809005791015693
prime p: 11233688489962973338844802243855066309619057073058245210667885044580710403537025877181943919444268451387897571270403920077238069995990276143846449096478403
prime q: 8784043089018529857113045198094546951702263784704005736527840877045442563172893548671083896835744209733189342603630638586802678054189266328008006731555271
prime exponent p: 8735389670346415723853835420469992359595440538279052938432591629795720945192088633183487587511158716749775486016789364404476959933101247430347784287573925
prime exponent q: 7568240491089313012675972506455997195093153895770273096400217632226802566063759511714930853086457002102541608853878078618832110395774159825400920214596333
crt coefficient: 7682099122771530487734293443893764412335043357041532111008506208973843014546928432001347862281087160360054782853678831202444919205277659896883572013566175

=================================================
PasswordEncrypt.java
=================================================

import java.math.BigInteger;
import java.security.PublicKey;

import javax.crypto.Cipher;

import org.apache.axis.encoding.Base64;

import sun.security.rsa.RSAPublicKeyImpl;

public class PasswordEncrypt {

private static BigInteger mod;
private static BigInteger pubExp;
private static PublicKey key;
private static Cipher cipher;

static
{
try
{
mod = new BigInteger("98677203744446260465542414765650691002275163600893336774030639809055241671590316795866062624226355106366106365150146141568384880010496063021240945844297495973945754466316385920919706173968448314515263558835923413988948374505246140063606334219872299586481788510932872327106443573840159515004641256381252312213", 10);
pubExp = new BigInteger("65537", 10);

key = new RSAPublicKeyImpl(mod, pubExp);
cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, key);
}
catch(Exception e)
{
throw new RuntimeException(e);
}
}

public PasswordEncrypt()
{
super();
}

public static String encrypt(String password) throws Exception
{
// encrypt the password
byte[] data = password.getBytes();
data = cipher.doFinal(data);
return new String(Base64.encode(data));
}

public static void main(String[] args)
{
try{
System.out.println(encrypt("test"));
}catch(Exception ex){System.out.println(ex.getStackTrace());}
}
}

========================
Results:
========================

iMeOizw/O75tdjDSzXSlkUCGahuNYSvhc5oW/jKzV7+hS6eaxtWzbhcssgAd4ygxGbBL3gZxzxEVePRfLedFPqX/DAHMKSVbeCqbtE+1TtHjvIo46SReAahNANvDJnAXmCO2Bp4p+l4hGrTCAz9EXkhUQdel6AIc0DwJOSB0I+4=

=================================================
PasswordDecrypt.java
=================================================
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.RSAPrivateCrtKeySpec;

import javax.crypto.Cipher;

import org.apache.axis.encoding.Base64;

public class PasswordDecrypt {

private static BigInteger mod;
private static BigInteger pubExp;
private static PrivateKey key;
private static RSAPrivateCrtKeySpec keySpec;
private static Cipher cipher;

static
{
try
{
// get out the parts of the private key
BigInteger privExp = new BigInteger("66010151355116476266381509769151653939465423309378897073565730501377708014162855770748799174644973590768519601973656972282825907877079480277007252494924174792809949607545280699010499959716736392703906869830507078213365818919271289454064367852372369106548543273821152999734305850287488981430290809005791015693");
BigInteger p = new BigInteger("11233688489962973338844802243855066309619057073058245210667885044580710403537025877181943919444268451387897571270403920077238069995990276143846449096478403");
BigInteger q = new BigInteger("8784043089018529857113045198094546951702263784704005736527840877045442563172893548671083896835744209733189342603630638586802678054189266328008006731555271");
BigInteger pExp = new BigInteger("8735389670346415723853835420469992359595440538279052938432591629795720945192088633183487587511158716749775486016789364404476959933101247430347784287573925");
BigInteger qExp = new BigInteger("7568240491089313012675972506455997195093153895770273096400217632226802566063759511714930853086457002102541608853878078618832110395774159825400920214596333");
BigInteger crtCoef = new BigInteger("7682099122771530487734293443893764412335043357041532111008506208973843014546928432001347862281087160360054782853678831202444919205277659896883572013566175");

// get the parts of the public key
mod = new BigInteger("98677203744446260465542414765650691002275163600893336774030639809055241671590316795866062624226355106366106365150146141568384880010496063021240945844297495973945754466316385920919706173968448314515263558835923413988948374505246140063606334219872299586481788510932872327106443573840159515004641256381252312213", 10);
pubExp = new BigInteger("65537", 10);

keySpec = new RSAPrivateCrtKeySpec(mod, pubExp, privExp, p,q, pExp, qExp, crtCoef);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
key = keyFactory.generatePrivate(keySpec);
cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, key);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}

public PasswordDecrypt()
{
}

public static String decrypt(String dataString) throws Exception
{
// decode and decrypt the data
byte[] data = Base64.decode(dataString);
data = cipher.doFinal(data);
return new String(data);
}

public static void main(String[] args) throws Exception
{
System.out.println(decrypt("iMeOizw/O75tdjDSzXSlkUCGahuNYSvhc5oW/jKzV7+hS6eaxtWzbhcssgAd4ygxGbBL3gZxzxEVePRfLedFPqX/DAHMKSVbeCqbtE+1TtHjvIo46SReAahNANvDJnAXmCO2Bp4p+l4hGrTCAz9EXkhUQdel6AIc0DwJOSB0I+4="));
}
}


=============================
Results:
=============================
test



You can use this class to regenerate your new keys and secure your data. Simply replace the keys in the code and secure your data.

Happy Securing Data !!

Wednesday, August 15, 2007

Oracle Identity Manager XML Import failure with IE 7.x

If you have been trying to import connector xml files and were unsuccessful with IE 7.x, that is due to the fact java policy now explicitly needs a File Read Permission. Here are the steps to make it work:

Windows ->Start -> Run -> policytool



File Open -> {Your JRE Location}/lib/security/java.policy

Select CodeBase

Hit Edit Policy Entry

Add Permission

From the Permission dropdown - select FilePermission
From the Target dropdown - select or target
From the Actions dropdown - select read



Hit ok, Done and File Save.

This should resolve your issue.

Monday, August 6, 2007

Calling OIM User Operations from a JSP Web Page



Environment : Tomcat 4.1 , JBoss 4.0.3 SP1, OIM 9.0.3.1

===================================
File: raj.jsp
===================================
<%@page import="com.oim.xl.integration.UserOperations" %>
<%@page import="java.util.*" %>
<html>
<title>OIM</title>
<body>
Finding user with first name System <BR>
<%
String s="";
UserOperations uo=new UserOperations();
List lst=uo.getUserLogin("System");
ListIterator it = lst.listIterator();
while (it.hasNext()) {
s = (String) it.next();

}

%>
The result is : <%=s %>

</body>
</html>

===================================
File: UserOperations.class
===================================

package com.oim.xl.integration;

import java.util.*;
import Thor.API.tcResultSet;
import Thor.API.tcUtilityFactory;
import com.thortech.xl.util.config.ConfigurationClient;
import Thor.API.Operations.tcOrganizationOperationsIntf;
import Thor.API.Operations.tcUserOperationsIntf;

public class UserOperations {

/** The utility factory instance. */
private tcUtilityFactory ioUtilityFactory;
public UserOperations(){
Hashtable env=new Hashtable();
env.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
env.put("java.naming.provider.url", "jnp://192.168.1.101:1099");
System.setProperty("java.security.manager", "");
System.setProperty("XL.HomeDir", "C:\\tomcat4.1\\config");
System.setProperty("java.security.policy", "C:\\tomcat4.1\\config\\xl.policy");
System.setProperty("java.security.auth.login.config", "C:\\tomcat4.1\\config\\auth.conf");
System.setProperty("java.naming.provider.url", "jnp://192.168.1.101:1099/UserOperations");
Enumeration enum = env.keys();
while(enum.hasMoreElements())
{
String s = (String)enum.nextElement();
}
try{
log("Connect as : xelsysadm ");
ioUtilityFactory = new tcUtilityFactory(env, "xelsysadm","xelsysadm");
log("Connected as : " + ioUtilityFactory.getUserName());
}catch(Exception ex)
{
ex.printStackTrace();
}
}

/** Retrieves user login based on the first name. */
public List getUserLogin(String psFirstName){
Vector mvUsers=new Vector();
try{
tcUserOperationsIntf moUserUtility =(tcUserOperationsIntf)ioUtilityFactory.getUtility("Thor.API.Operations.tcUserOperationsIntf");
log("Interface tcUserOperationsIntf moUserUtility successfully created.");
Hashtable mhSearchCriteria = new Hashtable();
mhSearchCriteria.put("Users.First Name", psFirstName);
tcResultSet moResultSet = moUserUtility.findUsers(mhSearchCriteria);
log("Finding Users with First Name "+ psFirstName);
for (int i=0; i<moResultSet.getRowCount(); i++){
moResultSet.goToRow(i);
mvUsers.add(moResultSet.getStringValue("Users.User ID"));
log("Key is : "+ String.valueOf(moResultSet.getLongValue("Users.Key")) + " for user id : "+ moResultSet.getStringValue("Users.User ID"));
}
} catch(Exception ex){ex.printStackTrace();}
return mvUsers;
}

private void log(String msg){
System.out.println(msg);
}

public String setEmailID(String userLogin, String emailid){
String rtnval="SUCCESS";
try
{
log("Changing Email Id for user : "+userLogin+" to "+emailid);
tcUserOperationsIntf moUserUtility =(tcUserOperationsIntf)ioUtilityFactory.getUtility("Thor.API.Operations.tcUserOperationsIntf");
Hashtable mhSearchCriteria = new Hashtable();
mhSearchCriteria.put("Users.User ID", userLogin);
tcResultSet moResultSet = moUserUtility.findUsers(mhSearchCriteria);
for (int i=0; i<moResultSet.getRowCount(); i++){
moResultSet.goToRow(i);
log("Key is : "+ String.valueOf(moResultSet.getLongValue("Users.Key")) + " for user id : "+ moResultSet.getStringValue("Users.User ID") + " with old email id: "+ moResultSet.getStringValue("Users.Email"));
}
HashMap hm = new HashMap();
hm.put("Users.Email",emailid);
moUserUtility.updateUser(moResultSet, hm);
log("Email Id Changed Successfully to "+emailid);
}
catch(Exception e)
{
log("Change User Email Id Failed... Exception...." + e.getMessage());
rtnval="Error "+e.getMessage();
}
return rtnval;

}

/** Retrieves the administrators of an organization based on the organization name. */
public List getAdministratorsOfOrganization(String psOrganizationName){
Vector mvOrganizations=new Vector();
try{
tcOrganizationOperationsIntf moOrganizationUtility =(tcOrganizationOperationsIntf)ioUtilityFactory.getUtility("Thor.API.Operations.tcOrganizationOperationsIntf");
Hashtable mhSearchCriteria = new Hashtable();
mhSearchCriteria.put("Organizations.Organization Name", psOrganizationName);
tcResultSet moResultSet =moOrganizationUtility.findOrganizations(mhSearchCriteria);
tcResultSet moAdmins;
for (int i=0; i<moResultSet.getRowCount(); i++){
moResultSet.goToRow(i);
moAdmins=moOrganizationUtility.getAdministrators(moResultSet.getLongValue("Organizations.Key"));
mvOrganizations.add(moAdmins.getStringValue("Groups.Group Name"));
}
}
catch(Exception ex) { ex.printStackTrace();}
return mvOrganizations;
}

/**
* @param args
*
public static void main(String[] args) {
List moList;
UserOperations uo=new UserOperations();
moList=uo.getUserLogin("System");
uo.setEmailID("teste43", "rajnishbhatia19@hotmail.com");
System.exit(0);

}*/
}

================================
Results
================================

See attached OIM.jpg :

Wednesday, August 1, 2007

JUNIT Test Case for getting values from Oblix

============================
File: IDXMLUnitTest.java
============================

import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;


import junit.framework.TestCase;


public class IDXMLUnitTest extends TestCase {


public IDXMLUnitTest(String name) {
super(name);
}

/* (non-Javadoc)
* @see junit.framework.TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
}

/* (non-Javadoc)
* @see junit.framework.TestCase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
}

public void testSearch()
{
String url = "http://localhost:8081/identity/oblix/apps/userservcenter/bin/userservcenter.cgi?wsdl";
System.out.println(url);

String authen = "adminpassword";

String xml = ""+authen+"" +
"cn=RajnishBhatia,o=NetscapeRootmail" +
"
";

System.out.println("\n\n"+xml+"\n\n");


try
{

URL u = new URL(url);
System.out.println(" url .... " + url);
URLConnection c = u.openConnection();
HttpURLConnection conn = (HttpURLConnection) c;
conn.setRequestProperty("Content-Type", "text/xml");

conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
OutputStream out = conn.getOutputStream();
OutputStreamWriter wout = new OutputStreamWriter(out, "UTF-8");
wout.write(xml);
wout.flush();
out.close();

if(conn.getResponseCode() == 200) {
InputStream in = conn.getInputStream();
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document document = builder.parse(new InputSource(in));
Element rootElement = document.getDocumentElement();
NodeList parentNode = rootElement.getElementsByTagName("ObValue");
Node node = parentNode.item(0);
System.out.println(" ...value ... " +node.getNodeName() + " .... " + node.getFirstChild().getNodeValue());
in.close();
}else
{
System.out.println(" conn.getResponseCode()" + conn.getResponseCode()) ;
}

conn.disconnect();
}catch(Exception e)
{
System.out.println("Exception ......." + e.getMessage());
}
}
}
==================
Result
==================
rajnishbhatia19@hotmail.com

courtesy:Dilip Nimse

Thursday, July 26, 2007

Calling A Web Service in .NET without adding a web reference

If you ever need to reach a web service and use its output in your codewithout adding a web reference in your project, here is the code you can use:

I'm posting a code I used to connect to Netpoint Oblix CoreID Oracle Access Manager and retrieve values from it. This is a console app sample. You can use the same code in your web apps as well.
=====================================================
File Program.cs
=====================================================
using System;
using System.Xml;
namespace IDXML
{
class Program
{
private const string m_InFileName ="C:\\Projects\\IDXML\\IDXML\\InputXML.xml";
static void GetOblixData(string URL)
{
try
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(m_InFileName);
Byte[] bdata =System.Text.Encoding.ASCII.GetBytes(xDoc.OuterXml);
System.Net.WebClient wc = newSystem.Net.WebClient();
wc.Headers.Add("Content-Type","text/xml");
Byte[] bresp;
bresp = wc.UploadData(URL, bdata);
string resp =System.Text.Encoding.ASCII.GetString(bresp);
//Console.WriteLine(resp);
XmlDocument xresp = new XmlDocument();
xresp.LoadXml(resp);
XmlNodeList elems =xresp.GetElementsByTagName("ObValue");
XmlNode x = elems[0]; // Assuming One / Picking First - ... If more then just loop
Console.WriteLine(x.InnerText);
}
catch(Exception ex)
{

Console.WriteLine(ex.Message+""+ex.StackTrace);
}
}

static void Main(string[] args)
{
try
{

GetOblixData("http://localhost:8081/identity/oblix/apps/userservcenter/bin/userservcenter.cgi?wsdl");
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
}
}
}


=====================================================
File Input.xml (I used this file to provide input xml sothat we can change it. You can simply prepare an xml in your code or read from somewhere else).
=====================================================

<?xml version="1.0"?>
<SOAP-ENV:Envelopexmlns:SOAP-ENV="http://schemas-xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<oblix:authenticationxmlns:oblix="http:/www.oblix.com" type="basic">
<oblix:login>admin</oblix:login>
<oblix:password>password</oblix:password>
</oblix:authentication>
<oblix:request application="userservcenter"function="view" version="NPWSDL1.0"xmlns:oblix="http://www.oblix.com">
<oblix:params>
<oblix:uid>cn=RajnishBhatia,o=NetscapeRoot</oblix:uid>
<oblix:attrName>mail</oblix:attrName>
</oblix:params>
</oblix:request>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
=====================================================
Result
=====================================================
rajnishbhatia19@hotmail.com