Ejemplo n.º 1
0
        public void LogonAuthCycleWithTokenAndRefresh() {
            Loggy.Default.Level = LogLevel.Error;

            CheckRate(i => {
                if (i%100 == 0) {
                    var es = (UserService) _container.Get<IUserService>();
                    es.UserCache.Clear();
                }
                var identity = _container.Get<ILogonService>().Logon("esuser", Epass);
                var req = new HttpRequestDescriptor
                {
                    User = new GenericPrincipal(identity, null),
                    UserAgent = "testagent",
                    RemoteEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3456)
                };
                var roles =(RoleResolverService) _container.Get<IRoleResolverService>();
                roles.Cache.Clear();
                var isinrole = roles.IsInRole(identity, SecurityConst.ROLE_ADMIN);
                isinrole = roles.IsInRole(identity, "role2");
                isinrole = roles.IsInRole(identity, "role1");
                

                var hts = _container.Get<IHttpTokenService>();
                var token = hts.Create(req);
                var enc = _container.Get<ITokenEncryptor>();
                var cookie = enc.Encrypt(token);
                req.Headers = new Dictionary<string, string>();
                req.Headers["Cookie"] = "testauth=" + cookie;
                var token2 = hts.Extract(req);
                hts.IsValid(req, token2);

            });
            Loggy.Default.Level = LogLevel.All;
        }
Ejemplo n.º 2
0
 public void RedirectsHtmlNotAuth()
 {
     var id = new Identity { IsAuthenticated = false };
     var req = new HttpRequestDescriptor { User = new GenericPrincipal(id, null), Uri = new Uri("http://host/test.html") };
     var auth = a.Authorize(req);
     Assert.True(!string.IsNullOrWhiteSpace(auth.Redirect));
 }
Ejemplo n.º 3
0
        public void LogonAuthCycleWithTokenAndRefresh()
        {
            Loggy.Default.Level = LogLevel.Error;

            CheckRate(i => {
                if (i % 100 == 0)
                {
                    var es = (UserService)_container.Get <IUserService>();
                    es.UserCache.Clear();
                }
                var identity = _container.Get <ILogonService>().Logon("esuser", Epass);
                var req      = new HttpRequestDescriptor
                {
                    User           = new GenericPrincipal(identity, null),
                    UserAgent      = "testagent",
                    RemoteEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3456)
                };
                var roles = (RoleResolverService)_container.Get <IRoleResolverService>();
                roles.Cache.Clear();
                var isinrole = roles.IsInRole(identity, SecurityConst.ROLE_ADMIN);
                isinrole     = roles.IsInRole(identity, "role2");
                isinrole     = roles.IsInRole(identity, "role1");


                var hts               = _container.Get <IHttpTokenService>();
                var token             = hts.Create(req);
                var enc               = _container.Get <ITokenEncryptor>();
                var cookie            = enc.Encrypt(token);
                req.Headers           = new Dictionary <string, string>();
                req.Headers["Cookie"] = "testauth=" + cookie;
                var token2            = hts.Extract(req);
                hts.IsValid(req, token2);
            });
            Loggy.Default.Level = LogLevel.All;
        }
Ejemplo n.º 4
0
 public void CustomRoleDeny() {
     var id = new Identity { IsAuthenticated = true, User = new User()};
     var req = new HttpRequestDescriptor { User = new GenericPrincipal(id, null), Uri = new Uri("http://host/roled.html") };
     var auth = a.Authorize(req);
     Assert.False(auth.Process);
     Assert.NotNull(auth.Error);
 }
Ejemplo n.º 5
0
 public void DenyUnknownNotAuth()
 {
     var id = new Identity { IsAuthenticated = false };
     var req = new HttpRequestDescriptor { User = new GenericPrincipal(id, null), Uri = new Uri("http://host/operation") };
     var auth = a.Authorize(req);
     Assert.NotNull(auth.Error);
 }
Ejemplo n.º 6
0
        public void CanGetJsonFromQueryUrlEncoded()
        {
            var req = new HttpRequestDescriptor {
                Uri = new Uri(@"http://localhost/test?{""my"":""test+%23%20%23%2Bbest=1&2""}")
            };
            var result = RequestParameters.Create(req);

            Assert.NotNull(result.QueryJson);
            Assert.AreEqual("test # #+best=1&2", result.Get("my"));
        }
Ejemplo n.º 7
0
        public void AllowAllAuthToAnyHtml()
        {
            var id = new Identity {
                IsAuthenticated = true
            };
            var req = new HttpRequestDescriptor {
                User = new GenericPrincipal(id, null), Uri = new Uri("http://host/some.html")
            };
            var auth = a.Authorize(req);

            Assert.True(auth.Process);
        }
