Esempio n. 1
0
        public HttpResponseMessage <bool> Login(HttpRequestMessage request)
        {
            var connectionstring = System.Configuration.ConfigurationManager.ConnectionStrings["EarthwatchersConnection"].ConnectionString;
            var isOkay           = Authenticator.Authenticate(connectionstring);

            if (isOkay)
            {
                //Guardo un nuevo Scoring
                ScoreRepository scoreRepository = new ScoreRepository(connectionstring);
                scoreRepository.AddLoginScore(System.Web.HttpContext.Current.User.Identity.Name);

                if (!Session.HasLoggedUser())
                {
                    EarthwatcherRepository ewRepo = new EarthwatcherRepository(connectionstring);
                    var ew = ewRepo.GetEarthwatcher(System.Web.HttpContext.Current.User.Identity.Name, false);
                    if (ew != null)
                    {
                        Session.GenerateCookie(ew);
                    }
                }

                return(new HttpResponseMessage <bool>(isOkay)
                {
                    StatusCode = HttpStatusCode.OK
                });
            }
            else
            {
                return(new HttpResponseMessage <bool>(isOkay)
                {
                    StatusCode = HttpStatusCode.Forbidden
                });
            }
        }
Esempio n. 2
0
        public HttpResponseMessage <List <Rank> > GetContestLeaderBoard(int userid, HttpRequestMessage request)
        {
            try
            {
                IEarthwatcherRepository ewRepo = new EarthwatcherRepository(connectionstring);
                var earthwatcher = ewRepo.GetEarthwatcher(userid);

                var leaderBoard       = scoreRepository.GetLeaderBoardNationalRanking(true, earthwatcher.PlayingRegion);
                var rankingCollection = leaderBoard.Take(10).ToList();
                if (!rankingCollection.Any(x => x.EarthwatcherId == userid))
                {
                    rankingCollection.Add(leaderBoard.Where(x => x.EarthwatcherId == userid).First());
                }
                foreach (var r in rankingCollection)
                {
                    if (string.IsNullOrEmpty(r.Name))
                    {
                        r.Name = r.Nick;
                    }
                }

                return(new HttpResponseMessage <List <Rank> >(rankingCollection)
                {
                    StatusCode = HttpStatusCode.OK
                });
            }
            catch (Exception ex)
            {
                return(new HttpResponseMessage <List <Rank> >(null)
                {
                    StatusCode = HttpStatusCode.InternalServerError, ReasonPhrase = ex.Message
                });
            }
        }
Esempio n. 3
0
        public void GenerateAndUpdatePassword(Earthwatcher earthwatcher)
        {
            var prefix         = PasswordChecker.GeneratePrefix();
            var hashedPassword = PasswordChecker.GenerateHashedPassword(earthwatcher.Password, prefix);

            // store in database
            var connectionstring = System.Configuration.ConfigurationManager.ConnectionStrings["EarthwatchersConnection"].ConnectionString;
            var repos            = new EarthwatcherRepository(connectionstring);

            repos.UpdatePassword(earthwatcher, prefix, hashedPassword);
        }
Esempio n. 4
0
        private static bool TryGetPrincipal(string connectionString, string userName, string password, out IPrincipal principal)
        {
            var earthwatcher = new EarthwatcherRepository(connectionString).GetEarthwatcher(userName, false);

            if (earthwatcher != null)
            {
                var membershipProvider = new EarthwatchersMembershipProvider(connectionString);
                var result             = membershipProvider.ValidateUser(userName, password, earthwatcher.ApiEwId);
                if (result)
                {
                    principal = new GenericPrincipal(new GenericIdentity(userName), earthwatcher.GetRoles());
                    return(true);
                }
            }
            principal = null;
            return(false);
        }
Esempio n. 5
0
        public static Earthwatcher CurrentUser()
        {
            if (!HttpContext.Current.Request.Headers.AllKeys.Contains("Authorization"))
            {
                return(null);
            }

            string authHeader = HttpContext.Current.Request.Headers["Authorization"];
            var    creds      = ParseAuthHeader(authHeader);

            if (creds.Length < 2)
            {
                return(null);
            }

            EarthwatcherRepository repo = new EarthwatcherRepository(System.Configuration.ConfigurationManager.ConnectionStrings["EarthwatchersConnection"].ConnectionString);
            var user = repo.GetEarthwatcher(creds[0], false);

            return(user);
        }
Esempio n. 6
0
        public bool ValidateUser(string username, string password, int apiEwId = 0)
        {
            var   isValid = false;
            ApiEw apiEw   = null;
            EarthwatcherRepository earthwatcherRepo = new EarthwatcherRepository(connectionString);

            if (apiEwId != 0)
            {
                apiEw = earthwatcherRepo.GetApiEwById(apiEwId);
            }
            if (apiEw == null)
            {
                var credentials = CredentialsRepository.GetCredentials(connectionString, username);
                isValid = PasswordChecker.CheckPassword(password, credentials.Prefix, credentials.Hash);
            }
            else
            {
                //TODO: Validacion de AccessToken, por ahora si entro con una api lo manda derecho
                isValid = true;
            }
            return(isValid);
        }