Search in sources :

Example 1 with AzureActiveDirectoryAccount

use of com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.AzureActiveDirectoryAccount in project microsoft-authentication-library-common-for-android by AzureAD.

the class ADALOAuth2TokenCache method save.

/**
 * Method responsible for saving tokens contained in the TokenResponse to storage.
 *
 * @param strategy
 * @param request
 * @param response
 */
@Override
public ICacheRecord save(final AzureActiveDirectoryOAuth2Strategy strategy, final AzureActiveDirectoryAuthorizationRequest request, final AzureActiveDirectoryTokenResponse response) {
    final String methodName = "save";
    Logger.info(TAG + ":" + methodName, "Saving Tokens...");
    final String issuerCacheIdentifier = strategy.getIssuerCacheIdentifier(request);
    final AzureActiveDirectoryAccount account = strategy.createAccount(response);
    final String msalEnvironment = Uri.parse(issuerCacheIdentifier).getAuthority();
    account.setEnvironment(msalEnvironment);
    final AzureActiveDirectoryRefreshToken refreshToken = strategy.getRefreshTokenFromResponse(response);
    refreshToken.setEnvironment(msalEnvironment);
    Logger.info(TAG, "Constructing new ADALTokenCacheItem");
    final ADALTokenCacheItem cacheItem = new ADALTokenCacheItem(strategy, request, response);
    logTokenCacheItem(cacheItem);
    // There is more than one valid user identifier for some accounts... AAD Accounts as of this writing have 3
    Logger.info(TAG + ":" + methodName, "Setting items to cache for user...");
    for (final String cacheIdentifier : account.getCacheIdentifiers()) {
        // Azure AD Uses Resource and Not Scope... but we didn't override... heads up
        final String scope = request.getScope();
        final String clientId = request.getClientId();
        Logger.infoPII(TAG + ":" + methodName, "issuerCacheIdentifier: [" + issuerCacheIdentifier + "]");
        Logger.infoPII(TAG + ":" + methodName, "scope: [" + scope + "]");
        Logger.infoPII(TAG + ":" + methodName, "clientId: [" + clientId + "]");
        Logger.infoPII(TAG + ":" + methodName, "cacheIdentifier: [" + cacheIdentifier + "]");
        setItemToCacheForUser(issuerCacheIdentifier, scope, clientId, cacheItem, cacheIdentifier);
    }
    // For legacy reasons creating a cache entry where the userid is null
    // ADAL supported a single user mode where it was not necessary for the developer to provide the user id
    // on calls to acquireTokenSilentAsync
    setItemToCacheForUser(issuerCacheIdentifier, request.getScope(), request.getClientId(), cacheItem, null);
    // TODO At some point, the type-safety of this call needs to get beefed-up
    Logger.info(TAG + ":" + methodName, "Syncing SSO state to caches...");
    for (final IShareSingleSignOnState<MicrosoftAccount, MicrosoftRefreshToken> sharedSsoCache : mSharedSSOCaches) {
        try {
            sharedSsoCache.setSingleSignOnState(account, refreshToken);
        } catch (ClientException e) {
            Logger.errorPII(TAG, "Exception setting single sign on state for account " + account.getUsername(), e);
        }
    }
    // Returning null, since the ADAL cache's schema doesn't support this return type.
    return null;
}
Also used : AzureActiveDirectoryAccount(com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.AzureActiveDirectoryAccount) MicrosoftAccount(com.microsoft.identity.common.internal.providers.microsoft.MicrosoftAccount) AzureActiveDirectoryRefreshToken(com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.AzureActiveDirectoryRefreshToken) MicrosoftRefreshToken(com.microsoft.identity.common.internal.providers.microsoft.MicrosoftRefreshToken) ClientException(com.microsoft.identity.common.exception.ClientException)

Example 2 with AzureActiveDirectoryAccount

