Exemplo n.º 1
0
        public void SignOut()
        {
            ICookieFactory cookieFactory = new CookieFactory();
            ICookieClient  cookieClient  = cookieFactory.Create();

            cookieClient.Remove(AuthConfigProvider.AuthConfig.CookieName);
        }
Exemplo n.º 2
0
        public TBody GetBody()
        {
            //  获取 密钥
            string secret = SecretBuilder.Build();

            if (string.IsNullOrWhiteSpace(secret))
            {
                throw new Exception("应用程序密钥(AppSecret)为空或null");
            }

            ICookieFactory cookieFactory = new CookieFactory();
            ICookieClient  cookieClient  = cookieFactory.Create();

            if (!cookieClient.Contains(AuthConfigProvider.AuthConfig.CookieName))
            {
                return(null);
            }

            // 获取cookie, 并解密 数据
            string            token            = cookieClient.GetCookie(AuthConfigProvider.AuthConfig.CookieName);
            IAlgorithmFactory algorithmFactory = new HMACSHAAlgorithmFactory();
            IJsonSerializer   serializer       = new JsonNetSerializer();
            IDateTimeProvider provider         = new UtcDateTimeProvider();
            IJwtValidator     validator        = new JwtValidator(serializer, provider);
            IBase64UrlEncoder urlEncoder       = new JwtBase64UrlEncoder();
            IJwtDecoder       decoder          = new JwtDecoder(serializer, validator, urlEncoder, algorithmFactory);
            TBody             authUser         = decoder.DecodeToObject <TBody>(token, secret, true);

            SignIn(authUser);

            return(authUser);
        }
Exemplo n.º 3
0
        public void Create_NullParameters_DoesNotThrow()
        {
            // Arrange

            // Act
            TestDelegate action = () => CookieFactory.Create(null, null);

            // Assert
            Assert.DoesNotThrow(action);
        }
Exemplo n.º 4
0
        public void SignIn(TBody body)
        {
            //  获取 密钥
            string secret = SecretBuilder.Build();

            if (string.IsNullOrWhiteSpace(secret))
            {
                throw new Exception("应用程序密钥(AppSecret)为空或null");
            }

            ICookieFactory cookieFactory = new CookieFactory();
            ICookieClient  cookieClient  = cookieFactory.Create();

            cookieClient.SetCookie(AuthConfigProvider.AuthConfig.CookieName, body.SerializeObject(), AuthConfigProvider.AuthConfig.Expires,
                                   value =>
            {
                ICryptor cryptor = new DesCryptor(SecretBuilder.AppKey, secret);
                return(cryptor.Encrypt(value));
            });
        }
Exemplo n.º 5
0
        public void SignIn(TBody body)
        {
            //  获取 密钥
            string secret = SecretBuilder.Build();

            if (string.IsNullOrWhiteSpace(secret))
            {
                throw new Exception("应用程序密钥(AppSecret)为空或null");
            }

            //  生成加密token;
            IAlgorithmFactory algorithmFactory = new HMACSHAAlgorithmFactory();
            IJwtAlgorithm     algorithm        = algorithmFactory.Create(AuthConfigProvider.AuthConfig.JwtAlgorithmType);
            IJsonSerializer   serializer       = new JsonNetSerializer();
            IBase64UrlEncoder urlEncoder       = new JwtBase64UrlEncoder();
            IJwtEncoder       encoder          = new JwtEncoder(algorithm, serializer, urlEncoder);
            string            token            = encoder.Encode(body, secret);

            //  写入Cookie
            ICookieFactory cookieFactory = new CookieFactory();
            ICookieClient  cookieClient  = cookieFactory.Create();

            cookieClient.SetCookie(AuthConfigProvider.AuthConfig.CookieName, token, AuthConfigProvider.AuthConfig.Expires);
        }
