Ejemplo n.º 1
0
        /// <summary>
        /// Gets challenges used to verify domain ownership.
        /// </summary>
        /// <param name="account">Existing account.</param>
        /// <param name="acmeCertificateFulfillmentPromise">The certificate fulfillment promise retrieved from the RequestCertificate call.</param>
        /// <param name="challengeType">The challenge type expected back.</param>
        /// <returns>Challenge used to verify domain ownership</returns>
        /// <remarks>If requesting a challenge for a wildcard domain, only dns challenge is supported.</remarks>
        /// <exception cref="NotSupportedException">If the challenge type is not supported.</exception>
        /// <exception cref="AcmeProtocolException">On all other Acme related exceptions</exception>
        public async Task <ChallengeCollection> GetChallengesAsync(AcmeAccount account, AcmeCertificateFulfillmentPromise acmeCertificateFulfillmentPromise, ChallengeType challengeType)
        {
            var response = await _acmeApi.GetChallengesAsync(acmeCertificateFulfillmentPromise);

            var errorResponse = response.Where(t => t.Status == AcmeApiResponseStatus.Error);

            if (errorResponse.Any())
            {
                throw new AcmeProtocolException(string.Join(" | ", errorResponse.Select(t => t.Message)));
            }

            ChallengeCollection challenges = new ChallengeCollection();

            foreach (var resp in response)
            {
                AcmeChallenge sChallenge = resp.Data.Challenges.FirstOrDefault(t => t.Type.Equals(challengeType.Value));
                if (sChallenge == null)
                {
                    throw new NotSupportedException($"{challengeType.Value} challenge type not supported in this context.");
                }
                IAcmeChallengeContent challengeContent = null;
                switch (challengeType.Value)
                {
                case ProtoacmeContants.CHALLENGE_HTTP:
                    challengeContent = new HttpChallenge(account, sChallenge, resp.Data.Identifier?.Value);
                    challenges.Add(challengeContent);
                    break;

                case ProtoacmeContants.CHALLENGE_DNS:
                    challengeContent = new DnsChallenge(account, sChallenge, resp.Data.Identifier?.Value);
                    challenges.Add(challengeContent);
                    break;

                case ProtoacmeContants.CHALLENGE_TLS:
                    challengeContent = new TlsChallenge(account, sChallenge, resp.Data.Identifier?.Value);
                    challenges.Add(challengeContent);
                    break;

                default:
                    break;
                }
            }

            return(challenges);
        }
Ejemplo n.º 2
0
        public async Task IssueWildCardCertificate()
        {
            var restApi = new AcmeRestApi(ProtoacmeContants.LETSENCRYPT_STAGING_ENDPOINT);
            var client  = new ProtoacmeClient(restApi);

            //1. Load up the account and challenge data.
            AcmeAccount account = AcmeAccount.FromFile(@"c:\temp\account.dat");
            AcmeCertificateFulfillmentPromise promise = AcmeCertificateFulfillmentPromise.FromFile(@"c:\temp\promise.dat");
            ChallengeCollection challenges            = ChallengeCollection.FromFile <DnsChallenge>(@"c:\temp\challenge.dat");

            //2. Tell Lets Encrypt to verify our challenge.
            var startVerifyResult = await client.Challenge.ExecuteChallengeVerification(challenges[0]);

            AcmeChallengeStatus challengeStatus = null;

            while (challengeStatus == null || challengeStatus.Status == "pending")
            {
                challengeStatus = await client.Challenge.GetChallengeVerificationStatus(challenges[0]);

                await Task.Delay(3000);
            }
            if (challengeStatus.Status != "valid")
            {
                throw new Exception($"Failed to validate challenge token");
            }

            //3. Create the CSR
            CSR csr = CertificateUtility.GenerateCsr(wildCardDns);

            SaveCRTPrivateKey(csr);

            //4. Download the certificate
            var cert = await client.Certificate.DownloadCertificateAsync(account, promise, csr, CertificateType.Cert);

            //5. Save the certificate
            using (FileStream fs = new FileStream(@"c:\temp\mycert.cer", FileMode.Create))
            {
                byte[] buffer = cert.Array;
                fs.Write(buffer, 0, buffer.Length);
            }
        }
