|
| 1 | +package com.microsoft.msi; |
| 2 | + |
| 3 | +import com.microsoft.aad.msal4j.IAuthenticationResult; |
| 4 | +import com.microsoft.aad.msal4j.ManagedIdentityApplication; |
| 5 | +import com.microsoft.aad.msal4j.ManagedIdentityId; |
| 6 | +import com.microsoft.aad.msal4j.ManagedIdentityParameters; |
| 7 | +import org.slf4j.Logger; |
| 8 | +import org.slf4j.LoggerFactory; |
| 9 | + |
| 10 | +import java.io.BufferedReader; |
| 11 | +import java.io.IOException; |
| 12 | +import java.io.InputStreamReader; |
| 13 | + |
| 14 | +/** |
| 15 | + * Sample app to test MSI using a single jar. |
| 16 | + * To create a jar run mvn install -Dmaven.test.skip=true for msal4j pom and then msi-sample-jar pom. |
| 17 | + * Copy the jar with dependencies and run on a VM or cloud shell using java -jar msi-sample-jar-1.0.1-jar-with-dependencies.jar |
| 18 | + */ |
| 19 | +public class App |
| 20 | +{ |
| 21 | + public static void main( String[] args ) throws IOException { |
| 22 | + String response; |
| 23 | + String resource = "https://management.azure.com"; |
| 24 | + int option = 1; |
| 25 | + BufferedReader reader = new BufferedReader( |
| 26 | + new InputStreamReader(System.in)); |
| 27 | + final Logger logger = LoggerFactory.getLogger(App.class); |
| 28 | + |
| 29 | + while (option != 0) { |
| 30 | + System.out.println("Enter one of the following options to create a managed identity application:"); |
| 31 | + System.out.println("1: System assigned managed identity"); |
| 32 | + System.out.println("2: User assigned managed identity"); |
| 33 | + System.out.println("0: Quit"); |
| 34 | + |
| 35 | + option = Integer.parseInt(reader.readLine()); |
| 36 | + |
| 37 | + switch (option) { |
| 38 | + case 1: |
| 39 | + acquireTokenWithSAMI(resource, logger, reader); |
| 40 | + break; |
| 41 | + case 2: |
| 42 | + acquireTokenWithUAMI(resource, logger, reader); |
| 43 | + break; |
| 44 | + case 0: |
| 45 | + return; |
| 46 | + default: |
| 47 | + System.out.println("Invalid option, try again."); |
| 48 | + } |
| 49 | + |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private static void acquireTokenWithSAMI(String resource, Logger logger, BufferedReader reader) throws IOException { |
| 54 | + System.out.println("Enter a scope to acquire token for."); |
| 55 | + resource = reader.readLine(); |
| 56 | + |
| 57 | + ManagedIdentityApplication msiApp = ManagedIdentityApplication |
| 58 | + .builder(ManagedIdentityId.systemAssigned()) |
| 59 | + .logPii(true) |
| 60 | + .build(); |
| 61 | + |
| 62 | + try { |
| 63 | + logger.info("Trying to acquire a token for system assigned managed identity with provided resource."); |
| 64 | + IAuthenticationResult result = msiApp.acquireTokenForManagedIdentity(ManagedIdentityParameters.builder(resource).build()).get(); |
| 65 | + logger.info("Access token recieved: " + result.accessToken().substring(0, 10) + "\nScopes: " + result.scopes()); |
| 66 | + } catch (Exception e) { |
| 67 | + e.printStackTrace(); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private static void acquireTokenWithUAMI(String resource, Logger logger, BufferedReader reader) throws IOException { |
| 72 | + System.out.println("Enter the options to create a user assigned managed identity"); |
| 73 | + System.out.println("1. User assigned client id."); |
| 74 | + System.out.println("2. User assigned resource id."); |
| 75 | + |
| 76 | + int option = Integer.parseInt(reader.readLine()); |
| 77 | + |
| 78 | + ManagedIdentityId msiId; |
| 79 | + |
| 80 | + switch (option) { |
| 81 | + case 1: |
| 82 | + System.out.println("Enter client id of the user assigned managed identity"); |
| 83 | + String clientId = reader.readLine(); |
| 84 | + msiId = ManagedIdentityId.userAssignedClientId(clientId); |
| 85 | + break; |
| 86 | + case 2: |
| 87 | + System.out.println("Enter resource id of the user assigned managed identity"); |
| 88 | + String resourceId = reader.readLine(); |
| 89 | + msiId = ManagedIdentityId.userAssignedResourceId(resourceId); |
| 90 | + break; |
| 91 | + default: |
| 92 | + System.out.println("Invalid option"); |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + System.out.println("Enter a scope to acquire token for. "); |
| 97 | + resource = reader.readLine(); |
| 98 | + |
| 99 | + ManagedIdentityApplication msiApp = ManagedIdentityApplication |
| 100 | + .builder(msiId) |
| 101 | + .logPii(true) |
| 102 | + .build(); |
| 103 | + |
| 104 | + try { |
| 105 | + logger.info("Trying to acquire a token for system assigned managed identity with provided resource."); |
| 106 | + IAuthenticationResult result = msiApp.acquireTokenForManagedIdentity(ManagedIdentityParameters.builder(resource).build()).get(); |
| 107 | + logger.info("Access token received: " + result.accessToken().substring(0, 10) + "\nScopes: " + result.scopes()); |
| 108 | + } catch (Exception e) { |
| 109 | + e.printStackTrace(); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments