From 111f14d1acd1512357844f0f56d66341f2fcc567 Mon Sep 17 00:00:00 2001 From: Jianghao Lu Date: Mon, 18 Jan 2016 15:57:41 -0800 Subject: [PATCH 1/2] static token cache --- .../ServiceClientInterfaceTemplate.cshtml | 2 +- .../credentials/UserTokenCredentials.java | 26 ++++++++++++++----- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/AutoRest/Generators/Java/Java/Templates/ServiceClientInterfaceTemplate.cshtml b/AutoRest/Generators/Java/Java/Templates/ServiceClientInterfaceTemplate.cshtml index cc8a235462..b78b59d49f 100644 --- a/AutoRest/Generators/Java/Java/Templates/ServiceClientInterfaceTemplate.cshtml +++ b/AutoRest/Generators/Java/Java/Templates/ServiceClientInterfaceTemplate.cshtml @@ -114,7 +114,7 @@ if(!property.IsReadOnly) @: * @@param @param.Name @((param.Documentation ?? "the " + param.Type.ToString() + " value").EscapeXmlComment()) } - @foreach (var exception in method.ExceptionStatements) + foreach (var exception in method.ExceptionStatements) { @: * @@throws @exception } diff --git a/ClientRuntimes/Java/azure-android-client-authentication/src/main/java/com/microsoft/rest/credentials/UserTokenCredentials.java b/ClientRuntimes/Java/azure-android-client-authentication/src/main/java/com/microsoft/rest/credentials/UserTokenCredentials.java index 1501355265..bab3a205b3 100644 --- a/ClientRuntimes/Java/azure-android-client-authentication/src/main/java/com/microsoft/rest/credentials/UserTokenCredentials.java +++ b/ClientRuntimes/Java/azure-android-client-authentication/src/main/java/com/microsoft/rest/credentials/UserTokenCredentials.java @@ -12,6 +12,7 @@ import com.microsoft.aad.adal.AuthenticationCallback; import com.microsoft.aad.adal.AuthenticationContext; import com.microsoft.aad.adal.AuthenticationResult; +import com.microsoft.aad.adal.DefaultTokenCacheStore; import com.microsoft.aad.adal.PromptBehavior; import java.io.IOException; @@ -36,6 +37,8 @@ public class UserTokenCredentials extends TokenCredentials { private Activity activity; /** The count down latch to synchronize token acquisition. */ private CountDownLatch signal = new CountDownLatch(1); + /** The static token cache. */ + private static DefaultTokenCacheStore tokenCacheStore; /** * Initializes a new instance of the UserTokenCredentials. @@ -57,6 +60,20 @@ public UserTokenCredentials(Activity activity, String clientId, String domain, S this.environment = environment; } this.activity = activity; + if (tokenCacheStore == null) { + try { + tokenCacheStore = new DefaultTokenCacheStore(activity); + } catch (NoSuchAlgorithmException | NoSuchPaddingException e) { + tokenCacheStore = null; + } + } + } + + /** + * Clear the items stored in token cache. + */ + public static void clearTokenCache() { + tokenCacheStore.removeAll(); } /** @@ -110,19 +127,14 @@ public void refreshToken() throws IOException { private void acquireAccessToken() throws IOException { String authorityUrl = this.getEnvironment().getAuthenticationEndpoint() + this.getDomain(); - AuthenticationContext context; - try { - context = new AuthenticationContext(activity, authorityUrl, true); - } catch (NoSuchAlgorithmException | NoSuchPaddingException e) { - return; - } + AuthenticationContext context = new AuthenticationContext(activity, authorityUrl, true, tokenCacheStore); final TokenCredentials self = this; context.acquireToken( this.getEnvironment().getTokenAudience(), this.getClientId(), this.getClientRedirectUri(), null, - PromptBehavior.Always, + PromptBehavior.Auto, null, new AuthenticationCallback() { @Override From 2e08ff59eef273ba1f0a4be1272f23837cc4d949 Mon Sep 17 00:00:00 2001 From: Jianghao Lu Date: Tue, 19 Jan 2016 15:18:10 -0800 Subject: [PATCH 2/2] Android authentication updates from sample --- .../credentials/UserTokenCredentials.java | 58 ++++++++++++++----- 1 file changed, 44 insertions(+), 14 deletions(-) diff --git a/ClientRuntimes/Java/azure-android-client-authentication/src/main/java/com/microsoft/rest/credentials/UserTokenCredentials.java b/ClientRuntimes/Java/azure-android-client-authentication/src/main/java/com/microsoft/rest/credentials/UserTokenCredentials.java index bab3a205b3..85e4d15984 100644 --- a/ClientRuntimes/Java/azure-android-client-authentication/src/main/java/com/microsoft/rest/credentials/UserTokenCredentials.java +++ b/ClientRuntimes/Java/azure-android-client-authentication/src/main/java/com/microsoft/rest/credentials/UserTokenCredentials.java @@ -39,27 +39,50 @@ public class UserTokenCredentials extends TokenCredentials { private CountDownLatch signal = new CountDownLatch(1); /** The static token cache. */ private static DefaultTokenCacheStore tokenCacheStore; + /** The behavior of when to prompt a login. */ + private PromptBehavior promptBehavior; /** * Initializes a new instance of the UserTokenCredentials. * + * @param activity The caller activity. * @param clientId the active directory application client id. * @param domain the domain or tenant id containing this application. * @param clientRedirectUri the Uri where the user will be redirected after authenticating with AD. + */ + public UserTokenCredentials( + Activity activity, + String clientId, + String domain, + String clientRedirectUri) { + this(activity, clientId, domain, clientRedirectUri, PromptBehavior.Auto, AzureEnvironment.AZURE); + } + + /** + * Initializes a new instance of the UserTokenCredentials. + * + * @param activity The caller activity. + * @param clientId the active directory application client id. + * @param domain the domain or tenant id containing this application. + * @param clientRedirectUri the Uri where the user will be redirected after authenticating with AD. + * @param promptBehavior the behavior of when to prompt a login. * @param environment the Azure environment to authenticate with. * If null is provided, AzureEnvironment.AZURE will be used. */ - public UserTokenCredentials(Activity activity, String clientId, String domain, String clientRedirectUri, AzureEnvironment environment) { + public UserTokenCredentials( + Activity activity, + String clientId, + String domain, + String clientRedirectUri, + PromptBehavior promptBehavior, + AzureEnvironment environment) { super(null, null); // defer token acquisition this.clientId = clientId; this.domain = domain; this.clientRedirectUri = clientRedirectUri; - if (environment == null) { - this.environment = AzureEnvironment.AZURE; - } else { - this.environment = environment; - } this.activity = activity; + this.promptBehavior = promptBehavior; + this.environment = environment; if (tokenCacheStore == null) { try { tokenCacheStore = new DefaultTokenCacheStore(activity); @@ -86,14 +109,23 @@ public String getClientId() { } /** - * Gets the tenant or domain the containing the application. + * Gets the tenant or domain containing the application. * - * @return the tenant or domain the containing the application. + * @return the tenant or domain containing the application. */ public String getDomain() { return domain; } + /** + * Sets the tenant of domain containing the application. + * + * @param domain the tenant or domain containing the application. + */ + public void setDomain(String domain) { + this.domain = domain; + } + /** * Gets the Uri where the user will be redirected after authenticating with AD. * @@ -114,9 +146,7 @@ public AzureEnvironment getEnvironment() { @Override public String getToken() throws IOException { - if (token == null) { - acquireAccessToken(); - } + refreshToken(); return token; } @@ -126,15 +156,15 @@ public void refreshToken() throws IOException { } private void acquireAccessToken() throws IOException { - String authorityUrl = this.getEnvironment().getAuthenticationEndpoint() + this.getDomain(); + final String authorityUrl = this.getEnvironment().getAuthenticationEndpoint() + this.getDomain(); AuthenticationContext context = new AuthenticationContext(activity, authorityUrl, true, tokenCacheStore); - final TokenCredentials self = this; + final UserTokenCredentials self = this; context.acquireToken( this.getEnvironment().getTokenAudience(), this.getClientId(), this.getClientRedirectUri(), null, - PromptBehavior.Auto, + promptBehavior, null, new AuthenticationCallback() { @Override