Ejemplo n.º 8
0
        public void CustomRedirection()
        {
            var id = new Identity {
                IsAuthenticated = false
            };
            var req = new HttpRequestDescriptor {
                User = new GenericPrincipal(id, null), Uri = new Uri("http://host/spredir.html")
            };
            var auth = a.Authorize(req);

            Assert.AreEqual(@"/public.html?referer=/spredir.html", auth.Redirect);
        }
Ejemplo n.º 9
0
        public void CustomPublicResource()
        {
            var id = new Identity {
                IsAuthenticated = false
            };
            var req = new HttpRequestDescriptor {
                User = new GenericPrincipal(id, null), Uri = new Uri("http://host/public.html")
            };
            var auth = a.Authorize(req);

            Assert.True(auth.Process);
        }
Ejemplo n.º 10
0
        public void DenyUnknownNotAuth()
        {
            var id = new Identity {
                IsAuthenticated = false
            };
            var req = new HttpRequestDescriptor {
                User = new GenericPrincipal(id, null), Uri = new Uri("http://host/operation")
            };
            var auth = a.Authorize(req);

            Assert.NotNull(auth.Error);
        }
Ejemplo n.º 11
0
        public void RedirectsHtmlNotAuth()
        {
            var id = new Identity {
                IsAuthenticated = false
            };
            var req = new HttpRequestDescriptor {
                User = new GenericPrincipal(id, null), Uri = new Uri("http://host/test.html")
            };
            var auth = a.Authorize(req);

            Assert.True(!string.IsNullOrWhiteSpace(auth.Redirect));
        }
Ejemplo n.º 12
0
        public void AllowAnyCss()
        {
            var id = new Identity {
                IsAuthenticated = false
            };
            var req = new HttpRequestDescriptor {
                User = new GenericPrincipal(id, null), Uri = new Uri("http://host/test.css")
            };
            var auth = a.Authorize(req);

            Assert.True(auth.Process);
        }
Ejemplo n.º 13
0
        public void AllowPathedRole()
        {
            var id = new Identity {
                IsAuthenticated = true, IsAdmin = true
            };
            var req = new HttpRequestDescriptor {
                User = new GenericPrincipal(id, null), Uri = new Uri("http://host/admin-data.html")
            };
            var auth = a.Authorize(req);

            Assert.True(auth.Process);
        }
Ejemplo n.º 14
0
        public void CustomRoleDeny()
        {
            var id = new Identity {
                IsAuthenticated = true, User = new User()
            };
            var req = new HttpRequestDescriptor {
                User = new GenericPrincipal(id, null), Uri = new Uri("http://host/roled.html")
            };
            var auth = a.Authorize(req);

            Assert.False(auth.Process);
            Assert.NotNull(auth.Error);
        }
        public void GuestAuth()
        {
            var req = new HttpRequestDescriptor {
                RemoteEndPoint = new IPEndPoint(IPAddress.Parse("234.43.123.25"), 21324),
                LocalEndPoint  = new IPEndPoint(IPAddress.Parse("192.168.0.100"), 14141)
            };
            var i = (Identity)di.GetUserIdentity(req);

            Assert.True(i.IsGuest);
            Assert.True(i.IsAuthenticated);
            Assert.AreEqual("guest", i.AuthenticationType);
            Assert.False(i.IsAdmin);
            Assert.AreEqual(di.AnyGuestName, i.Name);
        }
        public void LocalGuestAuth()
        {
            var req = new HttpRequestDescriptor
            {
                RemoteEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 21324),
                LocalEndPoint  = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 14141)
            };
            var i = (Identity)di.GetUserIdentity(req);

            Assert.True(i.IsGuest);
            Assert.True(i.IsAuthenticated);
            Assert.AreEqual("localguest", i.AuthenticationType);
            Assert.True(i.IsAdmin);
            Assert.AreEqual(di.LocalGuestName, i.Name);
        }
Ejemplo n.º 17
0
        public void CanGetJsonFromFormUrlEncoded()
        {
            var str  = new MemoryStream();
            var strw = new StreamWriter(str);

            strw.Write(@"{""my"":""test+%23%20%23+best=1&2""}");
            strw.Flush();
            str.Position = 0;
            var req = new HttpRequestDescriptor {
                Uri = new Uri(@"http://localhost/test"), Method = "POST", Stream = str, ContentLength = str.Length
            };
            var result = RequestParameters.Create(req);

            Assert.AreEqual("test+# #+best=1&2", result.Get("my"));
            Assert.NotNull(result.FormJson);
        }
        public void TrustAuthTwo()
        {
            var req = new HttpRequestDescriptor {
                RemoteEndPoint = new IPEndPoint(IPAddress.Parse("192.168.0.2"), 21324),
                LocalEndPoint  = new IPEndPoint(IPAddress.Parse("192.168.0.3"), 3450),
                Headers        = new Dictionary <string, string> {
                    { "Origin", "http://127.0.0.1:9200" }
                }
            };
            var i = (Identity)di.GetUserIdentity(req);

            Assert.False(i.IsGuest);
            Assert.True(i.IsAuthenticated);
            Assert.AreEqual("trusted", i.AuthenticationType);
            Assert.True(i.IsAdmin);
            Assert.AreEqual("controller", i.Name);
        }
