public async Task JwtFromUrlBadResponseFromBadInput()
        {
            var auth = new ClientAuthorizationSettings
            {
                AuthorizationType     = ClientAuthorizationSettings.AuthorizationTypeEnum.JwtFromUrl,
                PostUrl               = "http://localhost",
                PostBody              = "-",
                ResponseTokenJsonPath = "AccessToken"
            };

            _httpClientMock
            .Setup(x => x.SendAsync(It.IsAny <HttpRequestMessage>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent("not json")     // This will cause exception when parsed
            });

            try
            {
                await _authenticationHelper.GetAuthorizationForClientAsync(Tenant, auth, ClientName);

                Assert.Fail("Expected an exception");
            }
            catch (Exception e)
            {
                Assert.IsNotNull(e is FulcrumAssertionFailedException, $"Unexpected exception: {e.GetType().FullName}");
            }
        }
        public async Task JwtFromUrlBadResponseFromException()
        {
            var auth = new ClientAuthorizationSettings
            {
                AuthorizationType     = ClientAuthorizationSettings.AuthorizationTypeEnum.JwtFromUrl,
                PostUrl               = "http://localhost",
                PostBody              = "{}",
                ResponseTokenJsonPath = "AccessToken"
            };

            _httpClientMock
            .Setup(x => x.SendAsync(It.IsAny <HttpRequestMessage>(), It.IsAny <CancellationToken>()))
            .ThrowsAsync(new Exception());

            try
            {
                await _authenticationHelper.GetAuthorizationForClientAsync(Tenant, auth, ClientName);

                Assert.Fail("Expected an exception");
            }
            catch (Exception e)
            {
                Assert.IsNotNull(e is FulcrumAssertionFailedException, $"Unexpected exception: {e.GetType().FullName}");
            }
        }
 public async Task PlatformServiceFailsIfNoTokenRefresher()
 {
     var auth = new ClientAuthorizationSettings
     {
         AuthorizationType = ClientAuthorizationSettings.AuthorizationTypeEnum.NexusPlatformService
     };
     await _authenticationHelper.GetAuthorizationForClientAsync(Tenant, auth, ClientName);
 }
