Esempio n. 1
0
        /// <summary>
        /// Sign in with header
        /// </summary>
        /// <param name="context"></param>
        /// <param name="options"></param>
        /// <param name="badge"></param>
        /// <param name="tag"></param>
        /// <returns></returns>
        public bool SignIn <T>(HttpContext context, T options, DewBadge badge, object tag) where T : DewBadgeOptions
        {
            var opt = options as DewBadgeOptionsJWT;

            context.Response.Headers.Add(opt.HeaderName, opt.Bearer + badge.GetSign(options.Secret));
            return(true);
        }
Esempio n. 2
0
        /// <summary>
        /// Sign out wit cookies
        /// </summary>
        /// <param name="context"></param>
        /// <param name="options"></param>
        /// <param name="badge"></param>
        /// <param name="tag"></param>
        /// <returns></returns>
        public bool SignOut <T>(HttpContext context, T options, DewBadge badge, object tag) where T : DewBadgeOptions
        {
            var opt = options as DewBadgeOptionsCookies;

            context.Response.Cookies.Delete(opt.CookieName);
            return(true);
        }
Esempio n. 3
0
 /// <summary>
 /// Refresh cookie
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="options"></param>
 /// <param name="context"></param>
 /// <param name="badge"></param>
 /// <param name="tag"></param>
 public void Refresh <T>(HttpContext context, T options, DewBadge badge, object tag = null) where T : DewBadgeOptionsCookies
 {
     if (options is DewBadgeOptionsCookies opt)
     {
         var header = context.Request.Cookies.FirstOrDefault(x => { return(x.Key == opt.CookieName); });
         if (!header.Equals(default(KeyValuePair <string, Microsoft.Extensions.Primitives.StringValues>)))
         {
             context.Response.Cookies.Append(opt.CookieName, badge.GetSign(opt.Secret), new CookieOptions()
             {
                 Expires = DateTime.Now.AddMinutes(opt.CookieExpiring)
             });
         }
     }
 }
Esempio n. 4
0
        /// <summary>
        /// Sign in with cookies
        /// </summary>
        /// <param name="context"></param>
        /// <param name="options"></param>
        /// <param name="badge"></param>
        /// <param name="tag"></param>
        /// <returns></returns>
        public bool SignIn <T>(HttpContext context, T options, DewBadge badge, object tag) where T : DewBadgeOptions
        {
            var opt      = options as DewBadgeOptionsCookies;
            var remember = false;

            if (tag != null)
            {
                remember = (bool)tag;
            }
            context.Response.Cookies.Append(opt.CookieName, badge.GetSign(opt.Secret), new CookieOptions()
            {
                Expires = remember ? DateTime.Now.AddMinutes(opt.CookieRemember) : DateTime.Now.AddMinutes(opt.CookieExpiring)
            });
            return(true);
        }
Esempio n. 5
0
        /// <summary>
        /// Sign out with Dew Badge
        /// </summary>
        /// <param name="context"></param>
        /// <param name="badge"></param>
        /// <param name="tag">Custom field</param>
        /// <returns></returns>
        public static bool DewBadgeSignOut <T>(this HttpContext context, DewBadge badge, object tag = null) where T : class, IDewBadgeSigner, new()
        {
            bool result  = true;
            var  options = context.GetDewBadgeOptions();
            var  signer  = new T();

            if (options != null)
            {
                if (!signer.SignOut(context, options, badge, tag))
                {
                    result = false;
                }
            }
            else
            {
                result = false;
            }
            return(result);
        }
Esempio n. 6
0
 /// <summary>
 /// Sign out with header
 /// </summary>
 /// <param name="context"></param>
 /// <param name="options"></param>
 /// <param name="badge"></param>
 /// <param name="tag"></param>
 /// <returns></returns>
 public bool SignOut <T>(HttpContext context, T options, DewBadge badge, object tag) where T : DewBadgeOptions
 {
     return(true);
 }
Esempio n. 7
0
        /// <summary>
        /// Attribute on executing override
        /// </summary>
        /// <param name="context"></param>
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            base.OnActionExecuting(context);
            var      sign    = context.HttpContext.GetDewBadgeSign();
            var      options = context.HttpContext.GetDewBadgeOptions();
            DewBadge badge   = null;

            try
            {
                badge = context.HttpContext.GetDewBadge <DewBadge>();
            }
            catch (IntegrityException e)
            {
                badge = new DewBadge()
                {
                };
                if (options.DEBUG_MODE)
                {
                    badge.DebugMessage = e.Message;
                }
            }
            catch (EncryptionException e)
            {
                badge = new DewBadge()
                {
                };
                if (options.DEBUG_MODE)
                {
                    badge.DebugMessage = e.Message;
                }
            }
            catch (InvalidAlgorithmException e)
            {
                badge = new DewBadge()
                {
                };
                if (options.DEBUG_MODE)
                {
                    badge.DebugMessage = e.Message;
                }
            }
            catch (Exception)
            {
                badge = null;
            }
            if (badge == null || sign == null)
            {
                badge.ResponseNoAuth(options, context);
                return;
            }
            if (!badge.IsExpired())
            {
                if (_type != null && _claims == null)
                {
                    if (!badge.AuthType(_type))
                    {
                        badge.ResponseOnForbidden(options, context);
                    }
                }
                else
                {
                    if (_type != null && _claims != null)
                    {
                        if (!badge.AuthType(_type) || !badge.HasClaims(_claims))
                        {
                            badge.ResponseOnForbidden(options, context);
                        }
                    }
                    else
                    {
                        if (_type == null && _claims != null)
                        {
                            if (!badge.HasClaims(_claims))
                            {
                                badge.ResponseOnForbidden(options, context);
                            }
                        }
                    }
                }
            }
            else
            {
                badge.ResponseOnExpired(options, context);
            }
            var temp = options as DewBadgeOptionsCookies;

            if (temp.RefreshExpireOnBrowse)
            {
                new DewBadgeSignerCookies().Refresh <DewBadgeOptionsCookies>(context.HttpContext, temp, badge);
            }
        }