Example #1
0
        public virtual JsonResult DomainIdentification(string domainAddress, string subFolder)
        {
            string from = "APILOGINFO - " + HttpContext.Request.UserHostAddress;
            InsideWordWebLog.Instance.Buffer(from, "DomainIdentification(" + domainAddress + ", " + subFolder + ")");
            ApiMsgVM returnMessage = new ApiMsgVM((int)ApiMsgVM.StatusEnum.failure);

            string subFolderDecoded = null;
            IWStringUtility.TryUrlDecode(subFolder, out subFolderDecoded, "");

            Uri domainUri = null;
            Uri pathUri = null;
            if (!IWStringUtility.TryUrlDecode(domainAddress, out domainAddress) ||
                !Uri.TryCreate(domainAddress, UriKind.Absolute, out domainUri))
            {
                returnMessage.StatusCode = (int)ApiMsgVM.StatusEnum.failure;
                returnMessage.StatusMessage = domainAddress + " is an invalid uri";
            }
            else if (!IWStringUtility.TryUriConcat(domainUri, subFolderDecoded, out pathUri))
            {
                returnMessage.StatusCode = (int)ApiMsgVM.StatusEnum.failure;
                returnMessage.StatusMessage = domainUri.AbsoluteUri + " and " + subFolder + " form an invalid uri";
            }
            else
            {
                ProviderDomain aDomain = new ProviderDomain();
                ProviderIssuedKey issuedKey = new ProviderIssuedKey();
                ProviderMember aMember = new ProviderMember();

                if (!aDomain.Load(domainUri.AbsoluteUri))
                {
                    returnMessage.StatusCode = (int)ApiMsgVM.StatusEnum.failure;
                    returnMessage.StatusMessage = domainUri.AbsoluteUri
                                                +" does not exist in our system. Use "
                                                +Url.Action(MVC.API.DomainIdentificationRequest())
                                                +" to request a key and identify yourself first.";
                }
                else if(!aMember.Load(aDomain.MemberId))
                {
                    returnMessage.StatusCode = (int)ApiMsgVM.StatusEnum.failure;
                    returnMessage.StatusMessage = "The member associated with this domain, "
                                                +domainUri.AbsoluteUri
                                                +", does not exist. Contact support to resolve this issue.";
                }
                else if (!issuedKey.LoadBy(aMember.Id.Value, domainUri.AbsoluteUri, true, 1))
                {
                    returnMessage.StatusCode = (int)ApiMsgVM.StatusEnum.failure;
                    returnMessage.StatusMessage = "Your issued key has been used up already or was never issued. Use "
                                                + Url.Action(MVC.API.DomainIdentificationRequest())
                                                + " to request a new key for identification.";
                }
                else
                {
                    // all the data is good and we're ready to check if the key has been placed in the correct uri.
                    bool isFetchSuccess = false;
                    string htmlPage = null;
                    HtmlDocument htmlDoc = new HtmlDocument();
                    try
                    {
                        using (WebClient client = new WebClient())
                        {
                            // TODO: DOS attack is possible here by sending us to a page with a gig of data.
                            // put some sort of precautionary check here to avoid loading too much data.
                            htmlPage = client.DownloadString(pathUri.AbsoluteUri);
                        }
                        htmlDoc.LoadHtml(htmlPage);
                        isFetchSuccess = true;
                    }
                    catch (Exception caughtException)
                    {
                        returnMessage.StatusCode = (int)ApiMsgVM.StatusEnum.failure;
                        returnMessage.StatusMessage = "Failed to read the webpage at " + pathUri.AbsoluteUri;
                        isFetchSuccess = false;
                    }

                    if (isFetchSuccess)
                    {
                        HtmlNode embeddedIssuedKey = htmlDoc.GetElementbyId(issuedKey.IssuedKey);
                        if (embeddedIssuedKey == null ||
                            embeddedIssuedKey.Name.CompareTo("input") != 0)
                        {
                            returnMessage.StatusCode = (int)ApiMsgVM.StatusEnum.failure;
                            returnMessage.StatusMessage = "Could not find hidden input tag with id containing the issued key at page " + pathUri.AbsoluteUri;
                        }
                        else
                        {
                            //we found it so let's validate the domain and return the issued keys
                            aDomain.IsValidated = true;
                            aDomain.EditDate = DateTime.UtcNow;
                            aDomain.Save();

                            returnMessage.StatusCode = (int)ApiMsgVM.StatusEnum.success;
                            returnMessage.StatusMessage = "You have been successfully validated. Here are the issued keys for this month and next months. Do not share these with anyone.";
                            returnMessage.Content = aMember.CurrentMonthIssuedKey.IssuedKey + "," + aMember.NextMonthIssuedKey.IssuedKey;

                            // decommission the issued key
                            issuedKey.TryDecommission();
                        }
                    }
                }
            }
            InsideWordWebLog.Instance.Buffer(from, "Done DomainIdentification - " + returnMessage);
            return Json(returnMessage);
        }
