cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject cxf git commit: Adding a Jose type enum
Date Fri, 19 Jun 2015 09:41:41 GMT
Repository: cxf
Updated Branches:
  refs/heads/3.0.x-fixes c9c02834d -> d13484a09


Adding a Jose type enum


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/d13484a0
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/d13484a0
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/d13484a0

Branch: refs/heads/3.0.x-fixes
Commit: d13484a09bcd2593ea351d1e9fa299398797b440
Parents: c9c0283
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Fri Jun 19 10:40:14 2015 +0100
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Fri Jun 19 10:41:13 2015 +0100

----------------------------------------------------------------------
 .../cxf/rs/security/jose/JoseHeaders.java       | 17 +++++---
 .../apache/cxf/rs/security/jose/JoseType.java   | 43 ++++++++++++++++++++
 .../cxf/rs/security/jose/jwe/JweHeaders.java    |  5 ++-
 .../cxf/rs/security/jose/jws/JwsHeaders.java    |  9 +++-
 .../jose/jws/JwsCompactReaderWriterTest.java    | 16 ++++----
 5 files changed, 75 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/d13484a0/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/JoseHeaders.java
----------------------------------------------------------------------
diff --git a/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/JoseHeaders.java
b/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/JoseHeaders.java
index 9f787b5..66c7863 100644
--- a/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/JoseHeaders.java
+++ b/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/JoseHeaders.java
@@ -30,6 +30,10 @@ public class JoseHeaders extends JsonMapObject {
     public JoseHeaders() {
     }
     
+    public JoseHeaders(JoseType type) {
+        init(type);
+    }
+    
     public JoseHeaders(JoseHeaders headers) {
         this(headers.asMap());
     }
@@ -37,13 +41,16 @@ public class JoseHeaders extends JsonMapObject {
     public JoseHeaders(Map<String, Object> values) {
         super(values);
     }
-    
-    public void setType(String type) {
-        setHeader(JoseConstants.HEADER_TYPE, type);
+    private void init(JoseType type) {
+        setType(type);
+    }
+    public void setType(JoseType type) {
+        setHeader(JoseConstants.HEADER_TYPE, type.toString());
     }
     
-    public String getType() {
-        return (String)getHeader(JoseConstants.HEADER_TYPE);
+    public JoseType getType() {
+        Object prop = getHeader(JoseConstants.HEADER_TYPE);
+        return prop == null ? null : JoseType.getType(prop.toString());
     }
     
     public void setContentType(String type) {

http://git-wip-us.apache.org/repos/asf/cxf/blob/d13484a0/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/JoseType.java
----------------------------------------------------------------------
diff --git a/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/JoseType.java
b/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/JoseType.java
new file mode 100644
index 0000000..9d38332
--- /dev/null
+++ b/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/JoseType.java
@@ -0,0 +1,43 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.cxf.rs.security.jose;
+
+
+public enum JoseType {
+    JOSE(JoseConstants.TYPE_JOSE),
+    JOSE_JSON(JoseConstants.TYPE_JOSE_JSON),
+    JWT(JoseConstants.TYPE_JWT);
+    
+    private final String type;
+    private JoseType(String type) {
+        this.type = type;
+    }
+    public static JoseType getType(String type) {
+        if (type == null) {
+            return null;
+        } else if (JoseConstants.TYPE_JOSE_JSON.equals(type)) {
+            return JOSE_JSON;    
+        } else {
+            return valueOf(type);
+        }
+    }
+    public String toString() {
+        return type;
+    }
+}

http://git-wip-us.apache.org/repos/asf/cxf/blob/d13484a0/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jwe/JweHeaders.java
----------------------------------------------------------------------
diff --git a/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jwe/JweHeaders.java
b/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jwe/JweHeaders.java
index cda481b..b287df5 100644
--- a/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jwe/JweHeaders.java
+++ b/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jwe/JweHeaders.java
@@ -26,6 +26,7 @@ import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.rs.security.jose.JoseConstants;
 import org.apache.cxf.rs.security.jose.JoseHeaders;
 import org.apache.cxf.rs.security.jose.JoseHeadersReaderWriter;
+import org.apache.cxf.rs.security.jose.JoseType;
 import org.apache.cxf.rs.security.jose.jwa.ContentAlgorithm;
 import org.apache.cxf.rs.security.jose.jwa.KeyAlgorithm;
 
@@ -36,7 +37,9 @@ public class JweHeaders extends JoseHeaders {
     private JweHeaders protectedHeaders;
     public JweHeaders() {
     }
-    
+    public JweHeaders(JoseType type) {
+        super(type);
+    }
     public JweHeaders(JoseHeaders headers) {
         super(headers.asMap());
     }

http://git-wip-us.apache.org/repos/asf/cxf/blob/d13484a0/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jws/JwsHeaders.java
----------------------------------------------------------------------
diff --git a/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jws/JwsHeaders.java
b/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jws/JwsHeaders.java
index 6e19eaf..ea71ee7 100644
--- a/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jws/JwsHeaders.java
+++ b/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jws/JwsHeaders.java
@@ -21,12 +21,15 @@ package org.apache.cxf.rs.security.jose.jws;
 import java.util.Map;
 
 import org.apache.cxf.rs.security.jose.JoseHeaders;
+import org.apache.cxf.rs.security.jose.JoseType;
 import org.apache.cxf.rs.security.jose.jwa.SignatureAlgorithm;
 
 public class JwsHeaders extends JoseHeaders {
     public JwsHeaders() {
     }
-    
+    public JwsHeaders(JoseType type) {
+        super(type);
+    }
     public JwsHeaders(JoseHeaders headers) {
         super(headers.asMap());
     }
@@ -37,6 +40,10 @@ public class JwsHeaders extends JoseHeaders {
     public JwsHeaders(SignatureAlgorithm sigAlgo) {
         init(sigAlgo);
     }
+    public JwsHeaders(JoseType type, SignatureAlgorithm sigAlgo) {
+        super(type);
+        init(sigAlgo);
+    }
     private void init(SignatureAlgorithm sigAlgo) {
         setSignatureAlgorithm(sigAlgo);
     }

http://git-wip-us.apache.org/repos/asf/cxf/blob/d13484a0/rt/rs/security/jose/src/test/java/org/apache/cxf/rs/security/jose/jws/JwsCompactReaderWriterTest.java
----------------------------------------------------------------------
diff --git a/rt/rs/security/jose/src/test/java/org/apache/cxf/rs/security/jose/jws/JwsCompactReaderWriterTest.java
b/rt/rs/security/jose/src/test/java/org/apache/cxf/rs/security/jose/jws/JwsCompactReaderWriterTest.java
index c215e56..d2367b1 100644
--- a/rt/rs/security/jose/src/test/java/org/apache/cxf/rs/security/jose/jws/JwsCompactReaderWriterTest.java
+++ b/rt/rs/security/jose/src/test/java/org/apache/cxf/rs/security/jose/jws/JwsCompactReaderWriterTest.java
@@ -30,6 +30,7 @@ import java.util.Map;
 
 import org.apache.cxf.rs.security.jose.JoseConstants;
 import org.apache.cxf.rs.security.jose.JoseHeaders;
+import org.apache.cxf.rs.security.jose.JoseType;
 import org.apache.cxf.rs.security.jose.jwa.AlgorithmUtils;
 import org.apache.cxf.rs.security.jose.jwa.SignatureAlgorithm;
 import org.apache.cxf.rs.security.jose.jwk.JsonWebKey;
@@ -97,7 +98,7 @@ public class JwsCompactReaderWriterTest extends Assert {
     @Test
     public void testWriteJwsSignedByMacSpecExample() throws Exception {
         JoseHeaders headers = new JoseHeaders();
-        headers.setType(JoseConstants.TYPE_JWT);
+        headers.setType(JoseType.JWT);
         headers.setAlgorithm(SignatureAlgorithm.HS256.getJwaName());
         JwsCompactProducer jws = initSpecJwtTokenWriter(headers);
         jws.signWith(new HmacJwsSignatureProvider(ENCODED_MAC_KEY, SignatureAlgorithm.HS256));
@@ -108,8 +109,7 @@ public class JwsCompactReaderWriterTest extends Assert {
     
     @Test
     public void testWriteReadJwsUnsigned() throws Exception {
-        JoseHeaders headers = new JoseHeaders();
-        headers.setType(JoseConstants.TYPE_JWT);
+        JoseHeaders headers = new JoseHeaders(JoseType.JWT);
         headers.setAlgorithm(AlgorithmUtils.PLAIN_TEXT_ALGO);
         
         JwtClaims claims = new JwtClaims();
@@ -137,7 +137,7 @@ public class JwsCompactReaderWriterTest extends Assert {
                                                                         SignatureAlgorithm.HS256)));
         JwtToken token = jws.getJwtToken();
         JoseHeaders headers = token.getHeaders();
-        assertEquals(JoseConstants.TYPE_JWT, headers.getType());
+        assertEquals(JoseType.JWT, headers.getType());
         assertEquals(SignatureAlgorithm.HS256.getJwaName(), headers.getAlgorithm());
         validateSpecClaim(token.getClaims());
     }
@@ -161,9 +161,9 @@ public class JwsCompactReaderWriterTest extends Assert {
     }
     
     private void doTestWriteJwsWithJwkSignedByMac(Object jsonWebKey) throws Exception {
-        JoseHeaders headers = new JoseHeaders();
-        headers.setType(JoseConstants.TYPE_JWT);
-        headers.setAlgorithm(SignatureAlgorithm.HS256.getJwaName());
+        JwsHeaders headers = new JwsHeaders();
+        headers.setType(JoseType.JWT);
+        headers.setSignatureAlgorithm(SignatureAlgorithm.HS256);
         headers.setHeader(JoseConstants.HEADER_JSON_WEB_KEY, jsonWebKey);
         
         JwtClaims claims = new JwtClaims();
@@ -185,7 +185,7 @@ public class JwsCompactReaderWriterTest extends Assert {
                                                                         SignatureAlgorithm.HS256)));
         JwtToken token = jws.getJwtToken();
         JoseHeaders headers = token.getHeaders();
-        assertEquals(JoseConstants.TYPE_JWT, headers.getType());
+        assertEquals(JoseType.JWT, headers.getType());
         assertEquals(SignatureAlgorithm.HS256.getJwaName(), headers.getAlgorithm());
         
         JsonWebKey key = headers.getJsonWebKey();


Mime
View raw message