Author: rfeng
Date: Fri Mar 14 11:14:24 2008
New Revision: 637194

URL: http://svn.apache.org/viewvc?rev=637194&view=rev
Log:
Adjust the calculator-implementation-policies sample

Modified:
    
incubator/tuscany/java/sca/samples/calculator-implementation-policies/src/main/java/calculator/security/JaasLoginModule.java
    
incubator/tuscany/java/sca/samples/calculator-implementation-policies/src/main/resources/Calculator.composite
    
incubator/tuscany/java/sca/samples/calculator-implementation-policies/src/main/resources/definitions.xml

Modified: 
incubator/tuscany/java/sca/samples/calculator-implementation-policies/src/main/java/calculator/security/JaasLoginModule.java
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/samples/calculator-implementation-policies/src/main/java/calculator/security/JaasLoginModule.java?rev=637194&r1=637193&r2=637194&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/samples/calculator-implementation-policies/src/main/java/calculator/security/JaasLoginModule.java
 (original)
+++ 
incubator/tuscany/java/sca/samples/calculator-implementation-policies/src/main/java/calculator/security/JaasLoginModule.java
 Fri Mar 14 11:14:24 2008
@@ -19,6 +19,7 @@
 
 package calculator.security;
 
+import java.security.Principal;
 import java.util.Map;
 
 import javax.security.auth.Subject;
