Search in sources :

Example 1 with MicrosoftTokenRequest

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

the class ObjectMapperTest method test_JsonToObjectMS.

@Test
public void test_JsonToObjectMS() {
    MicrosoftTokenRequest tr = ObjectMapper.deserializeJsonStringToObject(JSON_TOKEN_REQUEST, MicrosoftTokenRequest.class);
    Assert.assertEquals(CLIENT_ID, tr.getClientId());
    final Iterator<Map.Entry<String, String>> iterator = tr.getExtraParameters().iterator();
    Map.Entry<String, String> param = iterator.next();
    Assert.assertEquals("id_token", param.getKey());
    Assert.assertEquals("idtokenval", param.getValue());
    param = iterator.next();
    Assert.assertEquals("other_param", param.getKey());
    Assert.assertEquals("other_value", param.getValue());
    Assert.assertFalse(iterator.hasNext());
}
Also used : Map(java.util.Map) MicrosoftTokenRequest(com.microsoft.identity.common.internal.providers.microsoft.MicrosoftTokenRequest) Test(org.junit.Test)

Example 2 with MicrosoftTokenRequest

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

the class OAuth2Strategy method performTokenRequest.

protected HttpResponse performTokenRequest(final GenericTokenRequest request) throws IOException, ClientException {
    final String methodName = ":performTokenRequest";
    Logger.verbose(TAG + methodName, "Performing token request...");
    final String requestBody = getRequestBody(request);
    final Map<String, String> headers = new TreeMap<>();
    headers.put(CLIENT_REQUEST_ID, DiagnosticContext.getRequestContext().get(DiagnosticContext.CORRELATION_ID));
    if (request instanceof MicrosoftTokenRequest && !TextUtils.isEmpty(((MicrosoftTokenRequest) request).getBrokerVersion())) {
        headers.put(Device.PlatformIdParameters.BROKER_VERSION, ((MicrosoftTokenRequest) request).getBrokerVersion());
    }
    headers.putAll(Device.getPlatformIdParameters());
    headers.put(AuthenticationConstants.SdkPlatformFields.PRODUCT, DiagnosticContext.getRequestContext().get(AuthenticationConstants.SdkPlatformFields.PRODUCT));
    headers.put(AuthenticationConstants.SdkPlatformFields.VERSION, Device.getProductVersion());
    headers.putAll(EstsTelemetry.getInstance().getTelemetryHeaders());
    headers.put(HttpConstants.HeaderField.CONTENT_TYPE, TOKEN_REQUEST_CONTENT_TYPE);
    if (request instanceof MicrosoftTokenRequest) {
        headers.put(AuthenticationConstants.AAD.APP_PACKAGE_NAME, ((MicrosoftTokenRequest) request).getClientAppName());
        headers.put(AuthenticationConstants.AAD.APP_VERSION, ((MicrosoftTokenRequest) request).getClientAppVersion());
    }
    final URL requestUrl = new URL(getTokenEndpoint());
    final HttpResponse response = httpClient.post(requestUrl, headers, requestBody.getBytes(ObjectMapper.ENCODING_SCHEME));
    // Record the clock skew between *this device* and EVO...
    if (null != response.getDate()) {
        recordClockSkew(response.getDate().getTime());
    }
    return response;
}
Also used : HttpResponse(com.microsoft.identity.common.internal.net.HttpResponse) TreeMap(java.util.TreeMap) MicrosoftTokenRequest(com.microsoft.identity.common.internal.providers.microsoft.MicrosoftTokenRequest) URL(java.net.URL)

Example 3 with MicrosoftTokenRequest

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

the class BaseController method performSilentTokenRequest.

protected TokenResult performSilentTokenRequest(@SuppressWarnings(WarningType.rawtype_warning) @NonNull final OAuth2Strategy strategy, @NonNull final RefreshTokenRecord refreshToken, @NonNull final SilentTokenCommandParameters parameters) throws ClientException, IOException {
    final String methodName = ":performSilentTokenRequest";
    Logger.info(TAG + methodName, "Requesting tokens...");
    HttpWebRequest.throwIfNetworkNotAvailable(parameters.getAndroidApplicationContext(), parameters.isPowerOptCheckEnabled());
    // Check that the authority is known
    final Authority.KnownAuthorityResult authorityResult = Authority.getKnownAuthorityResult(parameters.getAuthority());
    if (!authorityResult.getKnown()) {
        throw authorityResult.getClientException();
    }
    final TokenRequest refreshTokenRequest = strategy.createRefreshTokenRequest(parameters.getAuthenticationScheme());
    refreshTokenRequest.setClientId(parameters.getClientId());
    refreshTokenRequest.setScope(TextUtils.join(" ", parameters.getScopes()));
    refreshTokenRequest.setRefreshToken(refreshToken.getSecret());
    if (refreshTokenRequest instanceof MicrosoftTokenRequest) {
        ((MicrosoftTokenRequest) refreshTokenRequest).setClaims(parameters.getClaimsRequestJson());
        ((MicrosoftTokenRequest) refreshTokenRequest).setClientAppName(parameters.getApplicationName());
        ((MicrosoftTokenRequest) refreshTokenRequest).setClientAppVersion(parameters.getApplicationVersion());
    }
    // NOTE: this should be moved to the strategy; however requires a larger refactor
    if (parameters.getSdkType() == SdkType.ADAL) {
        ((MicrosoftTokenRequest) refreshTokenRequest).setIdTokenVersion("1");
    }
    // Set Broker version to Token Request if it's a brokered request.
    if (parameters instanceof BrokerSilentTokenCommandParameters) {
        ((MicrosoftTokenRequest) refreshTokenRequest).setBrokerVersion(((BrokerSilentTokenCommandParameters) parameters).getBrokerVersion());
    }
    if (!StringExtensions.isNullOrBlank(refreshTokenRequest.getScope())) {
        Logger.infoPII(TAG + methodName, "Scopes: [" + refreshTokenRequest.getScope() + "]");
    }
    return strategyRequestToken(strategy, refreshTokenRequest);
}
Also used : BrokerSilentTokenCommandParameters(com.microsoft.identity.common.internal.commands.parameters.BrokerSilentTokenCommandParameters) AzureActiveDirectoryAuthority(com.microsoft.identity.common.internal.authorities.AzureActiveDirectoryAuthority) Authority(com.microsoft.identity.common.internal.authorities.Authority) MicrosoftTokenRequest(com.microsoft.identity.common.internal.providers.microsoft.MicrosoftTokenRequest) TokenRequest(com.microsoft.identity.common.internal.providers.oauth2.TokenRequest) MicrosoftTokenRequest(com.microsoft.identity.common.internal.providers.microsoft.MicrosoftTokenRequest)

