Skip to content

Commit d2d41fa

Browse files
authored
Merge pull request #119 from AzureAD/sagonzal/cleanUpPublicApi
Make Account internal. Add ClientCrendential interfaces
2 parents 0078e1a + e8c10a5 commit d2d41fa

16 files changed

+122
-127
lines changed

src/integrationtest/java/com.microsoft.aad.msal4j/ClientCredentialsIT.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@
1818
import java.security.cert.X509Certificate;
1919
import java.util.Collections;
2020

21-
import static com.microsoft.aad.msal4j.TestConstants.GRAPH_DEFAULT_SCOPE;
2221
import static com.microsoft.aad.msal4j.TestConstants.KEYVAULT_DEFAULT_SCOPE;
2322

2423
@Test
2524
public class ClientCredentialsIT {
2625

2726
@Test
28-
public void acquireTokenClientCredentials_AsymmetricKeyCredential() throws Exception{
27+
public void acquireTokenClientCredentials_ClientCertificate() throws Exception{
2928
String clientId = "55e7e5af-ca53-482d-9aa3-5cb1cc8eecb5";
3029
IClientCredential credential = getCertificateFromKeyStore();
3130
assertAcquireTokenCommon(clientId, credential);
@@ -48,7 +47,7 @@ public void acquireTokenClientCredentials_ClientAssertion() throws Exception{
4847

4948
ClientAssertion clientAssertion = JwtHelper.buildJwt(
5049
clientId,
51-
(AsymmetricKeyCredential) certificateFromKeyStore,
50+
(ClientCertificate) certificateFromKeyStore,
5251
"https://login.microsoftonline.com/common/oauth2/v2.0/token");
5352

5453

@@ -73,7 +72,6 @@ private void assertAcquireTokenCommon(String clientId, IClientCredential credent
7372
Assert.assertNotNull(result.accessToken());
7473
}
7574

76-
7775
private IClientCredential getCertificateFromKeyStore() throws
7876
NoSuchProviderException, KeyStoreException, IOException, NoSuchAlgorithmException,
7977
CertificateException, UnrecoverableKeyException {

src/main/java/com/microsoft/aad/msal4j/Account.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
@Getter
1717
@Setter
1818
@AllArgsConstructor
19-
public class Account implements IAccount {
19+
class Account implements IAccount {
2020

2121
String homeAccountId;
2222

src/main/java/com/microsoft/aad/msal4j/ClientAssertion.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,14 @@
88
import lombok.Getter;
99
import lombok.experimental.Accessors;
1010

11-
/**
12-
* Credential type containing an assertion of type
13-
* "urn:ietf:params:oauth:token-type:jwt".
14-
*/
1511
@Accessors(fluent = true)
1612
@Getter
1713
@EqualsAndHashCode
18-
public final class ClientAssertion implements IClientCredential{
14+
final class ClientAssertion implements IClientAssertion {
1915

20-
public static final String assertionType = JWTAuthentication.CLIENT_ASSERTION_TYPE;
16+
static final String assertionType = JWTAuthentication.CLIENT_ASSERTION_TYPE;
2117
private final String assertion;
2218

23-
/**
24-
* Constructor to create credential with a jwt token encoded as a base64 url
25-
* encoded string.
26-
*
27-
* @param assertion The jwt used as credential.
28-
*/
2919
ClientAssertion(final String assertion) {
3020
if (StringHelper.isBlank(assertion)) {
3121
throw new NullPointerException("assertion");

src/main/java/com/microsoft/aad/msal4j/AsymmetricKeyCredential.java renamed to src/main/java/com/microsoft/aad/msal4j/ClientCertificate.java

Lines changed: 6 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,17 @@
2424
import lombok.experimental.Accessors;
2525
import org.apache.commons.codec.binary.Base64;
2626

27-
/**
28-
* Credential type containing X509 public certificate and RSA private key.
29-
*/
30-
public final class AsymmetricKeyCredential implements IClientCredential{
27+
final class ClientCertificate implements IClientCertificate {
3128

3229
private final static int MIN_KEY_SIZE_IN_BITS = 2048;
3330

34-
/**
35-
* Returns private key of the credential.
36-
*
37-
* @return private key.
38-
*/
3931
@Accessors(fluent = true)
4032
@Getter
4133
private final PrivateKey key;
4234

4335
private final X509Certificate publicCertificate;
4436

45-
/**
46-
* Constructor to create credential with client id, private key and public
47-
* certificate.
48-
*
49-
* @param key
50-
* RSA private key to sign the assertion.
51-
* @param publicCertificate
52-
* Public certificate used for thumb print.
53-
*/
54-
private AsymmetricKeyCredential(final PrivateKey key, final X509Certificate publicCertificate) {
37+
private ClientCertificate(final PrivateKey key, final X509Certificate publicCertificate) {
5538
if (key == null) {
5639
throw new NullPointerException("PrivateKey is null or empty");
5740
}
@@ -86,46 +69,17 @@ else if("sun.security.mscapi.RSAPrivateKey".equals(key.getClass().getName())){
8669
this.publicCertificate = publicCertificate;
8770
}
8871

89-
/**
90-
* Base64 encoded hash of the the public certificate.
91-
*
92-
* @return base64 encoded string
93-
* @throws CertificateEncodingException if an encoding error occurs
94-
* @throws NoSuchAlgorithmException if requested algorithm is not available in the environment
95-
*/
9672
public String publicCertificateHash()
9773
throws CertificateEncodingException, NoSuchAlgorithmException {
98-
return Base64.encodeBase64String(AsymmetricKeyCredential
74+
return Base64.encodeBase64String(ClientCertificate
9975
.getHash(this.publicCertificate.getEncoded()));
10076
}
10177

102-
/**
103-
* Base64 encoded public certificate.
104-
*
105-
* @return base64 encoded string
106-
* @throws CertificateEncodingException if an encoding error occurs
107-
*/
10878
public String publicCertificate() throws CertificateEncodingException {
10979
return Base64.encodeBase64String(this.publicCertificate.getEncoded());
11080
}
11181

112-
/**
113-
* Static method to create KeyCredential instance.
114-
*
115-
* @param pkcs12Certificate
116-
* PKCS12 certificate stream containing public and private key.
117-
* Caller is responsible for handling the input stream.
118-
* @param password
119-
* certificate password
120-
* @return KeyCredential instance
121-
* @throws KeyStoreException {@link KeyStoreException}
122-
* @throws NoSuchProviderException {@link NoSuchProviderException}
123-
* @throws NoSuchAlgorithmException {@link NoSuchAlgorithmException}
124-
* @throws CertificateException {@link CertificateException}
125-
* @throws IOException {@link IOException}
126-
* @throws UnrecoverableKeyException {@link UnrecoverableKeyException}
127-
*/
128-
static AsymmetricKeyCredential create(final InputStream pkcs12Certificate, final String password)
82+
static ClientCertificate create(final InputStream pkcs12Certificate, final String password)
12983
throws KeyStoreException, NoSuchProviderException,
13084
NoSuchAlgorithmException, CertificateException, IOException, UnrecoverableKeyException {
13185
final KeyStore keystore = KeyStore.getInstance("PKCS12", "SunJSSE");
@@ -139,17 +93,8 @@ static AsymmetricKeyCredential create(final InputStream pkcs12Certificate, final
13993
return create(key, publicCertificate);
14094
}
14195

142-
/**
143-
* Static method to create KeyCredential instance.
144-
*
145-
* @param key
146-
* RSA private key to sign the assertion.
147-
* @param publicCertificate
148-
* Public certificate used for thumb print.
149-
* @return KeyCredential instance
150-
*/
151-
static AsymmetricKeyCredential create(final PrivateKey key, final X509Certificate publicCertificate) {
152-
return new AsymmetricKeyCredential(key, publicCertificate);
96+
static ClientCertificate create(final PrivateKey key, final X509Certificate publicCertificate) {
97+
return new ClientCertificate(key, publicCertificate);
15398
}
15499

155100
private static byte[] getHash(final byte[] inputBytes) throws NoSuchAlgorithmException {

src/main/java/com/microsoft/aad/msal4j/ClientCredentialFactory.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,44 +19,44 @@ public class ClientCredentialFactory {
1919
* @param secret secret of application requesting a token
2020
* @return {@link ClientSecret}
2121
*/
22-
public static IClientCredential createFromSecret(String secret){
22+
public static IClientSecret createFromSecret(String secret){
2323
return new ClientSecret(secret);
2424
}
2525

2626
/**
27-
* Static method to create a {@link AsymmetricKeyCredential} instance from a certificate
27+
* Static method to create a {@link ClientCertificate} instance from a certificate
2828
* @param pkcs12Certificate InputStream containing PCKS12 formatted certificate
2929
* @param password certificate password
30-
* @return {@link AsymmetricKeyCredential}
30+
* @return {@link ClientCertificate}
3131
* @throws CertificateException
3232
* @throws UnrecoverableKeyException
3333
* @throws NoSuchAlgorithmException
3434
* @throws KeyStoreException
3535
* @throws NoSuchProviderException
3636
* @throws IOException
3737
*/
38-
public static IClientCredential createFromCertificate(final InputStream pkcs12Certificate, final String password)
38+
public static IClientCertificate createFromCertificate(final InputStream pkcs12Certificate, final String password)
3939
throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException,
4040
KeyStoreException, NoSuchProviderException, IOException {
41-
return AsymmetricKeyCredential.create(pkcs12Certificate, password);
41+
return ClientCertificate.create(pkcs12Certificate, password);
4242
}
4343

4444
/**
45-
* Static method to create a {@link AsymmetricKeyCredential} instance.
45+
* Static method to create a {@link ClientCertificate} instance.
4646
* @param key RSA private key to sign the assertion.
4747
* @param publicCertificate x509 public certificate used for thumbprint
48-
* @return {@link AsymmetricKeyCredential}
48+
* @return {@link ClientCertificate}
4949
*/
50-
public static IClientCredential createFromCertificate(final PrivateKey key, final X509Certificate publicCertificate) {
51-
return AsymmetricKeyCredential.create(key, publicCertificate);
50+
public static IClientCertificate createFromCertificate(final PrivateKey key, final X509Certificate publicCertificate) {
51+
return ClientCertificate.create(key, publicCertificate);
5252
}
5353

5454
/**
5555
* Static method to create a {@link ClientAssertion} instance.
5656
* @param clientAssertion Jwt token encoded as a base64 URL encoded string
5757
* @return {@link ClientAssertion}
5858
*/
59-
public static IClientCredential createFromClientAssertion(String clientAssertion){
59+
public static IClientAssertion createFromClientAssertion(String clientAssertion){
6060
return new ClientAssertion(clientAssertion);
6161
}
6262
}

src/main/java/com/microsoft/aad/msal4j/ClientSecret.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,8 @@
77
import lombok.Getter;
88
import lombok.experimental.Accessors;
99

10-
11-
/**
12-
* Representation of client credential containing a secret in string format
13-
*/
1410
@EqualsAndHashCode
15-
public final class ClientSecret implements IClientCredential {
16-
11+
final class ClientSecret implements IClientSecret {
1712

1813
@Accessors(fluent = true)
1914
@Getter

src/main/java/com/microsoft/aad/msal4j/ConfidentialClientApplication.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ private void initClientAuthentication(IClientCredential clientCredential) {
6767
clientAuthentication = new ClientSecretPost(
6868
new ClientID(clientId()),
6969
new Secret(((ClientSecret) clientCredential).clientSecret()));
70-
} else if (clientCredential instanceof AsymmetricKeyCredential) {
70+
} else if (clientCredential instanceof ClientCertificate) {
7171
ClientAssertion clientAssertion = JwtHelper.buildJwt(
7272
clientId(),
73-
(AsymmetricKeyCredential) clientCredential,
73+
(ClientCertificate) clientCredential,
7474
this.authenticationAuthority.selfSignedJwtAudience());
7575

7676
clientAuthentication = createClientAuthFromClientAssertion(clientAssertion);

src/main/java/com/microsoft/aad/msal4j/IClientApplicationBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
/**
1414
* Interface representing an application for which tokens can be acquired.
1515
*/
16-
public interface IClientApplicationBase {
16+
interface IClientApplicationBase {
1717

1818
String DEFAULT_AUTHORITY = "https://login.microsoftonline.com/common/";
1919

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.microsoft.aad.msal4j;
5+
6+
/**
7+
* Credential type containing an assertion of type
8+
* "urn:ietf:params:oauth:token-type:jwt".
9+
*/
10+
public interface IClientAssertion extends IClientCredential{
11+
12+
/**
13+
* @return Jwt token encoded as a base64 URL encoded string
14+
*/
15+
String assertion();
16+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.microsoft.aad.msal4j;
5+
6+
import java.security.NoSuchAlgorithmException;
7+
import java.security.PrivateKey;
8+
import java.security.cert.CertificateEncodingException;
9+
10+
/**
11+
* Credential type containing X509 public certificate and RSA private key.
12+
*/
13+
public interface IClientCertificate extends IClientCredential{
14+
15+
/**
16+
* Returns private key of the credential.
17+
*
18+
* @return private key.
19+
*/
20+
PrivateKey key();
21+
22+
/**
23+
* Base64 encoded hash of the the public certificate.
24+
*
25+
* @return base64 encoded string
26+
* @throws CertificateEncodingException if an encoding error occurs
27+
* @throws NoSuchAlgorithmException if requested algorithm is not available in the environment
28+
*/
29+
String publicCertificateHash() throws CertificateEncodingException, NoSuchAlgorithmException;
30+
31+
/**
32+
* Base64 encoded public certificate.
33+
*
34+
* @return base64 encoded string
35+
* @throws CertificateEncodingException if an encoding error occurs
36+
*/
37+
String publicCertificate() throws CertificateEncodingException;
38+
}

0 commit comments

Comments
 (0)