cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ff...@apache.org
Subject cxf git commit: [CXF-7006]add an util method in AuthorizationUtils which can take AuthorizationFailure cause
Date Wed, 10 Aug 2016 07:28:14 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 73da1a165 -> 395acbd2f


[CXF-7006]add an util method in AuthorizationUtils which can take AuthorizationFailure cause


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

Branch: refs/heads/master
Commit: 395acbd2f16b03314f34ee0b6a212b708b375873
Parents: 73da1a1
Author: Freeman Fang <freeman.fang@gmail.com>
Authored: Wed Aug 10 15:28:00 2016 +0800
Committer: Freeman Fang <freeman.fang@gmail.com>
Committed: Wed Aug 10 15:28:00 2016 +0800

----------------------------------------------------------------------
 .../security/oauth2/utils/AuthorizationUtils.java   | 14 +++++++++++---
 .../oauth2/utils/AuthorizationUtilsTest.java        | 16 ++++++++++++++++
 2 files changed, 27 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/395acbd2/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/utils/AuthorizationUtils.java
----------------------------------------------------------------------
diff --git a/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/utils/AuthorizationUtils.java
b/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/utils/AuthorizationUtils.java
index 51446a2..088c16b 100644
--- a/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/utils/AuthorizationUtils.java
+++ b/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/utils/AuthorizationUtils.java
@@ -91,6 +91,10 @@ public final class AuthorizationUtils {
     }
     
     public static void throwAuthorizationFailure(Set<String> challenges, String realm)
{
+        throwAuthorizationFailure(challenges, realm, null);
+    }
+
+    public static void throwAuthorizationFailure(Set<String> challenges, String realm,
Throwable cause) {
         ResponseBuilder rb = JAXRSUtils.toResponseBuilder(401);
         
         StringBuilder sb = new StringBuilder();
@@ -109,8 +113,12 @@ public final class AuthorizationUtils {
             }
             rb.header(HttpHeaders.WWW_AUTHENTICATE, sb.toString());
         }
-        Response r = rb.build();
-        throw ExceptionUtils.toNotAuthorizedException(null, r);
+        Response r = null;
+        if (cause != null) {
+            r = rb.entity(cause.getMessage()).build(); 
+        } else {
+            r = rb.build();
+        }
+        throw ExceptionUtils.toNotAuthorizedException(cause, r);
     }
-
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/395acbd2/rt/rs/security/oauth-parent/oauth2/src/test/java/org/apache/cxf/rs/security/oauth2/utils/AuthorizationUtilsTest.java
----------------------------------------------------------------------
diff --git a/rt/rs/security/oauth-parent/oauth2/src/test/java/org/apache/cxf/rs/security/oauth2/utils/AuthorizationUtilsTest.java
b/rt/rs/security/oauth-parent/oauth2/src/test/java/org/apache/cxf/rs/security/oauth2/utils/AuthorizationUtilsTest.java
index af2980d..6c3d14a 100644
--- a/rt/rs/security/oauth-parent/oauth2/src/test/java/org/apache/cxf/rs/security/oauth2/utils/AuthorizationUtilsTest.java
+++ b/rt/rs/security/oauth-parent/oauth2/src/test/java/org/apache/cxf/rs/security/oauth2/utils/AuthorizationUtilsTest.java
@@ -74,4 +74,20 @@ public class AuthorizationUtilsTest extends Assert {
             assertNull(value);
         }
     }
+    
+    @Test
+    public void testThrowAuthorizationFailureWithCause() {
+        try {
+            AuthorizationUtils.throwAuthorizationFailure(Collections.singleton("Basic"),

+                                                         null, new RuntimeException("expired
token"));
+            fail("WebApplicationException expected");
+        } catch (WebApplicationException ex) {
+            Response r = ex.getResponse();
+            assertEquals("expired token", r.getEntity());
+            assertEquals(401, r.getStatus());
+            Object value = r.getMetadata().getFirst(HttpHeaders.WWW_AUTHENTICATE);
+            assertNotNull(value);
+            assertEquals("Basic", value.toString());
+        }
+    }
 }


Mime
View raw message