Ejemplo n.º 19
0
        public void CanReadHardFormDataFromPost()
        {
            var str  = new MemoryStream();
            var strw = new StreamWriter(str);

            strw.Write(@"x=1&y=2=3+4&z=5=%26");
            strw.Flush();
            str.Position = 0;
            var req = new HttpRequestDescriptor {
                Uri = new Uri(@"http://localhost/test"), Method = "POST", Stream = str, ContentLength = str.Length
            };
            var result = RequestParameters.Create(req);

            Assert.AreEqual("1", result.Get("x"));
            Assert.AreEqual("2=3 4", result.Get("y"));
            Assert.AreEqual("5=&", result.Get("z"));
        }
Ejemplo n.º 20
0
 public void CanRetrieve() {
     var logon = _container.Get<ILogonService>();
     var req = new HttpRequestDescriptor
     {
         User = new GenericPrincipal(logon.Logon("fuser", Fpass), null),
         UserAgent = "testagent",
         RemoteEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3456)
     };
     
     var hts = _container.Get<IHttpTokenService>();
     var token = hts.Create(req);
     var enc = _container.Get<ITokenEncryptor>();
     var cookie = enc.Encrypt(token);
     req.Headers = new Dictionary<string, string>();
     req.Headers["Cookie"] = "testauth=" + cookie;
     var token2 = hts.Extract(req);
     Assert.True(hts.IsValid(req,token2));
     Assert.AreEqual(token.stringify(),token2.stringify());
 }
Ejemplo n.º 21
0
 public void CanCreateAndSet() {
     var logon = _container.Get<ILogonService>();
     var req = new HttpRequestDescriptor {
         User = new GenericPrincipal(logon.Logon("fuser", Fpass), null),
         UserAgent = "testagent",
         RemoteEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"),3456 )
     };
     var hts = _container.Get<IHttpTokenService>();
     var token = hts.Create(req);
     Assert.True(hts.IsValid(req,token));
     var res = new HttpResponseDescriptor();
     hts.Store(res,new Uri("http://my.best.com"),token);
     var cookie = res.Cookies["testauth"];
     Assert.NotNull(cookie);
     Assert.True(cookie.Secure);
     Assert.True(cookie.HttpOnly);
     Assert.AreEqual(".best.com",cookie.Domain);
     Assert.AreEqual("/",cookie.Path);
     Assert.Less(100,cookie.Value.Length);
 }
Ejemplo n.º 22
0
        public void CanRetrieve()
        {
            var logon = _container.Get <ILogonService>();
            var req   = new HttpRequestDescriptor
            {
                User           = new GenericPrincipal(logon.Logon("fuser", Fpass), null),
                UserAgent      = "testagent",
                RemoteEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3456)
            };

            var hts    = _container.Get <IHttpTokenService>();
            var token  = hts.Create(req);
            var enc    = _container.Get <ITokenEncryptor>();
            var cookie = enc.Encrypt(token);

            req.Headers           = new Dictionary <string, string>();
            req.Headers["Cookie"] = "testauth=" + cookie;
            var token2 = hts.Extract(req);

            Assert.True(hts.IsValid(req, token2));
            Assert.AreEqual(token.stringify(), token2.stringify());
        }
Ejemplo n.º 23
0
        public static string Call(this IHostServer host, string command)
        {
            var h  = host.GetHandler(command);
            var ms = new MemoryStream();
            var rs = new HttpResponseDescriptor {
                Stream = ms, NoCloseStream = true
            };
            var rq = new HttpRequestDescriptor {
                Uri = new Uri("http://localhost" + command)
            };

            var ctx = new WebContext {
                Request = rq, Response = rs
            };

            h.Run(host, ctx, null, new CancellationToken());
            var len = ms.Position;

            ms.Position = 0;
            var result = Encoding.UTF8.GetString(ms.GetBuffer(), 0, (int)len);

            return(result);
        }
