Ejemplo n.º 1
0
        public async Task <ResponseModelBase> GetUserInfo(Guid userId)
        {
            var usr = await ldb.FindByUniqueId(userId);

            if (usr == null)
            {
                return(ErrorModel.Of("user_not_found"));
            }

            return(OkModel.Of(new UserInfoResponseModel(usr)));
        }
        public async Task <ResponseModelBase> ConfirmGiftProduct(Guid userId, Guid confirmCode, string productKey, string addressToGiftTo)
        {
            var user = await ldb.FindByUniqueId(userId);

            var userToGiveTo = await ldb.FindByEmailAddress(addressToGiftTo);

            if (user == null || userToGiveTo == null)
            {
                return(ErrorModel.Of("user_not_found"));
            }

            if (user.UniqueConfirmationCode != confirmCode)
            {
                return(ErrorModel.Of("email_confirmation_code_incorrect"));
            }

            var productObject = user.OwnedProducts.Where(a => a.ProductKey == productKey).FirstOrDefault();

            if (productObject == null)
            {
                return(ErrorModel.Of("product_key_not_found"));
            }

            //Transfer it
            user.OwnedProducts.Remove(productObject);
            userToGiveTo.OwnedProducts.Add(productObject);

            //Transform it into a gift object
            //productObject.EditionId = new Guid("e6c94547-355b-4d39-a5f0-513a3cc3b807");

            user.UniqueConfirmationCode = Guid.NewGuid();
            await ldb.UpdateUser(user);

            await ldb.UpdateUser(userToGiveTo);

            //And send the email
            await Backend.EmailSender.SendEmail(userToGiveTo, Backend.EmailSender.ProductKeyGiftedTemplate,
                                                new Dictionary <string, string>()
            {
                { "-giftingUser-", user.Username },
                { "-displayName-", productObject.DisplayName },
                { "-productName-", productObject.ProductName },
                { "-productId-", productObject.ProductId.ToString() },
                { "-editionName-", productObject.EditionName },
                { "-editionId-", productObject.EditionId.ToString() },
                { "-productKey-", productObject.ProductKey },
                { "-downloadUrl-", (await Backend.ProductDatabase.GetProduct(
                                        productObject.ProductId, productObject.EditionId)).Product.DownloadUrl },
                { "-redemptionDate-", DateTime.UtcNow.ToString("G") }
            });

            return(OkModel.Of("product_key_gifted"));
        }
Ejemplo n.º 3
0
        public async Task <ResponseModelBase <bool> > ValidateSessionKey([FromBody] AuthenticatedRequestModel model)
        {
            if (!ModelState.IsValid)
            {
                return(ErrorModel.Of(false, "invalid_request"));
            }

            var session = await ldb.FindBySessionKey(model.SessionKey);

            if (session == null)
            {
                return(OkModel.Of(false));
            }
            //and tell the client that the session key is true
            return(OkModel.Of(true));
        }
        public async Task <ResponseModelBase> RequestGiftProduct([FromBody] GiftProductRequestModel model)
        {
            if (!ModelState.IsValid)
            {
                return(ErrorModel.Of("invalid_request"));
            }
            var user = await ldb.FindBySessionKey(model.SessionKey);

            if (user == null)
            {
                return(ErrorModel.Of("not_logged_in"));
            }

            var otherUser = await ldb.FindByEmailAddress(model.EmailAddressToGiftTo);

            if (otherUser == null)
            {
                return(ErrorModel.Of("user_not_found"));
            }

            var productObject = user.OwnedProducts.Where(a => a.ProductKey == model.ProductKeyToGift).FirstOrDefault();

            if (productObject == null)
            {
                return(ErrorModel.Of("product_key_not_found"));
            }

            await Backend.EmailSender.SendEmail(user, Backend.EmailSender.ProductKeyGiftRequestedTemplate,
                                                new Dictionary <string, string>()
            {
                { "-displayName-", productObject.DisplayName },
                { "-productName-", productObject.ProductName },
                { "-productId-", productObject.ProductId.ToString() },
                { "-editionName-", productObject.EditionName },
                { "-editionId-", productObject.EditionId.ToString() },
                { "-productKey-", productObject.ProductKey },
                { "-downloadUrl-", (await Backend.ProductDatabase.GetProduct(
                                        productObject.ProductId, productObject.EditionId)).Product.DownloadUrl },
                { "-redemptionDate-", DateTime.UtcNow.ToString("G") },
                { "-giftEmailAddress-", model.EmailAddressToGiftTo },
                { "-giftUsername-", otherUser.Username }
            });

            return(OkModel.Of("product_key_gift_validation_sent"));
        }
Ejemplo n.º 5
0
        public async Task <ResponseModelBase> ChangeUsername([FromBody] ChangeUsernameRequestModel model)
        {
            if (!ModelState.IsValid)
            {
                return(ErrorModel.Of("invalid_request"));
            }

            if (!await ldb.ValidateAccount(model.EmailAddress, model.Password))
            {
                return(ErrorModel.Of("username_or_password_incorrect"));
            }

            if (model.NewUsername.Length < 5)
            {
                return(ErrorModel.Of("username_too_short"));
            }

            //Change their username
            var user = await ldb.FindByEmailAddress(model.EmailAddress);

            if (user == null)
            {
                return(ErrorModel.Of("user_not_found"));
            }

            //Check not in use
            var other = await ldb.FindByUsername(model.NewUsername);

            if (other != null)
            {
                return(ErrorModel.Of("username_in_use"));
            }

            user.Username = model.NewUsername;
            //Update
            await ldb.UpdateUser(user);

            return(OkModel.Of("username_changed"));
        }
