Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authenticate with device code #33

Merged
merged 4 commits into from
Oct 11, 2018
Merged
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,32 @@
import com.microsoft.aad.adal4j.AuthenticationContext;
import com.microsoft.aad.adal4j.AuthenticationResult;
import com.microsoft.aad.adal4j.ClientCredential;
import com.microsoft.aad.adal4j.DeviceCode;
import com.microsoft.azure.kusto.data.exceptions.DataClientException;
import com.microsoft.azure.kusto.data.exceptions.DataServiceException;

import javax.naming.ServiceUnavailableException;
import java.awt.*;
import java.net.URI;
import java.net.MalformedURLException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

class AadAuthenticationHelper {
public class AadAuthenticationHelper {

private final static String DEFAULT_AAD_TENANT = "common";
private final static String CLIENT_ID = "db662dc1-0cfe-4e1c-a843-19a68e65be58";
private final static String RESOURCE = "https://graph.windows.net";

private ClientCredential clientCredential;
private String userUsername;
private String userPassword;
private String clusterUrl;
private String aadAuthorityUri;

AadAuthenticationHelper(ConnectionStringBuilder csb) {
public AadAuthenticationHelper(ConnectionStringBuilder csb) {
clusterUrl = csb.getClusterUrl();

if (!isNullOrEmpty(csb.getApplicationClientId()) && !isNullOrEmpty(csb.getApplicationKey())) {
Expand All @@ -38,6 +43,35 @@ class AadAuthenticationHelper {
aadAuthorityUri = String.format("https://login.microsoftonline.com/%s", aadAuthorityId);
}

public AuthenticationResult acquireAccessTokenUsingDeviceCodeFlow() throws Exception {
AuthenticationContext context = null;
AuthenticationResult result = null;
ExecutorService service = null;
try {
service = Executors.newFixedThreadPool(1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is not here the same as using: Executors.newSingleThreadExecutor()?

context = new AuthenticationContext( aadAuthorityUri, true, service);

Future<DeviceCode> future = context.acquireDeviceCode(clientCredential.getClientId(), RESOURCE, null);
DeviceCode deviceCode = future.get();
System.out.println(deviceCode.getMessage());
System.out.println("Press Enter after authenticating");
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().browse(new URI(deviceCode.getVerificationUrl()));
}
System.in.read();
Future<AuthenticationResult> futureResult = context.acquireTokenByDeviceCode(deviceCode, null);
result = futureResult.get();

} finally {
assert service != null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to if(service != null) ...

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

String acquireAccessToken() throws DataServiceException, DataClientException {
if (clientCredential != null) {
return acquireAadApplicationAccessToken().getAccessToken();
Expand Down