private bool SendOTP(OTPRecord otp, IUser user, DeliveryChannelType?channel, FlowType?flow)
        {
            // Select / order delivery services
            var deliveryServices = _deliveryServices;

            if (channel != null)
            {
                deliveryServices = deliveryServices.Where(ds => ds.ChannelType == channel);
            }
            deliveryServices = deliveryServices.OrderByDescending(ds => ds.Priority);


            // send through the first channel that does not fail
            var success = false;

            foreach (var ds in deliveryServices)
            {
                success = ds.TrySendOTP(otp, user, flow);
                if (success)
                {
                    break; // break on first success
                }
            }

            return(success);
        }
Esempio n. 2
0
 public void Delete(OTPRecord otp)
 {
     if (otp != null)
     {
         Delete(otp.Id); // This prevents exceptions from weird data.
     }
 }
Esempio n. 3
0
        public OTPRecord AddOTP(OTPRecord otp)
        {
            var userId = otp.UserRecord.Id;

            // delete all expired records for the user
            DeleteExpired(userId, otp.PasswordType);
            // create the new record in the db
            _repository.Create(otp);
            return(otp);
        }
Esempio n. 4
0
        public bool TrySendOTP(OTPRecord otp, IUser user, FlowType?flow)
        {
            if (otp == null || // parameter validation
                user == null ||
                otp.UserRecord.UserName != user.UserName)
            {
                return(false);
            }

            var currentSite = _workContextAccessor.GetContext().CurrentSite;
            var data        = new Dictionary <string, object>();

            // get link
            var link     = _nonceLinkProvider.FormatURI(otp.Password, flow);
            var userlang = _workContextAccessor.GetContext().CurrentSite.SiteCulture;

            if (user.ContentItem.As <FavoriteCulturePart>() != null)
            {
                userlang = user.ContentItem.As <FavoriteCulturePart>().Culture;
            }
            var templatePart    = _workContextAccessor.GetContext().CurrentSite.As <NonceTemplateSettingsPart>().SelectedTemplate;
            int templateidToUse = 0;

            if (templatePart != null)
            {
                templateidToUse = templatePart.Id;
            }
            if (templatePart.ContentItem.As <LocalizationPart>() != null)
            {
                int translatedId = 0;
                if (_localizableContentService.TryGetLocalizedId(templateidToUse, userlang, out translatedId))
                {
                    if (translatedId > 0)
                    {
                        templateidToUse = translatedId;
                    }
                }
            }
            if (templateidToUse == 0)
            {
                Logger.Error("NonceTemplatePart must be added to CustomTemplate used for nonce");
                return(false);
            }
            else
            {
                dynamic contentModel = new {
                    ContentItem = user,
                    Link        = link
                };
                List <string> sendTo = new List <string>(new string[] { user.Email });
                _templateService.SendTemplatedEmail(contentModel, templateidToUse, sendTo, null);
                return(true);
            }
        }
        public bool SendOTP(OTPRecord otp, DeliveryChannelType?channel)
        {
            if (otp == null)
            {
                throw new ArgumentNullException("otp");
            }

            // get recipient
            var user = _membershipService.GetUser(otp.UserRecord.UserName);

            return(SendOTP(otp, user, channel));
        }
        public bool TrySendOTP(OTPRecord otp, IUser user, FlowType?flow)
        {
            if (otp == null || // parameter validation
                user == null ||
                otp.UserRecord.UserName != user.UserName)
            {
                return(false);
            }
            var currentSite = _workContextAccessor.GetContext().CurrentSite;
            var data        = new Dictionary <string, object>();
            //// get link
            var link = _nonceLinkProvider.FormatURI(otp.Password, flow);

            data.Add("Subject", T("{0} - Login", currentSite.SiteName).Text);
            data.Add("Body", T("<html><body>To login on \"{0}\", please open the following link: <a href=\"{1}\">Login</a></body></html>", currentSite.SiteName, link).Text);
            data.Add("Recipients", user.Email);
            _messageService.Send(SmtpMessageChannel.MessageType, data);
            return(true);
        }
        private OTPRecord NewOTP(UserPart user, Dictionary <string, string> additionalInformation)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            // use the base nonce from the IUserServices
            var      nonce = _userService.CreateNonce(user, new TimeSpan(0, ValidityTime(), 0));
            string   userName;
            DateTime expiration;

            // get the expiration actually assigned on the nonce
            _userService.DecryptNonce(nonce, out userName, out expiration);
            // create the OTP
            var otp = new OTPRecord {
                UserRecord        = user.As <UserPart>().Record,
                Password          = nonce,
                PasswordType      = PasswordType.Nonce.ToString(),
                ExpirationUTCDate = expiration,
                AdditionalData    = additionalInformation != null
                    ? JsonConvert.SerializeObject(additionalInformation, Formatting.Indented)
                    : string.Empty
            };
            // delete all old nonces that match the one we are creating
            var oldOtps = _otpRepositoryService.Get(user, PasswordType.Nonce.ToString());

            foreach (var old in oldOtps
                     .Where(or =>
                            CompareDictionaries(
                                JsonConvert.DeserializeObject <Dictionary <string, string> >(or.AdditionalData),
                                additionalInformation)))
            {
                _otpRepositoryService.Delete(old);
            }
            // save the OTP
            return(_otpRepositoryService.AddOTP(otp));
        }
Esempio n. 8
0
 public bool TrySendOTP(OTPRecord otp, IUser user)
 {
     return(TrySendOTP(otp, user, null));
 }
 private bool SendOTP(OTPRecord otp, IUser user, DeliveryChannelType?channel)
 {
     return(SendOTP(otp, user, channel, FlowType.Website));
 }