Title: [2641] branches/v2_1/openejb2/modules/core/src/test/org/openejb/corba/util: GERONIMO-1616 Correct GSSUP encoding/decoding to append and remove @target
Revision
2641
Author
djencks
Date
2006-05-03 03:31:31 -0400 (Wed, 03 May 2006)

Log Message

GERONIMO-1616 Correct GSSUP encoding/decoding to append and remove @target

Modified Paths

Added Paths

Diff

Modified: branches/v2_1/openejb2/modules/core/src/java/org/openejb/corba/security/config/css/CSSGSSUPMechConfigDynamic.java (2640 => 2641)

--- branches/v2_1/openejb2/modules/core/src/java/org/openejb/corba/security/config/css/CSSGSSUPMechConfigDynamic.java	2006-05-03 04:43:22 UTC (rev 2640)
+++ branches/v2_1/openejb2/modules/core/src/java/org/openejb/corba/security/config/css/CSSGSSUPMechConfigDynamic.java	2006-05-03 07:31:31 UTC (rev 2641)
@@ -44,15 +44,13 @@
  */
 package org.openejb.corba.security.config.css;
 
-import java.security.AccessController;
-import java.security.PrivilegedAction;
 import java.util.Iterator;
 import java.util.Set;
+
 import javax.security.auth.Subject;
 
-import org.apache.geronimo.security.jaas.NamedUsernamePasswordCredential;
 import org.apache.geronimo.security.ContextManager;
-
+import org.apache.geronimo.security.jaas.NamedUsernamePasswordCredential;
 import org.openejb.corba.security.config.tss.TSSASMechConfig;
 import org.openejb.corba.security.config.tss.TSSGSSUPMechConfig;
 import org.openejb.corba.util.Util;

Modified: branches/v2_1/openejb2/modules/core/src/java/org/openejb/corba/util/Util.java (2640 => 2641)

--- branches/v2_1/openejb2/modules/core/src/java/org/openejb/corba/util/Util.java	2006-05-03 04:43:22 UTC (rev 2640)
+++ branches/v2_1/openejb2/modules/core/src/java/org/openejb/corba/util/Util.java	2006-05-03 07:31:31 UTC (rev 2641)
@@ -64,6 +64,7 @@
 import java.util.LinkedHashSet;
 import java.util.HashMap;
 import java.util.HashSet;
+
 import javax.ejb.spi.HandleDelegate;
 import javax.naming.InitialContext;
 import javax.naming.NamingException;
@@ -115,6 +116,7 @@
     private static Codec codec;
     private static HandleDelegate handleDelegate;
     private static CorbaApplicationServer corbaApplicationServer = new CorbaApplicationServer();