@@ -34,17 +35,13 @@
  */
 public class JaasLoginModule implements LoginModule {
 
-    private CallbackHandler callbackHandler = null;
-    private Subject subject = null;
-
-    public boolean abort() throws LoginException {
-        return true;
-    }
-
-    
-    public boolean commit() throws LoginException {
-        return true;
-    }
+    private CallbackHandler callbackHandler;
+    private Subject subject;
+    private Principal userPrincipal;
+    private String userId;
+    private String password;
+    private boolean succeeded;
+    private boolean commitSucceeded;
 
     public void initialize(Subject subject,
                            CallbackHandler callbackHandler,
@@ -58,26 +55,123 @@
         Callback[] callbacks = new Callback[2];
         callbacks[0] = new NameCallback("UserId:");
         callbacks[1] = new PasswordCallback("Password:", false);
-        
+
         try {
             callbackHandler.handle(callbacks);
-            String userId = ((NameCallback)callbacks[0]).getName();
-            String password = new 
String(((PasswordCallback)callbacks[1]).getPassword());
-            
-            if ( userId.equals("CalculatorUser") && 
password.equals("CalculatorUserPasswd")) {
+            userId = ((NameCallback)callbacks[0]).getName();
+            password = new 
String(((PasswordCallback)callbacks[1]).getPassword());
+
+            if (userId.equals("CalculatorUser") && 
password.equals("CalculatorUserPasswd")) {
                 System.out.println("Successfully AUTHENTICATED!!");
+                succeeded = true;
                 return true;
             } else {
-                 System.out.println("Incorrect userId / password! 
AUTHENTICATION FAILED!!");
+                System.out.println("Incorrect userId / password! 
AUTHENTICATION FAILED!!");
                 return false;
             }
-        } catch ( Exception e ) {
+        } catch (Exception e) {
             e.printStackTrace();
             return false;
         }
     }
 
+    /**
+     * <p> This method is called if the LoginContext's
+     * overall authentication succeeded
+     * (the relevant REQUIRED, REQUISITE, SUFFICIENT and OPTIONAL LoginModules
+     * succeeded).
+     *
+     * <p> If this LoginModule's own authentication attempt
+     * succeeded (checked by retrieving the private state saved by the
+     * <code>login</code> method), then this method associates a
+     * <code>UserPrincipal</code>
+     * with the <code>Subject</code> located in the
+     * <code>LoginModule</code>.  If this LoginModule's own
+     * authentication attempted failed, then this method removes
+     * any state that was originally saved.
+     *
+     * <p>
+     *
+     * @exception LoginException if the commit fails.
+     *
+     * @return true if this LoginModule's own login and commit
+     *          attempts succeeded, or false otherwise.
+     */
+    public boolean commit() throws LoginException {
+        if (succeeded == false) {
+            return false;
+        } else {
+            // add a Principal (authenticated identity) to the Subject
+
+            // assume the user we authenticated is the UserPrincipal
+            userPrincipal = new UserPrincipal(userId);
+            if (!subject.getPrincipals().contains(userPrincipal))
+                subject.getPrincipals().add(userPrincipal);
+
+            // in any case, clean out state
+            userId = null;
+            password = null;
+            commitSucceeded = true;
+            return true;
+        }
+    }
+
+    /**
+     * <p> This method is called if the LoginContext's
+     * overall authentication failed.
+     * (the relevant REQUIRED, REQUISITE, SUFFICIENT and OPTIONAL LoginModules
+     * did not succeed).
+     *
+     * <p> If this LoginModule's own authentication attempt
+     * succeeded (checked by retrieving the private state saved by the
+     * <code>login</code> and <code>commit</code> methods),
+     * then this method cleans up any state that was originally saved.
+     *
+     * <p>
+     *
+     * @exception LoginException if the abort fails.
+     *
+     * @return false if this LoginModule's own login and/or commit attempts
+     *          failed, and true otherwise.
+     */
+    public boolean abort() throws LoginException {
+        if (succeeded == false) {
+            return false;
+        } else if (succeeded == true && commitSucceeded == false) {
+            // login succeeded but overall authentication failed
+            succeeded = false;
+            userId = null;
+            password = null;
+            userPrincipal = null;
+        } else {
+            // overall authentication succeeded and commit succeeded,
+            // but someone else's commit failed
+            logout();
+        }
+        return true;
+    }
+
+    /**
+     * Logout the user.
+     *
+     * <p> This method removes the <code>SimplePrincipal</code>
+     * that was added by the <code>commit</code> method.
+     *
+     * <p>
+     *
+     * @exception LoginException if the logout fails.
+     *
+     * @return true in all cases since this <code>LoginModule</code>
+     *          should not be ignored.
+     */
     public boolean logout() throws LoginException {
+        subject.getPrincipals().remove(userPrincipal);
+        succeeded = false;
+        succeeded = commitSucceeded;
+        userId = null;
+        if (password != null)
+            password = null;
+        userPrincipal = null;
         return true;
     }
 

Modified: 
incubator/tuscany/java/sca/samples/calculator-implementation-policies/src/main/resources/Calculator.composite
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/samples/calculator-implementation-policies/src/main/resources/Calculator.composite?rev=637194&r1=637193&r2=637194&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/samples/calculator-implementation-policies/src/main/resources/Calculator.composite
 (original)
+++ 
incubator/tuscany/java/sca/samples/calculator-implementation-policies/src/main/resources/Calculator.composite
 Fri Mar 14 11:14:24 2008
@@ -18,7 +18,6 @@
  * under the License.    
 -->
 <composite xmlns="http://www.osoa.org/xmlns/sca/1.0";
-                  xmlns:sca="http://www.osoa.org/xmlns/sca/1.0";
            targetNamespace="http://sample";
            xmlns:sample="http://sample";
            name="Calculator"

Modified: 
incubator/tuscany/java/sca/samples/calculator-implementation-policies/src/main/resources/definitions.xml
URL: 
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/samples/calculator-implementation-policies/src/main/resources/definitions.xml?rev=637194&r1=637193&r2=637194&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/samples/calculator-implementation-policies/src/main/resources/definitions.xml
 (original)
+++ 
incubator/tuscany/java/sca/samples/calculator-implementation-policies/src/main/resources/definitions.xml
 Fri Mar 14 11:14:24 2008
@@ -34,6 +34,7 @@
     <policySet name="JaasPolicy" provides="tuscany:jaasAuthentication" 
appliesTo="sca:implementation.java"
         xmlns="http://www.osoa.org/xmlns/sca/1.0";>
         <tuscany:jaasAuthentication>
+            <tuscany:configurationName>Calculator</tuscany:configurationName>
             
<tuscany:callbackHandler>calculator.security.CalculatorCallbackHandler</tuscany:callbackHandler>
         </tuscany:jaasAuthentication>
     </policySet>



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to