Getting username and organization for a logged inn user.

Hi.

Im struggling to find out how I can get username, organization, given name, etc for the logged in user. Id like to do this from both a portlet and a provider. The userInfo portlet seams to do this, but I cant figure out how it is done.

Any one who has some example code?

Best regards,

Kristian

[337 byte] By [kristian_groenli] at [2007-11-25 21:04:13]
# 1

Hi!

You need the HttpServletRequest or the tokenid, here is some sample sourcecode:

SSOTokenManager manager = SSOTokenManager.getInstance();

SSOToken token = manager.createSSOToken(request);

if (manager.isValidToken(token))

{

/* get user dn */

String userDN = token.getPrincipal().getName();

String userName = userDN.substring(userDN.indexOf("=")+1,userDN.indexOf(","));

// And so on

To get more information use AMStoreConnection to get the AMUser Object:

AMStoreConnection amsc = new AMStoreConnection(ssotoken);

if (amsc.doesEntryExist(uDN)) {

AMUser user = amsc.getUser(uDN);

// From user you can get any attributes you need...

}

Here is the documentation:

http://docs.sun.com/source/817-7651/overview-summary.html

hth Chris

CRabel at 2007-7-4 20:31:34 > top of Java-index,Web & Directory Servers,Portal Servers...
# 2

Hi Chris.

Thanks for quick response.

Ive tried using the SSOTokenManager, but I get two quite different problems I cant figure out.

If you know about a fix for any of the following I'd be very happy.

1. Using SSOTokenManager from a portlet: a portles uses javax.portlet.RenderRequest, not javax.servlet.http.HttpServletRequest. I cant use the portlet.RenderRequest as a parameter in createSSOToken(request);

2. Using SSOTokenManager from a provider: Seams to work, but when I try package the provider (I use Sun Java Studio Enterprice 8) I get errors:

Error In Portal Server Test Harness

Exception: javax.servlet.ServletException

_

Message

com/iplanet/sso/SSOTokenManager

%>

_

Backtrace

javax.servlet.ServletException: com/iplanet/sso/SSOTokenManager

at org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:830)

at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:763)

at org.apache.jsp.desktop.default_.harness.PSPackageTarget_jsp._jspService(PSPackageTarget_jsp.java:474)

at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:105)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:860)

at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:336)

at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)

at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:251)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:860)

at sun.reflect.GeneratedMethodAccessor61.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:585)

at org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:249)

at java.security.AccessController.doPrivileged(Native Method)

at javax.security.auth.Subject.doAsPrivileged(Subject.java:517)

at org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:282)

at org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:165)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:257)

at org.apache.catalina.core.ApplicationFilterChain.access$000(ApplicationFilterChain.java:55)

at org.apache.catalina.core.ApplicationFilterChain$1.run(ApplicationFilterChain.java:161)

at java.security.AccessController.doPrivileged(Native Method)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)

at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:723)

at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:599)

at org.apache.catalina.core.ApplicationDispatcher.access$100(ApplicationDispatcher.java:80)

at org.apache.catalina.core.ApplicationDispatcher$PrivilegedInclude.run(ApplicationDispatcher.java:111)

at java.security.AccessController.doPrivileged(Native Method)

at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:509)

at org.apache.jsp.PSPackage_jsp._jspService(PSPackage_jsp.java:53)

at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:105)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:860)

at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:336)

at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)

at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:251)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:860)

at sun.reflect.GeneratedMethodAccessor61.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:585)

at org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:249)

at java.security.AccessController.doPrivileged(Native Method)

at javax.security.auth.Subject.doAsPrivileged(Subject.java:517)

at org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:282)

at org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:165)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:257)

at org.apache.catalina.core.ApplicationFilterChain.access$000(ApplicationFilterChain.java:55)

at org.apache.catalina.core.ApplicationFilterChain$1.run(ApplicationFilterChain.java:161)

at java.security.AccessController.doPrivileged(Native Method)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)

at com.sun.tools.ide.portletbuilder.harness.filters.EncodeFilter.doFilter(EncodeFilter.java:27)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:210)

at org.apache.catalina.core.ApplicationFilterChain.access$000(ApplicationFilterChain.java:55)

at org.apache.catalina.core.ApplicationFilterChain$1.run(ApplicationFilterChain.java:161)

at java.security.AccessController.doPrivileged(Native Method)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:263)

at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:551)

at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:225)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:173)

at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:551)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)

at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:551)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:132)

at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:551)

at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:933)

at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:185)

at com.sun.enterprise.web.connector.grizzly.ProcessorTask.process(ProcessorTask.java:653)

at com.sun.enterprise.web.connector.grizzly.ProcessorTask.process(ProcessorTask.java:534)

at com.sun.enterprise.web.connector.grizzly.ProcessorTask.doTask(ProcessorTask.java:403)

at com.sun.enterprise.web.connector.grizzly.WorkerThread.run(WorkerThread.java:55)

kristian_groenli at 2007-7-4 20:31:34 > top of Java-index,Web & Directory Servers,Portal Servers...
# 3

Hi!

I'm not really an expert on portlets but I would try the following:

Map attributes =

portletRequest.getAttribute(PortletRequest.USER_INFO);

Should give you a lot of data.

Also, RenderRequest gives you the username with this code:

String userDN = request.getUserPrincipal().getName();

I am not sure how you can get an SSOToken, but maybe the following works:

String tokenid = request.getRequestedSessionId();

SSOToken token = manager.createSSOToken(tokenid);

hth

Chris

CRabel at 2007-7-4 20:31:34 > top of Java-index,Web & Directory Servers,Portal Servers...
# 4

It should not be necessary to use the SSO Token. Using the SSO Token will make your portlet code non-portable. Instead, use the UserMapping file to map ldap attributes. There is an example file in /opt/SUNWps/samples/portlet/userInfoMapping.properties. Import this file with the pdeploy command. For details see http://docs.sun.com/source/819-4159/cli-ch2.html#wp21169

PAPI Providers have access to the ldap attributes by using the ProfileProviderAdapter.getStringAttribute() method. Call this method with the name of the LDAP attribute that you are looking for.

- Jim

jimfaut at 2007-7-4 20:31:34 > top of Java-index,Web & Directory Servers,Portal Servers...
# 5

Thanks a lot.

Im slowly getting closer (See code at the end of message.)

Ive picked up the info for the user from my LDAP client (uid, lname, etc).

Viewing user information is ok, but in LDAP under uid=Kristian I cant find any specific information about the organization.

(From an LDAP string uid=Kristian,ou=people,o=Kristian,dc=my,dc=org I would like to print out o=Kristian)

Im also having trouble finding out how I can change, ex last name (givenname or lname)?

Hope you can help me some more.

Portlet doView

Map userMap = (Map)request.getAttribute(PortletRequest.USER_INFO);

String lname = (String)userMap.get("lname");

String fname = (String)userMap.get("fname");

String userid = (String)userMap.get("userid");

userInfo.properties

uid=userid

givenname=fname

sn=lname

Best regards

Kristian

kristian_groenli at 2007-7-4 20:31:34 > top of Java-index,Web & Directory Servers,Portal Servers...