33
44package com .microsoft .aad .msal4j ;
55
6+ import com .nimbusds .oauth2 .sdk .auth .PrivateKeyJWT ;
67import org .junit .jupiter .api .Test ;
78import org .junit .jupiter .api .TestInstance ;
89
910import static org .junit .jupiter .api .Assertions .assertEquals ;
1011import static org .junit .jupiter .api .Assertions .assertNotNull ;
1112import static org .junit .jupiter .api .Assertions .assertNull ;
1213import static org .junit .jupiter .api .Assertions .assertThrows ;
13- import static org .mockito .Mockito . doReturn ;
14- import static org .mockito .Mockito .mock ;
14+ import static org .mockito .ArgumentMatchers . any ;
15+ import static org .mockito .Mockito .* ;
1516
1617import java .math .BigInteger ;
17- import java .security .NoSuchAlgorithmException ;
18- import java .security .PrivateKey ;
18+ import java .security .*;
1919import java .security .cert .CertificateException ;
2020import java .security .interfaces .RSAPrivateKey ;
21- import java .util .Collections ;
22- import java .util .List ;
21+ import java .util .*;
2322
2423@ TestInstance (TestInstance .Lifecycle .PER_CLASS )
2524class ClientCertificateTest {
@@ -43,7 +42,7 @@ void testGetClient() {
4342 }
4443
4544 @ Test
46- void testIClientCertificateInterface_Sha256AndSha1 () throws NoSuchAlgorithmException , CertificateException {
45+ void testIClientCertificateInterface_Sha1andSha256 () throws NoSuchAlgorithmException , CertificateException {
4746 //See https://github.com/AzureAD/microsoft-authentication-library-for-java/issues/863 for context on this test.
4847 //Essentially, it aims to test compatibility for customers that implemented IClientCertificate in older versions of the library.
4948
@@ -56,6 +55,40 @@ void testIClientCertificateInterface_Sha256AndSha1() throws NoSuchAlgorithmExcep
5655 assertNotNull (certificate .publicCertificateHash256 ());
5756 }
5857
58+ @ Test
59+ void testIClientCertificateInterface_CredentialFactoryUsesSha256 () throws Exception {
60+ DefaultHttpClient httpClientMock = mock (DefaultHttpClient .class );
61+
62+ ConfidentialClientApplication cca =
63+ ConfidentialClientApplication .builder ("clientId" , ClientCredentialFactory .createFromCertificate (TestHelper .getPrivateKey (), TestHelper .getX509Cert ()))
64+ .authority ("https://login.microsoftonline.com/tenant" )
65+ .instanceDiscovery (false )
66+ .validateAuthority (false )
67+ .httpClient (httpClientMock )
68+ .build ();
69+
70+ HashMap <String , String > tokenResponseValues = new HashMap <>();
71+ tokenResponseValues .put ("access_token" , "accessTokenSha256" );
72+
73+ when (httpClientMock .send (any (HttpRequest .class ))).thenAnswer ( parameters -> {
74+ HttpRequest request = parameters .getArgument (0 );
75+ Set <String > headerParams = ((PrivateKeyJWT ) cca .clientAuthentication ()).getClientAssertion ().getHeader ().getIncludedParams ();
76+ if (request .body ().contains (((PrivateKeyJWT ) cca .clientAuthentication ()).getClientAssertion ().serialize ())
77+ && headerParams .contains ("x5t#S256" )) {
78+
79+ return TestHelper .expectedResponse (200 , TestHelper .getSuccessfulTokenResponse (tokenResponseValues ));
80+ }
81+ return null ;
82+ });
83+
84+ ClientCredentialParameters parameters = ClientCredentialParameters .builder (Collections .singleton ("scopes" )).build ();
85+
86+ IAuthenticationResult result = cca .acquireToken (parameters ).get ();
87+
88+ assertNotNull (result );
89+ assertEquals ("accessTokenSha256" , result .accessToken ());
90+ }
91+
5992 class TestClientCredential implements IClientCertificate {
6093 @ Override
6194 public PrivateKey privateKey () {
0 commit comments