Example 4 with MicrosoftTokenRequest

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

the class BaseController method performTokenRequest.

protected TokenResult performTokenRequest(@SuppressWarnings(WarningType.rawtype_warning) @NonNull final OAuth2Strategy strategy, @SuppressWarnings(WarningType.rawtype_warning) @NonNull final AuthorizationRequest request, @NonNull final AuthorizationResponse response, @NonNull final InteractiveTokenCommandParameters parameters) throws IOException, ClientException {
    final String methodName = ":performTokenRequest";
    HttpWebRequest.throwIfNetworkNotAvailable(parameters.getAndroidApplicationContext(), parameters.isPowerOptCheckEnabled());
    // Suppressing unchecked warnings due to casting of type AuthorizationRequest to GenericAuthorizationRequest and AuthorizationResponse to GenericAuthorizationResponse in arguments of method call to createTokenRequest
    @SuppressWarnings(WarningType.unchecked_warning) final TokenRequest tokenRequest = strategy.createTokenRequest(request, response, parameters.getAuthenticationScheme());
    if (tokenRequest instanceof MicrosoftTokenRequest) {
        ((MicrosoftTokenRequest) tokenRequest).setClientAppName(parameters.getApplicationName());
        ((MicrosoftTokenRequest) tokenRequest).setClientAppVersion(parameters.getApplicationVersion());
    }
    if (tokenRequest instanceof IHasExtraParameters && parameters instanceof IHasExtraParameters) {
        ((IHasExtraParameters) tokenRequest).setExtraParameters(((IHasExtraParameters) parameters).getExtraParameters());
    }
    logExposedFieldsOfObject(TAG + methodName, tokenRequest);
    // Suppressing unchecked warnings due to casting of type TokenRequest to GenericTokenRequest in argument of method call to requestToken
    @SuppressWarnings(WarningType.unchecked_warning) final TokenResult tokenResult = strategy.requestToken(tokenRequest);
    logResult(TAG, tokenResult);
    return tokenResult;
}
Also used : TokenResult(com.microsoft.identity.common.internal.providers.oauth2.TokenResult) AcquireTokenResult(com.microsoft.identity.common.internal.result.AcquireTokenResult) MicrosoftTokenRequest(com.microsoft.identity.common.internal.providers.microsoft.MicrosoftTokenRequest) TokenRequest(com.microsoft.identity.common.internal.providers.oauth2.TokenRequest) MicrosoftTokenRequest(com.microsoft.identity.common.internal.providers.microsoft.MicrosoftTokenRequest) IHasExtraParameters(com.microsoft.identity.common.internal.commands.parameters.IHasExtraParameters)

Aggregations

MicrosoftTokenRequest (com.microsoft.identity.common.internal.providers.microsoft.MicrosoftTokenRequest)4 TokenRequest (com.microsoft.identity.common.internal.providers.oauth2.TokenRequest)2 Authority (com.microsoft.identity.common.internal.authorities.Authority)1 AzureActiveDirectoryAuthority (com.microsoft.identity.common.internal.authorities.AzureActiveDirectoryAuthority)1 BrokerSilentTokenCommandParameters (com.microsoft.identity.common.internal.commands.parameters.BrokerSilentTokenCommandParameters)1 IHasExtraParameters (com.microsoft.identity.common.internal.commands.parameters.IHasExtraParameters)1 HttpResponse (com.microsoft.identity.common.internal.net.HttpResponse)1 TokenResult (com.microsoft.identity.common.internal.providers.oauth2.TokenResult)1 AcquireTokenResult (com.microsoft.identity.common.internal.result.AcquireTokenResult)1 URL (java.net.URL)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 Test (org.junit.Test)1