Unified · Split
| @@ -196,7 +196,6 @@ · 195 unmodified lines | |||
| 196 | <DependentUpon>SecurityService.svc</DependentUpon> | 196 | <DependentUpon>SecurityService.svc</DependentUpon> |
| 197 | </Compile> | 197 | </Compile> |
| 198 | <Compile Include="Embedded\SecurityServiceHostFactory.cs" /> | 198 | <Compile Include="Embedded\SecurityServiceHostFactory.cs" /> |
| 199 | <Compile Include="Security\AddChallengeOnUnauthorizedResult.cs" /> | ||
| 200 | <Compile Include="Security\AuthenticationHandler.cs" /> | 199 | <Compile Include="Security\AuthenticationHandler.cs" /> |
| 201 | <Compile Include="Security\AuthenticationMiddleware.cs" /> | 200 | <Compile Include="Security\AuthenticationMiddleware.cs" /> |
| 202 | <Compile Include="Security\AuthenticationOptions.cs" /> | 201 | <Compile Include="Security\AuthenticationOptions.cs" /> |
| @@ -129,8 +129,8 @@ function clearCachedCredentials(securedUrl, successCallback) { · 128 unmodified lines | |||
| 129 | 129 | ||
| 130 | if (successCallback) | 130 | if (successCallback) |
| 131 | request.onreadystatechange = function() { | 131 | request.onreadystatechange = function() { |
| 132 | if (this.readyState == XMLHttpRequest.DONE) | 132 | if (this.readyState === XMLHttpRequest.DONE) |
| 133 | successCallback(this.status == 401); | 133 | successCallback(this.status === 401); |
| 134 | }; | 134 | }; |
| 135 | 135 | ||
| 136 | request.send(); | 136 | request.send(); |
| @@ -407,6 +407,10 @@ function isEmpty(str) { · 270 unmodified lines | |||
| 407 | return !str || String(str).length === 0; | 407 | return !str || String(str).length === 0; |
| 408 | } | 408 | } |
| 409 | 409 | ||
| 410 | String.prototype.trimLeft = String.prototype.trimLeft || function () { | ||
| 411 | return this.replace(/^\s+/,""); | ||
| 412 | } | ||
| 413 | |||
| 410 | String.prototype.truncate = function(limit) { | 414 | String.prototype.truncate = function(limit) { |
| 411 | const text = this.trim(); | 415 | const text = this.trim(); |
| 412 | 416 | ||
| @@ -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 | } | ||
| @@ -83,6 +83,9 @@ namespace GSF.Web.Security · 82 unmodified lines | |||
| 83 | { | 83 | { |
| 84 | SecurityPrincipal securityPrincipal; | 84 | SecurityPrincipal securityPrincipal; |
| 85 | 85 | ||
| 86 | // Track original principal | ||
| 87 | Context.Environment["OriginalPrincipal"] = Request.User; | ||
| 88 | |||
| 86 | // No authentication required for anonymous resources | 89 | // No authentication required for anonymous resources |
| 87 | if (Options.IsAnonymousResource(Request.Path.Value)) | 90 | if (Options.IsAnonymousResource(Request.Path.Value)) |
| 88 | return null; | 91 | return null; |
| @@ -156,10 +159,25 @@ namespace GSF.Web.Security · 67 unmodified lines | |||
| 156 | // Check configured options that define if a resource should redirect to the login | 159 | // Check configured options that define if a resource should redirect to the login |
| 157 | // page upon authentication failure or simply return an unauthorized (401) response | 160 | // page upon authentication failure or simply return an unauthorized (401) response |
| 158 | if (Options.IsAuthFailureRedirectResource(urlPath)) | 161 | if (Options.IsAuthFailureRedirectResource(urlPath)) |
| 159 | Response.Redirect(Options.LoginPage); | 162 | { |
| 163 | Response.Redirect(Options.LoginPage); | ||
| 164 | } | ||
| 160 | else | 165 | else |
| 161 | Response.StatusCode = (int)HttpStatusCode.Unauthorized; | 166 | { |
| 167 | string currentIdentity = securityPrincipal?.Identity?.Name ?? "anonymous"; | ||
| 168 | object value; | ||
| 162 | 169 | ||
| 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 | } | ||
| 163 | Response.ReasonPhrase = SecurityPrincipal.GetFailureReasonPhrase(securityPrincipal, AuthorizationHeader?.Scheme, AssemblyInfo.EntryAssembly.Debuggable); | 181 | Response.ReasonPhrase = SecurityPrincipal.GetFailureReasonPhrase(securityPrincipal, AuthorizationHeader?.Scheme, AssemblyInfo.EntryAssembly.Debuggable); |
| 164 | 182 | ||
| 165 | return true; | 183 | return true; |
| @@ -67,7 +67,7 @@ namespace GSF.Web.Security · 66 unmodified lines | |||
| 67 | /// <summary> | 67 | /// <summary> |
| 68 | /// Creates a new instance of the <see cref="AuthenticationOptions"/> class. | 68 | /// Creates a new instance of the <see cref="AuthenticationOptions"/> class. |
| 69 | /// </summary> | 69 | /// </summary> |
| 70 | public AuthenticationOptions() : base("x-gsf-auth") | 70 | public AuthenticationOptions() : base(SessionHandler.DefaultSessionToken) |
| 71 | { | 71 | { |
| 72 | m_authFailureRedirectResourceCache = new ConcurrentDictionary<string, bool>(StringComparer.OrdinalIgnoreCase); | 72 | m_authFailureRedirectResourceCache = new ConcurrentDictionary<string, bool>(StringComparer.OrdinalIgnoreCase); |
| 73 | m_anonymousResourceCache = new ConcurrentDictionary<string, bool>(StringComparer.OrdinalIgnoreCase); | 73 | m_anonymousResourceCache = new ConcurrentDictionary<string, bool>(StringComparer.OrdinalIgnoreCase); |
| @@ -45,7 +45,7 @@ namespace GSF.Web.Security · 44 unmodified lines | |||
| 45 | /// <summary> | 45 | /// <summary> |
| 46 | /// Default value for <see cref="SessionToken"/>; | 46 | /// Default value for <see cref="SessionToken"/>; |
| 47 | /// </summary> | 47 | /// </summary> |
| 48 | public const string DefaultSessionToken = "session"; | 48 | public const string DefaultSessionToken = "x-gsf-auth"; |
| 49 | 49 | ||
| 50 | #endregion | 50 | #endregion |
| 51 | 51 | ||