Exemplo n.º 1
0
        /// <summary>
        /// Verifies the users short code.
        /// </summary>
        /// <param name="manager">The manager.</param>
        /// <param name="codeType">Type of the code.</param>
        /// <param name="userid">The userid.</param>
        /// <param name="code">The code.</param>
        public async Task <bool> VerifyUserTwoFactorCodeAsync(TwoFactorComponentType component, string userid, string data)
        {
            var user = await FindByIdAsync(userid);

            if (user == null)
            {
                return(false);
            }

            var twofactorMethod = user.TwoFactor.FirstOrDefault(x => x.Component == component);

            if (twofactorMethod == null || twofactorMethod.Type == TwoFactorType.None)
            {
                return(true);
            }

            if (twofactorMethod.Type == TwoFactorType.PinCode)
            {
                return(twofactorMethod.Data == data);
            }

            if (twofactorMethod.Type == TwoFactorType.EmailCode)
            {
                return(await VerifyTwoFactorTokenAsync(userid, twofactorMethod.Type.ToString(), data));
            }

            if (twofactorMethod.Type == TwoFactorType.GoogleCode)
            {
                return(GoogleHelper.VerifyGoogleTwoFactorCode(twofactorMethod.Data, data));
            }

            return(false);
        }
Exemplo n.º 2
0
        public async Task <ActionResult> Remove(TwoFactorComponentType componentType)
        {
            var user = UserManager.FindById(User.Id());

            if (user == null)
            {
                return(Unauthorized());
            }

            var twofactor = user.TwoFactor.FirstOrDefault(x => x.Component == componentType) ?? new UserTwoFactor {
                Type = TwoFactorType.None
            };

            if (twofactor.Type == TwoFactorType.EmailCode)
            {
                var twofactorCode = await UserManager.GenerateUserTwoFactorCodeAsync(TwoFactorType.EmailCode, user.Id);

                if (!await EmailService.Send(EmailType.TwoFactorUnlockCode, user, Request.GetIPAddress(), new EmailParam("[TFACODE]", twofactorCode), new EmailParam("[TFATYPE]", componentType)))
                {
                    return(ViewMessage(new ViewMessageModel(ViewMessageType.Warning, "Email Send Failed!", "An error occured sending twofactor code email, If problems persists please contact <a href='/Support'>Support</a>")));
                }
            }

            return(View("Remove", new RemoveTwoFactorModel
            {
                Type = twofactor.Type,
                ComponentType = componentType,
            }));
        }
Exemplo n.º 3
0
        public async Task <ActionResult> SendTwoFactorEmailCode(TwoFactorComponentType componentType)
        {
            var twoFactor = await UserManager.GetUserTwoFactorAsync(User.Id(), componentType);

            if (twoFactor == null || twoFactor.Type != TwoFactorType.EmailCode)
            {
                return(JsonError("Unauthorized"));
            }

            var twofactorCode = await UserManager.GenerateUserTwoFactorCodeAsync(TwoFactorType.EmailCode, User.Id());

            switch (componentType)
            {
            case TwoFactorComponentType.Login:
                if (await EmailService.Send(EmailType.TwoFactorLogin, twoFactor.User, Request.GetIPAddress(), new EmailParam("[TFACODE]", twofactorCode)))
                {
                    return(JsonSuccess());
                }
                break;

            case TwoFactorComponentType.Withdraw:
                if (await EmailService.Send(EmailType.TwoFactorWithdraw, twoFactor.User, Request.GetIPAddress(), new EmailParam("[TFACODE]", twofactorCode)))
                {
                    return(JsonSuccess());
                }
                break;

            default:
                break;
            }
            return(JsonError());
        }
Exemplo n.º 4
0
        public async Task <UserTwoFactor> GetUserTwoFactorAsync(string userId, TwoFactorComponentType component)
        {
            var user = await FindByIdAsync(userId);

            if (user == null)
            {
                throw new UnauthorizedAccessException();
            }

            return(user.TwoFactor.FirstOrDefault(x => x.Component == component));
        }
Exemplo n.º 5
0
        public async Task <ActionResult> VerifyEmailCode(TwoFactorComponentType componentType, string code)
        {
            var user = UserManager.FindById(User.Id());

            if (user == null)
            {
                return(JsonError("Unauthorized"));
            }

            if (await UserManager.VerifyTwoFactorTokenAsync(user.Id, TwoFactorType.EmailCode.ToString(), code))
            {
                return(JsonSuccess());
            }
            return(JsonError());
        }
Exemplo n.º 6
0
        public async Task <TwoFactorType> GetUserTwoFactorTypeAsync(string userId, TwoFactorComponentType component)
        {
            var user = await FindByIdAsync(userId);

            if (user == null)
            {
                throw new UnauthorizedAccessException();
            }

            var twofactorMethod = user.TwoFactor.FirstOrDefault(x => x.Component == component);

            return(twofactorMethod == null
                                ? TwoFactorType.None
                                : twofactorMethod.Type);
        }
Exemplo n.º 7
0
        public ActionResult GetTwoFactor(TwoFactorComponentType component)
        {
            var user = UserManager.FindById(User.Id());

            if (user == null)
            {
                return(Unauthorized());
            }

            var twoFactor = user.TwoFactor.FirstOrDefault(x => x.Component == component)
                            ?? new UserTwoFactor {
                Type = TwoFactorType.None
            };

            return(PartialView("_ViewPartial", new ViewTwoFactorModel
            {
                Type = twoFactor.Type,
                ComponentType = component
            }));
        }
Exemplo n.º 8
0
        public async Task <ActionResult> SendEmailCode(TwoFactorComponentType componentType, string dataEmail)
        {
            var user = UserManager.FindById(User.Id());

            if (user == null)
            {
                return(JsonError("Unauthorized"));
            }

            if (!ValidationHelpers.IsValidEmailAddress(dataEmail))
            {
                return(JsonError($"'{dataEmail}' is an invalid email address."));
            }

            var twofactorCode = await UserManager.GenerateUserTwoFactorCodeAsync(TwoFactorType.EmailCode, User.Id());

            if (await EmailService.Send(EmailType.TwoFactorUnlockCode, user, Request.GetIPAddress(), new EmailParam("[TFACODE]", twofactorCode), new EmailParam("[TFATYPE]", componentType)))
            {
                return(JsonSuccess());
            }
            return(JsonError());
        }
Exemplo n.º 9
0
        public ActionResult Create(TwoFactorComponentType componentType)
        {
            var user = UserManager.FindById(User.Id());

            if (user == null)
            {
                return(Unauthorized());
            }

            // If twofactor exists something is dodgy, return unauthorised
            var twofactor = user.TwoFactor.FirstOrDefault(x => x.Component == componentType && x.Type != TwoFactorType.None);

            if (twofactor != null)
            {
                return(RedirectToRoute("Security"));
            }

            return(View(new CreateTwoFactorModel
            {
                ComponentType = componentType,
                GoogleData = GoogleHelper.GetGoogleTwoFactorData(user.UserName)
            }));
        }