Skip to content

Commit

Permalink
Support get parameter for session auth, bump version 1.6.5
Browse files Browse the repository at this point in the history
  • Loading branch information
LucHeart committed Oct 6, 2023
1 parent 1565994 commit 61f4c57
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 16 deletions.
2 changes: 1 addition & 1 deletion API/API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<AssemblyName>OpenShock.API</AssemblyName>
<RootNamespace>OpenShock.API</RootNamespace>
<AssemblyVersion>1.6.4</AssemblyVersion>
<AssemblyVersion>1.6.5</AssemblyVersion>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Company>OpenShock</Company>
<Product>API</Product>
Expand Down
2 changes: 1 addition & 1 deletion API/Controller/Shockers/ControlLogController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ [FromQuery] [Range(1, 500)] uint limit = 100)
{
Id = Guid.Empty,
Name = "Guest",
Image = new Uri("https://www.gravatar.com/avatar/0"),
Image = new Uri("https://www.gravatar.com/avatar/0?d=https%3A%2F%2Fshocklink.net%2Fstatic%2Fimages%2FIcon512.png"),
CustomName = x.CustomName
}
: new ControlLogSenderLight
Expand Down
2 changes: 1 addition & 1 deletion API/Hubs/ShareLinkHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public override async Task OnConnectedAsync()
{
Id = Guid.Empty,
Name = "Guest",
Image = new Uri("https://www.gravatar.com/avatar/0"),
Image = new Uri("https://www.gravatar.com/avatar/0?d=https%3A%2F%2Fshocklink.net%2Fstatic%2Fimages%2FIcon512.png"),
ConnectionId = Context.ConnectionId,
CustomName = customName,
AdditionalItems = additionalItems
Expand Down
4 changes: 3 additions & 1 deletion API/Models/Response/LoginResponse.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace OpenShock.API.Models.Response;
using System.Text.Json.Serialization;

namespace OpenShock.API.Models.Response;

public class LoginResponse
{
Expand Down
2 changes: 1 addition & 1 deletion LiveControlGateway/LiveControlGateway.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<AssemblyName>OpenShock.LiveControlGateway</AssemblyName>
<RootNamespace>OpenShock.LiveControlGateway</RootNamespace>
<Company>OpenShock</Company>
<AssemblyVersion>1.6.4</AssemblyVersion>
<AssemblyVersion>1.6.5</AssemblyVersion>
<Product>LiveControlGateway</Product>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
Expand Down
26 changes: 15 additions & 11 deletions ServicesCommon/Authentication/LoginSessionAuthentication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,27 @@ public LoginSessionAuthentication(IOptionsMonitor<LoginSessionAuthenticationSche

protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{
if (Context.Request.Query.TryGetValue("session", out var getSession) &&
!string.IsNullOrEmpty(getSession)) return SessionAuth(getSession!);

if (Context.Request.Headers.TryGetValue("OpenShockSession", out var sessionKeyHeader) &&
!string.IsNullOrEmpty(sessionKeyHeader)) return SessionAuth(sessionKeyHeader!);

if (Context.Request.Headers.TryGetValue("ShockLinkToken", out var tokenHeader) &&
!string.IsNullOrEmpty(tokenHeader)) return TokenAuth(tokenHeader!);

if (Context.Request.Headers.TryGetValue("OpenShockToken", out var tokenHeaderO) &&
!string.IsNullOrEmpty(tokenHeaderO)) return TokenAuth(tokenHeaderO!);
return Task.FromResult(Fail("OpenShockSession/OpenShockToken header/cookie was not found"));

return Task.FromResult(Fail("OpenShockSession/OpenShockToken header/getparam was not found"));
}

private async Task<AuthenticateResult> TokenAuth(string token)
{
var tokenDto = await _db.ApiTokens.Include(x => x.User).SingleOrDefaultAsync(x => x.Token == token &&
(x.ValidUntil == null || x.ValidUntil >= DateOnly.FromDateTime(DateTime.UtcNow)));
if (tokenDto == null) return Fail("Token is not valid");

_authService.CurrentClient = new LinkUser
{
DbUser = tokenDto.User
Expand All @@ -66,10 +69,11 @@ private async Task<AuthenticateResult> TokenAuth(string token)
var claims = new List<Claim>
{
new(ClaimTypes.NameIdentifier, _authService.CurrentClient.DbUser.Id.ToString()),
new (ControlLogAdditionalItem.ApiTokenId, tokenDto.Id.ToString())
new(ControlLogAdditionalItem.ApiTokenId, tokenDto.Id.ToString())
};
claims.AddRange(tokenDto.Permissions.Select(tokenDtoPermission => PermissionTypeBindings.TypeToName[tokenDtoPermission]));

claims.AddRange(tokenDto.Permissions.Select(tokenDtoPermission =>
PermissionTypeBindings.TypeToName[tokenDtoPermission]));

var ident = new ClaimsIdentity(claims, nameof(LoginSessionAuthentication));
var ticket = new AuthenticationTicket(new ClaimsPrincipal(ident), Scheme.Name);

Expand All @@ -79,9 +83,9 @@ private async Task<AuthenticateResult> TokenAuth(string token)
private async Task<AuthenticateResult> SessionAuth(string sessionKey)
{
var session = await _userSessions.FindByIdAsync(sessionKey);
if(session == null) return Fail("Session was not found");
if (session == null) return Fail("Session was not found");

var retrievedUser = await _db.Users.FirstAsync(user => user.Id == session.UserId );
var retrievedUser = await _db.Users.FirstAsync(user => user.Id == session.UserId);

_authService.CurrentClient = new LinkUser
{
Expand All @@ -101,7 +105,7 @@ private async Task<AuthenticateResult> SessionAuth(string sessionKey)

return AuthenticateResult.Success(ticket);
}

private AuthenticateResult Fail(string reason)
{
_failReason = reason;
Expand Down

0 comments on commit 61f4c57

Please sign in to comment.