alopresto commented on a change in pull request #3482: NIFI-6280 - Broke out 
the matching for /access/knox/** and /access/oi…
URL: https://github.com/apache/nifi/pull/3482#discussion_r285303972
 
 

 ##########
 File path: 
nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-security/src/test/groovy/org/apache/nifi/web/security/jwt/JwtAuthenticationFilterTest.groovy
 ##########
 @@ -0,0 +1,166 @@
+/*
+ * 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.nifi.web.security.jwt;
+
+import net.minidev.json.JSONObject;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass
+import org.junit.Rule;
+import org.junit.Test
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+@RunWith(JUnit4.class)
+class JwtAuthenticationFilterTest extends GroovyTestCase {
+
+    public static String jwtString;
+
+    @Rule
+    public ExpectedException expectedException = ExpectedException.none();
+
+    @BeforeClass
+    public static void setUpOnce() throws Exception {
+        final String ALG_HEADER = "{\"alg\":\"HS256\"}"
+        final int EXPIRATION_SECONDS = 500
+        Calendar now = Calendar.getInstance()
+        final long currentTime = (long) (now.getTimeInMillis() / 1000.0)
+        final long TOKEN_ISSUED_AT = currentTime
+        final long TOKEN_EXPIRATION_SECONDS = currentTime + EXPIRATION_SECONDS
+
+        // Generate a token that we will add a valid signature from a 
different token
+        // Always use LinkedHashMap to enforce order of the keys because the 
signature depends on order
+        Map<String, Object> claims = new LinkedHashMap<>()
+        claims.put("sub", "unknownuser")
+        claims.put("iss", "MockIdentityProvider")
+        claims.put("aud", "MockIdentityProvider")
+        claims.put("preferred_username", "unknownuser")
+        claims.put("kid", 1)
+        claims.put("exp", TOKEN_EXPIRATION_SECONDS)
+        claims.put("iat", TOKEN_ISSUED_AT)
+        final String EXPECTED_PAYLOAD = new JSONObject(claims).toString()
+        jwtString = JwtServiceTest.generateHS256Token(ALG_HEADER, 
EXPECTED_PAYLOAD, true, true)
+
+    }
+
+    @AfterClass
+    public static void tearDownOnce() throws Exception {
+
+    }
+
+    @Before
+    public void setUp() throws Exception {
+
+    }
+
+    @After
+    public void tearDown() throws Exception {
+
+    }
+
+    @Test
+    void testValidAuthenticationHeaderString() {
+        // Arrange
+        String authenticationHeader = ("Bearer " + jwtString)
+
+        // Act
+        boolean validHeader = new 
JwtAuthenticationFilter().validJwtFormat(authenticationHeader)
+
+        // Assert
+        assertTrue(validHeader)
+    }
+
+    @Test
+    void testMissingBearer() {
+        // Arrange
+
+        // Act
+        boolean invalidHeader = new 
JwtAuthenticationFilter().validJwtFormat(jwtString)
+
+        // Assert
+        assertFalse(invalidHeader)
+    }
+
+    @Test
+    void testBadTokenFormat() {
+        // Arrange
+        String[] tokenStrings = jwtString.split("\\.")
+        String badToken = "Bearer " + tokenStrings[1] + tokenStrings[2]
+
+        // Act
+        boolean invalidToken = new 
JwtAuthenticationFilter().validJwtFormat(badToken)
 
 Review comment:
   I think the `invalidToken` variable should be renamed `isTokenValid` so the 
expression `assertFalse(isTokenValid)` makes sense literally, while 
`assertFalse(invalidToken)` is confusing due to naming. 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to