Example #1
0
        public async Task <IActionResult> Login()
        {
            var form = await HttpContext.Request.ReadFormAsync();

            var username        = form["username"];
            var password        = form["password"];
            var code            = form["code"];
            var hashAlgorithm   = (HashAlgorithmType)Enum.Parse(typeof(HashAlgorithmType), form["hashAlgorithm"]);
            var numberOfDigits  = Convert.ToByte(form["numberOfDigits"]);
            var periodInSeconds = Convert.ToByte(form["periodInSeconds"]);
            var secret          = Encoding.UTF8.GetBytes(form["secret"]);

            var user = new AuthenticatorUser <int> {
                UserName = username
            };
            await _userManager.CreateAsync(user, password);

            var result = await _userManager.EnableAuthenticatorAsync(user, new Authenticator
                                                                     { HashAlgorithm = hashAlgorithm, NumberOfDigits = numberOfDigits, PeriodInSeconds = periodInSeconds, Secret = secret }, code);

            if (result)
            {
                return(Content("OK"));
            }
            else
            {
                return(Content("BADCODE"));
            }
        }
        public async Task NullAuthenticatorParamsrSetAuthenticatorParamsAsync()
        {
            var user                   = new AuthenticatorUser <string>();
            var dbContrext             = new Mock <DbContext>(MockBehavior.Strict);
            var authenticatorUserStore = new AuthenticatorUserStore(dbContrext.Object);

            await Assert.ThrowsAsync <ArgumentNullException>(() => authenticatorUserStore.SetAuthenticatorParamsAsync(user, null));
        }
        public void SuccessUsernameContructor()
        {
            var userName          = Guid.NewGuid().ToString();
            var authenticatorUser = new AuthenticatorUser(userName);

            Assert.NotEqual(string.Empty, authenticatorUser.Id);
            Assert.Equal(userName, authenticatorUser.UserName);
        }
        public async Task NullSecretGetAuthenticatorParamsAsync(HashAlgorithmType hashAlgorithm, byte numberOfDigits, byte periodInSeconds)
        {
            var user = new AuthenticatorUser <string>();

            user.AuthenticatorHashAlgorithm   = hashAlgorithm;
            user.AuthenticatorNumberOfDigits  = numberOfDigits;
            user.AuthenticatorPeriodInSeconds = periodInSeconds;
            var dbContrext             = new Mock <DbContext>(MockBehavior.Strict);
            var authenticatorUserStore = new AuthenticatorUserStore(dbContrext.Object);

            var authenticatorParams = await authenticatorUserStore.GetAuthenticatorParamsAsync(user);

            Assert.Equal(user.AuthenticatorHashAlgorithm, authenticatorParams.HashAlgorithm);
            Assert.Equal(user.AuthenticatorNumberOfDigits, authenticatorParams.NumberOfDigits);
            Assert.Equal(user.AuthenticatorPeriodInSeconds, authenticatorParams.PeriodInSeconds);
            Assert.Null(authenticatorParams.Secret);
        }
        public async Task GetAuthenticatorParamsAsync(HashAlgorithmType hashAlgorithm, byte numberOfDigits, byte periodInSeconds)
        {
            var secret = Guid.NewGuid().ToString();
            var user   = new AuthenticatorUser <string>();

            user.AuthenticatorHashAlgorithm   = hashAlgorithm;
            user.AuthenticatorNumberOfDigits  = numberOfDigits;
            user.AuthenticatorPeriodInSeconds = periodInSeconds;
            user.AuthenticatorSecretEncrypted = Convert.ToBase64String(Encoding.UTF8.GetBytes(secret));
            var dbContrext             = new Mock <DbContext>(MockBehavior.Strict);
            var authenticatorUserStore = new AuthenticatorUserStore(dbContrext.Object);

            var authenticatorParams = await authenticatorUserStore.GetAuthenticatorParamsAsync(user);

            Assert.Equal(user.AuthenticatorHashAlgorithm, authenticatorParams.HashAlgorithm);
            Assert.Equal(user.AuthenticatorNumberOfDigits, authenticatorParams.NumberOfDigits);
            Assert.Equal(user.AuthenticatorPeriodInSeconds, authenticatorParams.PeriodInSeconds);
            Assert.Equal(Encoding.UTF8.GetBytes(secret), authenticatorParams.Secret);
        }
        public void SuccessContructor()
        {
            var authenticatorUser = new AuthenticatorUser();

            Assert.NotEqual(string.Empty, authenticatorUser.Id);
        }