Search in sources :

Example 1 with GenerateShrResult

use of com.microsoft.identity.common.internal.result.GenerateShrResult in project microsoft-authentication-library-common-for-android by AzureAD.

the class DevicePoPUtils method generateSignedHttpRequest.

/**
 * Generates an AT-less SHR using the PoPMgr's internal signing key.
 *
 * @param context          The current application's {@link Context}.
 * @param clockSkewManager An instance of {@link IClockSkewManager}, used to mitigate
 *                         clock-skew/drift.
 * @param popSchemeParams  The input params used to create the resulting SHR.
 * @return The {@link GenerateShrResult} containing the resulint SHR.
 * @throws ClientException If an error is encountered.
 */
public static synchronized GenerateShrResult generateSignedHttpRequest(@NonNull final Context context, @NonNull final IClockSkewManager clockSkewManager, @NonNull final IPoPAuthenticationSchemeParams popSchemeParams) throws ClientException {
    // Clock-skew correction values
    final long ONE_SECOND_MILLIS = 1000L;
    final long timestampMillis = clockSkewManager.getAdjustedReferenceTime().getTime();
    final String httpMethodStr = popSchemeParams.getHttpMethod();
    final URL resourceUrl = popSchemeParams.getUrl();
    final String nonce = popSchemeParams.getNonce();
    final String clientClaims = popSchemeParams.getClientClaims();
    final IDevicePopManager popMgr = Device.getDevicePoPManagerInstance();
    // Generate keys, if none exist (should already be initialized)
    if (!popMgr.asymmetricKeyExists()) {
        popMgr.generateAsymmetricKey(context);
    }
    final String shr = popMgr.mintSignedHttpRequest(httpMethodStr, timestampMillis / ONE_SECOND_MILLIS, resourceUrl, nonce, clientClaims);
    // Create our result object
    final GenerateShrResult result = new GenerateShrResult();
    result.setShr(shr);
    return result;
}
Also used : GenerateShrResult(com.microsoft.identity.common.internal.result.GenerateShrResult) URL(java.net.URL)

Example 2 with GenerateShrResult

use of com.microsoft.identity.common.internal.result.GenerateShrResult in project microsoft-authentication-library-common-for-android by AzureAD.

the class LocalMSALController method generateSignedHttpRequest.

@Override
public GenerateShrResult generateSignedHttpRequest(@NonNull final GenerateShrCommandParameters parameters) throws Exception {
    final Context context = parameters.getAndroidApplicationContext();
    final IClockSkewManager clockSkewManager = new ClockSkewManager(context);
    final OAuth2TokenCache cache = parameters.getOAuth2TokenCache();
    final String clientId = parameters.getClientId();
    final String homeAccountId = parameters.getHomeAccountId();
    final IPoPAuthenticationSchemeParams popSchemeParams = parameters.getPopParameters();
    final GenerateShrResult result;
    if (userHasLocalAccountRecord(cache, clientId, homeAccountId)) {
        // Perform the signing locally...
        result = DevicePoPUtils.generateSignedHttpRequest(context, clockSkewManager, popSchemeParams);
    } else {
        // Populate the error on the result and return...
        result = new GenerateShrResult();
        result.setErrorCode(GenerateShrResult.Errors.NO_ACCOUNT_FOUND);
        result.setErrorMessage("Account does not exist.");
    }
    return result;
}
Also used : Context(android.content.Context) OAuth2TokenCache(com.microsoft.identity.common.internal.providers.oauth2.OAuth2TokenCache) GenerateShrResult(com.microsoft.identity.common.internal.result.GenerateShrResult) IClockSkewManager(com.microsoft.identity.common.internal.util.IClockSkewManager) IClockSkewManager(com.microsoft.identity.common.internal.util.IClockSkewManager) ClockSkewManager(com.microsoft.identity.common.internal.util.ClockSkewManager) IPoPAuthenticationSchemeParams(com.microsoft.identity.common.internal.authscheme.IPoPAuthenticationSchemeParams)

Example 3 with GenerateShrResult

use of com.microsoft.identity.common.internal.result.GenerateShrResult in project microsoft-authentication-library-common-for-android by AzureAD.

the class GenerateShrCommand method execute.

@Override
public GenerateShrResult execute() throws Exception {
    final String methodName = ":execute";
    GenerateShrResult result = null;
    final GenerateShrCommandParameters parameters = (GenerateShrCommandParameters) getParameters();
    // Iterate over our controllers, to service the request either locally or via the broker...
    // if the local (embedded) cache contains tokens for the supplied user, we will sign using
    // the embedded PoP keys. If no local user-state exists, the broker will be delegated to
    // where the same check is performed.
    BaseController controller;
    for (int ii = 0; ii < getControllers().size(); ii++) {
        controller = getControllers().get(ii);
        com.microsoft.identity.common.internal.logging.Logger.verbose(TAG + methodName, "Executing with controller: " + controller.getClass().getSimpleName());
        result = controller.generateSignedHttpRequest(parameters);
        if (null != result.getErrorCode()) {
            final String errorCode = result.getErrorCode();
            final String errorMessage = result.getErrorMessage();
            // of as thrown Exceptions
            if (NO_ACCOUNT_FOUND.equalsIgnoreCase(errorCode)) {
                if (getControllers().size() > ii + 1) {
                    // Try our next controller
                    continue;
                } else {
                    throw new UiRequiredException(errorCode, errorMessage);
                }
            } else {
                throw new ClientException(errorCode, errorMessage);
            }
        }
    }
    return result;
}
Also used : GenerateShrResult(com.microsoft.identity.common.internal.result.GenerateShrResult) BaseController(com.microsoft.identity.common.internal.controllers.BaseController) UiRequiredException(com.microsoft.identity.common.exception.UiRequiredException) ClientException(com.microsoft.identity.common.exception.ClientException) GenerateShrCommandParameters(com.microsoft.identity.common.internal.commands.parameters.GenerateShrCommandParameters)

Aggregations

GenerateShrResult (com.microsoft.identity.common.internal.result.GenerateShrResult)3 Context (android.content.Context)1 ClientException (com.microsoft.identity.common.exception.ClientException)1 UiRequiredException (com.microsoft.identity.common.exception.UiRequiredException)1 IPoPAuthenticationSchemeParams (com.microsoft.identity.common.internal.authscheme.IPoPAuthenticationSchemeParams)1 GenerateShrCommandParameters (com.microsoft.identity.common.internal.commands.parameters.GenerateShrCommandParameters)1 BaseController (com.microsoft.identity.common.internal.controllers.BaseController)1 OAuth2TokenCache (com.microsoft.identity.common.internal.providers.oauth2.OAuth2TokenCache)1 ClockSkewManager (com.microsoft.identity.common.internal.util.ClockSkewManager)1 IClockSkewManager (com.microsoft.identity.common.internal.util.IClockSkewManager)1 URL (java.net.URL)1