diff --git a/README.md b/README.md index bd067af..404729b 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ This project framework provides the following features: ### Prerequisites -- Windows 10 or Ubuntu 18.04/20.04 +- Windows 11 or Ubuntu 20.04/22.04 - [MIP SDK Java Wrapper - 1.14 Preview](https://aka.ms/mipsdkbins) - Visual Studio Code - An Azure AD Application Registration for use with a [MIP SDK public client.](https://docs.microsoft.com/en-us/information-protection/develop/setup-configure-mip#register-a-client-application-with-azure-active-directory) @@ -67,6 +67,7 @@ This project framework provides the following features: appInfo.setApplicationName("MIP SDK Java Sample"); appInfo.setApplicationVersion("1.14"); ``` +8. Update line 68 in **Action.java** to include the path to where you've extracted the MIP SDK DLLs. At this point, you should be able to build the project. If your app states that dependencies are missing: diff --git a/mip-filesdk-java-sample/pom.xml b/mip-filesdk-java-sample/pom.xml index bb9a28e..c9267a7 100644 --- a/mip-filesdk-java-sample/pom.xml +++ b/mip-filesdk-java-sample/pom.xml @@ -20,29 +20,29 @@ - - org.slf4j - slf4j-api - 1.7.28 - + + org.slf4j + slf4j-simple + 2.1.0-alpha1 + + + com.microsoft.informationprotection + java-sdk-wrapper + 1.14.128 + com.microsoft.azure msal4j - 1.11.0 + 1.14.4-beta + junit junit 4.13.2 - - - - com.microsoft.informationprotection - java-sdk-wrapper - 1.12.61 - + diff --git a/mip-filesdk-java-sample/src/main/java/com/microsoft/mipsdksample/Action.java b/mip-filesdk-java-sample/src/main/java/com/microsoft/mipsdksample/Action.java index fa95ffb..cfb2bc9 100644 --- a/mip-filesdk-java-sample/src/main/java/com/microsoft/mipsdksample/Action.java +++ b/mip-filesdk-java-sample/src/main/java/com/microsoft/mipsdksample/Action.java @@ -65,7 +65,7 @@ public Action(ApplicationInfo appInfo, String userName) throws InterruptedExcept authDelegate = new AuthDelegateImpl(appInfo); // Initialize MIP For File SDK components. - MIP.initialize(MipComponent.FILE, "C:\\mip\\releases\\1.11.53\\java\\file\\bins\\debug\\amd64"); + MIP.initialize(MipComponent.FILE, "C:\\mip\\binaries\\java-windows\\bins\\release\\amd64"); // Create MIP Configuration // MIP Configuration can be used to set various delegates, feature flags, and other SDK behavior. diff --git a/mip-filesdk-java-sample/src/main/java/com/microsoft/mipsdksample/App.java b/mip-filesdk-java-sample/src/main/java/com/microsoft/mipsdksample/App.java index 9397031..c3d9d48 100644 --- a/mip-filesdk-java-sample/src/main/java/com/microsoft/mipsdksample/App.java +++ b/mip-filesdk-java-sample/src/main/java/com/microsoft/mipsdksample/App.java @@ -44,9 +44,9 @@ public static void main(String[] args) throws InterruptedException, ExecutionExc ApplicationInfo appInfo = new ApplicationInfo(); FileOptions options = new FileOptions(); - appInfo.setApplicationId("YOUR CLIENT ID"); + appInfo.setApplicationId("7dc8a6a5-1798-427f-8b13-088de27760c1"); appInfo.setApplicationName("MIP SDK Java Sample"); - appInfo.setApplicationVersion("1.11"); + appInfo.setApplicationVersion("1.14"); System.out.print("Enter a username: "); String userName = reader.readLine(); diff --git a/mip-filesdk-java-sample/src/main/java/com/microsoft/mipsdksample/AuthDelegateImpl.java b/mip-filesdk-java-sample/src/main/java/com/microsoft/mipsdksample/AuthDelegateImpl.java index 5172950..e7e93fc 100644 --- a/mip-filesdk-java-sample/src/main/java/com/microsoft/mipsdksample/AuthDelegateImpl.java +++ b/mip-filesdk-java-sample/src/main/java/com/microsoft/mipsdksample/AuthDelegateImpl.java @@ -33,38 +33,53 @@ import com.microsoft.aad.msal4j.Prompt; import com.microsoft.aad.msal4j.PublicClientApplication; import com.microsoft.aad.msal4j.SilentParameters; +import com.microsoft.aad.msal4j.SystemBrowserOptions; import com.microsoft.informationprotection.ApplicationInfo; import com.microsoft.informationprotection.IAuthDelegate; import com.microsoft.informationprotection.Identity; import java.net.URI; +import java.net.URISyntaxException; import java.util.Collections; import java.util.Set; public class AuthDelegateImpl implements IAuthDelegate { - private static String CLIENT_ID = ""; - private static String AUTHORITY = ""; - private static Set SCOPE = Collections.singleton(""); + // Set to true if application registration is multi-tenant + private static boolean isMultiTenant = true; - public AuthDelegateImpl(ApplicationInfo appInfo) - { - CLIENT_ID = appInfo.getApplicationId(); + // Only required if application registratio is set to single tenant + private static String tenantId = "d86993cb-5731-4a0e-a83c-464c851bf053"; + + private static ApplicationInfo _appInfo; + + public AuthDelegateImpl(ApplicationInfo appInfo) { + _appInfo = appInfo; } @Override public String acquireToken(Identity userName, String authority, String resource, String claims) { - if(resource.endsWith("/")){ - SCOPE = Collections.singleton(resource + ".default"); - } - else { - SCOPE = Collections.singleton(resource + "/.default"); + + // If the application is a single tenant application, replace /common with the + // tenant Id. + if (authority.toLowerCase().contains("common") && isMultiTenant == false) { + + URI authorityUri; + try { + authorityUri = new URI(authority); + } catch (URISyntaxException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return ""; + } + + authority = String.format("https://%s/%s", authorityUri.getHost(), tenantId); } - AUTHORITY = authority; String token = ""; try { - token = acquireTokenInteractive().accessToken(); + token = acquireTokenInteractive(authority, resource, claims) + .accessToken(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -72,29 +87,38 @@ public String acquireToken(Identity userName, String authority, String resource, return token; } - - private static IAuthenticationResult acquireTokenInteractive() throws Exception { + private static IAuthenticationResult acquireTokenInteractive(String authority, String resource, String claims) + throws Exception { + + Set scopes = Collections.singleton(""); + + // Append .default to resource to generate scopes + if (resource.endsWith("/")) { + scopes = Collections.singleton(resource + ".default"); + } else { + scopes = Collections.singleton(resource + "/.default"); + } - // Load token cache from file and initialize token cache aspect. The token cache will have - // dummy data, so the acquireTokenSilently call will fail. + // Load token cache from file and initialize token cache aspect. The token cache + // will have dummy data, so the acquireTokenSilently call will fail. TokenCacheAspect tokenCacheAspect = new TokenCacheAspect("sample_cache.json"); - PublicClientApplication pca = PublicClientApplication.builder(CLIENT_ID) - .authority(AUTHORITY) + PublicClientApplication pca = PublicClientApplication.builder(_appInfo.getApplicationId()) + .authority(authority) .setTokenCacheAccessAspect(tokenCacheAspect) .build(); Set accountsInCache = pca.getAccounts().join(); - // Take first account in the cache. In a production application, you would filter + // Take first account in the cache. In a production application, you would + // filter // accountsInCache to get the right account for the user authenticating. IAccount account = accountsInCache.iterator().next(); IAuthenticationResult result; try { - SilentParameters silentParameters = - SilentParameters - .builder(SCOPE, account) - .build(); + SilentParameters silentParameters = SilentParameters + .builder(scopes, account) + .build(); // try to acquire token silently. This call will fail since the token cache // does not have any data for the user you are trying to acquire a token for @@ -103,13 +127,12 @@ private static IAuthenticationResult acquireTokenInteractive() throws Exception if (ex.getCause() instanceof MsalException) { InteractiveRequestParameters parameters = InteractiveRequestParameters - .builder(new URI("http://localhost")) - .prompt(Prompt.SELECT_ACCOUNT) // Change this value to avoid repeated auth prompts. - .scopes(SCOPE) + .builder(new URI("http://localhost")) + .prompt(Prompt.SELECT_ACCOUNT) // Change this value to avoid repeated auth prompts. + .scopes(scopes) + .claimsChallenge(claims) .build(); - // Try to acquire a token interactively with system browser. If successful, you should see - // the token and account information printed out to console result = pca.acquireToken(parameters).join(); } else { // Handle other exceptions accordingly