History

GSF.Web: Changed default session token to "x-gsf-auth" since session gets added automatically and used for other purposes by IE/Edge

J. Ritchie Carroll committed 9 years ago

Commit 4fd27cf1 parent e0a7b626 files · +28 −99Browse files

Unified · Split

6 files
  1. Source/Libraries/GSF.Web/GSF.Web.csproj +0 −1
  2. Source/Libraries/GSF.Web/Model/Scripts/gsf.web.client.js +6 −2
  3. Source/Libraries/GSF.Web/Security/AddChallengeOnUnauthorizedResult.cs +0 −92
  4. Source/Libraries/GSF.Web/Security/AuthenticationHandler.cs +20 −2
  5. Source/Libraries/GSF.Web/Security/AuthenticationOptions.cs +1 −1
  6. Source/Libraries/GSF.Web/Security/SessionHandler.cs +1 −1
Source/Libraries/GSF.Web/GSF.Web.csproj+0−1
@@ -196,7 +196,6 @@ · 195 unmodified lines
196196 <DependentUpon>SecurityService.svc</DependentUpon>
197197 </Compile>
198198 <Compile Include="Embedded\SecurityServiceHostFactory.cs" />
199− <Compile Include="Security\AddChallengeOnUnauthorizedResult.cs" />
200199 <Compile Include="Security\AuthenticationHandler.cs" />
201200 <Compile Include="Security\AuthenticationMiddleware.cs" />
202201 <Compile Include="Security\AuthenticationOptions.cs" />
Source/Libraries/GSF.Web/Model/Scripts/gsf.web.client.js+6−2
@@ -129,8 +129,8 @@ function clearCachedCredentials(securedUrl, successCallback) { · 128 unmodified lines
129129
130130 if (successCallback)
131131 request.onreadystatechange = function() {
132− if (this.readyState == XMLHttpRequest.DONE)
133− successCallback(this.status == 401);
132+ if (this.readyState === XMLHttpRequest.DONE)
133+ successCallback(this.status === 401);
134134 };
135135
136136 request.send();
@@ -407,6 +407,10 @@ function isEmpty(str) { · 270 unmodified lines
407407 return !str || String(str).length === 0;
408408 }
409409
410+String.prototype.trimLeft = String.prototype.trimLeft || function () {
411+ return this.replace(/^\s+/,"");
412+}
413+
410414 String.prototype.truncate = function(limit) {
411415 const text = this.trim();
412416
Source/Libraries/GSF.Web/Security/AddChallengeOnUnauthorizedResult.cs+0−92
@@ -1,92 +0,0 @@
1−//******************************************************************************************************
2−// AddChallengeOnUnauthorizedResult.cs - Gbtc
3−//
4−// Copyright © 2017, Grid Protection Alliance. All Rights Reserved.
5−//
6−// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
7−// the NOTICE file distributed with this work for additional information regarding copyright ownership.
8−// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may not use this
9−// file except in compliance with the License. You may obtain a copy of the License at:
10−//
11−// http://opensource.org/licenses/MIT
12−//
13−// Unless agreed to in writing, the subject software distributed under the License is distributed on an
14−// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
15−// License for the specific language governing permissions and limitations.
16−//
17−// Code Modification History:
18−// ----------------------------------------------------------------------------------------------------
19−// 08/18/2017 - J. Ritchie Carroll
20−// Generated original version of source code based on ASP.NET BasicAuthentication sample.
21−//
22−//******************************************************************************************************
23−
24−using System.Linq;
25−using System.Net;
26−using System.Net.Http;
27−using System.Net.Http.Headers;
28−using System.Threading;
29−using System.Threading.Tasks;
30−using System.Web.Http;
31−
32−namespace GSF.Web.Security
33−{
34− /// <summary>
35− /// Represents an HTTP action result that will add a challenge header when a result is unauthorized.
36− /// </summary>
37− public class AddChallengeOnUnauthorizedResult : IHttpActionResult
38− {
39− #region [ Constructors ]
40−
41− /// <summary>
42− /// Creates a new <see cref="AddChallengeOnUnauthorizedResult"/>.
43− /// </summary>
44− /// <param name="challenge">Authentication header value containing the challenge.</param>
45− /// <param name="innerResult">Inner HTTP action result to execute for response.</param>
46− public AddChallengeOnUnauthorizedResult(AuthenticationHeaderValue challenge, IHttpActionResult innerResult)
47− {
48− Challenge = challenge;
49− InnerResult = innerResult;
50− }
51−
52− #endregion
53−
54− #region [ Properties ]
55−
56− /// <summary>
57− /// Gets the authentication header value containing the challenge.
58− /// </summary>
59− public AuthenticationHeaderValue Challenge { get; }
60−
61− /// <summary>
62− /// Gets the inner HTTP action result to execute for response.
63− /// </summary>
64− public IHttpActionResult InnerResult { get; }
65−
66− #endregion
67−
68− #region [ Methods ]
69−
70− /// <summary>
71− /// Executes <see cref="InnerResult"/> so challenge header can be added when unauthorized.
72− /// </summary>
73− /// <param name="cancellationToken">Token to cancel async operation.</param>
74− /// <returns>Response message result of execution.</returns>
75− public async Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
76− {
77− HttpResponseMessage response = await InnerResult.ExecuteAsync(cancellationToken);
78−
79− // If request is not authenticated, add a challenge header
80− if (response.StatusCode == HttpStatusCode.Unauthorized)
81− {
82− // Only add one challenge per authentication scheme
83− if (response.Headers.WwwAuthenticate.All(header => header.Scheme != Challenge.Scheme))
84− response.Headers.WwwAuthenticate.Add(Challenge);
85− }
86−
87− return response;
88− }
89−
90− #endregion
91− }
92−}
Source/Libraries/GSF.Web/Security/AuthenticationHandler.cs+20−2
@@ -83,6 +83,9 @@ namespace GSF.Web.Security · 82 unmodified lines
8383 {
8484 SecurityPrincipal securityPrincipal;
8585
86+ // Track original principal
87+ Context.Environment["OriginalPrincipal"] = Request.User;
88+
8689 // No authentication required for anonymous resources
8790 if (Options.IsAnonymousResource(Request.Path.Value))
8891 return null;
@@ -156,10 +159,25 @@ namespace GSF.Web.Security · 67 unmodified lines
156159 // Check configured options that define if a resource should redirect to the login
157160 // page upon authentication failure or simply return an unauthorized (401) response
158161 if (Options.IsAuthFailureRedirectResource(urlPath))
159− Response.Redirect(Options.LoginPage);
162+ {
163+ Response.Redirect(Options.LoginPage);
164+ }
160165 else
161− Response.StatusCode = (int)HttpStatusCode.Unauthorized;
166+ {
167+ string currentIdentity = securityPrincipal?.Identity?.Name ?? "anonymous";
168+ object value;
162169
170+ if (Context.Environment.TryGetValue("OriginalPrincipal", out value))
171+ {
172+ IPrincipal originalPrincpal = value as IPrincipal;
173+
174+ if ((object)originalPrincpal != null && (object)originalPrincpal.Identity != null)
175+ currentIdentity = originalPrincpal.Identity.Name;
176+ }
177+
178+ Response.Headers.Add("CurrentIdentity", new[] { currentIdentity });
179+ Response.StatusCode = (int)HttpStatusCode.Unauthorized;
180+ }
163181 Response.ReasonPhrase = SecurityPrincipal.GetFailureReasonPhrase(securityPrincipal, AuthorizationHeader?.Scheme, AssemblyInfo.EntryAssembly.Debuggable);
164182
165183 return true;
Source/Libraries/GSF.Web/Security/AuthenticationOptions.cs+1−1
@@ -67,7 +67,7 @@ namespace GSF.Web.Security · 66 unmodified lines
6767 /// <summary>
6868 /// Creates a new instance of the <see cref="AuthenticationOptions"/> class.
6969 /// </summary>
70− public AuthenticationOptions() : base("x-gsf-auth")
70+ public AuthenticationOptions() : base(SessionHandler.DefaultSessionToken)
7171 {
7272 m_authFailureRedirectResourceCache = new ConcurrentDictionary<string, bool>(StringComparer.OrdinalIgnoreCase);
7373 m_anonymousResourceCache = new ConcurrentDictionary<string, bool>(StringComparer.OrdinalIgnoreCase);
Source/Libraries/GSF.Web/Security/SessionHandler.cs+1−1
@@ -45,7 +45,7 @@ namespace GSF.Web.Security · 44 unmodified lines
4545 /// <summary>
4646 /// Default value for <see cref="SessionToken"/>;
4747 /// </summary>
48− public const string DefaultSessionToken = "session";
48+ public const string DefaultSessionToken = "x-gsf-auth";
4949
5050 #endregion
5151

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t