oltu-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simonetrip...@apache.org
Subject svn commit: r1605452 - in /oltu/trunk/jose/jws/src: main/java/org/apache/oltu/jose/jws/signature/impl/ test/java/org/apache/oltu/jose/jws/signature/impl/
Date Wed, 25 Jun 2014 15:43:49 GMT
Author: simonetripodi
Date: Wed Jun 25 15:43:48 2014
New Revision: 1605452

URL: http://svn.apache.org/r1605452
Log:
trivial: code format

Modified:
    oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/signature/impl/SignatureMethodRSAImpl.java
    oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/signature/impl/SignatureMethodsHMAC256Impl.java
    oltu/trunk/jose/jws/src/test/java/org/apache/oltu/jose/jws/signature/impl/SignatureMethodRSAImplTest.java
    oltu/trunk/jose/jws/src/test/java/org/apache/oltu/jose/jws/signature/impl/SignatureMethodsHMAC256ImplTest.java

Modified: oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/signature/impl/SignatureMethodRSAImpl.java
URL: http://svn.apache.org/viewvc/oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/signature/impl/SignatureMethodRSAImpl.java?rev=1605452&r1=1605451&r2=1605452&view=diff
==============================================================================
--- oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/signature/impl/SignatureMethodRSAImpl.java
(original)
+++ oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/signature/impl/SignatureMethodRSAImpl.java
Wed Jun 25 15:43:48 2014
@@ -15,68 +15,60 @@
  * limitations under the License.
  */
 package org.apache.oltu.jose.jws.signature.impl;
- 
+
 import java.security.Signature;
 import org.apache.oltu.commons.encodedtoken.TokenDecoder;
 import org.apache.oltu.jose.jws.JwsConstants;
 import org.apache.oltu.jose.jws.signature.SignatureMethod;
 
 /**
- * Class that asymmetrically sign and verify the
- * issued token 
- * 
+ * Class that asymmetrically sign and verify the issued token.
  */