Exemplo n.º 6
0
        public TBody GetBody()
        {
            //  获取 密钥
            string secret = SecretBuilder.Build();

            if (string.IsNullOrWhiteSpace(secret))
            {
                throw new Exception("应用程序密钥(AppSecret)为空或null");
            }

            ICookieFactory cookieFactory = new CookieFactory();
            ICookieClient  cookieClient  = cookieFactory.Create();

            if (!cookieClient.Contains(AuthConfigProvider.AuthConfig.CookieName))
            {
                return(null);
            }

            string token = cookieClient.GetCookie(AuthConfigProvider.AuthConfig.CookieName, value =>
            {
                ICryptor cryptor = new DesCryptor(SecretBuilder.AppKey, secret);
                return(cryptor.Decrypt(value));
            });

            TBody authUser = token.DeserializeObject <TBody>();

            DateTime expires = authUser.exp.AsDateTime();

            if (expires < DateTime.Now)
            {
                return(null);                           // 已失效
            }
            SignIn(authUser);

            return(authUser);
        }
Exemplo n.º 7
0
        public FluentSessionRecorder SetCookie(string name, string value, string domain = null, string path = null, DateTime?expiry = null)
        {
            var cookie = CookieFactory.Create(name, value, domain, path, expiry);

            return(SetCookie(cookie));
        }
Exemplo n.º 8
0
        public ActionResult Index(long?industryId = null, long?placeId = null, string theme = null, string feature = "", bool?showForm = null)
        {
            //in this context the APIToken is the widget key
            bool valid = APIContext.Current.ApiToken != null && APIContext.Current.ApiToken.IsValid && !APIContext.Current.ApiToken.IsExpired;

            Log();
            if (!valid)
            {
                throw new HttpException(403, "Api token not valid");
            }

            if (Request.Cookies["sessionId"] == null)
            {
                return(View("~/areas/widget/views/Authorize/Authorize.cshtml"));
            }
            HttpCookie cc = CookieFactory.Create("enabled");

            cc.Expires = DateTime.Now.AddDays(-1);
            Response.Cookies.Add(cc);

            Feature?startFeature = null;

            if (feature.ToLower() == "dashboard")
            {
                startFeature = Feature.Dashboard;
            }
            else if (feature.ToLower() == "competition")
            {
                startFeature = Feature.Competition;
            }
            else if (feature.ToLower() == "community")
            {
                startFeature = Feature.Community;
            }
            else if (feature.ToLower() == "advertising")
            {
                startFeature = Feature.Advertising;
            }
            else if (feature.ToLower() == "featureselect")
            {
                startFeature = Feature.FeatureSelect;
            }
            else if (feature.ToLower() == "select")
            {
                startFeature = Feature.Select;
            }
            WebContext.Current.StartFeature = startFeature;



            if (!string.IsNullOrWhiteSpace(theme))
            {
                HttpCookie c = SizeUp.Core.Web.CookieFactory.Create("theme", theme);
                Response.Cookies.Add(c);
            }
            else
            {
                HttpCookie c = SizeUp.Core.Web.CookieFactory.Create("theme");
                c.Expires = DateTime.Now.AddDays(-1d);
                Response.Cookies.Add(c);
            }



            using (var context = ContextFactory.SizeUpContext)
            {
                if (placeId != null)
                {
                    WebContext.Current.CurrentPlace = Core.DataLayer.Place.Get(context, placeId);
                }
                if (industryId != null)
                {
                    WebContext.Current.CurrentIndustry = Core.DataLayer.Industry.Get(context, industryId);
                }

                string urlToken = HttpUtility.UrlEncode(APIContext.Current.ApiToken.GetToken());
                string urlBase  = "/{0}/{1}/{2}/{3}";
                string url      = string.Format("/{0}?wt={1}", "widget/select", urlToken);

                if (WebContext.Current.CurrentPlace.Id != null && WebContext.Current.CurrentIndustry != null)
                {
                    var place    = WebContext.Current.CurrentPlace;
                    var industry = WebContext.Current.CurrentIndustry;
                    urlBase = string.Format(urlBase, place.State.SEOKey, place.County.SEOKey, place.City.SEOKey, industry.SEOKey);

                    if (WebContext.Current.StartFeature == Feature.Advertising)
                    {
                        url = string.Format("/{0}{1}?wt={2}", "widget/advertising", urlBase, urlToken);
                    }
                    else if (WebContext.Current.StartFeature == Feature.Competition)
                    {
                        url = string.Format("/{0}{1}?wt={2}", "widget/competition", urlBase, urlToken);
                    }
                    else if (WebContext.Current.StartFeature == Feature.Dashboard)
                    {
                        url = string.Format("/{0}{1}?wt={2}", "widget/dashboard", urlBase, urlToken);
                    }
                    else if (WebContext.Current.StartFeature == Feature.Community)
                    {
                        url = string.Format("/{0}{1}?wt={2}", "widget/community", urlBase, urlToken);
                    }
                    else if (WebContext.Current.StartFeature == Feature.FeatureSelect)
                    {
                        url = string.Format("/{0}?wt={1}#featureSelect=true", "widget/select", urlToken);
                    }
                }

                //wells fargo
                if (showForm != null)
                {
                    url = string.Format("/{0}?wt={1}", "widget/select", urlToken);
                }
                return(Redirect(url));
            }
        }
