private string Read(HttpContext httpContext, SessionsMiddlewareSettings sessionManagerSettings) { var currentPath = httpContext.Request.Path.Value; var pathImpersonationEntry = GetCurrentPathImpersonationEntry(httpContext, sessionManagerSettings); if (pathImpersonationEntry != null) { var hasImpersonationSessions = sessionManagerSettings.ImpersonationInformation?.ImpersonationSessions?.Count > 0; if (hasImpersonationSessions == false) { throw new Exception($"Impersonation requested but no impersonation sessions specified"); } else { var candidateSessions = sessionManagerSettings.ImpersonationInformation.ImpersonationSessions.Where(a => a.Key.Equals(pathImpersonationEntry.ImpersonationSessionKey)).ToList(); if (candidateSessions.Count > 1) { throw new Exception($"{candidateSessions.Count} impersonation sessions with the key '{pathImpersonationEntry.ImpersonationSessionKey}' were found while only one is expected."); } else if (candidateSessions.Count == 0) { throw new Exception($"Could not find an impersonation session with the key '{pathImpersonationEntry.ImpersonationSessionKey}'"); } else { return(JsonConvert.SerializeObject(candidateSessions.Single())); } } } else { return(string.Empty); } }
public async Task <string> ReadAsync(HttpContext httpContext, SessionsMiddlewareSettings sessionManagerSettings) { var sessionKey = GetSessionKey(httpContext); var hasSessionKey = !string.IsNullOrEmpty(sessionKey); if (hasSessionKey) { var errorMessage = GetRequestValidationErrorsIfAny(); if (!string.IsNullOrEmpty(errorMessage)) { throw new Exception(errorMessage); } else { var sessionInformation = await new SessionFetcher(_httpClientFactory, _sessionsMiddlewareSetting).GetSessionAsync(sessionKey); if (new SessionValidator().IsValidSession(sessionKey, sessionInformation)) { return(sessionInformation.Value); } else { throw new Exception($"Your session has expired or is invalid."); } } } else { throw new Exception($"You are currently not authenticated. Please sign in to continue."); } }
public SessionsMiddleware(RequestDelegate next, IHttpClientFactory httpClientFactory, SessionsMiddlewareSettings sessionsMiddlewareSettings) { _next = next; _httpClientFactory = httpClientFactory; _sessionManagerSettings = sessionsMiddlewareSettings; InitializeSessionReaders(); }
private List <EndpointPath> GetImpersonationMatches(HttpContext httpContext, SessionsMiddlewareSettings sessionManagerSettings, string currentPath) { var exactMatches = sessionManagerSettings.ImpersonationInformation.EndpointPaths.Where(a => a.Path.Equals(currentPath, StringComparison.InvariantCultureIgnoreCase)).ToList(); var hasExactMatches = exactMatches.Count > 0; if (hasExactMatches) { return(exactMatches); } else { var wildCardMatches = GetWildCardImpersonationMatchesIfRequired(httpContext, sessionManagerSettings, currentPath).ToList(); return(wildCardMatches); } }
public PersistantSessionsReader(IHttpClientFactory httpClientFactory, IOptions <SessionsMiddlewareSettings> sessionsMiddlewareSettings) { _httpClientFactory = httpClientFactory; _sessionsMiddlewareSettings = sessionsMiddlewareSettings.Value; }
public bool UseThisReader(HttpContext httpContext, SessionsMiddlewareSettings sessionManagerSettings) { return(true); }
public HeaderSessionReader(IHttpClientFactory httpClientFactory, SessionsMiddlewareSettings sessionsMiddlewareSetting) { _httpClientFactory = httpClientFactory; _sessionsMiddlewareSetting = sessionsMiddlewareSetting; }
public SessionFetcher(IHttpClientFactory httpClientFactory, SessionsMiddlewareSettings sessionsMiddlewareSettings) { _sessionsMiddlewareSettings = sessionsMiddlewareSettings; _httpClientFactory = httpClientFactory; }
private EndpointPath GetCurrentPathImpersonationEntry(HttpContext httpContext, SessionsMiddlewareSettings sessionManagerSettings) { var hasImpersonationList = sessionManagerSettings.ImpersonationInformation?.EndpointPaths?.Count > 0; if (hasImpersonationList && httpContext.Request.Path.HasValue) { var currentPath = httpContext.Request.Path.Value; var matches = GetImpersonationMatches(httpContext, sessionManagerSettings, currentPath); if (matches.Count > 1) { throw new Exception($"Found {matches.Count} impersonation entries for '{currentPath}'. Only one entry per path is allowed"); } else if (matches.Count == 1) { return(matches.Single()); } else { return(null); } } else { return(null); } }
private bool CurrentPathIsInImpersonationList(HttpContext httpContext, SessionsMiddlewareSettings sessionManagerSettings) { var pathImpersonationEntry = GetCurrentPathImpersonationEntry(httpContext, sessionManagerSettings); return(pathImpersonationEntry != null); }
public async Task <string> ReadAsync(HttpContext httpContext, SessionsMiddlewareSettings sessionManagerSettings) { return(await Task.Run(() => Read(httpContext, sessionManagerSettings))); }
public bool UseThisReader(HttpContext httpContext, SessionsMiddlewareSettings sessionManagerSettings) { return(CurrentPathIsInImpersonationList(httpContext, sessionManagerSettings)); }
private IEnumerable <EndpointPath> GetWildCardImpersonationMatchesIfRequired(HttpContext httpContext, SessionsMiddlewareSettings sessionManagerSettings, string currentPath) { const string wildCardSpecifier = "/*"; var wildCardImpersonations = sessionManagerSettings.ImpersonationInformation.EndpointPaths.Where(a => a.Path.EndsWith(wildCardSpecifier)).ToList(); foreach (var item in wildCardImpersonations) { var nonWildCardPart = item.Path.Substring(0, wildCardSpecifier.Length); var canBeTested = currentPath.Length >= nonWildCardPart.Length; if (canBeTested) { var isMatch = currentPath.Substring(0, nonWildCardPart.Length).Equals(nonWildCardPart, StringComparison.InvariantCultureIgnoreCase); if (isMatch) { yield return(item); } } } }