use of com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.AzureActiveDirectoryAccount in project microsoft-authentication-library-common-for-android by AzureAD.

the class AdalMigrationAdapter method createAccount.

/**
 * Creates a {@link MicrosoftAccount} from the supplied {@link ADALTokenCacheItem}.
 *
 * @param refreshToken The credential used to derive the new account.
 * @return The newly created MicrosoftAccount.
 */
@Nullable
public static MicrosoftAccount createAccount(@NonNull final ADALTokenCacheItem refreshToken) {
    final String methodName = ":createAccount";
    try {
        final String rawIdToken = refreshToken.getRawIdToken();
        final String uid = refreshToken.getUserInfo().getUserId();
        final String utid = refreshToken.getTenantId();
        final String environment = new URL(refreshToken.getAuthority()).getHost();
        final JsonObject clientInfo = new JsonObject();
        clientInfo.addProperty("uid", uid);
        clientInfo.addProperty("utid", utid);
        final String clientInfoJson = clientInfo.toString();
        final String base64EncodedClientInfo = new String(Base64.encode(clientInfoJson.getBytes(), 0));
        final ClientInfo clientInfoObj = new ClientInfo(base64EncodedClientInfo);
        final IDToken idToken = new IDToken(rawIdToken);
        AzureActiveDirectoryAccount account = new AzureActiveDirectoryAccount(idToken, clientInfoObj);
        account.setEnvironment(environment);
        return account;
    } catch (MalformedURLException | ServiceException e) {
        final String errorMsg = "Failed to create Account";
        Logger.error(TAG + methodName, errorMsg, null);
        Logger.errorPII(TAG + methodName, errorMsg, e);
        return null;
    }
}
Also used : AzureActiveDirectoryAccount(com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.AzureActiveDirectoryAccount) MalformedURLException(java.net.MalformedURLException) ServiceException(com.microsoft.identity.common.exception.ServiceException) JsonObject(com.google.gson.JsonObject) IDToken(com.microsoft.identity.common.internal.providers.oauth2.IDToken) ClientInfo(com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.ClientInfo) URL(java.net.URL) Nullable(androidx.annotation.Nullable)

Example 3 with AzureActiveDirectoryAccount

use of com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.AzureActiveDirectoryAccount in project azure-activedirectory-library-for-android by AzureAD.

the class CoreAdapter method asAadAccount.

/**
 * Gets the supplied {@link UserInfo} as an {@link AzureActiveDirectoryAccount}.
 *
 * @param userInfo The UserInfo to transform.
 * @return The newly created Account.
 */
public static AzureActiveDirectoryAccount asAadAccount(final UserInfo userInfo) {
    final AzureActiveDirectoryAccount account = new AzureActiveDirectoryAccount();
    account.setDisplayableId(userInfo.getDisplayableId());
    account.setName(userInfo.getGivenName());
    account.setIdentityProvider(account.getIdentityProvider());
    account.setUid(userInfo.getUserId());
    // TODO Need to get the UTID attribute.
    return account;
}
Also used : AzureActiveDirectoryAccount(com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.AzureActiveDirectoryAccount)

Aggregations

AzureActiveDirectoryAccount (com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.AzureActiveDirectoryAccount)3 Nullable (androidx.annotation.Nullable)1 JsonObject (com.google.gson.JsonObject)1 ClientException (com.microsoft.identity.common.exception.ClientException)1 ServiceException (com.microsoft.identity.common.exception.ServiceException)1 MicrosoftAccount (com.microsoft.identity.common.internal.providers.microsoft.MicrosoftAccount)1 MicrosoftRefreshToken (com.microsoft.identity.common.internal.providers.microsoft.MicrosoftRefreshToken)1 AzureActiveDirectoryRefreshToken (com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.AzureActiveDirectoryRefreshToken)1 ClientInfo (com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.ClientInfo)1 IDToken (com.microsoft.identity.common.internal.providers.oauth2.IDToken)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1