Esempio n. 4
0
        private void SetupConfigMock(ClientAuthorizationSettings settings)
        {
            var conf = new JObject {
                { $"{ClientName}-authentication", JObject.FromObject(settings) }
            };

            LeverConfiguration = new MockLeverConfiguration(conf);
        }
        private void SetupConfigMock(ClientAuthorizationSettings settings)
        {
            var authenticationId = Guid.NewGuid().ToString();

            _clientConfigurations.First(x => x.Name == ClientName).Authentication = authenticationId;
            settings.Id = authenticationId;
            _authentications.Add(settings);
            CreateLeverConfiguration();
        }
 public async Task TestBasicAuthNoPassword()
 {
     var auth = new ClientAuthorizationSettings
     {
         AuthorizationType = ClientAuthorizationSettings.AuthorizationTypeEnum.Basic,
         Username          = "******"
     };
     await _authenticationHelper.GetAuthorizationForClientAsync(Tenant, auth, ClientName);
 }
 public async Task JwtFromUrlNoResponseTokenPath()
 {
     var auth = new ClientAuthorizationSettings
     {
         AuthorizationType = ClientAuthorizationSettings.AuthorizationTypeEnum.JwtFromUrl,
         PostUrl           = "http://localhost",
         PostBody          = "{}",
     };
     await _authenticationHelper.GetAuthorizationForClientAsync(Tenant, auth, ClientName);
 }
 public async Task JwtFromUrlNoUrl()
 {
     var auth = new ClientAuthorizationSettings
     {
         AuthorizationType     = ClientAuthorizationSettings.AuthorizationTypeEnum.JwtFromUrl,
         PostBody              = "{}",
         ResponseTokenJsonPath = "AccessToken"
     };
     await _authenticationHelper.GetAuthorizationForClientAsync(Tenant, auth, ClientName);
 }
        public async Task BearerTokenSuccess()
        {
            var auth = new ClientAuthorizationSettings
            {
                AuthorizationType = ClientAuthorizationSettings.AuthorizationTypeEnum.BearerToken,
                Token             = Jwt
            };
            var result = await _authenticationHelper.GetAuthorizationForClientAsync(Tenant, auth, ClientName);

            Assert.IsNotNull(result, JsonConvert.SerializeObject(auth, Formatting.Indented));
            Assert.AreEqual("bearer", result.Type.ToLowerInvariant());
            Assert.AreEqual(Jwt, result.Token, result.Token);
        }
        public async Task TestBasicAuthSuccess()
        {
            const string username = "******";
            const string password = "******";

            var auth = new ClientAuthorizationSettings
            {
                AuthorizationType = ClientAuthorizationSettings.AuthorizationTypeEnum.Basic,
                Username          = username,
                Password          = password
            };

            var result = await _authenticationHelper.GetAuthorizationForClientAsync(Tenant, auth, ClientName);

            Assert.AreEqual($"{username}:{password}", Base64Decode(result.Token));
            Assert.AreEqual("basic", result.Type.ToLowerInvariant());
        }
        public async Task JwtFromUrlSuccessAfterBadResponse()
        {
            var auth = new ClientAuthorizationSettings
            {
                AuthorizationType     = ClientAuthorizationSettings.AuthorizationTypeEnum.JwtFromUrl,
                PostUrl               = "http://localhost",
                PostBody              = "{}",
                ResponseTokenJsonPath = "data.AccessToken"
            };

            _httpClientMock
            .Setup(x => x.SendAsync(It.IsAny <HttpRequestMessage>(), It.IsAny <CancellationToken>()))
            .ThrowsAsync(new Exception());

            try
            {
                await _authenticationHelper.GetAuthorizationForClientAsync(Tenant, auth, ClientName);

                Assert.Fail("Expected an exception");
            }
            catch (Exception e)
            {
                Assert.IsNotNull(e is FulcrumAssertionFailedException, $"Unexpected exception: {e.GetType().FullName}");
            }

            // Now, setup a successful response and make sure the bad response is not cached
            _httpClientMock
            .Setup(x => x.SendAsync(It.IsAny <HttpRequestMessage>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(JObject.FromObject(new
                {
                    data = new
                    {
                        AccessToken = Jwt
                    }
                }).ToString())
            });

            var result = await _authenticationHelper.GetAuthorizationForClientAsync(Tenant, auth, ClientName);

            Assert.AreEqual("bearer", result.Type.ToLowerInvariant());
            Assert.AreEqual(Jwt, result.Token);
        }
        public async Task PlatformServiceSuccess()
        {
            var          tokenRefresherMock = new Mock <ITokenRefresher>();
            const string platformJwt        = "platform-dancing";

            tokenRefresherMock
            .Setup(x => x.GetJwtTokenAsync(It.IsAny <CancellationToken>()))
            .ReturnsAsync(new AuthenticationToken {
                Type = "Bearer", AccessToken = platformJwt, ExpiresOn = DateTimeOffset.UtcNow.AddHours(1)
            });

            var helper = new ServiceAuthenticationHelper(tokenRefresherMock.Object);

            var auth = new ClientAuthorizationSettings
            {
                AuthorizationType = ClientAuthorizationSettings.AuthorizationTypeEnum.NexusPlatformService
            };
            var result = await helper.GetAuthorizationForClientAsync(Tenant, auth, ClientName);

            Assert.IsNotNull(result, JsonConvert.SerializeObject(auth, Formatting.Indented));
            Assert.AreEqual("bearer", result.Type.ToLowerInvariant());
            Assert.AreEqual(platformJwt, result.Token, result.Token);
        }
        public async Task JwtFromUrlSuccess()
        {
            const string expectedContentType = "xx-application/json";
            var          auth = new ClientAuthorizationSettings
            {
                AuthorizationType     = ClientAuthorizationSettings.AuthorizationTypeEnum.JwtFromUrl,
                PostUrl               = "http://localhost",
                PostBody              = "{}",
                PostContentType       = expectedContentType,
                ResponseTokenJsonPath = "data.AccessToken"
            };

            string contentType = null;

            _httpClientMock.Setup(x => x.SendAsync(It.IsAny <HttpRequestMessage>(), It.IsAny <CancellationToken>()))
            .Callback((HttpRequestMessage request, CancellationToken cancellationToken) =>
            {
                contentType = request.Content.Headers.ContentType.MediaType;
            })
            .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(JObject.FromObject(new
                {
                    data = new
                    {
                        AccessToken = Jwt
                    }
                }).ToString())
            });

            var result = await _authenticationHelper.GetAuthorizationForClientAsync(Tenant, auth, ClientName);

            Assert.AreEqual("bearer", result.Type.ToLowerInvariant());
            Assert.AreEqual(Jwt, result.Token, result.Token);
            Assert.AreEqual(expectedContentType, contentType);
        }