Exemplo n.º 1
0
        public async Task TestInternalAuth(string goodKey, string requestKey, bool valid)
        {
            var options = new CrpcOptions
            {
                InternalKeys = new string[] { goodKey },
            };

            var middleware = new AuthMiddleware(_loggerFactory, Options.Create(options));
            var context    = new DefaultHttpContext();

            middleware.SetAuthentication(AuthenticationType.AllowInternalAuthentication);
            context.Request.Headers.Add("Authorization", $"bearer {requestKey}");

            if (valid)
            {
                await middleware.InvokeAsync(context, (ctx) => Task.CompletedTask);

                return;
            }

            var ex = await Assert.ThrowsAsync <CrpcException>(async() =>
            {
                await middleware.InvokeAsync(context, (ctx) => Task.CompletedTask);
            });

            Assert.Equal(CrpcCodes.Unauthorized, ex.Message);
        }
Exemplo n.º 2
0
        public async Task TestUnsafeNoAuth(string key)
        {
            var options = new CrpcOptions
            {
                InternalKeys = new string[] { key },
            };

            var middleware = new AuthMiddleware(_loggerFactory, Options.Create(options));
            var context    = new DefaultHttpContext();

            middleware.SetAuthentication(AuthenticationType.UnsafeNoAuthentication);
            context.Request.Headers.Add("Authorization", $"bearer {key}");

            await middleware.InvokeAsync(context, (ctx) => Task.CompletedTask);
        }
Exemplo n.º 3
0
        public async Task TestNoAuthenticationTypeSet()
        {
            var options    = new CrpcOptions();
            var middleware = new AuthMiddleware(_loggerFactory, Options.Create(options));
            var context    = new DefaultHttpContext();

            context.Response.Body = new MemoryStream();

            var ex = await Assert.ThrowsAsync <InvalidOperationException>(async() =>
            {
                await middleware.InvokeAsync(context, (ctx) => Task.CompletedTask);
            });

            Assert.Equal("Authentication type not set", ex.Message);
        }