Skip to content

Commit 3ebe3c2

Browse files
authored
Merge pull request #105394 from navyasric/java-code-updates
Java code updates
2 parents 099f477 + ced509a commit 3ebe3c2

File tree

5 files changed

+279
-173
lines changed

5 files changed

+279
-173
lines changed

articles/active-directory/develop/quickstart-v2-java-webapp.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ To run this sample you will need:
9393
aad.secretKey=Enter_the_Client_Secret_Here
9494
aad.redirectUriSignin=https://localhost:8080/msal4jsample/secure/aad
9595
aad.redirectUriGraph=https://localhost:8080/msal4jsample/graph/me
96+
aad.msGraphEndpointHost="https://graph.microsoft.com/"
9697
```
9798
9899
> [!div renderon="docs"]
@@ -105,13 +106,13 @@ To run this sample you will need:
105106
1. To use https with localhost, fill in the server.ssl.key properties. To generate a self-signed certificate, use the keytool utility (included in JRE).
106107
107108
```
108-
Example:
109+
Example:
109110
keytool -genkeypair -alias testCert -keyalg RSA -storetype PKCS12 -keystore keystore.p12 -storepass password
110111

111112
server.ssl.key-store-type=PKCS12
112113
server.ssl.key-store=classpath:keystore.p12
113114
server.ssl.key-store-password=password
114-
server.ssl.key-alias=testCert
115+
server.ssl.key-alias=testCert
115116
```
116117
117118
Put the generated keystore file in the "resources" folder.

articles/active-directory/develop/scenario-daemon-acquire-token.md

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -124,24 +124,48 @@ else:
124124
This code is extracted from the [MSAL Java dev samples](https://github.com/AzureAD/microsoft-authentication-library-for-java/blob/dev/src/samples/confidential-client/).
125125

126126
```Java
127-
ClientCredentialParameters clientCredentialParam = ClientCredentialParameters.builder(
128-
Collections.singleton(GRAPH_DEFAULT_SCOPE))
129-
.build();
130-
131-
CompletableFuture<IAuthenticationResult> future = app.acquireToken(clientCredentialParam);
132-
133-
BiConsumer<IAuthenticationResult, Throwable> processAuthResult = (res, ex) -> {
134-
if (ex != null) {
135-
System.out.println("Oops! We have an exception - " + ex.getMessage());
136-
}
137-
System.out.println("Returned ok - " + res);
138-
System.out.println("ID Token - " + res.idToken());
139-
140-
/* Call a protected API with res.accessToken() */
141-
};
142-
143-
future.whenCompleteAsync(processAuthResult);
144-
future.join();
127+
private static IAuthenticationResult acquireToken() throws Exception {
128+
129+
// Load token cache from file and initialize token cache aspect. The token cache will have
130+
// dummy data, so the acquireTokenSilently call will fail.
131+
TokenCacheAspect tokenCacheAspect = new TokenCacheAspect("sample_cache.json");
132+
133+
IClientCredential credential = ClientCredentialFactory.createFromSecret(CLIENT_SECRET);
134+
ConfidentialClientApplication cca =
135+
ConfidentialClientApplication
136+
.builder(CLIENT_ID, credential)
137+
.authority(AUTHORITY)
138+
.setTokenCacheAccessAspect(tokenCacheAspect)
139+
.build();
140+
141+
IAuthenticationResult result;
142+
try {
143+
SilentParameters silentParameters =
144+
SilentParameters
145+
.builder(SCOPE)
146+
.build();
147+
148+
// try to acquire token silently. This call will fail since the token cache does not
149+
// have a token for the application you are requesting an access token for
150+
result = cca.acquireTokenSilently(silentParameters).join();
151+
} catch (Exception ex) {
152+
if (ex.getCause() instanceof MsalException) {
153+
154+
ClientCredentialParameters parameters =
155+
ClientCredentialParameters
156+
.builder(SCOPE)
157+
.build();
158+
159+
// Try to acquire a token. If successful, you should see
160+
// the token information printed out to console
161+
result = cca.acquireToken(parameters).join();
162+
} else {
163+
// Handle other exceptions accordingly
164+
throw ex;
165+
}
166+
}
167+
return result;
168+
}
145169
```
146170

147171
---

articles/active-directory/develop/scenario-daemon-app-configuration.md

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,11 @@ When you build a confidential client with certificates, the [parameters.json](ht
103103

104104
# [Java](#tab/java)
105105

106-
[TestData](https://github.com/AzureAD/microsoft-authentication-library-for-java/blob/dev/src/samples/public-client/TestData.java) is the class used to configure MSAL Java dev samples:
107-
108106
```Java
109-
public class TestData {
110-
111-
final static String TENANT_SPECIFIC_AUTHORITY = "https://login.microsoftonline.com/<TenantId>/";
112-
final static String GRAPH_DEFAULT_SCOPE = "https://graph.microsoft.com/.default";
113-
final static String CONFIDENTIAL_CLIENT_ID = "";
114-
final static String CONFIDENTIAL_CLIENT_SECRET = "";
115-
}
107+
private final static String CLIENT_ID = "";
108+
private final static String AUTHORITY = "https://login.microsoftonline.com/<tenant>/";
109+
private final static String CLIENT_SECRET = "";
110+
private final static Set<String> SCOPE = Collections.singleton("https://graph.microsoft.com/.default");
116111
```
117112

118113
---
@@ -151,6 +146,9 @@ import com.microsoft.aad.msal4j.ClientCredentialFactory;
151146
import com.microsoft.aad.msal4j.ClientCredentialParameters;
152147
import com.microsoft.aad.msal4j.ConfidentialClientApplication;
153148
import com.microsoft.aad.msal4j.IAuthenticationResult;
149+
import com.microsoft.aad.msal4j.IClientCredential;
150+
import com.microsoft.aad.msal4j.MsalException;
151+
import com.microsoft.aad.msal4j.SilentParameters;
154152
```
155153

