<commit_before>﻿using AzureFunctions.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Linq;
using System.Net;
using System.Security.Claims;
using System.Security.Principal;
using System.Text;
using System.Threading;
using System.Web;

namespace AzureFunctions.Authentication
{
    public class FrontEndAuthProvider : IAuthProvider
    {
        public bool TryAuthenticateRequest(HttpContextBase context)
        {
            IPrincipal principal = null;
            var request = context.Request;
            var response = context.Response;
            var displayName = request.Headers[Constants.FrontEndDisplayNameHeader];
            var principalName = request.Headers[Constants.FrontEndPrincipalNameHeader];
            var portalToken = request.Headers[Constants.PortalTokenHeader];

            if (principalName.Equals(Constants.AnonymousUserName, StringComparison.OrdinalIgnoreCase))
            {
                if (request.UrlReferrer?.AbsoluteUri.StartsWith(Constants.PortalReferrer, StringComparison.OrdinalIgnoreCase) == true ||
                    request.UrlReferrer?.AbsoluteUri.StartsWith(Constants.MsPortalReferrer, StringComparison.OrdinalIgnoreCase) == true)
                {
                    principal = new AzureFunctionsPrincipal(new AzureFunctionsIdentity(Constants.PortalAnonymousUser));
                }
                else if (string.IsNullOrEmpty(portalToken))
                {
                    principal = new AzureFunctionsPrincipal(new AzureFunctionsIdentity(Constants.AnonymousUserName));
                }
                else
                {
                    principal = ParsePortalToken(portalToken);
                }
            }
            else if (!string.IsNullOrWhiteSpace(principalName) ||
                     !string.IsNullOrWhiteSpace(displayName))
            {
                principal = new AzureFunctionsPrincipal(new AzureFunctionsIdentity(principalName ?? displayName));
            }
            else
            {
                // throw?
                principal = new AzureFunctionsPrincipal(new AzureFunctionsIdentity(Constants.AnonymousUserName));
            }

            HttpContext.Current.User = principal;
            Thread.CurrentPrincipal = principal;<commit_msg>fix nullref when using default hostname<commit_after>﻿using AzureFunctions.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Linq;
using System.Net;
using System.Security.Claims;
using System.Security.Principal;
using System.Text;
using System.Threading;
using System.Web;

namespace AzureFunctions.Authentication
{
    public class FrontEndAuthProvider : IAuthProvider
    {
        public bool TryAuthenticateRequest(HttpContextBase context)
        {
            IPrincipal principal = null;
            var request = context.Request;
            var response = context.Response;
            var displayName = request.Headers[Constants.FrontEndDisplayNameHeader];
            var principalName = request.Headers[Constants.FrontEndPrincipalNameHeader];
            var portalToken = request.Headers[Constants.PortalTokenHeader];

            if (string.Equals(principalName, Constants.AnonymousUserName, StringComparison.OrdinalIgnoreCase))
            {
                if (request.UrlReferrer?.AbsoluteUri.StartsWith(Constants.PortalReferrer, StringComparison.OrdinalIgnoreCase) == true ||
                    request.UrlReferrer?.AbsoluteUri.StartsWith(Constants.MsPortalReferrer, StringComparison.OrdinalIgnoreCase) == true)
                {
                    principal = new AzureFunctionsPrincipal(new AzureFunctionsIdentity(Constants.PortalAnonymousUser));
                }
                else if (string.IsNullOrEmpty(portalToken))
                {
                    principal = new AzureFunctionsPrincipal(new AzureFunctionsIdentity(Constants.AnonymousUserName));
                }
                else
                {
                    principal = ParsePortalToken(portalToken);
                }
            }
            else if (!string.IsNullOrWhiteSpace(principalName) ||
                     !string.IsNullOrWhiteSpace(displayName))
            {
                principal = new AzureFunctionsPrincipal(new AzureFunctionsIdentity(principalName ?? displayName));
            }
            else
            {
                // throw?
                principal = new AzureFunctionsPrincipal(new AzureFunctionsIdentity(Constants.AnonymousUserName));
            }

            HttpContext.Current.User = principal;
            Thread.CurrentPrincipal = principal;
{'fraction_non_alphanumeric': 0.07254222607189259, 'fraction_numerical': 0.0, 'mean_word_length': 2.2969307637401855, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6536119', 'n_tokens_mistral': 1079, 'n_tokens_neox': 1049, 'n_words': 191}