Exemplo n.º 1
0
        /// <summary></summary>
        public IntegrationApiController(IConfiguration configuration, ITestLogic testLogic)
        {
            _testLogic = testLogic;

            var platformSettings = configuration.GetSection("Platform").Get <PlatformSettings>();
            var tokenRefresher   = new TokenRefresher(configuration, platformSettings.ClientId, platformSettings.ClientSecret);

            _integrationApiClient = new IntegrationApiRestClient(new HttpSender(platformSettings.IntegrationApiUrl, tokenRefresher.GetServiceClient()));
        }
Exemplo n.º 2
0
        public BusinessApiTranslationsTestController(IConfiguration configuration, ITestLogic testLogic) : base(configuration, testLogic)
        {
            var platformSettings = configuration.GetSection("Platform").Get <PlatformSettings>();

            var tokenRefresher1 = new TokenRefresher(configuration, configuration["Capability1:ClientId"], configuration["Capability1:ClientSecret"]);

            _bapiClientAsCapability1Client = new BusinessApiRestClient(new HttpSender(platformSettings.BusinessApiUrl, tokenRefresher1));

            var tokenRefresher2 = new TokenRefresher(configuration, configuration["Capability2:ClientId"], configuration["Capability2:ClientSecret"]);

            _bapiClientAsCapability2Client = new BusinessApiRestClient(new HttpSender(platformSettings.BusinessApiUrl, tokenRefresher2));
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="UserPartnerCredentials" /> class.
        /// </summary>
        /// <param name="aadApplicationId">The id of the application in Azure Active Directory.</param>
        /// <param name="aadAuthenticationToken">The Azure Active Directory token.</param>
        /// <param name="aadTokenRefresher">An optional delegate which will be called when the Azure Active Directory token
        /// expires and can no longer be used to generate the partner credentials. This delegate should return
        /// an up to date Azure Active Directory token.</param>
        public UserPartnerCredentials(string aadApplicationId, AuthenticationToken aadAuthenticationToken, TokenRefresher aadTokenRefresher = null)
            : base(aadApplicationId)
        {
            aadAuthenticationToken.AssertNotNull(nameof(aadAuthenticationToken));

            if (aadAuthenticationToken.IsExpired())
            {
                throw new ArgumentException(Resources.AuthenticationTokenExpired);
            }

            AADToken       = aadAuthenticationToken;
            tokenRefresher = aadTokenRefresher;
        }
Exemplo n.º 4
0
        private async Task <List <TokenRefreshAuditEntry> > RunRefresherAndReturnCompletedRefreshInfo(
            int runDurationMsecs,
            int tokenLifetimeMsecs,
            int refreshExpirationPercentage,
            int minimumNumberOfRefreshes = 1)
        {
            // Arrange
            var audit = new TokenRefreshAudit(true);

            var refresher =
                new TokenRefresher(audit, new TokenRefresherParameters {
                TokenRefreshExpirationPercentage = refreshExpirationPercentage
            });

            var cancellationSource = new CancellationTokenSource(runDurationMsecs);
            var token = AccessTokenBuilder.Create().WithValidState().ExpiresInMSecs(tokenLifetimeMsecs);

            // Act
            var refreshTask = refresher.StartTokenBackgroundRefreshAsync(token, cancellationSource.Token,
                                                                         t =>
            {
                var newToken = AccessTokenBuilder.Create().WithValidState().ExpiresInMSecs(tokenLifetimeMsecs);
                return(Task.FromResult(newToken));
            });

            try
            {
                await Task.Delay(runDurationMsecs);

                refreshTask.Wait(20); // allow a little bit extra to let the task end cleanly
            }
            catch (AggregateException e)
            {
                if (e.Flatten().InnerExceptions.Any(innerException => !(innerException is TaskCanceledException)))
                {
                    // We're only expecting cancellation exceptions
                    throw;
                }
            }

            cancellationSource.Dispose();
            var result = audit.AuditItems
                         .Where(aie => aie.ActualRefreshStart.HasValue).OrderBy(aie => aie.ActualRefreshStart)
                         .ToList();

            var estimatedMaximumNumberOfRefreshes = runDurationMsecs / tokenLifetimeMsecs * refreshExpirationPercentage;

            Assert.Greater(result.Count, minimumNumberOfRefreshes, "There was an unexpectedly low number of refreshes");
            Assert.Less(result.Count, estimatedMaximumNumberOfRefreshes, "There was an unexpectedly high number of refreshes");
            return(result);
        }
        /// <summary>
        /// Generates partner credentials using a user plus application Azure Active Directory token.
        /// </summary>
        /// <param name="clientId">The client id of the application in Azure Active Directory. This application should be an Azure native application.</param>
        /// <param name="authenticationToken">The Azure Active Directory token.</param>
        /// <param name="aadTokenRefresher">An optional delegate which will be called when the Azure Active Directory token
        /// expires and can no longer be used to generate the partner credentials. This delegate should return
        /// an up to date Azure Active Directory token.</param>
        /// <param name="requestContext">An optional request context.</param>
        /// <returns>The partner service credentials.</returns>
        public static async Task <IPartnerCredentials> GenerateByUserCredentialsAsync(string clientId, AuthenticationToken authenticationToken, TokenRefresher aadTokenRefresher = null, IRequestContext requestContext = null)
        {
            UserPartnerCredentials partnerCredentials = new UserPartnerCredentials(clientId, authenticationToken, aadTokenRefresher);

            await partnerCredentials.AuthenticateAsync(requestContext).ConfigureAwait(false);

            return(partnerCredentials);
        }