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

Android interactive login flow #626

Merged
merged 2 commits into from
Jan 19, 2016
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,27 +37,66 @@ 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;
/** 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);
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
tokenCacheStore = null;
}
}
}

/**
* Clear the items stored in token cache.
*/
public static void clearTokenCache() {
tokenCacheStore.removeAll();
}

/**
Expand All @@ -69,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.
*
Expand All @@ -97,9 +146,7 @@ public AzureEnvironment getEnvironment() {

@Override
public String getToken() throws IOException {
if (token == null) {
acquireAccessToken();
}
refreshToken();
return token;
}

Expand All @@ -109,20 +156,15 @@ 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;
}
final TokenCredentials self = this;
final String authorityUrl = this.getEnvironment().getAuthenticationEndpoint() + this.getDomain();
AuthenticationContext context = new AuthenticationContext(activity, authorityUrl, true, tokenCacheStore);
final UserTokenCredentials self = this;
context.acquireToken(
this.getEnvironment().getTokenAudience(),
this.getClientId(),
this.getClientRedirectUri(),
null,
PromptBehavior.Always,
promptBehavior,
null,
new AuthenticationCallback<AuthenticationResult>() {
@Override
Expand Down