Exemplo n.º 1
0
        public async Task SetJwt_ShouldSucceed()
        {
            string jwtToken        = null;
            string arangodbBaseUrl = $"http://{_fixture.ArangoDbHost}:8529/";

            using (var transport = HttpApiTransport.UsingNoAuth(
                       new Uri(arangodbBaseUrl),
                       nameof(HttpApiTransportTest)))
            {
                var authClient = new AuthApiClient(transport);

                var jwtTokenResponse = await authClient.GetJwtTokenAsync(
                    new JwtTokenRequestBody
                {
                    Username = _fixture.Username,
                    Password = _fixture.Password
                });

                jwtToken = jwtTokenResponse.Jwt;

                // Use token in current transport
                transport.SetJwtToken(jwtToken);
                var databaseApi = new DatabaseApiClient(transport);

                var userDatabasesResponse = await databaseApi.GetUserDatabasesAsync();

                Assert.NotEmpty(userDatabasesResponse.Result);
            }
        }
Exemplo n.º 2
0
        private static async Task <HttpApiTransport> GetTransportAsync(ArangoDbOptions options)
        {
            if (!string.IsNullOrWhiteSpace(options.JwtToken))
            {
                return(HttpApiTransport.UsingJwtAuth(new Uri(options.HostUri), options.Database, options.JwtToken));
            }

            if (options.IsGenerateJwtTokenBasedOnUserNameAndPassword)
            {
                if (string.IsNullOrWhiteSpace(options.UserName))
                {
                    throw new ArgumentNullException(nameof(options.UserName));
                }
                if (string.IsNullOrWhiteSpace(options.Password))
                {
                    throw new ArgumentNullException(nameof(options.Password));
                }

                var transport        = HttpApiTransport.UsingNoAuth(new Uri(options.HostUri), options.Database);
                var authClient       = new AuthApiClient(transport);
                var jwtTokenResponse = await authClient.GetJwtTokenAsync(options.UserName, options.Password);

                transport.SetJwtToken(jwtTokenResponse.Jwt);
                return(transport);
            }

            if (!string.IsNullOrWhiteSpace(options.UserName) && !string.IsNullOrWhiteSpace(options.Password))
            {
                return(HttpApiTransport.UsingBasicAuth(new Uri(options.HostUri), options.Database, options.UserName, options.Password));
            }

            return(HttpApiTransport.UsingNoAuth(new Uri(options.HostUri), options.Database));
        }
Exemplo n.º 3
0
        public async Task UsingJwtAuth_ShouldSucceed()
        {
            string jwtToken        = null;
            string arangodbBaseUrl = $"http://{_fixture.ArangoDbHost}:8529/";

            using (var transport = HttpApiTransport.UsingNoAuth(
                       new Uri(arangodbBaseUrl),
                       nameof(HttpApiTransportTest)))
            {
                var authClient = new AuthApiClient(transport);
                authClient.ThrowErrorsAsExceptions = true;

                var jwtTokenResponse = await authClient.GetJwtTokenAsync(
                    new JwtTokenRequestBody
                {
                    Username = _fixture.Username,
                    Password = _fixture.Password
                });

                jwtToken = jwtTokenResponse.Jwt;

                DatabaseApiClient databaseApi = new DatabaseApiClient(transport)
                {
                    ThrowErrorsAsExceptions = true
                };

                // Not authorized, should throw.
                var ex = await Assert.ThrowsAsync <ApiErrorException>(async() =>
                                                                      await databaseApi.GetCurrentDatabaseInfoAsync());
            }

            // Use token in a new transport created via `UsingJwtAuth`.
            using (var transport = HttpApiTransport.UsingJwtAuth(
                       new Uri(arangodbBaseUrl),
                       nameof(HttpApiTransportTest),
                       jwtToken))
            {
                var databaseApi           = new DatabaseApiClient(transport);
                var userDatabasesResponse = await databaseApi.GetUserDatabasesAsync();

                Assert.NotEmpty(userDatabasesResponse.Results);
            }
        }