private void TestSHA256AndAssert(Key key, int digits, DateTime time, string expected)
        {
            var otp    = new TimeBasedOtpGenerator(key, digits, new SHA256HMACAlgorithm());
            var result = otp.GenerateOtp(time);

            Assert.AreEqual(expected, result);
        }
Example #2
0
        public string GenerateOtpCodeForServerAccount(AccountModel model)
        {
            var serverAccount = model.ServerAccountSettings.Query().First();
            var otpAccount    = model.OtpAccounts.Query().First(r => r.Id == serverAccount.OtpAccountId);

            var totpcode = new TimeBasedOtpGenerator(
                new Key(otpAccount.Secret),
                otpAccount.Digits,
                new SHA1HMACAlgorithm());

            return(totpcode.GenerateOtp(DateTime.UtcNow));
        }
Example #3
0
        public string GenerateOtpCode(int serverAccountId, string accountName)
        {
            var model = AccountModel.GetModel(serverAccountId);

            var otpAccount = model.OtpAccounts.Query().FirstOrDefault(r => r.Label == accountName);

            if (otpAccount == null)
            {
                throw new Exception("Could not find OTP account for given name");
            }

            var totpcode = new TimeBasedOtpGenerator(
                new Key(otpAccount.Secret),
                otpAccount.Digits,
                new SHA1HMACAlgorithm());

            return(totpcode.GenerateOtp(DateTime.UtcNow));
        }
Example #4
0
        private string GetOtpWithImplicitHMAC(Key key, int digits, DateTime time)
        {
            var otp = new TimeBasedOtpGenerator(key, digits);

            return(otp.GenerateOtp(time));
        }
        public async Task <bool> CheckOneTimePassword([FromBody] OtpCodeDto otpCodeDto)
        {
            var secretkey = string.Empty;
            var email     = string.Empty;
            var userId    = string.Empty;


            if (otpCodeDto.ChannelId == "sms")
            {
                var smsUser = this._context.SmsUser.Where(smsuser => smsuser.UserName == otpCodeDto.UserName).FirstOrDefault();

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

                secretkey = smsUser.SecretKey;
                email     = smsUser.EMail;
                userId    = smsUser.UserId;
            }

            if (otpCodeDto.ChannelId == "directline")
            {
                var directLineUser = this._context.DirectLineUser.Where(smsuser => smsuser.UserName == otpCodeDto.UserName).FirstOrDefault();

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

                secretkey = directLineUser.SecretKey;
                email     = directLineUser.EMail;
                userId    = directLineUser.UserId;
            }

            int otpDigits = 6;

            var secretKey = secretkey;

            Key key    = new Key(secretKey);
            var secret = key.Base32;

            TimeBasedOtpGenerator otp = new TimeBasedOtpGenerator(key, otpDigits);
            var time      = GetNistTime();
            var tst       = otp.GenerateOtp(time);
            Key keySecret = new Key(secretKey);

            time = GetNistTime();

            TimeBasedOtpGenerator otp3 = new TimeBasedOtpGenerator(keySecret, otpDigits);

            var valid = otp.ValidateOtp(otpCodeDto.OneTimePasswordCode, time);

            if (valid)
            {
                var jwtoken        = new JwtManager();
                var expirationTime = DateTime.UtcNow.AddMinutes(59);
                var jwt            = jwtoken.GenerateJwtToken(email, userId, 60);

                if (otpCodeDto.ChannelId == "sms")
                {
                    _context.SmsLogin.Add(new Data.Entities.SmsLogin {
                        UserName = otpCodeDto.UserName, ExpirationTime = expirationTime, Jwt = jwt
                    });
                    _context.SaveChanges();
                }

                if (otpCodeDto.ChannelId == "directline")
                {
                    _context.DirectLineLogins.Add(new Data.Entities.DirectLineLogins {
                        UserName = otpCodeDto.UserName, ExpirationTime = expirationTime, Jwt = jwt.ToString()
                    });
                    _context.SaveChanges();
                }
            }

            return(valid);
        }