Skip to content

Commit

Permalink
Todos.
Browse files Browse the repository at this point in the history
  • Loading branch information
Utar94 committed Jul 11, 2024
1 parent 3120100 commit 264a23c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 28 deletions.
11 changes: 11 additions & 0 deletions backend/src/Logitar.Cms.Core/Caching/CachingExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Logitar.Cms.Contracts.Configurations;

namespace Logitar.Cms.Core.Caching;

public static class CachingExtensions
{
public static Configuration GetConfiguration(this ICacheService cache)
{
return cache.Configuration ?? throw new InvalidOperationException("The configuration was not found in the cache.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ internal class OpenAuthenticationService : IOpenAuthenticationService
private readonly OpenAuthenticationSettings _settings;
private readonly ITokenManager _tokenManager;

private string? _baseUrl = null;
private string BaseUrl
{
get
{
if (_baseUrl == null)
{
HttpContext context = _httpContextAccessor.HttpContext ?? throw new InvalidOperationException($"The {nameof(_httpContextAccessor.HttpContext)} is required.");
_baseUrl = context.GetBaseUri().ToString();
}
return _baseUrl;
}
}

private Configuration Configuration => _cacheService.GetConfiguration();

public OpenAuthenticationService(
ICacheService cacheService,
IHttpContextAccessor httpContextAccessor,
Expand All @@ -34,16 +50,13 @@ public OpenAuthenticationService(

public async Task<TokenResponse> GetTokenResponseAsync(Session session, CancellationToken cancellationToken)
{
string baseUrl = _httpContextAccessor.HttpContext?.GetBaseUri().ToString() ?? throw new InvalidOperationException("The HttpContext is required."); // TODO(fpion): refactor
Configuration configuration = _cacheService.Configuration ?? throw new InvalidOperationException("The configuration was not found in the cache."); // TODO(fpion): refactor
int expiresIn = _settings.AccessToken.LifetimeSeconds;

ClaimsIdentity subject = session.CreateAccessTokenIdentity();
CreatedToken access = await _tokenManager.CreateAsync(new CreateTokenParameters(subject, configuration.Secret)
int expiresIn = _settings.AccessToken.LifetimeSeconds;
CreatedToken access = await _tokenManager.CreateAsync(new CreateTokenParameters(subject, Configuration.Secret)
{
Audience = baseUrl,
Audience = BaseUrl,
Expires = DateTime.UtcNow.AddSeconds(expiresIn),
Issuer = baseUrl,
Issuer = BaseUrl,
Type = _settings.AccessToken.Type
}, cancellationToken);

Expand All @@ -56,23 +69,21 @@ public async Task<TokenResponse> GetTokenResponseAsync(Session session, Cancella

public async Task<Session> ValidateTokenAsync(string token, CancellationToken cancellationToken)
{
string baseUrl = _httpContextAccessor.HttpContext?.GetBaseUri().ToString() ?? throw new InvalidOperationException("The HttpContext is required."); // TODO(fpion): refactor
Configuration configuration = _cacheService.Configuration ?? throw new InvalidOperationException("The configuration was not found in the cache."); // TODO(fpion): refactor

ValidatedToken validatedToken = await _tokenManager.ValidateAsync(new ValidateTokenParameters(token, configuration.Secret)
ValidatedToken validatedToken = await _tokenManager.ValidateAsync(new ValidateTokenParameters(token, Configuration.Secret)
{
ValidAudiences = [baseUrl],
ValidIssuers = [baseUrl],
ValidAudiences = [BaseUrl],
ValidIssuers = [BaseUrl],
ValidTypes = [_settings.AccessToken.Type]
}, cancellationToken);

Claim[] claims = (validatedToken.ClaimsPrincipal.FindAll(Rfc7519ClaimNames.SessionId)).ToArray();
if (claims.Length != 1)
IEnumerable<Claim> claims = validatedToken.ClaimsPrincipal.FindAll(Rfc7519ClaimNames.SessionId);
int count = claims.Count();
var sessionId = count switch
{
throw new InvalidOperationException($"The access token did contain {claims.Length} session identifier claims."); // TODO(fpion): implement
}

Guid sessionId = Guid.Parse(claims.Single().Value);
return await _sessionQuerier.ReadAsync(sessionId, cancellationToken) ?? throw new InvalidOperationException($"The session 'Id={sessionId}' could not be found."); // TODO(fpion): refactor
0 => throw new InvalidOperationException($"The access token did not contain any '{Rfc7519ClaimNames.SessionId}' claim."),
1 => Guid.Parse(claims.Single().Value),
_ => throw new InvalidOperationException($"The access token did contain many ({count}) '{Rfc7519ClaimNames.SessionId}' claims."),
};
return await _sessionQuerier.ReadAsync(sessionId, cancellationToken) ?? throw new InvalidOperationException($"The session 'Id={sessionId}' could not be found.");
}
}
6 changes: 2 additions & 4 deletions backend/src/Logitar.Cms.Web/HttpActivityContextResolver.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Logitar.Cms.Contracts.Configurations;
using Logitar.Cms.Core;
using Logitar.Cms.Core;
using Logitar.Cms.Core.Caching;

namespace Logitar.Cms.Web;
Expand All @@ -9,7 +8,6 @@ public class HttpActivityContextResolver : IActivityContextResolver
private readonly ICacheService _cacheService;
private readonly IHttpContextAccessor _httpContextAccessor;

private Configuration Configuration => _cacheService.Configuration ?? throw new InvalidOperationException("The configuration was not found in the cache.");
private HttpContext HttpContext => _httpContextAccessor.HttpContext ?? throw new InvalidOperationException($"The {nameof(_httpContextAccessor.HttpContext)} is required.");

public HttpActivityContextResolver(ICacheService cacheService, IHttpContextAccessor httpContextAccessor)
Expand All @@ -20,7 +18,7 @@ public HttpActivityContextResolver(ICacheService cacheService, IHttpContextAcces

public Task<ActivityContext> ResolveAsync(CancellationToken cancellationToken)
{
ActivityContext context = new(Configuration, HttpContext.GetApiKey(), HttpContext.GetSession(), HttpContext.GetUser());
ActivityContext context = new(_cacheService.GetConfiguration(), HttpContext.GetApiKey(), HttpContext.GetSession(), HttpContext.GetUser());
return Task.FromResult(context);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Logitar.Cms.Contracts.Configurations;
using Logitar.Cms.Core.Caching;
using Logitar.Cms.Core.Caching;

namespace Logitar.Cms.Core;

Expand All @@ -16,8 +15,7 @@ public TestActivityContextResolver(ICacheService cacheService, TestContext conte

public Task<ActivityContext> ResolveAsync(CancellationToken cancellationToken)
{
Configuration configuration = _cacheService.Configuration ?? throw new InvalidOperationException("The configuration was not found in the cache.");
ActivityContext context = new(configuration, ApiKey: null, Session: null, _context.User);
ActivityContext context = new(_cacheService.GetConfiguration(), ApiKey: null, Session: null, _context.User);
return Task.FromResult(context);
}
}

0 comments on commit 264a23c

Please sign in to comment.