<commit_before>using AzureFunctions.Common;
using AzureFunctions.Contracts;
using System;
using System.IdentityModel.Tokens;
using System.Linq;
using System.Net;
using System.Security.Principal;
using System.Threading;
using System.Web;
using static AzureFunctions.Authentication.ClaimTypes;

namespace AzureFunctions.Authentication
{
    public class FrontEndAuthProvider : IAuthProvider
    {
        private readonly ISettings _settings;
        private readonly string[] _noAuthReferrers = new string[] 
        {
            Constants.PortalReferrer,
            Constants.PortalDeReferrer,
            Constants.PortalCnReferrer,
            Constants.PortalUsReferrer,
            Constants.PowerAppsReferrerSuffix,
        };

        public FrontEndAuthProvider(ISettings settings)
        {
            this._settings = settings;
        }

        public bool TryAuthenticateRequest(HttpContextBase context)
        {
            IPrincipal principal = null;
            var request = context.Request;
            var displayName = request.Headers[Constants.FrontEndDisplayNameHeader];
            var principalName = request.Headers[Constants.FrontEndPrincipalNameHeader];
            var portalToken = request.Headers[Constants.PortalTokenHeader] ?? request.Headers[Constants.Authorization];

            if (string.Equals(principalName, Constants.AnonymousUserName, StringComparison.OrdinalIgnoreCase))
            {
                if(request.UrlReferrer != null
                    && this._noAuthReferrers.FirstOrDefault(r => request.UrlReferrer.Host.EndsWith(r, StringComparison.OrdinalIgnoreCase)) != null)
                {
                    principal = new AzureFunctionsPrincipal(new AzureFunctionsIdentity(Constants.PortalAnonymousUser));
                }
                else if (string.IsNullOrEmpty(portalToken))
                {
                    principal = new AzureFunctionsPrincipal(new AzureFunctionsIdentity(Constants.AnonymousUserName));
                }
                else
                {
                    principal = ParsePortalToken(portalToken.Split(new[] { ' ' }).Last());
                }
            }
            else if (!string.IsNullOrWhiteSpace(principalName) ||
                     !string.IsNullOrWhiteSpace(displayName))<commit_msg>Check referer before checking X-MS-CLIENT-PRINCIPAL-NAME (#2218)<commit_after>using AzureFunctions.Common;
using AzureFunctions.Contracts;
using System;
using System.IdentityModel.Tokens;
using System.Linq;
using System.Net;
using System.Security.Principal;
using System.Threading;
using System.Web;
using static AzureFunctions.Authentication.ClaimTypes;

namespace AzureFunctions.Authentication
{
    public class FrontEndAuthProvider : IAuthProvider
    {
        private readonly ISettings _settings;
        private readonly string[] _noAuthReferrers = new string[] 
        {
            Constants.PortalReferrer,
            Constants.PortalDeReferrer,
            Constants.PortalCnReferrer,
            Constants.PortalUsReferrer,
            Constants.PowerAppsReferrerSuffix,
        };

        public FrontEndAuthProvider(ISettings settings)
        {
            this._settings = settings;
        }

        public bool TryAuthenticateRequest(HttpContextBase context)
        {
            IPrincipal principal = null;
            var request = context.Request;
            var displayName = request.Headers[Constants.FrontEndDisplayNameHeader];
            var principalName = request.Headers[Constants.FrontEndPrincipalNameHeader];
            var portalToken = request.Headers[Constants.PortalTokenHeader] ?? request.Headers[Constants.Authorization];

            if(request.UrlReferrer != null
                && this._noAuthReferrers.Any(r => request.UrlReferrer.Host.EndsWith(r, StringComparison.OrdinalIgnoreCase)))
            {
                principal = new AzureFunctionsPrincipal(new AzureFunctionsIdentity(Constants.PortalAnonymousUser));
            }
            else if (string.Equals(principalName, Constants.AnonymousUserName, StringComparison.OrdinalIgnoreCase))
            {
                if (string.IsNullOrEmpty(portalToken))
                {
                    principal = new AzureFunctionsPrincipal(new AzureFunctionsIdentity(Constants.AnonymousUserName));
                }
                else
                {
                    principal = ParsePortalToken(portalToken.Split(new[] { ' ' }).Last());
                }
            }
            else if (!string.IsNullOrWhiteSpace(principalName) ||
                     !string.IsNullOrWhiteSpace(displayName))
{'fraction_non_alphanumeric': 0.07620495006513243, 'fraction_numerical': 0.0008684324793747286, 'mean_word_length': 2.3924889543446244, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4550448', 'n_tokens_mistral': 1139, 'n_tokens_neox': 1101, 'n_words': 195}