Example #1
0
        public static SiteSession Create(SiteAuthenticate request, AuthenticateResponse user)
        {
            var to = new SiteSession {
                Slug = request.Slug,
                User = user,
            };

            if (user != null && request.provider != "authsecret")
            {
                if (!string.IsNullOrEmpty(user.BearerToken))
                {
                    to.BearerToken = user.BearerToken;
                }
                else if (!string.IsNullOrEmpty(user.SessionId))
                {
                    to.SessionId = user.SessionId;
                }
            }
            if (request.AccessToken != null)
            {
                if (request.provider == "bearer")
                {
                    to.BearerToken = request.AccessToken;
                }
                else if (request.provider == "session")
                {
                    to.SessionId = request.AccessToken;
                }
                else if (request.provider == "authsecret")
                {
                    to.AuthSecret = request.AccessToken;
                }
            }
            return(to);
        }
Example #2
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);
        }