+
     public static ORB getORB() {
         assert orb != null;
         return orb;
@@ -330,7 +332,7 @@
 
             // create and encode a GSSUP initial context token
             InitialContextToken init_token = new InitialContextToken();
-            init_token.username = user.getBytes("UTF-8");
+            init_token.username = (user + "@" + target).getBytes("UTF-8");
 
             init_token.password = pwd.getBytes("UTF-8");
 
@@ -368,7 +370,7 @@
      * @return Return true when decoding was successful, false otherwise.
      */
     public static boolean decodeGSSUPToken(Codec codec, byte[] token_arr,
-                                           InitialContextToken gssup_tok) {
+            InitialContextToken gssup_tok) {
         boolean result = false;
         if (gssup_tok != null) {
             ByteArrayInputStream bais = new ByteArrayInputStream(token_arr);
@@ -400,8 +402,18 @@
                             if (token != null) {
                                 gssup_tok.username = token.username;
                                 gssup_tok.password = token.password;
-                                gssup_tok.target_name = decodeGSSExportName(token.target_name).getBytes("UTF-8");
+                                String targetName = decodeGSSExportName(token.target_name);
+                                gssup_tok.target_name = targetName.getBytes("UTF-8");
 
+                                String userName = new String(gssup_tok.username, "UTF-8");
+                                int end = userName.length() - targetName.length() - 1;
+                                if (userName.charAt(end) == '@') {
+                                    if (userName.endsWith(targetName)) {
+                                        userName = userName.substring(0, end);
+                                        gssup_tok.username = userName.getBytes("UTF-8");
+                                    }
+                                }
+
                                 result = true;
                             }
                         }
@@ -425,8 +437,8 @@
     }
 
     private static final char[] HEXCHAR = {
-        '0', '1', '2', '3', '4', '5', '6', '7',
-        '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
+            '0', '1', '2', '3', '4', '5', '6', '7',
+            '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
     };
 
     public static void writeObject(Class type, Object object, OutputStream out) {
@@ -448,7 +460,7 @@
             out.write_longlong(((Long) object).longValue());
         } else if (type == Short.TYPE) {
             out.write_short(((Short) object).shortValue());
-        }  else {
+        } else {
             // object types must bbe written in the context of the corba application server
             // which properly write replaces our objects for corba
             ApplicationServer oldApplicationServer = ServerFederation.getApplicationServer();
@@ -623,7 +635,8 @@
             String verb;
             if (methodName.startsWith("get") && methodName.length() > 3 && method.getReturnType() != void.class) {
                 verb = "get";
-            } else if (methodName.startsWith("is") && methodName.length() > 2 && method.getReturnType() == boolean.class) {
+            } else
+            if (methodName.startsWith("is") && methodName.length() > 2 && method.getReturnType() == boolean.class) {
                 verb = "is";
             } else {
                 continue;
@@ -646,7 +659,8 @@
             }
 
             String propertyName;
-            if (methodName.length() > verb.length() + 1 && Character.isUpperCase(methodName.charAt(verb.length() + 1))) {
+            if (methodName.length() > verb.length() + 1 && Character.isUpperCase(methodName.charAt(verb.length() + 1)))
+            {
                 propertyName = methodName.substring(verb.length());
             } else {
                 propertyName = Character.toLowerCase(methodName.charAt(verb.length())) + methodName.substring(verb.length() + 1);
@@ -822,7 +836,7 @@
 
     /**
      * Return the a string containing an underscore '_' index of each uppercase character in the iiop name.
-     *
+     * <p/>
      * This is used for distinction of names that only differ by case, since corba does not support case sensitive names.
      */
     private static String upperCaseIndexString(String iiopName) {
@@ -838,7 +852,7 @@
 
     /**
      * Replaces any occurnace of the specified "oldChar" with the nes string.
-     *
+     * <p/>
      * This is used to replace occurances if '$' in corba names since '$' is a special character
      */
     private static String replace(String source, char oldChar, String newString) {
@@ -856,12 +870,12 @@
 
     /**
      * Return the a string containing a double underscore '__' list of parameter types encoded using the Java to IDL rules.
-     *
+     * <p/>
      * This is used for distinction of methods that only differ by parameter lists.
      */
     private static String buildOverloadParameterString(Class[] parameterTypes) {
         String name = "";
-        if (parameterTypes.length ==0) {
+        if (parameterTypes.length == 0) {
             name += "__";
         } else {
             for (int i = 0; i < parameterTypes.length; i++) {
@@ -976,80 +990,80 @@
     private static final Set keywords;
 
     static {
-       specialTypeNames = new HashMap();
-       specialTypeNames.put("boolean", "boolean");
-       specialTypeNames.put("char", "wchar");
-       specialTypeNames.put("byte", "octet");
-       specialTypeNames.put("short", "short");
-       specialTypeNames.put("int", "long");
-       specialTypeNames.put("long", "long_long");
-       specialTypeNames.put("float", "float");
-       specialTypeNames.put("double", "double");
-       specialTypeNames.put("java.lang.Class", "ClassDesc");
-       specialTypeNames.put("java.lang.String", "WStringValue");
-       specialTypeNames.put("org.omg.CORBA.Object", "Object");
+        specialTypeNames = new HashMap();
+        specialTypeNames.put("boolean", "boolean");
+        specialTypeNames.put("char", "wchar");
+        specialTypeNames.put("byte", "octet");
+        specialTypeNames.put("short", "short");
+        specialTypeNames.put("int", "long");
+        specialTypeNames.put("long", "long_long");
+        specialTypeNames.put("float", "float");
+        specialTypeNames.put("double", "double");
+        specialTypeNames.put("java.lang.Class", "ClassDesc");
+        specialTypeNames.put("java.lang.String", "WStringValue");
+        specialTypeNames.put("org.omg.CORBA.Object", "Object");
 
-       specialTypePackages = new HashMap();
-       specialTypePackages.put("boolean", "");
-       specialTypePackages.put("char", "");
-       specialTypePackages.put("byte", "");
-       specialTypePackages.put("short", "");
-       specialTypePackages.put("int", "");
-       specialTypePackages.put("long", "");
-       specialTypePackages.put("float", "");
-       specialTypePackages.put("double", "");
-       specialTypePackages.put("java.lang.Class", "javax.rmi.CORBA");
-       specialTypePackages.put("java.lang.String", "CORBA");
-       specialTypePackages.put("org.omg.CORBA.Object", "");
+        specialTypePackages = new HashMap();
+        specialTypePackages.put("boolean", "");
+        specialTypePackages.put("char", "");
+        specialTypePackages.put("byte", "");
+        specialTypePackages.put("short", "");
+        specialTypePackages.put("int", "");
+        specialTypePackages.put("long", "");
+        specialTypePackages.put("float", "");
+        specialTypePackages.put("double", "");
+        specialTypePackages.put("java.lang.Class", "javax.rmi.CORBA");
+        specialTypePackages.put("java.lang.String", "CORBA");
+        specialTypePackages.put("org.omg.CORBA.Object", "");
 
-       keywords = new HashSet();
-       keywords.add("abstract");
-       keywords.add("any");
-       keywords.add("attribute");
-       keywords.add("boolean");
-       keywords.add("case");
-       keywords.add("char");
-       keywords.add("const");
-       keywords.add("context");
-       keywords.add("custom");
-       keywords.add("default");
-       keywords.add("double");
-       keywords.add("enum");
-       keywords.add("exception");
-       keywords.add("factory");
-       keywords.add("false");
-       keywords.add("fixed");
-       keywords.add("float");
-       keywords.add("in");
-       keywords.add("inout");
-       keywords.add("interface");
-       keywords.add("long");
-       keywords.add("module");
-       keywords.add("native");
-       keywords.add("object");
-       keywords.add("octet");
-       keywords.add("oneway");
-       keywords.add("out");
-       keywords.add("private");
-       keywords.add("public");
-       keywords.add("raises");
-       keywords.add("readonly");
-       keywords.add("sequence");
-       keywords.add("short");
-       keywords.add("string");
-       keywords.add("struct");
-       keywords.add("supports");
-       keywords.add("switch");
-       keywords.add("true");
-       keywords.add("truncatable");
-       keywords.add("typedef");
-       keywords.add("union");
-       keywords.add("unsigned");
-       keywords.add("valuebase");
-       keywords.add("valuetype");
-       keywords.add("void");
-       keywords.add("wchar");
-       keywords.add("wstring");
+        keywords = new HashSet();
+        keywords.add("abstract");
+        keywords.add("any");
+        keywords.add("attribute");
+        keywords.add("boolean");
+        keywords.add("case");
+        keywords.add("char");
+        keywords.add("const");
+        keywords.add("context");
+        keywords.add("custom");
+        keywords.add("default");
+        keywords.add("double");
+        keywords.add("enum");
+        keywords.add("exception");
+        keywords.add("factory");
+        keywords.add("false");
+        keywords.add("fixed");
+        keywords.add("float");
+        keywords.add("in");
+        keywords.add("inout");
+        keywords.add("interface");
+        keywords.add("long");
+        keywords.add("module");
+        keywords.add("native");
+        keywords.add("object");
+        keywords.add("octet");
+        keywords.add("oneway");
+        keywords.add("out");
+        keywords.add("private");
+        keywords.add("public");
+        keywords.add("raises");
+        keywords.add("readonly");
+        keywords.add("sequence");
+        keywords.add("short");
+        keywords.add("string");
+        keywords.add("struct");
+        keywords.add("supports");
+        keywords.add("switch");
+        keywords.add("true");
+        keywords.add("truncatable");
+        keywords.add("typedef");
+        keywords.add("union");
+        keywords.add("unsigned");
+        keywords.add("valuebase");
+        keywords.add("valuetype");
+        keywords.add("void");
+        keywords.add("wchar");
+        keywords.add("wstring");
     }
 
 }

Added: branches/v2_1/openejb2/modules/core/src/test/org/openejb/corba/util/UtilGSSUPTokenTest.java (0 => 2641)

--- branches/v2_1/openejb2/modules/core/src/test/org/openejb/corba/util/UtilGSSUPTokenTest.java	                        (rev 0)
+++ branches/v2_1/openejb2/modules/core/src/test/org/openejb/corba/util/UtilGSSUPTokenTest.java	2006-05-03 07:31:31 UTC (rev 2641)
@@ -0,0 +1,76 @@
+/**
+ *
+ * Copyright 2006 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package org.openejb.corba.util;
+
+import java.util.Properties;
+
+import junit.framework.TestCase;
+import org.omg.CORBA.ORB;
+import org.omg.IOP.Codec;
+import org.omg.GSSUP.InitialContextToken;
+
+/**
+ * @version $Rev:$ $Date:$
+ */
+public class UtilGSSUPTokenTest extends TestCase {
+
+    public void testGSSUPTokenEncoding() throws Exception {
+        // before we do anything make sure the sun orb is present
+        try {
+            getClass().getClassLoader().loadClass("com.sun.corba.se.internal.CosNaming.NSORB");
+        } catch (ClassNotFoundException e) {
+//            log.info("Sun orb is not present in this vm, so this test can't run");
+            return;
+        }
+        // create the ORB
+        Properties properties = new Properties();
+        properties.put("org.omg.CORBA.ORBInitialPort", "8050");
+        ORB orb = ORB.init(new String[0], properties);
+        try {
+            new Thread(new ORBRunner(orb), "ORBRunner").start();
+            Util.setORB(orb);
+            Codec codec = Util.getCodec();
+            byte[] tokenBytes = Util.encodeGSSUPToken(orb, codec, "user", "password", "target");
+            InitialContextToken token = new InitialContextToken();
+            if (!Util.decodeGSSUPToken(codec, tokenBytes, token)) {
+                fail("could not decode token bytes");
+            }
+            String userName = new String(token.username, "UTF-8");
+            String password = new String(token.password, "UTF-8");
+            String target = new String(token.target_name, "UTF-8");
+            assertEquals(userName, "user");
+            assertEquals(password, "password");
+            assertEquals(target, "target");
+        } finally {
+            orb.destroy();
+        }
+    }
+
+    private static final class ORBRunner implements Runnable {
+        private final ORB orb;
+
+        public ORBRunner(ORB orb) {
+            this.orb = orb;
+        }
+
+        public void run() {
+            orb.run();
+        }
+    }
+
+}

Reply via email to