Commit 71d22f74 authored by Honfika's avatar Honfika

small cleanup

parent 7aaf9213
......@@ -14,5 +14,10 @@ namespace Titanium.Web.Proxy.Extensions
{
return CultureInfo.CurrentCulture.CompareInfo.IndexOf(str, value, CompareOptions.IgnoreCase) >= 0;
}
internal static int IndexOfIgnoreCase(this string str, string value)
{
return CultureInfo.CurrentCulture.CompareInfo.IndexOf(str, value, CompareOptions.IgnoreCase);
}
}
}
......@@ -38,11 +38,9 @@ namespace Titanium.Web.Proxy.Network.WinAuth.Security
try
{
int result;
var state = new State();
result = AcquireCredentialsHandle(
int result = AcquireCredentialsHandle(
WindowsIdentity.GetCurrent().Name,
authScheme,
SecurityCredentialsOutbound,
......@@ -110,13 +108,11 @@ namespace Titanium.Web.Proxy.Network.WinAuth.Security
try
{
int result;
var state = authStates[requestId];
state.UpdatePresence();
result = InitializeSecurityContext(ref state.Credentials,
int result = InitializeSecurityContext(ref state.Credentials,
ref state.Context,
hostname,
StandardContextAttributes,
......@@ -176,23 +172,22 @@ namespace Titanium.Web.Proxy.Network.WinAuth.Security
/// <returns></returns>
internal static bool ValidateWinAuthState(Guid requestId, State.WinAuthState expectedAuthState)
{
State state;
var stateExists = authStates.TryGetValue(requestId, out state);
var stateExists = authStates.TryGetValue(requestId, out var state);
if (expectedAuthState == State.WinAuthState.UNAUTHORIZED)
{
// Validation before initial token
return (stateExists == false ||
state.AuthState == State.WinAuthState.UNAUTHORIZED ||
state.AuthState == State.WinAuthState.AUTHORIZED); // Server may require re-authentication on an open connection
return stateExists == false ||
state.AuthState == State.WinAuthState.UNAUTHORIZED ||
state.AuthState == State.WinAuthState.AUTHORIZED; // Server may require re-authentication on an open connection
}
if (expectedAuthState == State.WinAuthState.INITIAL_TOKEN)
{
// Validation before final token
return (stateExists &&
(state.AuthState == State.WinAuthState.INITIAL_TOKEN ||
state.AuthState == State.WinAuthState.AUTHORIZED)); // Server may require re-authentication on an open connection
return stateExists &&
(state.AuthState == State.WinAuthState.INITIAL_TOKEN ||
state.AuthState == State.WinAuthState.AUTHORIZED); // Server may require re-authentication on an open connection
}
throw new Exception("Unsupported validation of WinAuthState");
......@@ -204,8 +199,7 @@ namespace Titanium.Web.Proxy.Network.WinAuth.Security
/// <param name="requestId"></param>
internal static void AuthenticatedResponse(Guid requestId)
{
State state;
if (authStates.TryGetValue(requestId, out state))
if (authStates.TryGetValue(requestId, out var state))
{
state.AuthState = State.WinAuthState.AUTHORIZED;
state.UpdatePresence();
......
......@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Titanium.Web.Proxy.EventArguments;
using Titanium.Web.Proxy.Extensions;
using Titanium.Web.Proxy.Http;
using Titanium.Web.Proxy.Models;
using Titanium.Web.Proxy.Network.WinAuth;
......@@ -85,8 +86,9 @@ namespace Titanium.Web.Proxy
{
string scheme = authSchemes.FirstOrDefault(x => authHeader.Value.Equals(x, StringComparison.OrdinalIgnoreCase));
if ((scheme != null && !WinAuthEndPoint.ValidateWinAuthState(args.WebSession.RequestId, State.WinAuthState.UNAUTHORIZED)) ||
(scheme == null && !WinAuthEndPoint.ValidateWinAuthState(args.WebSession.RequestId, State.WinAuthState.INITIAL_TOKEN)))
var expectedAuthState = scheme == null ? State.WinAuthState.INITIAL_TOKEN : State.WinAuthState.UNAUTHORIZED;
if (!WinAuthEndPoint.ValidateWinAuthState(args.WebSession.RequestId, expectedAuthState))
{
// Invalid state, create proper error message to client
await RewriteUnauthorizedResponse(args);
......@@ -117,7 +119,7 @@ namespace Titanium.Web.Proxy
//challenge value will start with any of the scheme selected
else
{
scheme = authSchemes.FirstOrDefault(x => authHeader.Value.StartsWith(x, StringComparison.OrdinalIgnoreCase) &&
scheme = authSchemes.First(x => authHeader.Value.StartsWith(x, StringComparison.OrdinalIgnoreCase) &&
authHeader.Value.Length > x.Length + 1);
string serverToken = authHeader.Value.Substring(scheme.Length + 1);
......@@ -152,6 +154,7 @@ namespace Titanium.Web.Proxy
internal async Task RewriteUnauthorizedResponse(SessionEventArgs args)
{
var response = args.WebSession.Response;
// Strip authentication headers to avoid credentials prompt in client web browser
foreach (var authHeaderName in authHeaderNames)
{
......@@ -164,9 +167,10 @@ namespace Titanium.Web.Proxy
") failed. Please check credentials.</h2></div>";
string originalErrorMessage = "<div class=\"inserted-by-proxy\"><h3>Response from remote web server below.</h3></div><br/>";
string body = await args.GetResponseBodyAsString();
if (body.ToLower().Contains("<body>"))
int idx = body.IndexOfIgnoreCase("<body>");
if (idx >= 0)
{
var bodyPos = body.ToLower().IndexOf("<body>") + ("<body>").Length;
var bodyPos = idx + "<body>".Length;
body = body.Insert(bodyPos, authErrorMessage + originalErrorMessage);
}
else
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment