-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f7de910
commit 2b94993
Showing
5 changed files
with
81 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/web/Jordnaer/Features/Authentication/CookieContainerFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System.Net; | ||
using Microsoft.AspNetCore.Hosting.Server; | ||
using Microsoft.AspNetCore.Hosting.Server.Features; | ||
|
||
namespace Jordnaer.Features.Authentication; | ||
|
||
public sealed class CookieContainerFactory( | ||
ILogger<CookieContainerFactory> logger, | ||
IServer server, | ||
CurrentUser currentUser) | ||
{ | ||
public CookieContainer? Create() | ||
{ | ||
if (currentUser.Id is null) | ||
{ | ||
logger.LogDebug("CurrentUser is not logged in, cannot create an authenticated SignalR Connection."); | ||
return null; | ||
} | ||
|
||
if (currentUser.Cookie is null) | ||
{ | ||
logger.LogWarning("CurrentUser {UserId} does not have a cookie, cannot create an authenticated SignalR Connection.", currentUser.Id); | ||
return null; | ||
} | ||
|
||
var allServerUris = server.Features.Get<IServerAddressesFeature>()?.Addresses; | ||
logger.LogDebug("All server addresses: {@ServerAddresses}", allServerUris); | ||
|
||
var serverUri = server.Features.Get<IServerAddressesFeature>()?.Addresses.FirstOrDefault(); | ||
if (serverUri is null) | ||
{ | ||
logger.LogError("Failed to get server address from IServer"); | ||
return null; | ||
} | ||
|
||
var cookieContainer = new CookieContainer(1); | ||
|
||
if (!serverUri.Contains("[::]")) | ||
{ | ||
cookieContainer.Add(CreateCookie(new Uri(serverUri).Host)); | ||
return cookieContainer; | ||
} | ||
|
||
logger.LogInformation( | ||
"First server address was {ServerUri}, trying to get hostname from environment variable 'WEBSITE_HOSTNAME' instead.", serverUri); | ||
|
||
var domain = Environment.GetEnvironmentVariable("WEBSITE_HOSTNAME"); | ||
if (domain is null) | ||
{ | ||
logger.LogError("Cannot determine domain for Cookie, " + | ||
"environment variable 'WEBSITE_HOSTNAME' was null."); | ||
return null; | ||
} | ||
|
||
cookieContainer.Add(CreateCookie(domain)); | ||
|
||
return cookieContainer; | ||
} | ||
|
||
private Cookie CreateCookie(string domain) | ||
{ | ||
logger.LogInformation("Creating cookie with domain {Domain}", domain); | ||
|
||
return new Cookie(name: AuthenticationConstants.CookieName, | ||
value: currentUser.Cookie, | ||
path: "/", | ||
domain: domain) | ||
{ | ||
Secure = true, | ||
HttpOnly = true | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters