-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #323 from JanssenProject/feat/mtls-for-registratio…
…n-endpoint Added MTLS verification during client registration
- Loading branch information
Showing
5 changed files
with
171 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
server/src/test/java/io/jans/as/server/service/MTLSServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package io.jans.as.server.service; | ||
|
||
import io.jans.as.model.crypto.AbstractCryptoProvider; | ||
import io.jans.as.model.error.ErrorResponseFactory; | ||
import io.jans.as.server.auth.Authenticator; | ||
import io.jans.as.server.auth.MTLSService; | ||
import io.jans.as.server.service.external.ExternalDynamicClientRegistrationService; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.testng.MockitoTestNGListener; | ||
import org.slf4j.Logger; | ||
import org.testng.annotations.Listeners; | ||
import org.testng.annotations.Test; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.StringReader; | ||
|
||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.*; | ||
import static org.testng.Assert.assertFalse; | ||
import static org.testng.Assert.assertTrue; | ||
|
||
@Listeners(MockitoTestNGListener.class) | ||
public class MTLSServiceTest { | ||
|
||
private static final String tlsClientAuthSubjectDn = "UID=4b18f6a7-2972-455c-948d-b0e59a8c1da9,1.3.6.1.4.1.311.60.2.1.3=#13024252,2.5.4.15=#131450726976617465204f7267616e697a6174696f6e,2.5.4.5=#130e3133383834373735303030313139,CN=hubfintech.com.br,OU=7eebe017-cb01-498c-81e4-6b4149b18e93,O=HUB PAGAMENTOS S.A,L=Tambore,ST=SP,C=BR"; | ||
private static final String certPem = "-----BEGIN CERTIFICATE-----\nMIIG7zCCBdegAwIBAgIUeHaL5NdAFgGRRpTW3oJ9E95bJMwwDQYJKoZIhvcNAQELBQAwcTELMAkGA1UEBhMCQlIxHDAaBgNVBAoTE09wZW4gQmFua2luZyBCcmFzaWwxFTATBgNVBAsTDE9wZW4gQmFua2luZzEtMCsGA1UEAxMkT3BlbiBCYW5raW5nIFNBTkRCT1ggSXNzdWluZyBDQSAtIEcxMB4XDTIxMTAxNTE5NTIwMFoXDTIyMTExNDE5NTIwMFowggEXMQswCQYDVQQGEwJCUjELMAkGA1UECBMCU1AxEDAOBgNVBAcTB1RhbWJvcmUxGzAZBgNVBAoTEkhVQiBQQUdBTUVOVE9TIFMuQTEtMCsGA1UECxMkN2VlYmUwMTctY2IwMS00OThjLTgxZTQtNmI0MTQ5YjE4ZTkzMRowGAYDVQQDExFodWJmaW50ZWNoLmNvbS5icjEXMBUGA1UEBRMOMTM4ODQ3NzUwMDAxMTkxHTAbBgNVBA8TFFByaXZhdGUgT3JnYW5pemF0aW9uMRMwEQYLKwYBBAGCNzwCAQMTAkJSMTQwMgYKCZImiZPyLGQBARMkNGIxOGY2YTctMjk3Mi00NTVjLTk0OGQtYjBlNTlhOGMxZGE5MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwFbKiRZprMK4Kyqs1Bsdk6lHZTMxMkSISqGhKau+wmFQD1iLW1C424FI1alK7IhW1YGs0toJtZwIMbuoqEUrRoPCQlojusnKpRW/sW4FRaVwXgyFNgg411kwZvWy089XXy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VsYXRlZCBkb2N1bWVudHMgdGhlcmVpbi4wRAYIKwYBBQUHAgEWOGh0dHA6Ly9jcHMuc2FuZGJveC5wa2kub3BlbmJhbmtpbmdicmFzaWwub3JnLmJyL3BvbGljaWVzMA0GCSqGSIb3DQEBCwUAA4IBAQB0ggJmZ3K+fpWIS3Lee+cXxmX5T6H4bJ4GhK4aDDj64EC8PYnUcceJ/cUV75uz3Xij8pSBgPJF4rgV3VjlZcpgLm8pIrBVEqoMVvUAMtj89q7Akjpx4tUZBLahW9RFQ1mVLkIcVjHsc9DJpW+SLGhGYSIPAKLtymZsTZsG8PjvKvLcjz7+jEhuib9PwB7MiPUp+JRy3fiXjDfX2/DEFLBc68q9VslhrZByiMzPeEJDYN+FOqwtAovYvlyGwSnGQCw3338ZMLboCbbYzzZH7VBUoo3b7TI86VO9kqQ8vni5+vU5cgfqBk6xYT8adt+bLHm1Urtc46jFo+lJgIJitG8k\n-----END CERTIFICATE-----"; | ||
|
||
@InjectMocks | ||
private MTLSService mtlsService; | ||
|
||
@Mock | ||
private Logger log; | ||
|
||
@Mock | ||
private Authenticator authenticator; | ||
|
||
@Mock | ||
private SessionIdService sessionIdService; | ||
|
||
@Mock | ||
private AbstractCryptoProvider cryptoProvider; | ||
|
||
@Mock | ||
private ErrorResponseFactory errorResponseFactory; | ||
|
||
@Mock | ||
private ExternalDynamicClientRegistrationService externalDynamicClientRegistrationService; | ||
|
||
@Test | ||
public void processRegisterMTLS_HappyFlow_ReturnsTrue() throws Exception { | ||
HttpServletRequest httpServletRequest = mock(HttpServletRequest.class); | ||
String jsonRequest = "{ \"tls_client_auth_subject_dn\":\"" + tlsClientAuthSubjectDn + "\" }"; | ||
BufferedReader requestReader = new BufferedReader(new StringReader(jsonRequest)); | ||
when(httpServletRequest.getReader()).thenReturn(requestReader); | ||
when(httpServletRequest.getHeader(eq("X-ClientCert"))).thenReturn(certPem); | ||
|
||
boolean result = mtlsService.processRegisterMTLS(httpServletRequest); | ||
|
||
assertTrue(result); | ||
verify(log).debug("Trying to authenticate client registration request via MTLS"); | ||
verify(log).debug(anyString(), anyString()); | ||
|
||
verifyNoMoreInteractions(log); | ||
} | ||
|
||
@Test | ||
public void processRegisterMTLS_ErrorReadingJsonRequest_ReturnsFalse() throws Exception { | ||
HttpServletRequest httpServletRequest = mock(HttpServletRequest.class); | ||
when(httpServletRequest.getReader()).thenThrow(new IOException()); | ||
when(httpServletRequest.getHeader(eq("X-ClientCert"))).thenReturn(certPem); | ||
|
||
boolean result = mtlsService.processRegisterMTLS(httpServletRequest); | ||
|
||
assertFalse(result); | ||
verify(log).debug("Trying to authenticate client registration request via MTLS"); | ||
verify(log).error(eq("Error getting TLS_CLIENT_AUTH_SUBJECT_DN field from request registration body"), any(IOException.class)); | ||
verify(log).debug(anyString(), (String)isNull()); | ||
|
||
verifyNoMoreInteractions(log); | ||
} | ||
|
||
@Test | ||
public void processRegisterMTLS_CouldntCreateX509Cert_ReturnsFalse() throws Exception { | ||
HttpServletRequest httpServletRequest = mock(HttpServletRequest.class); | ||
String jsonRequest = "{ \"tls_client_auth_subject_dn\":\"" + tlsClientAuthSubjectDn + "\" }"; | ||
BufferedReader requestReader = new BufferedReader(new StringReader(jsonRequest)); | ||
when(httpServletRequest.getReader()).thenReturn(requestReader); | ||
when(httpServletRequest.getHeader(eq("X-ClientCert"))).thenReturn("ABC"); | ||
|
||
boolean result = mtlsService.processRegisterMTLS(httpServletRequest); | ||
|
||
assertFalse(result); | ||
verify(log).debug("Trying to authenticate client registration request via MTLS"); | ||
verify(log).debug("Failed to parse client certificate"); | ||
|
||
verifyNoMoreInteractions(log); | ||
} | ||
|
||
@Test | ||
public void processRegisterMTLS_HeaderXClientCertNull_ReturnsFalse() throws Exception { | ||
HttpServletRequest httpServletRequest = mock(HttpServletRequest.class); | ||
String jsonRequest = "{ \"tls_client_auth_subject_dn\":\"" + tlsClientAuthSubjectDn + "\" }"; | ||
BufferedReader requestReader = new BufferedReader(new StringReader(jsonRequest)); | ||
when(httpServletRequest.getReader()).thenReturn(requestReader); | ||
|
||
boolean result = mtlsService.processRegisterMTLS(httpServletRequest); | ||
|
||
assertFalse(result); | ||
verify(log).debug("Trying to authenticate client registration request via MTLS"); | ||
verify(log).debug("Client certificate is missed in `X-ClientCert` header"); | ||
|
||
verifyNoMoreInteractions(log); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters