|
| 1 | +package io.camunda.operate.auth; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | + |
| 5 | +import java.net.URI; |
| 6 | +import java.net.URL; |
| 7 | +import org.apache.hc.client5.http.classic.methods.HttpPost; |
| 8 | +import org.apache.hc.client5.http.entity.UrlEncodedFormEntity; |
| 9 | +import org.apache.hc.core5.http.io.entity.EntityUtils; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | + |
| 12 | +public class JwtAuthenticationTest { |
| 13 | + |
| 14 | + @Test |
| 15 | + void shouldBuildBasicTokenRequest() throws Exception { |
| 16 | + // Given |
| 17 | + URL authUrl = URI.create("https://auth.example.com/token").toURL(); |
| 18 | + JwtCredential credential = |
| 19 | + new JwtCredential( |
| 20 | + "test-client-id", |
| 21 | + "test-client-secret", |
| 22 | + "test-audience", |
| 23 | + authUrl, |
| 24 | + "test-scope", |
| 25 | + null, // no resource parameter - backward compatibility |
| 26 | + null, |
| 27 | + null, |
| 28 | + null, |
| 29 | + null); |
| 30 | + |
| 31 | + JwtAuthentication authentication = new JwtAuthentication(credential); |
| 32 | + |
| 33 | + // Use reflection to access private method for testing |
| 34 | + java.lang.reflect.Method buildRequestMethod = |
| 35 | + JwtAuthentication.class.getDeclaredMethod("buildRequest"); |
| 36 | + buildRequestMethod.setAccessible(true); |
| 37 | + |
| 38 | + // When |
| 39 | + HttpPost request = (HttpPost) buildRequestMethod.invoke(authentication); |
| 40 | + |
| 41 | + // Then |
| 42 | + UrlEncodedFormEntity entity = (UrlEncodedFormEntity) request.getEntity(); |
| 43 | + String entityContent = EntityUtils.toString(entity); |
| 44 | + |
| 45 | + // Verify standard OAuth2 parameters are included |
| 46 | + assertTrue(entityContent.contains("client_id=test-client-id")); |
| 47 | + assertTrue(entityContent.contains("audience=test-audience")); |
| 48 | + assertTrue(entityContent.contains("scope=test-scope")); |
| 49 | + assertTrue(entityContent.contains("grant_type=client_credentials")); |
| 50 | + // Verify resource parameter is not included when null |
| 51 | + assertFalse(entityContent.contains("resource=")); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void shouldIncludeResourceParameterInTokenRequest() throws Exception { |
| 56 | + // Given |
| 57 | + URL authUrl = URI.create("https://auth.example.com/token").toURL(); |
| 58 | + JwtCredential credential = |
| 59 | + new JwtCredential( |
| 60 | + "test-client-id", |
| 61 | + "test-client-secret", |
| 62 | + "test-audience", |
| 63 | + authUrl, |
| 64 | + "test-scope", |
| 65 | + "test-resource", |
| 66 | + null, |
| 67 | + null, |
| 68 | + null, |
| 69 | + null); |
| 70 | + |
| 71 | + JwtAuthentication authentication = new JwtAuthentication(credential); |
| 72 | + |
| 73 | + // Use reflection to access private method for testing |
| 74 | + java.lang.reflect.Method buildRequestMethod = |
| 75 | + JwtAuthentication.class.getDeclaredMethod("buildRequest"); |
| 76 | + buildRequestMethod.setAccessible(true); |
| 77 | + |
| 78 | + // When |
| 79 | + HttpPost request = (HttpPost) buildRequestMethod.invoke(authentication); |
| 80 | + |
| 81 | + // Then |
| 82 | + UrlEncodedFormEntity entity = (UrlEncodedFormEntity) request.getEntity(); |
| 83 | + String entityContent = EntityUtils.toString(entity); |
| 84 | + |
| 85 | + assertTrue(entityContent.contains("resource=test-resource")); |
| 86 | + assertTrue(entityContent.contains("client_id=test-client-id")); |
| 87 | + assertTrue(entityContent.contains("audience=test-audience")); |
| 88 | + assertTrue(entityContent.contains("scope=test-scope")); |
| 89 | + assertTrue(entityContent.contains("grant_type=client_credentials")); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void shouldNotIncludeResourceParameterWhenNull() throws Exception { |
| 94 | + // Given |
| 95 | + URL authUrl = URI.create("https://auth.example.com/token").toURL(); |
| 96 | + JwtCredential credential = |
| 97 | + new JwtCredential( |
| 98 | + "test-client-id", |
| 99 | + "test-client-secret", |
| 100 | + "test-audience", |
| 101 | + authUrl, |
| 102 | + "test-scope", |
| 103 | + null, // resource is null |
| 104 | + null, |
| 105 | + null, |
| 106 | + null, |
| 107 | + null); |
| 108 | + |
| 109 | + JwtAuthentication authentication = new JwtAuthentication(credential); |
| 110 | + |
| 111 | + // Use reflection to access private method for testing |
| 112 | + java.lang.reflect.Method buildRequestMethod = |
| 113 | + JwtAuthentication.class.getDeclaredMethod("buildRequest"); |
| 114 | + buildRequestMethod.setAccessible(true); |
| 115 | + |
| 116 | + // When |
| 117 | + HttpPost request = (HttpPost) buildRequestMethod.invoke(authentication); |
| 118 | + |
| 119 | + // Then |
| 120 | + UrlEncodedFormEntity entity = (UrlEncodedFormEntity) request.getEntity(); |
| 121 | + String entityContent = EntityUtils.toString(entity); |
| 122 | + |
| 123 | + assertFalse(entityContent.contains("resource=")); |
| 124 | + assertTrue(entityContent.contains("client_id=test-client-id")); |
| 125 | + assertTrue(entityContent.contains("audience=test-audience")); |
| 126 | + assertTrue(entityContent.contains("scope=test-scope")); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + void shouldNotIncludeResourceParameterWhenEmpty() throws Exception { |
| 131 | + // Given |
| 132 | + URL authUrl = URI.create("https://auth.example.com/token").toURL(); |
| 133 | + JwtCredential credential = |
| 134 | + new JwtCredential( |
| 135 | + "test-client-id", |
| 136 | + "test-client-secret", |
| 137 | + "test-audience", |
| 138 | + authUrl, |
| 139 | + "test-scope", |
| 140 | + "", // resource is empty |
| 141 | + null, |
| 142 | + null, |
| 143 | + null, |
| 144 | + null); |
| 145 | + |
| 146 | + JwtAuthentication authentication = new JwtAuthentication(credential); |
| 147 | + |
| 148 | + // Use reflection to access private method for testing |
| 149 | + java.lang.reflect.Method buildRequestMethod = |
| 150 | + JwtAuthentication.class.getDeclaredMethod("buildRequest"); |
| 151 | + buildRequestMethod.setAccessible(true); |
| 152 | + |
| 153 | + // When |
| 154 | + HttpPost request = (HttpPost) buildRequestMethod.invoke(authentication); |
| 155 | + |
| 156 | + // Then |
| 157 | + UrlEncodedFormEntity entity = (UrlEncodedFormEntity) request.getEntity(); |
| 158 | + String entityContent = EntityUtils.toString(entity); |
| 159 | + |
| 160 | + assertFalse(entityContent.contains("resource=")); |
| 161 | + assertTrue(entityContent.contains("client_id=test-client-id")); |
| 162 | + assertTrue(entityContent.contains("audience=test-audience")); |
| 163 | + assertTrue(entityContent.contains("scope=test-scope")); |
| 164 | + } |
| 165 | +} |
0 commit comments