Example #2
0
        public virtual JsonResult DomainIdentificationRequest(string domainAddress)
        {
            string from = "APILOGINFO - " + HttpContext.Request.UserHostAddress;
            InsideWordWebLog.Instance.Buffer(from, "DomainIdentificationRequest(" + domainAddress + ")");
            ApiMsgVM returnMessage = new ApiMsgVM(1);

            Uri domainUri = null;

            if (!IWStringUtility.TryUrlDecode(domainAddress, out domainAddress) ||
                !Uri.TryCreate(domainAddress, UriKind.Absolute, out domainUri))
            {
                returnMessage.StatusCode = (int)ApiMsgVM.StatusEnum.failure;
                returnMessage.StatusMessage = domainAddress + " is an invalid uri";
            }
            else
            {
                ProviderDomain aDomain = new ProviderDomain();
                ProviderIssuedKey issuedKey = new ProviderIssuedKey();
                ProviderMember aMember = new ProviderMember();

                if (aDomain.Load(domainUri.AbsoluteUri))
                {
                    aMember.Load(aDomain.MemberId);
                }
                else
                {
                    // Domain doesn't exist already so create it and a member
                    aMember.CreateDate = DateTime.UtcNow;
                    aMember.EditDate = DateTime.UtcNow;
                    aMember.Save();

                    aDomain.CreateDate = DateTime.UtcNow;
                    aDomain.EditDate = DateTime.UtcNow;
                    aDomain.Domain = domainUri;
                    aDomain.IsValidated = false;
                    aDomain.MemberId = aMember.Id.Value;
                    aDomain.Save();
                }

                issuedKey.LoadOrCreate(aMember.Id.Value, domainUri.AbsoluteUri, true, 1, false);

                returnMessage.StatusCode = (int)ApiMsgVM.StatusEnum.success;
                returnMessage.StatusMessage = "Success";
                returnMessage.Content = issuedKey.IssuedKey;
            }
            InsideWordWebLog.Instance.Buffer(from, "Done DomainIdentificationRequest - " + returnMessage);
            return Json(returnMessage);
        }
        /*
        public override bool Copy(Provider untyped)
        {
            //Never copy over the id, otherwise we would be creating
            //a pseudo-reference copy, which we don't want.
            //Do not copy over the system times and only the business logic
            //times since the system times are specific to a given instance.
            ProviderAlternateMemberId anAlternateMemberId = (ProviderAlternateMemberId)untyped;
            _entityAlternateMemberId.MemberId = anAlternateMemberId._entityAlternateMemberId.MemberId;
            _entityAlternateMemberId.AlternateType = anAlternateMemberId._entityAlternateMemberId.AlternateType;
            _entityAlternateMemberId.AlternateId = anAlternateMemberId._entityAlternateMemberId.AlternateId;
            _entityAlternateMemberId.IsValidated = anAlternateMemberId._entityAlternateMemberId.IsValidated;
            _entityAlternateMemberId.IsNonce = anAlternateMemberId._entityAlternateMemberId.IsNonce;
            _entityAlternateMemberId.ExpiryDate = anAlternateMemberId._entityAlternateMemberId.ExpiryDate;
            _entityAlternateMemberId.EditDate = anAlternateMemberId._entityAlternateMemberId.EditDate;
            _entityAlternateMemberId.CreateDate = anAlternateMemberId._entityAlternateMemberId.CreateDate;
            _entityAlternateMemberId.UsePassword = anAlternateMemberId._entityAlternateMemberId.UsePassword;
            _entityAlternateMemberId.IsHidden = anAlternateMemberId._entityAlternateMemberId.IsHidden;
            _entityObject = _entityAlternateMemberId;
            return true;
        }
        */
        public bool ValidateData()
        {
            bool returnValue = true;
            if (!string.IsNullOrWhiteSpace(Data))
            {
                MailAddress email = null;

                // if this is an alt id used to validate an e-mail then do so.
                if (IWStringUtility.TryParse(Data, out email))
                {
                    ProviderEmail altIdEmail = new ProviderEmail();
                    if (altIdEmail.Load(email.Address))
                    {
                        altIdEmail.IsValidated = true;
                        altIdEmail.Save();
                    }
                }
                else
                {
                    Uri domain = null;

                    // if this is an alt id used to validate an a domain then do so.
                    if (Uri.TryCreate(Data, UriKind.Absolute, out domain))
                    {
                        ProviderDomain altIdDomain = new ProviderDomain();
                        if (altIdDomain.Load(domain.AbsoluteUri))
                        {
                            altIdDomain.IsValidated = true;
                            altIdDomain.Save();
                        }
                    }
                }
            }
            return returnValue;
        }