Ejemplo n.º 6
0
        public async Task <ResponseModelBase> ChangePassword([FromBody] ChangePasswordRequestModel model)
        {
            if (!ModelState.IsValid)
            {
                return(ErrorModel.Of("invalid_request"));
            }

            if (!await ldb.ValidateAccount(model.EmailAddress, model.OldPassword))
            {
                return(ErrorModel.Of("username_or_password_incorrect"));
            }

            if (model.NewPassword.Length < 8)
            {
                return(ErrorModel.Of("password_too_short"));
            }

            //Change their password
            var user = await ldb.FindByEmailAddress(model.EmailAddress);

            if (user == null)
            {
                return(ErrorModel.Of("user_not_found"));
            }

            user.PasswordHashes = await Task.Run(() => PasswordHasher.GenerateHashPermutations(model.NewPassword));

            //Clear all sessions
            ldb.DBContext.Sessions.RemoveRange(user.ActiveSessions);
            user.ActiveSessions.Clear();
            //And login tokens
            ldb.DBContext.ServerTokens.RemoveRange(user.ActiveServerTokens);
            user.ActiveServerTokens.Clear();
            //Update
            await ldb.UpdateUser(user);

            return(OkModel.Of("password_changed"));
        }
Ejemplo n.º 7
0
        public async Task <ResponseModelBase <UserServerTokenResponseModel> > CreateServerToken([FromBody] AuthenticatedRequestModel model)
        {
            if (!ModelState.IsValid)
            {
                return(ErrorModel.Of <UserServerTokenResponseModel>(null, "invalid_request"));
            }

            var session = await ldb.GetSessionFromKey(model.SessionKey);

            if (session == null)
            {
                return(ErrorModel.Of <UserServerTokenResponseModel>(null, "not_logged_in")); //Auth failed
            }
            var token = new UserServerTokenModel();

            token.ExpiryDate  = DateTime.UtcNow + TimeSpan.FromMinutes(2);
            token.ServerToken = Guid.NewGuid().ToString("N");
            session.Owner.AddToken(token);

            await ldb.UpdateUser(session.Owner);

            return(OkModel.Of(new UserServerTokenResponseModel(token)));
        }
        public async Task <ResponseModelBase> RedeemProduct([FromBody] RedeemProductRequestModel model)
        {
            if (!ModelState.IsValid)
            {
                return(ErrorModel.Of("invalid_request"));
            }
            var user = await ldb.FindBySessionKey(model.SessionKey);

            if (user == null)
            {
                return(ErrorModel.Of("not_logged_in"));
            }

            var tableStorageString = Startup.Configuration["Data:TableStorageConnectionString"];
            //update
            var account = CloudStorageAccount.Parse(tableStorageString);
            var client  = account.CreateCloudTableClient();
            var table   = client.GetTableReference("ZSBProductKeys");

            var partitionKey = model.ProductKey.Substring(0, 4);
            var key          = model.ProductKey.ToUpper().Replace("-", "");
            var result       = await table.ExecuteAsync(
                TableOperation.Retrieve <ProductKeyStorageModel>(partitionKey, key));

            if (result.HttpStatusCode != 200) //error
            {
                return(Models.ErrorModel.Of("product_key_not_found"));
            }

            //Do stuff (update the db)
            var obj = (ProductKeyStorageModel)result.Result;

            if (obj.HasBeenRedeemed)
            {
                return(ErrorModel.Of("product_key_already_redeemed"));
            }

            obj.HasBeenRedeemed             = true;
            obj.RedemptionDate              = DateTime.UtcNow;
            obj.RedeemerAccountId           = user.UniqueId;
            obj.RedeemerAccountEmailAddress = user.EmailAddress;

            //and update the user
            user.OwnedProducts.Add(new UserOwnedProductModel
            {
                EditionId      = obj.EditionId,
                ProductId      = obj.ProductId,
                ProductKey     = obj.Key,
                RedemptionDate = DateTime.UtcNow
            });

            await table.ExecuteAsync(TableOperation.Merge(obj));

            await ldb.UpdateUser(user);

            await Backend.EmailSender.SendEmail(user, Backend.EmailSender.ProductKeyRedeemedTemplate,
                                                new Dictionary <string, string>()
            {
                { "-displayName-", obj.DisplayName },
                { "-productName-", obj.ProductName },
                { "-productId-", obj.ProductId.ToString() },
                { "-editionName-", obj.EditionName },
                { "-editionId-", obj.EditionId.ToString() },
                { "-productKey-", obj.Key },
                { "-downloadUrl-", (await Backend.ProductDatabase.GetProduct(obj.ProductId, obj.EditionId)).Product.DownloadUrl },
                { "-redemptionDate-", DateTime.UtcNow.ToString("G") }
            });

            return(OkModel.Of("product_key_redeemed"));
        }
Ejemplo n.º 9
0
 public ResponseModelBase GetValidationTest()
 {
     return(OkModel.Of(AccountTests.GetRandomQuestion()));
 }