1

I'm working on implementing integrating Azure AD login authentication to my web app. I have created an account in azure development portal and registered my app details.

my app URL -> https://my-sample-app/my.dashboard/

my redirect url is ->https://my-sample-app/my.dashboard/ws/aad/callback/

Note : ws that comes after my app url is the servlet adapter configured

my web app is a java app and i'm using ADAL java SDK

I have referred this article Authenticate to an Azure API App from Java and did the similar way

this is the code logic written under web path "aad/callback"

    String appIdUri = System.getProperty("azure.app.id.uri", "https://login.microsoftonline.com/");

    String authority = System.getProperty("azure.authority.url", "https://login.microsoftonline.com/my-sample-app.onmicrosoft.com");

    String clientId = System.getProperty("azure.client.id", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");

    String clientSecret = System.getProperty("azure.client.secret", "xxxxxxxxxxxxxxxxxxxxxxxx");

    AuthenticationContext context = null;

    AuthenticationResult result = null;

    ExecutorService service = null;

    UserVO userVO = null;

    try {

      HttpsURLConnection conn = (HttpsURLConnection) new URL(appIdUri).openConnection();

      service = Executors.newFixedThreadPool(1);

      context = new AuthenticationContext(authority, false, service);

      ClientCredential credential = new ClientCredential(clientId, clientSecret);

      Future<AuthenticationResult> future = context.acquireToken(appIdUri, credential, null);

      result = future.get();

      HttpSession session = request.getSession();

      LOGGER.info("session :{}",session);

      String accessToken = null;

      if (result == null) {

        throw new ServiceUnavailableException("authentication result was null");

      } else {

        accessToken = result.getAccessToken();

      }

      String data = "{\"access_token\": \"" + accessToken + "\"}";

      LOGGER.info("access_token :{}", data);

      conn.setRequestMethod("POST");

      conn.setDoOutput(true);

      conn.addRequestProperty("Content-Length", data.length() + "");

      new DataOutputStream(conn.getOutputStream()).writeBytes(data);

      String authTokenResp = IOUtils.toString(conn.getInputStream());

      Gson gson = new Gson();

      Map<String, Object> map = gson.fromJson(authTokenResp, Map.class);

      String authenticationToken = (String) map.get("authenticationToken");

      System.out.println("Authentication Token: "+authenticationToken);

I'm able to see the access token value in the log statement but the authTokenResp output value that i received from authTokenResp = IOUtils.toString(conn.getInputStream()); looks like some html page (probably the login page response of portal.office.com ) doesn't has key authenticationToken in it.

I think I have made mistake by mentioning wrong URL for the appIdUri.

please can i someone tell me what URL should be given for appIdUri ? where can i find this URL value in azure portal ?

Heisenberg
  • 147
  • 1
  • 4
  • 14
  • The below code worked fine. now i'm able to get the access token and can see the user info in the access token Modified the authority URL as https://ogin.microsoftonline.com{your_tenant_name}/oauth2/token Did the below code changes: AuthenticationContext context = new AuthenticationContext(authority, false, service); ClientCredential credential = new ClientCredential(clientId, clientSecret); Future future = context.acquireTokenByAuthorizationCode(authCode, URI.create(redirectUrl), credential, null); result = future.get(); – Heisenberg Sep 12 '19 at 09:27

1 Answers1

0

This sample is just a client credential flow to get access token.

please can i someone tell me what URL should be given for appIdUri ? where can i find this URL value in azure portal ?

The first parameter of acquireToken method is the value of a resource which you want to access.It is the App ID URI of the target web API (secured resource). To find the App ID URI, in the Azure Portal, click Azure Active Directory, click Application registrations, open the application's Settings page, then click Properties. It may also be an external resource like https://graph.microsoft.com. This is required in one of either the authorization or token requests.

Is my-sample-app.onmicrosoft.com your tenant name?

String authority = System.getProperty("azure.authority.url", "https://login.microsoftonline.com/{your_tenant_name}");

If you want to integrate Azure AD login authentication to your web app, you should refer to this sample.

Tony Ju
  • 14,891
  • 3
  • 17
  • 31
  • Thanks for your reply @Tony Ju. Problem was with my authority URL. after including the token end point to my authority URL , i'm able to receive the access token. I'm able to see the logged in user details in the access token. Made it work after changing my authority URL like below, https://login.microsoftonline.com/{your_tenant_name}/oauth2/token – Heisenberg Sep 12 '19 at 09:21