<commit_before>﻿using System;
using System.Globalization;
using System.Linq;
using System.Security.Claims;
using JabbR.Infrastructure;
using Nancy;
using Nancy.Security;

namespace JabbR.Nancy
{
    public class JabbRModule : NancyModule
    {
        public JabbRModule()
            : base()
        {
            Before.AddItemToEndOfPipeline(AlertsToViewBag);
            After.AddItemToEndOfPipeline(RemoveAlters);
        }

        public JabbRModule(string modulePath)
            : base(modulePath)
        {
            Before.AddItemToEndOfPipeline(AlertsToViewBag);
            After.AddItemToEndOfPipeline(RemoveAlters);
        }

        protected ClaimsPrincipal Principal
        {
            get { return this.GetPrincipal(); }
        }

        protected bool IsAuthenticated
        {
            get { return this.IsAuthenticated(); }
        }

        protected bool HasValidCsrfTokenOrSecHeader
        {
            get
            {
                if (Request.Headers["sec-jabbr-client"].FirstOrDefault() != null)
                {
                    return true;
                }

                try
                {
                    this.ValidateCsrfToken();
                }
                catch (CsrfValidationException)
                {
                    return false;
                }

                return true;
            }
        }

        internal static Response AlertsToViewBag(NancyContext context)
        {
            var result = context.GetAuthenticationResult();

            if (result != null)
            {
                if (!String.IsNullOrEmpty(result.Message))<commit_msg>Allow useragents containing signalr and also useragents which do not include (or populate) the useragent header without checkinng CSRF.  This should maintain compatability with most pre-existing clients.<commit_after>﻿using System;
using System.Globalization;
using System.Linq;
using System.Security.Claims;
using JabbR.Infrastructure;
using Nancy;
using Nancy.Security;

namespace JabbR.Nancy
{
    public class JabbRModule : NancyModule
    {
        public JabbRModule()
            : base()
        {
            Before.AddItemToEndOfPipeline(AlertsToViewBag);
            After.AddItemToEndOfPipeline(RemoveAlters);
        }

        public JabbRModule(string modulePath)
            : base(modulePath)
        {
            Before.AddItemToEndOfPipeline(AlertsToViewBag);
            After.AddItemToEndOfPipeline(RemoveAlters);
        }

        protected ClaimsPrincipal Principal
        {
            get { return this.GetPrincipal(); }
        }

        protected bool IsAuthenticated
        {
            get { return this.IsAuthenticated(); }
        }

        protected bool HasValidCsrfTokenOrSecHeader
        {
            get
            {
                // if we have no useragent or signalr in the useragent, then we don't worry about CSRF because it's not a browser.
                if (string.IsNullOrEmpty(Request.Headers.UserAgent)
                    || Request.Headers.UserAgent.IndexOf("signalr", StringComparison.InvariantCultureIgnoreCase) >= 0)
                {
                    return true;
                }

                if (Request.Headers["sec-jabbr-client"].FirstOrDefault() != null)
                {
                    return true;
                }

                try
                {
                    this.ValidateCsrfToken();
                }
                catch (CsrfValidationException)
                {
                    return false;
                }

                return true;
            }
        }

        internal static Response AlertsToViewBag(NancyContext context)
        {
            var result = context.GetAuthenticationResult();

            if (result != null)
            {
                if (!String.IsNullOrEmpty(result.Message))
{'fraction_non_alphanumeric': 0.07080103359173126, 'fraction_numerical': 0.00025839793281653745, 'mean_word_length': 1.508749189889825, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3188977', 'n_tokens_mistral': 996, 'n_tokens_neox': 927, 'n_words': 199}