Ejemplo n.º 24
0
        public void Performance()
        {
            Loggy.Default.Level = LogLevel.Error;
            CheckRate(i => {
                var logon = _container.Get <ILogonService>();
                var req   = new HttpRequestDescriptor
                {
                    User           = new GenericPrincipal(logon.Logon("fuser", Fpass), null),
                    UserAgent      = "testagent",
                    RemoteEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3456)
                };

                var hts               = _container.Get <IHttpTokenService>();
                var token             = hts.Create(req);
                var enc               = _container.Get <ITokenEncryptor>();
                var cookie            = enc.Encrypt(token);
                req.Headers           = new Dictionary <string, string>();
                req.Headers["Cookie"] = "testauth=" + cookie;
                var token2            = hts.Extract(req);
                hts.IsValid(req, token2);
            });
            Loggy.Default.Level = LogLevel.All;
        }
Ejemplo n.º 25
0
        public void CanCreateAndSet()
        {
            var logon = _container.Get <ILogonService>();
            var req   = new HttpRequestDescriptor {
                User           = new GenericPrincipal(logon.Logon("fuser", Fpass), null),
                UserAgent      = "testagent",
                RemoteEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3456)
            };
            var hts   = _container.Get <IHttpTokenService>();
            var token = hts.Create(req);

            Assert.True(hts.IsValid(req, token));
            var res = new HttpResponseDescriptor();

            hts.Store(res, new Uri("http://my.best.com"), token);
            var cookie = res.Cookies["testauth"];

            Assert.NotNull(cookie);
            Assert.True(cookie.Secure);
            Assert.True(cookie.HttpOnly);
            Assert.AreEqual(".best.com", cookie.Domain);
            Assert.AreEqual("/", cookie.Path);
            Assert.Less(100, cookie.Value.Length);
        }
Ejemplo n.º 26
0
        public void Performance() {
            Loggy.Default.Level = LogLevel.Error;
            CheckRate(i => {
                var logon = _container.Get<ILogonService>();
                var req = new HttpRequestDescriptor
                {
                    User = new GenericPrincipal(logon.Logon("fuser", Fpass), null),
                    UserAgent = "testagent",
                    RemoteEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3456)
                };

                var hts = _container.Get<IHttpTokenService>();
                var token = hts.Create(req);
                var enc = _container.Get<ITokenEncryptor>();
                var cookie = enc.Encrypt(token);
                req.Headers = new Dictionary<string, string>();
                req.Headers["Cookie"] = "testauth=" + cookie;
                var token2 = hts.Extract(req);
                hts.IsValid(req, token2);
            });
            Loggy.Default.Level = LogLevel.All;
        }
Ejemplo n.º 27
0
 public void AllowPathedRole()
 {
     var id = new Identity { IsAuthenticated = true, IsAdmin = true};
     var req = new HttpRequestDescriptor { User = new GenericPrincipal(id, null), Uri = new Uri("http://host/admin-data.html") };
     var auth = a.Authorize(req);
     Assert.True(auth.Process);
 }
Ejemplo n.º 28
0
	    public static string Call(this IHostServer host, string command) {
            var h = host.GetHandler(command);
            var ms = new MemoryStream();
            var rs = new HttpResponseDescriptor { Stream = ms, NoCloseStream = true };
            var rq = new HttpRequestDescriptor { Uri = new Uri("http://localhost" + command) };

	        var ctx = new WebContext{Request = rq,Response=rs};
            h.Run(host, ctx, null, new CancellationToken());
            var len = ms.Position;
            ms.Position = 0;
            var result = Encoding.UTF8.GetString(ms.GetBuffer(), 0, (int)len);
            return result;
	    }   
Ejemplo n.º 29
0
 public void AllowAllAuthToAnyHtml() {
     var id = new Identity { IsAuthenticated = true };
     var req = new HttpRequestDescriptor { User = new GenericPrincipal(id, null), Uri = new Uri("http://host/some.html") };
     var auth = a.Authorize(req);
     Assert.True(auth.Process);
 }
Ejemplo n.º 30
0
 public void CustomRedirection() {
     var id = new Identity { IsAuthenticated = false };
     var req = new HttpRequestDescriptor { User = new GenericPrincipal(id, null), Uri = new Uri("http://host/spredir.html") };
     var auth = a.Authorize(req);
     Assert.AreEqual(@"/public.html?referer=/spredir.html", auth.Redirect);
 }
Ejemplo n.º 31
0
 public void CustomPublicResource() {
     var id = new Identity { IsAuthenticated = false };
     var req = new HttpRequestDescriptor { User = new GenericPrincipal(id, null), Uri = new Uri("http://host/public.html") };
     var auth = a.Authorize(req);
     Assert.True(auth.Process);
 }
Ejemplo n.º 32
0
 public void AllowAnyCss() {
     var id = new Identity {IsAuthenticated = false};
     var req = new HttpRequestDescriptor {User =new GenericPrincipal(id,null),  Uri = new Uri("http://host/test.css")};
     var auth = a.Authorize(req);
     Assert.True(auth.Process);
 }