0

I'm writing a Java app that aquires a token via the provided MS utility lib called adal4j.

It provides finished methods, I just need to provide the username, password, authority and my clientid. The code looks like this and works perfectly:

public AuthenticationResult getAccessTokenFromUserCredentials() throws Exception {
        AuthenticationContext context;
        AuthenticationResult result;
        ExecutorService service = null;
        try {
            service = Executors.newFixedThreadPool(1);
            context = new AuthenticationContext(this.authority, false, service);
            Future<AuthenticationResult> future = context.acquireToken(
                    "https://mysite.sharepoint.com", this.clientId, this.userName, password,
                    null);
            result = future.get();
        } finally {
            service.shutdown();
        }

        if (result == null) {
            throw new ServiceUnavailableException(
                    "authentication result was null");
        }
        return result;
    }

As I said, this works perfectly, but due to some proxy issues cannot be used where I need to use it.

My question is, can I get the accessToken via a simple HTTP request somehow? Like:

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("username", username);
conn.setRequestProperty("password", password);

Any tips for getting an access token from Java with a simple HTTP request would be greatly appreciated, thanks!

0

Actually, this is a OAUTH authentication process. So the library goes to a first endpoint (login.microsoftonline.com) to authenticate the user (generating an auth token), then requests an access token for the resource you need (for example a site collection access token) using the auth token you got from the first call.

Generally speaking, such a process requires redirection or iframes since it requires the user authentication on the microsoft domain in the first place. I wonder how the library manages to take only credentials and give back an access token.

Short answer, you can actually reimplement the library using a proxy, but microsoft uses the standard OAUTH process, so all you gotta do is implement this authentication process yourself.

Official documentation : https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-protocols-oauth-code

Auth token URL : https://login.microsoftonline.com/{tenant}/oauth2/authorize with parameters

Acces token URL : https://login.microsoftonline.com/{tenant}/oauth2/token with parameters

Hope this helps!

  • Thanks, this helpts, but... If I see correctly, this requires the user to login with his credentials in the browser, whereas the adal4j method that I currently use does not, probably via some black magic. – Franjo Pintarić Jul 26 '18 at 13:16
  • I believe there is no black magic :) after looking at the source code, it seems they get the access and auth tokens sending credentials in a body request. You can look it up in details and with courage :) github.com/AzureAD/azure-activedirectory-library-for-java/tree/… – Abner Peter Jul 26 '18 at 13:30
  • Thanks for the help, I will try to create my own class using their source code, I'll post the results – Franjo Pintarić Jul 26 '18 at 13:51
  • ADAL requires the user to use their credentials there's no way to break this. unless you create an Client id and client secret, then you can use that to log in, but it will act as an application account – Mike Jul 26 '18 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.