Esempio n. 1
0
        public async Task <AuthCode> CheckCode(string recipient, string code, bool updateUsage = true)
        {
            return(await Task.Run <AuthCode>(() =>
            {
                AuthCode authCode = null;
                var codeQuery = new { Recipient = recipient, CodeHash = CryptoProvider.SHA1(code.ToUpper()).ToLower() };
                authCode = _dataManager.Get <AuthCode>(codeQuery);
                if (authCode == null || authCode.UsedTime.HasValue)
                {
                    throw new ApplicationException("کد تائید کاربر نامعتبر میباشد.");
                }

                if (authCode.ExpieryTime < DateTimeOffset.UtcNow)
                {
                    throw new ApplicationException("کد تائید کاربر منقضی شده و غیر قابل استفاده میباشد.");
                }

                if (updateUsage)
                {
                    var usedTime = DateTimeOffset.UtcNow;
                    var count = _dataManager.Update <AuthCode>(new { UsedTime = usedTime }, new { Id = authCode.Id });
                    if (count <= 0)
                    {
                        throw new ApplicationException("خطا در بروز رسانی وضعیت کد تائید کاربر.");
                    }
                    authCode.UsedTime = usedTime;
                }

                return authCode;
            }));
        }
Esempio n. 2
0
        //public async Task<AuthCode> SendCode(string phone, AuthCodeMessageType messageType)
        //{
        //    if (string.IsNullOrEmpty(phone))
        //        throw new ApplicationException(Resx.AppResources.InvalidPhoneException);

        //    var code = OtpTools.GenRandomNumber(6);
        //    var authCode = new AuthCode()
        //    {
        //        Phone = phone,
        //        IsRegistered = false,
        //        MessageType = AuthCodeMessageType.SmsMessageWithCode,
        //        IsPassword = false,
        //        CodeHash = CryptoProvider.SHA1(CryptoProvider.SHA1(code)).ToLower(),
        //        //Token = CryptoProvider.HMACSHA1(phone, OtpTools.GetOtpTime()).ToLower(),
        //    };

        //    return await Task.Run<AuthCode>(async () =>
        //    {
        //        var user = _dataManager.Get<User>(new { Phone = phone });
        //        if (user != null)
        //        {
        //            authCode.IsRegistered = true;
        //        }

        //        authCode.CreateTime = DateTimeOffset.UtcNow;
        //        authCode.ExpieryTime = DateTimeOffset.UtcNow.AddSeconds(180);
        //        authCode.Id = _dataManager.Insert<AuthCode, long>(authCode);

        //        // Send Message
        //        _notificationProvider?.SendPhoneVerificationMessage(phone, code, user?.AppName, messageType);

        //        return authCode;
        //    });
        //}

        public async Task <AuthCode> SendCode(string recipient, AuthCodeMessageType messageType, string appName)
        {
            if (string.IsNullOrEmpty(recipient))
            {
                throw new ApplicationException("Invalid recipient.");
            }

            User user = null;

            if (messageType == AuthCodeMessageType.Email)
            {
                user = _dataManager.Get <User>(new { Email = recipient });
            }
            else
            {
                user = _dataManager.Get <User>(new { Phone = recipient });
            }

            var code     = OtpTools.GenRandomNumber(6);
            var authCode = new AuthCode()
            {
                Recipient    = recipient,
                IsRegistered = user != null,
                MessageType  = messageType,
                IsPassword   = false,
                CodeHash     = CryptoProvider.SHA1(CryptoProvider.SHA1(code)).ToLower(),
                //Token = CryptoProvider.HMACSHA1(phone, OtpTools.GetOtpTime()).ToLower(),
                CreateTime  = DateTimeOffset.UtcNow,
                ExpieryTime = messageType == AuthCodeMessageType.Email ? DateTimeOffset.UtcNow.AddDays(30) : DateTimeOffset.UtcNow.AddSeconds(180)
            };

            authCode.Id = _dataManager.Insert <AuthCode, long>(authCode);

            // Send Message
            switch (messageType)
            {
            case AuthCodeMessageType.SmsMessageWithCode:
            case AuthCodeMessageType.SmsMessageWithAppLink:
            case AuthCodeMessageType.ChatMessage:
            case AuthCodeMessageType.PhoneCall:
            case AuthCodeMessageType.PushMessage:
                await _notificationProvider?.SendPhoneVerificationMessage(recipient, user?.DisplayName, code, appName);

                break;

            case AuthCodeMessageType.Email:
                var token = Convert.ToBase64String(Encoding.Unicode.GetBytes($"{recipient}&{code}&{authCode.ExpieryTime}"));
                var link  = $"{EmailVerificationUrl}?token={HttpUtility.UrlEncode(token)}";
                await _notificationProvider?.SendEmailVerificationMessage(recipient, user?.DisplayName, link, appName);

                break;

            default:
                break;
            }

            return(authCode);
        }
Esempio n. 3
0
        public async Task <AuthCode> VerifyEmail(string token, bool approveUser = false)
        {
            var    value    = HttpUtility.UrlDecode(token);
            var    items    = Encoding.Unicode.GetString(Convert.FromBase64String(value)).Split(new Char[] { '&' });
            string email    = items?.Count() > 0 ? items[0] : null;
            var    code     = items?.Count() > 1 ? items[1] : null;
            var    authCode = await CheckCode(email, CryptoProvider.SHA1(code), false);

            if (approveUser && authCode.IsRegistered)
            {
                _dataManager.Update <User>(new { IsApproved = true }, new { Email = authCode.Recipient });
            }
            return(authCode);
        }