-public class SignatureMethodRSAImpl implements
-SignatureMethod<PrivateKey, PublicKey>{
-    
+public class SignatureMethodRSAImpl implements SignatureMethod<PrivateKey, PublicKey>{
+
     private String algorithm;
 
     public SignatureMethodRSAImpl(String algorithm) {
-        super();
         this.algorithm = algorithm;
     }
 
-
-    /*
-     * Calculate the signature of given header.payload as for 
-     * http://tools.ietf.org/html/draft-ietf-jose-json-web-signature-21#appendix-A.2.1
-     *  
+    /**
+     * Calculate the signature of given header.payload as for
+     * <a href="http://tools.ietf.org/html/draft-ietf-jose-json-web-signature-21#appendix-A.2.1">appendix-A.2.1</a>
+     *
+     * {@inheritDoc}
      */
     @Override
     public String calculate(String header, String payload, PrivateKey signingKey) {
-        StringBuilder sb = new StringBuilder();
-        sb.append(header).append(".").append(payload);
-        final String stringToSign = sb.toString();
-        byte[] bytes = stringToSign.getBytes();
+        byte[] token = toToken(header, payload);
         try {
             Signature signature = Signature.getInstance(getAlgorithmInternal());
-            
+
             signature.initSign(signingKey.getPrivateKey());
-            signature.update(bytes);
-            bytes = signature.sign();
-            
-            return TokenDecoder.base64Encode(bytes);
+            signature.update(token);
+            token = signature.sign();
+
+            return TokenDecoder.base64Encode(token);
         } catch (Exception e) {
             throw new RuntimeException(e);
-        }  
+        }
     }
 
-    /*
-     * Verify the signature of given header.payload as for 
-     * http://tools.ietf.org/html/draft-ietf-jose-json-web-signature-21#appendix-A.2.2
-     *  
+    /**
+     * Verify the signature of given header.payload as for
+     * <a href="http://tools.ietf.org/html/draft-ietf-jose-json-web-signature-21#appendix-A.2.2">appendix-A.2.2</a>
+     *
+     * {@inheritDoc}
      */
     @Override
-    public boolean verify(String signature, String header, String payload,
-            PublicKey verifyingKey) {
-        final String text = header + "." + payload;
+    public boolean verify(String signature, String header, String payload, PublicKey verifyingKey)
{
+        byte[] token = toToken(header, payload);
         try {
             Signature sign = Signature.getInstance(getAlgorithmInternal());
             sign.initVerify(verifyingKey.getPublicKey());
-            sign.update(text.getBytes());
- 
+            sign.update(token);
+
             return sign.verify(decode(signature));
-             
         } catch (Exception e) {
             return false;
         }
@@ -86,33 +78,53 @@ SignatureMethod<PrivateKey, PublicKey>{
     public String getAlgorithm() {
         return algorithm;
     }
-    
+
     // ---------- Private methods ---------------------------------------------
-    private String getAlgorithmInternal(){
+
+    private static byte[] toToken(String header, String payload) {
+        return new StringBuilder()
+               .append(header)
+               .append(".")
+               .append(payload)
+               .toString()
+               .getBytes();
+    }
+
+    private String getAlgorithmInternal() {
         String alg = null;
-        if (JwsConstants.RS256.equals(algorithm)) { 
-            alg = "SHA256withRSA"; 
-        } else if (JwsConstants.RS384.equals(algorithm)) { 
-            alg = "SHA384withRSA"; 
-        } else if (JwsConstants.RS512.equals(algorithm)) { 
-            alg = "SHA512withRSA"; 
+        if (JwsConstants.RS256.equals(algorithm)) {
+            alg = "SHA256withRSA";
+        } else if (JwsConstants.RS384.equals(algorithm)) {
+            alg = "SHA384withRSA";
+        } else if (JwsConstants.RS512.equals(algorithm)) {
+            alg = "SHA512withRSA";
         }
         return alg;
     }
-    
+
     private static byte[] decode(String arg) throws Exception {
         String s = arg;
         s = s.replace('-', '+'); // 62nd char of encoding
         s = s.replace('_', '/'); // 63rd char of encoding
+
         switch (s.length() % 4) // Pad with trailing '='s
         {
-          case 0: break; // No pad chars in this case
-          case 2: s += "=="; break; // Two pad chars
-          case 3: s += "="; break; // One pad char
-          default: throw new java.lang.Exception(
-            "Illegal base64url string!");
+          case 0: // No pad chars in this case
+              break;
+
+          case 2: // Two pad chars
+              s += "==";
+              break;
+
+          case 3: // One pad char
+              s += "=";
+              break;
+
+          default:
+              throw new Exception("Illegal base64url string!");
         }
+
         return TokenDecoder.base64DecodeToByte(s);
-      }
+    }
 
 }

Modified: oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/signature/impl/SignatureMethodsHMAC256Impl.java
URL: http://svn.apache.org/viewvc/oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/signature/impl/SignatureMethodsHMAC256Impl.java?rev=1605452&r1=1605451&r2=1605452&view=diff
==============================================================================
--- oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/signature/impl/SignatureMethodsHMAC256Impl.java
(original)
+++ oltu/trunk/jose/jws/src/main/java/org/apache/oltu/jose/jws/signature/impl/SignatureMethodsHMAC256Impl.java
Wed Jun 25 15:43:48 2014
@@ -23,21 +23,18 @@ import javax.crypto.spec.SecretKeySpec;
 import org.apache.oltu.commons.encodedtoken.TokenDecoder;
 import org.apache.oltu.jose.jws.signature.SignatureMethod;
 
-public class SignatureMethodsHMAC256Impl implements
-        SignatureMethod<SymmetricKeyImpl, SymmetricKeyImpl> {
+public class SignatureMethodsHMAC256Impl implements SignatureMethod<SymmetricKeyImpl,
SymmetricKeyImpl> {
 
     private static final String ALG = "HS256";
 
     @Override
-    public String calculate(String header, String payload,
-            SymmetricKeyImpl signingKey) {
-
+    public String calculate(String header, String payload, SymmetricKeyImpl signingKey) {
         StringBuilder sb = new StringBuilder();
         sb.append(header).append(".").append(payload);
         String stringToSign = sb.toString();
         byte[] bytes = stringToSign.getBytes();
-        try {
 
+        try {
             Mac mac = Mac.getInstance("HMACSHA256");
             mac.init(new SecretKeySpec(signingKey.getKey(), mac.getAlgorithm()));
             mac.update(bytes);
@@ -53,8 +50,7 @@ public class SignatureMethodsHMAC256Impl
     }
 
     @Override
-    public boolean verify(String signature, String header, String payload,
-            SymmetricKeyImpl verifyingKey) {
+    public boolean verify(String signature, String header, String payload, SymmetricKeyImpl
verifyingKey) {
         String signed = calculate(header, payload, verifyingKey);
         return signed.equals(signature);
     }

Modified: oltu/trunk/jose/jws/src/test/java/org/apache/oltu/jose/jws/signature/impl/SignatureMethodRSAImplTest.java
URL: http://svn.apache.org/viewvc/oltu/trunk/jose/jws/src/test/java/org/apache/oltu/jose/jws/signature/impl/SignatureMethodRSAImplTest.java?rev=1605452&r1=1605451&r2=1605452&view=diff
==============================================================================
--- oltu/trunk/jose/jws/src/test/java/org/apache/oltu/jose/jws/signature/impl/SignatureMethodRSAImplTest.java
(original)
+++ oltu/trunk/jose/jws/src/test/java/org/apache/oltu/jose/jws/signature/impl/SignatureMethodRSAImplTest.java
Wed Jun 25 15:43:48 2014
@@ -31,10 +31,10 @@ import org.junit.Test;
 
 
 /**
- * Unit test based on the example contained in 
+ * Unit test based on the example contained in
  * http://tools.ietf.org/html/draft-ietf-jose-json-web-signature-25#appendix-A.2 and
  * http://tools.ietf.org/html/draft-ietf-jose-cookbook-01#section-3.1
- * 
+ *
  */
 public class SignatureMethodRSAImplTest {
 
@@ -45,6 +45,7 @@ public class SignatureMethodRSAImplTest 
     private String payload;
 
     private RSAPrivateKey rsaPrivKey;
+
     private RSAPublicKey rsaPublicKey;
 
     @Before
@@ -64,7 +65,6 @@ public class SignatureMethodRSAImplTest 
     //validates the example in  http://tools.ietf.org/html/draft-ietf-jose-json-web-signature-25#appendix-A.2
     @Test
     public void testCalculate() throws Exception{
-        
         final byte[] n = { (byte) 161, (byte) 248, (byte) 22, (byte) 10, (byte) 226, (byte)
227, (byte) 201, (byte) 180,
                 (byte) 101, (byte) 206, (byte) 141, (byte) 45, (byte) 101, (byte) 98, (byte)
99, (byte) 54, (byte) 43,
                 (byte) 146, (byte) 125, (byte) 190, (byte) 41, (byte) 225, (byte) 240, (byte)
36, (byte) 119, (byte) 252,
@@ -137,7 +137,7 @@ public class SignatureMethodRSAImplTest 
 
         rsa256 = "{\"alg\":\"RS256\"}";
         payload = "{\"iss\":\"joe\",\r\n \"exp\":1300819380,\r\n \"http://example.com/is_root\":true}";
-        
+
         assertEquals("cC4hiUPoj9Eetdgtv3hF80EGrhuB__dzERat0XF9g2VtQgr9PJbu3XOiZj5RZmh7"+
                 "AAuHIm4Bh-0Qc_lF5YKt_O8W2Fp5jujGbds9uJdbF9CUAr7t1dnZcAcQjbKBYNX4"+
                 "BAynRFdiuB--f_nZLgrnbyTyWzO75vRK5h6xBArLIARNPvkSjtQBMHlb1L07Qe7K"+
@@ -151,7 +151,6 @@ public class SignatureMethodRSAImplTest 
     //validates the example in  http://tools.ietf.org/html/draft-ietf-jose-json-web-signature-25#appendix-A.2
     @Test
     public void testVerify() throws Exception{
-        
         final byte[] n = { (byte) 161, (byte) 248, (byte) 22, (byte) 10, (byte) 226, (byte)
227, (byte) 201, (byte) 180,
                 (byte) 101, (byte) 206, (byte) 141, (byte) 45, (byte) 101, (byte) 98, (byte)
99, (byte) 54, (byte) 43,
                 (byte) 146, (byte) 125, (byte) 190, (byte) 41, (byte) 225, (byte) 240, (byte)
36, (byte) 119, (byte) 252,
@@ -221,12 +220,12 @@ public class SignatureMethodRSAImplTest 
         RSAPrivateKeySpec privKeySpec = new RSAPrivateKeySpec(N, D);
         rsaPublicKey = (RSAPublicKey) keyFactory.generatePublic(pubKeySpec);
         rsaPrivKey = (RSAPrivateKey) keyFactory.generatePrivate(privKeySpec);
-        
+
         String accessToken = "eyJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFtcGxlLmNvbS9pc19yb290Ijp0cnVlfQ.cC4hiUPoj9Eetdgtv3hF80EGrhuB__dzERat0XF9g2VtQgr9PJbu3XOiZj5RZmh7AAuHIm4Bh-0Qc_lF5YKt_O8W2Fp5jujGbds9uJdbF9CUAr7t1dnZcAcQjbKBYNX4BAynRFdiuB--f_nZLgrnbyTyWzO75vRK5h6xBArLIARNPvkSjtQBMHlb1L07Qe7K0GarZRmB_eSN9383LcOLn6_dO--xi12jzDwusC-eOkHWEsqtFZESc6BfI7noOPqvhJ1phCnvWh6IeYI2w9QOYEUipUTI8np6LbgGY9Fs98rqVt5AXLIhWkWywlVmtVrBp0igcN_IoypGlUPQGe77Rw";
         String jwt[] = accessToken.split("\\.");
         assertTrue(sRsaImpl.verify(jwt[2], jwt[0], jwt[1], new PublicKey(rsaPublicKey)));
     }
-    
+
     //validates the example in http://tools.ietf.org/html/draft-ietf-jose-cookbook-01#section-3.1
     @Test
     public void testCalculateCookbook() throws Exception{
@@ -259,7 +258,7 @@ public class SignatureMethodRSAImplTest 
                 "b24ndCBrZWVwIHlvdXIgZmVldCwgdGhlcmXigJlzIG5vIGtub3dpbmcgd2hlcm"+
                 "UgeW91IG1pZ2h0IGJlIHN3ZXB0IG9mZiB0by4", new PrivateKey(rsaPrivKey)));
     }
-    
+
     //validates the example in http://tools.ietf.org/html/draft-ietf-jose-cookbook-01#section-3.1
     @Test
     public void testVerifyCookbook() throws Exception{
@@ -275,18 +274,18 @@ public class SignatureMethodRSAImplTest 
         RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(N, E);
         RSAPrivateKeySpec privKeySpec = new RSAPrivateKeySpec(N, D);
         rsaPublicKey = (RSAPublicKey) keyFactory.generatePublic(pubKeySpec);
-        rsaPrivKey = (RSAPrivateKey) keyFactory.generatePrivate(privKeySpec);       
-        
-        String accessToken = "eyJhbGciOiJSUzI1NiIsImtpZCI6ImJpbGJvLmJhZ2dpbnNAaG9iYml0b24uZXhhbXBsZSJ9."
+ 
-                "SXTigJlzIGEgZGFuZ2Vyb3VzIGJ1c2luZXNzLCBGcm9kbywgZ29pbmcgb3V0IH" + 
-                "lvdXIgZG9vci4gWW91IHN0ZXAgb250byB0aGUgcm9hZCwgYW5kIGlmIHlvdSBk" + 
-                "b24ndCBrZWVwIHlvdXIgZmVldCwgdGhlcmXigJlzIG5vIGtub3dpbmcgd2hlcm" + 
-                "UgeW91IG1pZ2h0IGJlIHN3ZXB0IG9mZiB0by4." + 
-                "MRjdkly7_-oTPTS3AXP41iQIGKa80A0ZmTuV5MEaHoxnW2e5CZ5NlKtainoFmK" + 
-                "ZopdHM1O2U4mwzJdQx996ivp83xuglII7PNDi84wnB-BDkoBwA78185hX-Es4J" + 
-                "IwmDLJK3lfWRa-XtL0RnltuYv746iYTh_qHRD68BNt1uSNCrUCTJDt5aAE6x8w" + 
-                "W1Kt9eRo4QPocSadnHXFxnt8Is9UzpERV0ePPQdLuW3IS_de3xyIrDaLGdjluP" + 
-                "xUAhb6L2aXic1U12podGU0KLUQSE_oI-ZnmKJ3F4uOZDnd6QZWJushZ41Axf_f" + 
+        rsaPrivKey = (RSAPrivateKey) keyFactory.generatePrivate(privKeySpec);
+
+        String accessToken = "eyJhbGciOiJSUzI1NiIsImtpZCI6ImJpbGJvLmJhZ2dpbnNAaG9iYml0b24uZXhhbXBsZSJ9."
+
+                "SXTigJlzIGEgZGFuZ2Vyb3VzIGJ1c2luZXNzLCBGcm9kbywgZ29pbmcgb3V0IH" +
+                "lvdXIgZG9vci4gWW91IHN0ZXAgb250byB0aGUgcm9hZCwgYW5kIGlmIHlvdSBk" +
+                "b24ndCBrZWVwIHlvdXIgZmVldCwgdGhlcmXigJlzIG5vIGtub3dpbmcgd2hlcm" +
+                "UgeW91IG1pZ2h0IGJlIHN3ZXB0IG9mZiB0by4." +
+                "MRjdkly7_-oTPTS3AXP41iQIGKa80A0ZmTuV5MEaHoxnW2e5CZ5NlKtainoFmK" +
+                "ZopdHM1O2U4mwzJdQx996ivp83xuglII7PNDi84wnB-BDkoBwA78185hX-Es4J" +
+                "IwmDLJK3lfWRa-XtL0RnltuYv746iYTh_qHRD68BNt1uSNCrUCTJDt5aAE6x8w" +
+                "W1Kt9eRo4QPocSadnHXFxnt8Is9UzpERV0ePPQdLuW3IS_de3xyIrDaLGdjluP" +
+                "xUAhb6L2aXic1U12podGU0KLUQSE_oI-ZnmKJ3F4uOZDnd6QZWJushZ41Axf_f" +
                 "cIe8u9ipH84ogoree7vjbU5y18kDquDg";
         String jwt[] = accessToken.split("\\.");
         assertTrue(sRsaImpl.verify(jwt[2], jwt[0], jwt[1], new PublicKey(rsaPublicKey)));

Modified: oltu/trunk/jose/jws/src/test/java/org/apache/oltu/jose/jws/signature/impl/SignatureMethodsHMAC256ImplTest.java
URL: http://svn.apache.org/viewvc/oltu/trunk/jose/jws/src/test/java/org/apache/oltu/jose/jws/signature/impl/SignatureMethodsHMAC256ImplTest.java?rev=1605452&r1=1605451&r2=1605452&view=diff
==============================================================================
--- oltu/trunk/jose/jws/src/test/java/org/apache/oltu/jose/jws/signature/impl/SignatureMethodsHMAC256ImplTest.java
(original)
+++ oltu/trunk/jose/jws/src/test/java/org/apache/oltu/jose/jws/signature/impl/SignatureMethodsHMAC256ImplTest.java
Wed Jun 25 15:43:48 2014
@@ -47,6 +47,7 @@ public class SignatureMethodsHMAC256Impl
     private String payload;
 
     private SymmetricKeyImpl key;
+
     private SignatureMethodsHMAC256Impl sHmacImpl;
 
     @Before



Mime
View raw message