156154
---
@@ -186,11 +184,13 @@ app = msal.ConfidentialClientApplication(
186184
# [Java](#tab/java)
187185

188186
```Java
189-
ConfidentialClientApplication app = ConfidentialClientApplication.builder(
190-
TestData.CONFIDENTIAL_CLIENT_ID,
191-
ClientCredentialFactory.create(TestData.CONFIDENTIAL_CLIENT_SECRET))
192-
.authority(TestData.TENANT_SPECIFIC_AUTHORITY)
193-
.build();
187+
IClientCredential credential = ClientCredentialFactory.createFromSecret(CLIENT_SECRET);
188+
189+
ConfidentialClientApplication cca =
190+
ConfidentialClientApplication
191+
.builder(CLIENT_ID, credential)
192+
.authority(AUTHORITY)
193+
.build();
194194
```
195195

196196
---
@@ -233,11 +233,13 @@ In MSAL Java, there are two builders to instantiate the confidential client appl
233233
InputStream pkcs12Certificate = ... ; /* Containing PCKS12-formatted certificate*/
234234
string certificatePassword = ... ; /* Contains the password to access the certificate */
235235

236-
ConfidentialClientApplication app = ConfidentialClientApplication.builder(
237-
TestData.CONFIDENTIAL_CLIENT_ID,
238-
ClientCredentialFactory.create(pkcs12Certificate, certificatePassword))
239-
.authority(TestData.TENANT_SPECIFIC_AUTHORITY)
240-
.build();
236+
IClientCredential credential = ClientCredentialFactory.createFromCertificate(pkcs12Certificate, certificatePassword);
237+
238+
ConfidentialClientApplication cca =
239+
ConfidentialClientApplication
240+
.builder(CLIENT_ID, credential)
241+
.authority(AUTHORITY)
242+
.build();
241243
```
242244

243245
or
@@ -246,11 +248,13 @@ or
246248
PrivateKey key = getPrivateKey(); /* RSA private key to sign the assertion */
247249
X509Certificate publicCertificate = getPublicCertificate(); /* x509 public certificate used as a thumbprint */
248250

249-
ConfidentialClientApplication app = ConfidentialClientApplication.builder(
250-
TestData.CONFIDENTIAL_CLIENT_ID,
251-
ClientCredentialFactory.create(rsaPrivateKey, publicKeyCertificate))
252-
.authority(TestData.TENANT_SPECIFIC_AUTHORITY)
253-
.build();
251+
IClientCredential credential = ClientCredentialFactory.createFromCertificate(key, publicCertificate);
252+
253+
ConfidentialClientApplication cca =
254+
ConfidentialClientApplication
255+
.builder(CLIENT_ID, credential)
256+
.authority(AUTHORITY)
257+
.build();
254258
```
255259

256260
---
@@ -312,7 +316,15 @@ For details, see the MSAL Python reference documentation for [ConfidentialClient
312316

313317
# [Java](#tab/java)
314318

315-
MSAL Java is in public preview. Signed assertions aren't yet supported.
319+
```Java
320+
IClientCredential credential = ClientCredentialFactory.createFromClientAssertion(assertion);
321+
322+
ConfidentialClientApplication cca =
323+
ConfidentialClientApplication
324+
.builder(CLIENT_ID, credential)
325+
.authority(AUTHORITY)
326+
.build();
327+
```
316328

317329
---
318330

0 commit comments

Comments
 (0)