Skip to content

Commit

Permalink
support for Azure.authenticate(File propertiesFile)
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown authored and unknown committed May 11, 2016
1 parent 69f21a2 commit 788f625
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
import com.microsoft.aad.adal4j.ClientCredential;
import com.microsoft.rest.credentials.TokenCredentials;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

Expand Down Expand Up @@ -52,6 +55,78 @@ public ApplicationTokenCredentials(String clientId, String domain, String secret
}
}


/**
* Contains the keys of the settings in a Properties file to read credentials from
*/
private enum CredentialSettings {
SUBSCRIPTION_ID("subscription"),
TENANT_ID("tenant"),
CLIENT_ID("client"),
CLIENT_KEY("key"),
MANAGEMENT_URI("managementURI"),
BASE_URL("baseURL"),
AUTH_URL("authURL");

private final String name;

private CredentialSettings(String name) {
this.name = name;
}

@Override
public String toString() {
return this.name;
}
}


/**
* Initializes the credentials based on the provided credentials file
* @param credentialsFile A file with credentials, using the standard Java properties format
* and the following keys:
* subscription=<subscription-id>
* tenant=<tenant-id>
* client=<client-id>
* key=<client-key>
* managementURI=<management-URI>
* baseURL=<base-URL>
* authURL=<authentication-URL>
* @return The credentials based on the file.
* @throws IOException
*/
public static ApplicationTokenCredentials fromFile(File credentialsFile) throws IOException {
// Set defaults
Properties authSettings = new Properties();
authSettings.put(CredentialSettings.AUTH_URL.toString(), AzureEnvironment.AZURE.getAuthenticationEndpoint());
authSettings.put(CredentialSettings.BASE_URL.toString(), AzureEnvironment.AZURE.getBaseUrl());
authSettings.put(CredentialSettings.MANAGEMENT_URI.toString(), AzureEnvironment.AZURE.getTokenAudience());

// Load the credentials from the file
FileInputStream credentialsFileStream = new FileInputStream(credentialsFile);
authSettings.load(credentialsFileStream);
credentialsFileStream.close();

final String clientId = authSettings.getProperty(CredentialSettings.CLIENT_ID.toString());
final String tenantId = authSettings.getProperty(CredentialSettings.TENANT_ID.toString());
final String clientKey = authSettings.getProperty(CredentialSettings.CLIENT_KEY.toString());
final String mgmtUri = authSettings.getProperty(CredentialSettings.MANAGEMENT_URI.toString());
final String authUrl = authSettings.getProperty(CredentialSettings.AUTH_URL.toString());
final String baseUrl = authSettings.getProperty(CredentialSettings.BASE_URL.toString());

return new ApplicationTokenCredentials(
clientId,
tenantId,
clientKey,
new AzureEnvironment(
authUrl,
mgmtUri,
true,
baseUrl)
);
}


/**
* Gets the active directory application client id.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
* An instance of this class describes an environment in Azure.
*/
public final class AzureEnvironment {
/**
/**
* Base URL for calls to Azure management API.
*/
private final String baseURL;

/**
* ActiveDirectory Endpoint for the Azure Environment.
*/
private String authenticationEndpoint;
Expand All @@ -33,10 +38,15 @@ public final class AzureEnvironment {
* @param validateAuthority whether the authentication endpoint should
* be validated with Azure AD.
*/
public AzureEnvironment(String authenticationEndpoint, String tokenAudience, boolean validateAuthority) {
public AzureEnvironment(
String authenticationEndpoint,
String tokenAudience,
boolean validateAuthority,
String baseUrl) {
this.authenticationEndpoint = authenticationEndpoint;
this.tokenAudience = tokenAudience;
this.validateAuthority = validateAuthority;
this.baseURL = baseUrl;
}

/**
Expand All @@ -45,16 +55,26 @@ public AzureEnvironment(String authenticationEndpoint, String tokenAudience, boo
public static final AzureEnvironment AZURE = new AzureEnvironment(
"https://login.windows.net/",
"https://management.core.windows.net/",
true);
true,
"https://management.azure.com/");

/**
* Provides the settings for authentication with Azure China.
*/
public static final AzureEnvironment AZURE_CHINA = new AzureEnvironment(
"https://login.chinacloudapi.cn/",
"https://management.core.chinacloudapi.cn/",
true);
true,
"https://management.chinacloudapi.cn"); //TODO: Should be confirmed...

/**
* Gets the base URL of the management service
* @return the Base URL for the management service
*/
public String getBaseUrl() {
return this.baseURL;
}

/**
* Gets the ActiveDirectory Endpoint for the Azure Environment.
*
Expand Down

0 comments on commit 788f625

Please sign in to comment.