Ejemplo n.º 3
0
        public ActionResult ChallengeablePlayers_Read([DataSourceRequest]DataSourceRequest request, int ladderId)
        {
            // for loading all players that are challengeable within a ladder

            // get user id
            string userID = User.Identity.GetUserId();
            int id = System.Convert.ToInt32(userID);
            Users.User user = Users.User.Load(id);

            bool pendingChallenge = false;

            Ladder ladder = Ladder.LoadById(ladderId);
            // load all ladder players and sort by rank
            IEnumerable<LadderPlayer> ladPlayers = LadderPlayerCollection.LoadByLadder(ladder).OrderBy(lp => lp.PlayerRank);

            LadderPlayer defLadderPlayer = new LadderPlayer()
            {
                PlayerId = -1
            };

            LadderPlayer isPlayerInLadder = LadderPlayerCollection.LoadByLadder(ladder).Where(p => p.PlayerId == user.Id).DefaultIfEmpty(defLadderPlayer).First();

            Dictionary<LadderPlayer, bool> challengeablePlayers = new Dictionary<LadderPlayer, bool>();
            // if player is in the ladder
            if (isPlayerInLadder != defLadderPlayer)
            {

                LadderPlayer userPlayer = LadderPlayerCollection.LoadAll().Where(p => p.PlayerId == user.Id).First();

                IEnumerable<Challenge> allChallenges = ChallengeCollection.LoadByLadder(ladder);
                IEnumerable<ChallengePlayer> ladderChallengePlayers = ChallengePlayerCollection.LoadAll().Where(p => p.Challenge.LadderId == ladder.Id);
                ChallengeCollection userChallenges = new ChallengeCollection();
                ChallengePlayerCollection pendingOpponents = new ChallengePlayerCollection();
                // every challenge in the ladder
                foreach (Challenge challenge in allChallenges)
                {

                    foreach (ChallengePlayer player in ladderChallengePlayers)
                    {
                        if (challenge.Id == player.ChallengeId)
                        {
                            if (challenge.Status.Status == "pending" || challenge.Status.Status == "scheduled")
                            {
                                if (player.Player.Id != user.Id)
                                {
                                    pendingOpponents.Add(player);
                                }
                            }
                        }
                    }

                }
                // if pending challenges is greater than ladder's MaxPendingChallenges
                if (pendingOpponents.Count() < ladder.MaxPendingChallenges)
                {

                    foreach (LadderPlayer ladderOpponent in ladPlayers)
                    {

                        if (userPlayer.PlayerId != ladderOpponent.PlayerId)
                        {

                            if ((ladderOpponent.PlayerRank - userPlayer.PlayerRank) <= ladder.LowerChallengeLimit)
                            {

                                if ((userPlayer.PlayerRank - ladderOpponent.PlayerRank) <= ladder.UpperChallengeLimit)
                                {
                                    // all players currently in the DB are past MaxInactiveDays.. Comment this out to test.
                                    //if ((DateTime.Now - ladderOpponent.Player.LastActivePlayerDate).TotalDays < ladder.MaxInactiveDays)

                                    foreach (ChallengePlayer opponent in pendingOpponents)
                                    {
                                        if (ladderOpponent.PlayerId == opponent.PlayerId)
                                        {
                                            pendingChallenge = true;
                                        }
                                    }
                                    if (!pendingChallenge)
                                    {
                                        challengeablePlayers.Add(ladderOpponent, true);

                                    }
                                    pendingChallenge = false;
                                }

                            }

                        }

                    }

                }

            }

            DataSourceResult result = challengeablePlayers.ToDataSourceResult(request, challengeablePlayer => new LadderPlayersViewModel
            {
                LadderPlayerId = challengeablePlayer.Key.Id,
                PlayerName = challengeablePlayer.Key.Player.FirstName + " " + challengeablePlayer.Key.Player.LastName,
                PlayerPoints = challengeablePlayer.Key.PlayerPoints,
                PlayerRank = challengeablePlayer.Key.PlayerRank,
                ChallengesWon = challengeablePlayer.Key.ChallengesWon,
                ChallengesLost = challengeablePlayer.Key.ChallengesLost,
                DateJoined = challengeablePlayer.Key.DateAdded.ToString("dd MMMM, yyyy")

            });
            return Json(result, JsonRequestBehavior.AllowGet);
        }
Ejemplo n.º 4
0
        public ActionResult ScheduledChallenges_Read([DataSourceRequest]DataSourceRequest request)
        {
            /* To get the challenges that the current user is participating in,
             * first find all ChallengePlayer objects associated with that user.
             * Next, loop through the ChallengePlayer objects to get the challenges
             * associated with them.
             */

            //get the current user that is logged in
            string userID = User.Identity.GetUserId();
            int id = System.Convert.ToInt32(userID);
            Users.User user = Users.User.Load(id);

            // get all ChallengePlayer objects for current user
            ChallengePlayerCollection challengePlayers = ChallengePlayerCollection.LoadByPlayer(user);

            // get all challenges associated with each challengePlayer object
            ChallengeCollection playerChallenges = new ChallengeCollection();
            foreach (ChallengePlayer cp in challengePlayers)
            {
                if (cp.Challenge.Status.Status == "scheduled") //scheduled
                {
                    playerChallenges.Add(cp.Challenge);
                }

            }

            DataSourceResult result = playerChallenges.ToDataSourceResult(request, challenge => new ScheduledChallengesViewModel
            {
                ChallengeId = challenge.Id,
                PlayerName = user.FirstName + " " + user.LastName,

                // Only accounting for Singles matches right now. No way to connect pairs for doubles yet
                OpponentName = ChallengePlayerCollection.LoadByChallenge(challenge).Where(cp => cp.PlayerId != user.Id).First().Player.FirstName + " " +
                               ChallengePlayerCollection.LoadByChallenge(challenge).Where(cp => cp.PlayerId != user.Id).First().Player.LastName,

                LadderName = challenge.Ladder.Name,
                FacilityName = challenge.Facility.FullName,
                CourtName = challenge.Court.Name,
                MatchType = challenge.MatchType.Type,
                MatchFormat = challenge.MatchFormat.Format,
                numSets = challenge.MatchFormat.NumSets,
                MatchDateTime = challenge.MatchDateTime.ToString("dd MMMM yyyy, HH:mm")

            });

            return Json(result);
        }