Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:

Expand Down
26 changes: 13 additions & 13 deletions mip-filesdk-java-sample/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,29 @@

<dependencies>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.28</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.1.0-alpha1</version>
</dependency>

<dependency>
<groupId>com.microsoft.informationprotection</groupId>
<artifactId>java-sdk-wrapper</artifactId>
<version>1.14.128</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>msal4j</artifactId>
<version>1.11.0</version>
<version>1.14.4-beta</version>
</dependency>


<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>

<dependency>
<groupId>com.microsoft.informationprotection</groupId>
<artifactId>java-sdk-wrapper</artifactId>
<version>1.12.61</version>
</dependency>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,68 +33,92 @@
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<String> 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();
}
return token;
}


private static IAuthenticationResult acquireTokenInteractive() throws Exception {
private static IAuthenticationResult acquireTokenInteractive(String authority, String resource, String claims)
throws Exception {

Set<String> 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<IAccount> 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
Expand All @@ -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
Expand Down