Esempio n. 1
0
        private HttpWebRequest CreateSiteWebRequest(SiteInfo siteInfo, string url)
        {
            var req         = (HttpWebRequest)WebRequest.Create(url);
            var siteSession = SessionBag.Get <SiteSession>();

            if (siteSession != null)
            {
                if (siteSession.BearerToken != null)
                {
                    req.AddBearerToken(siteSession.BearerToken);
                }
                else if (siteSession.SessionId != null)
                {
                    var overrideParam = "X-" + Keywords.SessionId;
                    req.Headers[overrideParam] = siteSession.SessionId;
                }
                else if (siteSession.UserName != null && siteSession.Password != null)
                {
                    req.AddBasicAuth(siteSession.UserName, siteSession.Password);
                }
                else if (siteSession.AuthSecret != null)
                {
                    var overrideParam = HttpHeaders.XParamOverridePrefix + Keywords.AuthSecret;
                    req.Headers[overrideParam] = siteSession.AuthSecret;
                }
            }
            return(req);
        }
Esempio n. 2
0
        private JsonServiceClient CreateSiteClient(SiteInfo siteInfo)
        {
            var client      = new JsonServiceClient(siteInfo.BaseUrl);
            var siteSession = SessionBag.Get <SiteSession>();

            if (siteSession != null)
            {
                if (siteSession.BearerToken != null)
                {
                    client.BearerToken = siteSession.BearerToken;
                }
                else if (siteSession.SessionId != null)
                {
                    client.RequestFilter = req =>
                                           req.Headers["X-" + Keywords.SessionId] = siteSession.SessionId;
                }
                else if (siteSession.UserName != null && siteSession.Password != null)
                {
                    client.SetCredentials(siteSession.UserName, siteSession.Password);
                }
                else if (siteSession.AuthSecret != null)
                {
                    client.RequestFilter = req =>
                                           req.Headers[HttpHeaders.XParamOverridePrefix + Keywords.AuthSecret] = siteSession.AuthSecret;
                }
            }
            return(client);
        }
 public object Any(GetSession request)
 {
     return(new GetSessionResponse
     {
         Result = SessionAs <CustomUserSession>(),
         UnAuthInfo = SessionBag.Get <UnAuthInfo>(typeof(UnAuthInfo).Name),
     });
 }
        public object Any(HelloRequest request)
        {
            SessionBag.Set(HelloServiceCounterKey, SessionBag.Get <int>(HelloServiceCounterKey) + 1);
            var userSession = SessionAs <CustomUserSession>();
            var roles       = string.Join(", ", userSession.Roles.ToArray());

            return(new HelloResponse {
                Result = "Hello, " + request.Name + ", your role(s): " + roles
            });
        }
Esempio n. 5
0
        public object Any(SiteAuthenticate request)
        {
            var siteInfo    = AssertSite(request.Slug);
            var siteSession = SessionBag.Get <SiteSession>();

            if (siteSession?.User != null && request.provider != AuthenticateService.LogoutAction && request.provider == null)
            {
                return(siteSession.User);
            }

            try
            {
                var client      = CreateSiteClient(siteInfo);
                var authRequest = request.ConvertTo <Authenticate>();

                if (request.AccessToken?.IndexOf(':') >= 0)                            //required for AuthProviders needing both Access+Secret (e.g. twitter)
                {
                    authRequest.AccessTokenSecret = request.AccessToken.LeftPart(':'); //secret is first part when both provided
                    authRequest.AccessToken       = request.AccessToken.RightPart(':');
                }

                if (nonProviders.Contains(request.provider))
                {
                    authRequest.provider = null;
                    if (request.provider == "bearer")
                    {
                        client.BearerToken = request.AccessToken;
                    }
                    else if (request.provider == "session")
                    {
                        client.SetSessionId(request.AccessToken);
                    }
                    else if (request.provider == "authsecret")
                    {
                        client.Headers[HttpHeaders.XParamOverridePrefix + Keywords.AuthSecret] = request.AccessToken;
                    }
                }

                var response = client.Post(authRequest);
                siteSession = SiteSession.Create(request, response);
                SessionBag.Set(siteSession);
            }
            finally
            {
                if (request.provider == AuthenticateService.LogoutAction)
                {
                    SessionBag.Remove <SiteSession>();
                    siteSession = null;
                }
            }

            return(siteSession?.User);
        }
Esempio n. 6
0
        public object Any(Session request)
        {
            var untyped = SessionBag["untyped"] as CustomSession ?? new CustomSession();
            var typed   = SessionBag.Get <CustomSession>("typed") ?? new CustomSession();

            untyped.Counter++;
            typed.Counter++;

            SessionBag["untyped"] = untyped;
            SessionBag.Set("typed", typed);

            var response = new SessionResponse {
                Typed   = typed,
                UnTyped = untyped,
            };

            return(response);
        }
        public object Any(UpdateSession request)
        {
            var session = SessionAs <CustomUserSession>();

            session.CustomName = request.CustomName;

            var unAuthInfo = SessionBag.Get <UnAuthInfo>() ?? new UnAuthInfo();

            unAuthInfo.CustomInfo = request.CustomName + " - CustomInfo";
            SessionBag.Set(unAuthInfo);

            this.SaveSession(session);

            return(new GetSessionResponse
            {
                Result = SessionAs <CustomUserSession>(),
                UnAuthInfo = unAuthInfo,
            });
        }