Exemplo n.º 9
0
        public void TestMethod()
        {
            //Arrange
            const string mirabeauUrl   = "https://github.com/wbtstr/wbtstr";
            const string mirabeauTitle = "GitHub - wbtstr/wbtstr: The uncomplicated test automation framework.";

            // Act
            I.Open("https://github.com/")
            .Open(UriParser.ParseWebUrl("https://github.com/"))
            .Type("username??")
            .ResizeWindow(1600, 1050)
            .CapturePage(out var snapshot1)
            .SetCookie(CookieFactory.Create(snapshot1.Cookies.Keys.First(), "Hello World!"))
            .SetCookie(snapshot1.Cookies.Keys.First(), "Hello World!")
            .WaitUntil(() => I.Find(".header-search-input").Displayed)
            .Focus(".header-search-input")
            .Find(".header-search-input", out var input)
            .Focus(input)
            .Wait(seconds: 1)
            .DeleteCookie(snapshot1.Cookies.Keys.First())
            .Enter("wbtstr.net").In(".header-search-input")
            .Append("!!!").In(".header-search-input")
            .DoubleClick(".header-search-input")
            .DoubleClick(input)
            .Wait(seconds: 1)
            .MaximizeWindow()
            .Find(".header-search-input", out var header)
            .Enter("wbtstr").In(header)
            .Append(Keys.Enter).In(header)
            .Wait(seconds: 1)
            .Hover(".repo-list a:first-child")
            .Find(".repo-list a:first-child", out var firstChild)
            .Hover(firstChild)
            .Wait(seconds: 1)
            .Click(".repo-list a:first-child")
            .TakeScreenshot()
            .RightClick("body")
            .Find("body", out var body)
            .RightClick(body);

            IElement md                 = I.Find(".markdown-body");
            string   mdTagName          = md.TagName;
            string   mdSelector         = md.Selector;
            string   mdGetAttribute     = md.GetAttribute("outerHTML");
            string   mdGetCssValue      = md.GetCssValue("height");
            int      mdHeight           = md.Height;
            int      mdWidth            = md.Width;
            int      mdUpperLeftCornerX = md.UpperLeftCornerX;
            int      mdUpperLeftCornerY = md.UpperLeftCornerY;
            string   HTML               = md.HTML;

            var h1 = I.FindMultiple("h1");

            Assert.True(h1.Count >= 0);

            IElement b                  = I.ExecuteJs <IElement>("return window.document.body");
            string   bTagName           = I.ExecuteJs <string>("return window.document.body.tagName");
            long     bChildElementCount = I.ExecuteJs <long>("return window.document.body.childElementCount");
            bool     bHasAttributes     = I.ExecuteJs <bool>("return window.document.body.hasAttributes()");

            var page       = I.CapturePage();
            var cookie     = page.Cookies[page.Cookies.Keys.First()];
            var value      = cookie.Value;
            var domain     = cookie.Domain;
            var path       = cookie.Path;
            var secure     = cookie.Secure;
            var isHttpOnly = cookie.IsHttpOnly;
            var expiry     = cookie.Expiry;

            // Assert
            Assert.AreEqual(mirabeauTitle, page.Title);
            Assert.AreEqual(mirabeauUrl, page.Url);
            Assert.AreEqual(b.TagName.ToUpper(), bTagName.ToUpper());
            Assert.NotZero(bChildElementCount);
            Assert.IsTrue(bHasAttributes);
        }