Skip to content

Commit cf1f07d

Browse files
authored
Merge pull request #67 from AzureAD/sagonzal/makeAllAPIsFluent
Upate to use fluent API
2 parents ffa9f72 + 7ab0fcc commit cf1f07d

22 files changed

+168
-146
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// All rights reserved.
3+
//
4+
// This code is licensed under the MIT License.
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files(the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions :
12+
//
13+
// The above copyright notice and this permission notice shall be included in
14+
// all copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
// THE SOFTWARE.
23+
124
package com.microsoft.aad.msal4j;
225

326
import java.net.URL;

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ private OAuthAuthorizationGrant processPasswordGrant(
9494

9595
if (userDiscoveryResponse.isAccountFederated()) {
9696
WSTrustResponse response = WSTrustRequest.execute(
97-
userDiscoveryResponse.getFederationMetadataUrl(),
97+
userDiscoveryResponse.federationMetadataUrl(),
9898
grant.getUsername(),
9999
grant.getPassword().getValue(),
100-
userDiscoveryResponse.getCloudAudienceUrn(),
100+
userDiscoveryResponse.cloudAudienceUrn(),
101101
msalRequest.requestContext(),
102102
this.clientApplication.getServiceBundle(),
103103
this.clientApplication.logPii());
@@ -136,10 +136,10 @@ private AuthorizationGrant getAuthorizationGrantIntegrated(String userName) thro
136136
this.clientApplication.getServiceBundle());
137137

138138
if (userRealmResponse.isAccountFederated() &&
139-
"WSTrust".equalsIgnoreCase(userRealmResponse.getFederationProtocol())) {
139+
"WSTrust".equalsIgnoreCase(userRealmResponse.federationProtocol())) {
140140

141-
String mexURL = userRealmResponse.getFederationMetadataUrl();
142-
String cloudAudienceUrn = userRealmResponse.getCloudAudienceUrn();
141+
String mexURL = userRealmResponse.federationMetadataUrl();
142+
String cloudAudienceUrn = userRealmResponse.cloudAudienceUrn();
143143

144144
// Discover the policy for authentication using the Metadata Exchange Url.
145145
// Get the WSTrust Token (Web Service Trust Token)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ private AuthenticationResult acquireTokenWithDeviceCode(DeviceCode deviceCode,
7878
try {
7979
return acquireTokenByAuthorisationGrantSupplier.execute();
8080
} catch (AuthenticationException ex) {
81-
if (ex.getErrorCode().equals(AUTHORIZATION_PENDING)) {
81+
if (ex.errorCode().equals(AUTHORIZATION_PENDING)) {
8282
TimeUnit.SECONDS.sleep(deviceCode.interval());
8383
} else {
8484
throw ex;

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// All rights reserved.
3+
//
4+
// This code is licensed under the MIT License.
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files(the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions :
12+
//
13+
// The above copyright notice and this permission notice shall be included in
14+
// all copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
// THE SOFTWARE.
23+
124
package com.microsoft.aad.msal4j;
225

326
import com.google.gson.annotations.SerializedName;

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,26 @@
4040
import java.security.interfaces.RSAPrivateKey;
4141
import java.util.Enumeration;
4242

43+
import lombok.Getter;
44+
import lombok.experimental.Accessors;
4345
import org.apache.commons.codec.binary.Base64;
4446

4547
/**
4648
* Credential type containing X509 public certificate and RSA private key.
4749
*/
4850
public final class AsymmetricKeyCredential implements IClientCredential{
51+
4952
public final static int MIN_KEY_SIZE_IN_BITS = 2048;
53+
54+
/**
55+
* Returns private key of the credential.
56+
*
57+
* @return private key.
58+
*/
59+
@Accessors(fluent = true)
60+
@Getter
5061
private final PrivateKey key;
62+
5163
private final X509Certificate publicCertificate;
5264

5365
/**
@@ -113,18 +125,10 @@ public String getPublicCertificateHash()
113125
* @return base64 encoded string
114126
* @throws CertificateEncodingException if an encoding error occurs
115127
*/
116-
public String getPublicCertificate() throws CertificateEncodingException {
128+
public String publicCertificate() throws CertificateEncodingException {
117129
return Base64.encodeBase64String(this.publicCertificate.getEncoded());
118130
}
119131

120-
/**
121-
* Returns private key of the credential.
122-
*
123-
* @return private key.
124-
*/
125-
public PrivateKey getKey() {
126-
return key;
127-
}
128132

129133
/**
130134
* Static method to create KeyCredential instance.
@@ -174,5 +178,4 @@ private static byte[] getHash(final byte[] inputBytes) throws NoSuchAlgorithmExc
174178
md.update(inputBytes);
175179
return md.digest();
176180
}
177-
178181
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,18 @@
2323

2424
package com.microsoft.aad.msal4j;
2525

26+
import lombok.Getter;
27+
import lombok.experimental.Accessors;
28+
2629
/**
2730
* MSAL generic exception class
2831
*/
2932
public class AuthenticationException extends RuntimeException {
3033

3134
private static final long serialVersionUID = 1L;
3235

36+
@Accessors(fluent = true)
37+
@Getter
3338
private AuthenticationErrorCode errorCode;
3439

3540
/**
@@ -68,8 +73,4 @@ public AuthenticationException(final String message, final Throwable t) {
6873

6974
this.errorCode = AuthenticationErrorCode.UNKNOWN;
7075
}
71-
72-
public AuthenticationErrorCode getErrorCode() {
73-
return errorCode;
74-
}
7576
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public IAuthenticationResult get() {
8484
}
8585
} catch(Exception ex) {
8686
if (ex instanceof AuthenticationException) {
87-
apiEvent.setApiErrorCode(((AuthenticationException) ex).getErrorCode());
87+
apiEvent.setApiErrorCode(((AuthenticationException) ex).errorCode());
8888
}
8989
clientApplication.log.error(
9090
LogHelper.createMessage(

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// All rights reserved.
3+
//
4+
// This code is licensed under the MIT License.
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files(the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions :
12+
//
13+
// The above copyright notice and this permission notice shall be included in
14+
// all copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
// THE SOFTWARE.
23+
124
package com.microsoft.aad.msal4j;
225

326
import lombok.AccessLevel;

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// All rights reserved.
3+
//
4+
// This code is licensed under the MIT License.
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files(the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions :
12+
//
13+
// The above copyright notice and this permission notice shall be included in
14+
// all copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
// THE SOFTWARE.
23+
124
package com.microsoft.aad.msal4j;
225

326
import lombok.AccessLevel;

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,21 @@
2323

2424
package com.microsoft.aad.msal4j;
2525

26+
import lombok.Getter;
27+
import lombok.experimental.Accessors;
28+
2629
/**
2730
* The exception type thrown when a claims challenge error occurs during token acquisition.
2831
*/
2932
public class ClaimsChallengeException extends AuthenticationException {
3033

34+
/**
35+
* claims challenge value
36+
*/
37+
@Accessors(fluent = true)
38+
@Getter
39+
private final String claims;
40+
3141
/**
3242
* Constructor
3343
*
@@ -39,14 +49,4 @@ public ClaimsChallengeException(String message, String claims) {
3949

4050
this.claims = claims;
4151
}
42-
43-
private final String claims;
44-
45-
/**
46-
*
47-
* @return claims challenge value
48-
*/
49-
public String getClaims() {
50-
return claims;
51-
}
5252
}

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

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,22 @@
2424
package com.microsoft.aad.msal4j;
2525

2626
import com.nimbusds.oauth2.sdk.auth.JWTAuthentication;
27-
import java.util.Objects;
27+
import lombok.EqualsAndHashCode;
28+
import lombok.Getter;
29+
import lombok.experimental.Accessors;
2830

2931
/***
3032
* Credential type containing an assertion of type
3133
* "urn:ietf:params:oauth:token-type:jwt".
3234
*/
35+
@Accessors(fluent = true)
36+
@Getter
37+
@EqualsAndHashCode
3338
public final class ClientAssertion {
3439

40+
public static final String assertionType = JWTAuthentication.CLIENT_ASSERTION_TYPE;
3541
private final String assertion;
3642

37-
private static final String assertionType = JWTAuthentication.CLIENT_ASSERTION_TYPE;
38-
3943
/**
4044
* Constructor to create credential with a jwt token encoded as a base64 url
4145
* encoded string.
@@ -50,37 +54,4 @@ public ClientAssertion(final String assertion) {
5054

5155
this.assertion = assertion;
5256
}
53-
54-
public String getAssertion() {
55-
return assertion;
56-
}
57-
58-
public String getAssertionType() {
59-
return assertionType;
60-
}
61-
62-
@Override
63-
public int hashCode() {
64-
int hash = 3;
65-
hash = 97 * hash + Objects.hashCode(this.assertion);
66-
return hash;
67-
}
68-
69-
@Override
70-
public boolean equals(Object obj) {
71-
if (this == obj) {
72-
return true;
73-
}
74-
if (obj == null) {
75-
return false;
76-
}
77-
if (getClass() != obj.getClass()) {
78-
return false;
79-
}
80-
final ClientAssertion other = (ClientAssertion) obj;
81-
if (!Objects.equals(this.assertion, other.assertion)) {
82-
return false;
83-
}
84-
return true;
85-
}
8657
}

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

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,23 @@
2323

2424
package com.microsoft.aad.msal4j;
2525

26-
import java.util.Objects;
26+
import lombok.EqualsAndHashCode;
27+
import lombok.Getter;
28+
import lombok.experimental.Accessors;
29+
2730

2831
/**
2932
* Credential including secret.
3033
*/
34+
@EqualsAndHashCode
3135
public final class ClientSecret implements IClientCredential {
3236

37+
/**
38+
* Gets the secret of the client requesting the token.
39+
*
40+
*/
41+
@Accessors(fluent = true)
42+
@Getter
3343
private final String clientSecret;
3444

3545
/**
@@ -45,38 +55,4 @@ public ClientSecret(final String clientSecret) {
4555

4656
this.clientSecret = clientSecret;
4757
}
48-
49-
/**
50-
* Gets the secret of the client requesting the token.
51-
*
52-
* @return string client secret value
53-
*/
54-
public String getClientSecret() {
55-
return clientSecret;
56-
}
57-
58-
@Override
59-
public int hashCode() {
60-
int hash = 3;
61-
hash = 71 * hash + Objects.hashCode(this.clientSecret);
62-
return hash;
63-
}
64-
65-
@Override
66-
public boolean equals(Object obj) {
67-
if (this == obj) {
68-
return true;
69-
}
70-
if (obj == null) {
71-
return false;
72-
}
73-
if (getClass() != obj.getClass()) {
74-
return false;
75-
}
76-
final ClientSecret other = (ClientSecret) obj;
77-
if (!Objects.equals(this.clientSecret, other.clientSecret)) {
78-
return false;
79-
}
80-
return true;
81-
}
8258
}

0 commit comments

Comments
 (0)