public async Task <AuthenticationResult> ExecuteAsync(
            AcquireTokenCommonParameters commonParameters,
            AcquireTokenByRefreshTokenParameters refreshTokenParameters,
            CancellationToken cancellationToken)
        {
            var requestContext = CreateRequestContextAndLogVersionInfo();

            if (commonParameters.Scopes == null || !commonParameters.Scopes.Any())
            {
                commonParameters.Scopes = new SortedSet <string>
                {
                    _clientApplicationBase.AppConfig.ClientId + "/.default"
                };
                requestContext.Logger.Info(LogMessages.NoScopesProvidedForRefreshTokenRequest);
            }

            var requestParameters = _clientApplicationBase.CreateRequestParameters(
                commonParameters,
                requestContext);

            requestParameters.IsRefreshTokenRequest = true;

            requestContext.Logger.Info(LogMessages.UsingXScopesForRefreshTokenRequest(commonParameters.Scopes.Count()));

            var handler = new ByRefreshTokenRequest(ServiceBundle, requestParameters, refreshTokenParameters);

            return(await handler.RunAsync(CancellationToken.None).ConfigureAwait(false));
        }
Example #2
0
        /// <inheritdoc />
        public async Task <AuthorizationResult> AcquireAuthorizationAsync(
            Uri authorizationUri,
            Uri redirectUri,
            RequestContext requestContext,
            CancellationToken cancellationToken)
        {
            requestContext.Logger.Info(LogMessages.CustomWebUiAcquiringAuthorizationCode);

            try
            {
                requestContext.Logger.InfoPii(
                    LogMessages.CustomWebUiCallingAcquireAuthorizationCodePii(authorizationUri, redirectUri),
                    LogMessages.CustomWebUiCallingAcquireAuthorizationCodeNoPii);
                var uri = await _customWebUi.AcquireAuthorizationCodeAsync(authorizationUri, redirectUri, cancellationToken)
                          .ConfigureAwait(false);

                if (uri == null || string.IsNullOrWhiteSpace(uri.Query))
                {
                    throw new AuthClientException(
                              AuthError.CustomWebUiReturnedInvalidUri,
                              AuthErrorMessage.CustomWebUiReturnedInvalidUri);
                }

                if (uri.Authority.Equals(redirectUri.Authority, StringComparison.OrdinalIgnoreCase) &&
                    uri.AbsolutePath.Equals(redirectUri.AbsolutePath, StringComparison.OrdinalIgnoreCase))
                {
                    IDictionary <string, string> inputQp = CoreHelpers.ParseKeyValueList(
                        authorizationUri.Query.Substring(1),
                        '&',
                        true,
                        null);

                    requestContext.Logger.Info(LogMessages.CustomWebUiRedirectUriMatched);
                    return(new AuthorizationResult(AuthorizationStatus.Success, uri.OriginalString));
                }

                throw new AuthClientException(
                          AuthError.CustomWebUiRedirectUriMismatch,
                          AuthErrorMessage.CustomWebUiRedirectUriMismatch(
                              uri.AbsolutePath,
                              redirectUri.AbsolutePath));
            }
            catch (OperationCanceledException)
            {
                requestContext.Logger.Info(LogMessages.CustomWebUiOperationCancelled);
                return(new AuthorizationResult(AuthorizationStatus.UserCancel, null));
            }
            catch (Exception ex)
            {
                requestContext.Logger.WarningPiiWithPrefix(ex, AuthErrorMessage.CustomWebUiAuthorizationCodeFailed);
                throw;
            }
        }