Author: prabath
Date: Wed Jan 23 22:22:09 2008
New Revision: 12803

Log:

added OpenID processing logic

Modified:
   
trunk/solutions/identity/modules/token-verifier-core/src/main/java/org/wso2/solutions/identity/openid/relyingparty/OpenIDConsumer.java

Modified: 
trunk/solutions/identity/modules/token-verifier-core/src/main/java/org/wso2/solutions/identity/openid/relyingparty/OpenIDConsumer.java
==============================================================================
--- 
trunk/solutions/identity/modules/token-verifier-core/src/main/java/org/wso2/solutions/identity/openid/relyingparty/OpenIDConsumer.java
      (original)
+++ 
trunk/solutions/identity/modules/token-verifier-core/src/main/java/org/wso2/solutions/identity/openid/relyingparty/OpenIDConsumer.java
      Wed Jan 23 22:22:09 2008
@@ -1,11 +1,10 @@
 package org.wso2.solutions.identity.openid.relyingparty;
 
 import java.io.IOException;
-import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
-import java.util.Map.Entry;
+
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
@@ -27,7 +26,6 @@
 import org.openid4java.message.ParameterList;
 import org.openid4java.message.ax.AxMessage;
 import org.openid4java.message.ax.FetchResponse;
-import org.openid4java.message.sreg.SRegMessage;
 import org.openid4java.message.sreg.SRegRequest;
 import org.openid4java.message.sreg.SRegResponse;
 import org.wso2.solutions.identity.IdentityConstants;
@@ -36,41 +34,6 @@
 
     private ConsumerManager manager;
     private static OpenIDConsumer consumer;
-    private static HashMap<String, String> attributes = new HashMap<String, 
String>();
-
-    /**
-     * 
-     */
-    static {
-
-        attributes.put(IdentityConstants.OpenId.ExchangeAttributes.EMAIL_NS,
-                IdentityConstants.OpenId.ExchangeAttributes.EMAIL);
-        attributes.put(
-                IdentityConstants.OpenId.ExchangeAttributes.GIVEN_NAME_NS,
-                IdentityConstants.OpenId.ExchangeAttributes.GIVEN_NAME);
-        attributes.put(IdentityConstants.OpenId.ExchangeAttributes.SURNAME_NS,
-                IdentityConstants.OpenId.ExchangeAttributes.SURNAME);
-        attributes.put(
-                IdentityConstants.OpenId.ExchangeAttributes.HOME_PHONE_NS,
-                IdentityConstants.OpenId.ExchangeAttributes.HOME_PHONE);
-        attributes.put(IdentityConstants.OpenId.ExchangeAttributes.WORK_PHONE,
-                IdentityConstants.OpenId.ExchangeAttributes.WORK_PHONE_NS);
-        attributes.put(
-                IdentityConstants.OpenId.ExchangeAttributes.MOBILE_PHONE,
-                IdentityConstants.OpenId.ExchangeAttributes.MOBILE_PHONE_NS);
-        attributes.put(
-                IdentityConstants.OpenId.ExchangeAttributes.STREET_ADDRESS_NS,
-                IdentityConstants.OpenId.ExchangeAttributes.STREET_ADDRESS);
-        attributes.put(IdentityConstants.OpenId.ExchangeAttributes.CITY_NS,
-                IdentityConstants.OpenId.ExchangeAttributes.CITY);
-        attributes.put(
-                IdentityConstants.OpenId.ExchangeAttributes.POSTAL_CODE_NS,
-                IdentityConstants.OpenId.ExchangeAttributes.POSTAL_CODE);
-        attributes.put(IdentityConstants.OpenId.ExchangeAttributes.COUNTRY_NS,
-                IdentityConstants.OpenId.ExchangeAttributes.COUNTRY);
-        attributes.put(IdentityConstants.OpenId.ExchangeAttributes.STATE_NS,
-                IdentityConstants.OpenId.ExchangeAttributes.STATE);
-    }
 
     /**
      * 
@@ -110,7 +73,7 @@
      * @return
      * @throws IOException
      */
