Exemple #1
0
        /// <summary>
        ///     When marking requests where you want to define VAPID details, call this method
        ///     before sendNotifications() or pass in the details and options to
        ///     sendNotification.
        /// </summary>
        /// <param name="vapidDetails"></param>
        public void SetVapidDetails(VapidDetails vapidDetails)
        {
            VapidHelper.ValidateSubject(vapidDetails.Subject);
            VapidHelper.ValidatePublicKey(vapidDetails.PublicKey);
            VapidHelper.ValidatePrivateKey(vapidDetails.PrivateKey);

            _vapidDetails = vapidDetails;
        }
        public IActionResult GenerateKeys()
        {
            VapidHelper.ValidateSubject("mailto:[email protected]");
            var keys = VapidHelper.GenerateVapidKeys();

            VapidHelper.ValidatePublicKey(keys.PublicKey);
            VapidHelper.ValidatePrivateKey(keys.PrivateKey);

            ViewBag.PublicKey  = keys.PublicKey;
            ViewBag.PrivateKey = keys.PrivateKey;
            return(View());
        }
        /// <summary>
        /// Sets the vapid details.
        /// </summary>
        /// <param name="p_options">Web push options.</param>
        private async Task SetVapidDetails(IOptions <WebPushOptions> p_options)
        {
            bool   v_validSubject = true;
            string v_vapidSubject;
            string v_vapidPublicKey;
            string v_vapidPrivateKey;

            try
            {
                VapidHelper.ValidateSubject(p_options.Value.VapidSubject);
            }
            catch (Exception ex)
            {
                Logger.LogWarning(ex, "Vapid subject is not valid");
                v_validSubject = false;
            }

            if (!v_validSubject && String.IsNullOrWhiteSpace(GcmAPIKey))
            {
                throw new ApplicationException("You must set a valid vapid subject or GCM API Key");
            }

            if (v_validSubject)
            {
                v_vapidSubject = p_options.Value.VapidSubject;

                try
                {
                    VapidHelper.ValidatePublicKey(p_options.Value.VapidPublicKey);
                    VapidHelper.ValidatePrivateKey(p_options.Value.VapidPrivateKey);

                    v_vapidPublicKey  = p_options.Value.VapidPublicKey;
                    v_vapidPrivateKey = p_options.Value.VapidPrivateKey;
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex, "Your Vapid keys are not valid. Using auto generated keys.");

                    var v_tempVapid = VapidHelper.GenerateVapidKeys();
                    v_vapidPublicKey  = v_tempVapid.PublicKey;
                    v_vapidPrivateKey = v_tempVapid.PrivateKey;

                    Logger.LogInformation("Flushing existing devices");
                    IEnumerable <Device> v_devices = this.DbContext.Devices;

                    this.DbContext.RemoveRange(v_devices);
                    await this.DbContext.SaveChangesAsync();
                }

                VapidDetails = new VapidDetails(v_vapidSubject, v_vapidPublicKey, v_vapidPrivateKey);
            }
        }