-    public String authRequest(String userSuppliedString, String returnToUrl,
+    protected String authRequest(String userSuppliedString, String returnToUrl,
             HttpServletRequest httpReq, HttpServletResponse httpResp)
             throws IOException {
 
@@ -212,7 +175,8 @@
             authSuccess = (AuthSuccess) verification.getAuthResponse();
 
             session = request.getSession(true);
-            session.setAttribute(IdentityConstants.OpenId.OPENID_IDENTIFIER,
+
+            request.setAttribute(IdentityConstants.OpenId.OPENID_IDENTIFIER,
                     authSuccess.getIdentity());
 
             // OpenID Attribute Exchange 1.0 - Draft 07.
@@ -285,6 +249,30 @@
     /**
      * 
      * @param request
+     */
+    public void doOpenIDAuthentication(OpenIDAuthenticationRequest request) {
+
+        String returnUrl = null;
+
+        try {
+
+            if (request.getReturnurl() != null)
+                returnUrl = request.getReturnurl();
+            else
+                returnUrl = (String) request.getRequest().getParameter(
+                        "returnUrl");
+
+            consumer = OpenIDConsumer.getInstance();
+            consumer.authRequest(request.getOpenIDUrl(), returnUrl, request
+                    .getRequest(), request.getReponse());
+        } catch (Exception e) {
+
+        }
+    }
+
+    /**
+     * 
+     * @param request
      * @throws InfocardException
      * @throws OpenIDException
      */
@@ -347,6 +335,42 @@
     /**
      * 
      * @param request
+     * @return
+     */
+    public static boolean isOpenIDAuthetication(HttpServletRequest request) {
+
+        ParameterList response = null;
+        Map map = null;
+
+        try {
+            map = request.getParameterMap();
+         
+            if (map == null)
+                return false;
+
+            response = new ParameterList(map);
+
+            if (response.hasParameter("openid.mode")) {
+                String mode = null;
+                mode = response.getParameterValue("openid.mode");
+
+                if (IdentityConstants.OpenId.OPENOD_RESPONSE.equals(mode)) {
+                    return true;
+                } else {
+                    return false;
+                }
+            } else {
+                return false;
+            }
+
+        } catch (Exception e) {
+            return false;
+        }
+    }
+
+    /**
+     * 
+     * @param request
      * @param session
      * @param openidResp
      * @throws OpenIDException
@@ -355,29 +379,38 @@
             HttpSession session, ParameterList openidResp)
             throws OpenIDException {
 
+        DiscoveryInformation discovered = null;
+        StringBuffer receivingURL = null;
+        String queryString = null;
+        VerificationResult verification = null;
+        Identifier verified = null;
+
         // Retrieve the previously stored discovery information
-        DiscoveryInformation discovered = (DiscoveryInformation) session
-                .getAttribute("discovered");
+        discovered = (DiscoveryInformation) session.getAttribute("discovered");
 
-        StringBuffer receivingURL = new StringBuffer(openidResp
-                
.getParameterValue(IdentityConstants.OpenId.ATTR_RETURN_TO));// 
request.getRequestURL());
-        String queryString = request.getQueryString();
+        receivingURL = new StringBuffer(openidResp
+                .getParameterValue(IdentityConstants.OpenId.ATTR_RETURN_TO));
+        queryString = request.getQueryString();
 
         if (queryString != null && queryString.length() > 0)
             receivingURL.append("?").append(request.getQueryString());
 
         // Verify the response
-        VerificationResult verification = manager.verify(receivingURL
-                .toString(), openidResp, discovered);
+        verification = manager.verify(receivingURL.toString(), openidResp,
+                discovered);
 
         // Examine the verification result and extract the verified
         // identifier
-        Identifier verified = verification.getVerifiedId();
+        verified = verification.getVerifiedId();
 
         if (verified != null) {
 
             Message authResponse = verification.getAuthResponse();
 
+            request.setAttribute(IdentityConstants.OpenId.OPENID_IDENTIFIER,
+                    openidResp.getParameter(
+                            
IdentityConstants.OpenId.ATTR_IDENTITY).getValue());
+
             if (authResponse instanceof AuthSuccess) {
 
                 AuthSuccess authSuccess = null;
@@ -422,12 +455,12 @@
                     iterator = attributes.keySet().iterator();
 
                     while (iterator.hasNext()) {
-                        key = (String)iterator.next();
-                        
request.setAttribute(key.toLowerCase(),attributes.get(key));
+                        key = (String) iterator.next();
+                        request.setAttribute(key.toLowerCase(), attributes
+                                .get(key));
                     }
                 }
             }
         }
-
     }
 }

_______________________________________________
Identity-dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/identity-dev

Reply via email to