Skip to content

Commit

Permalink
FEAT: Adding new methods to return realm configs
Browse files Browse the repository at this point in the history
  • Loading branch information
fmattioli committed Jul 7, 2024
1 parent 4bfae4b commit b502e28
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public static IServiceCollection AddKeyCloakAuth(this IServiceCollection service
);

services.AddSingleton<JwtSecurityTokenHandler>()
.AddSingleton(authSettings)
.AddScoped<IAuthService, AuthService>();

return services;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using Microsoft.AspNetCore.Http;

using Feijuca.Keycloak.MultiTenancy.Services.Models;
using Microsoft.AspNetCore.Http;
using System.IdentityModel.Tokens.Jwt;

namespace Feijuca.Keycloak.MultiTenancy.Services
{
public class AuthService(IHttpContextAccessor httpContextAccessor, JwtSecurityTokenHandler jwtSecurityTokenHandler) : IAuthService
public class AuthService(IHttpContextAccessor httpContextAccessor, JwtSecurityTokenHandler jwtSecurityTokenHandler, AuthSettings authSettings) : IAuthService
{
private readonly IHttpContextAccessor _httpContextAccessor = httpContextAccessor;
private readonly JwtSecurityTokenHandler _tokenHandler = jwtSecurityTokenHandler;
private readonly AuthSettings _authSettings = authSettings;

public string GetTenantFromToken()
{
Expand All @@ -25,6 +26,16 @@ public Guid GetUserFromToken()
return Guid.Parse(userClaim);
}

public string GetClientId()
{
return _authSettings.ClientId!;
}

public Realm GetRealm(string realmName)
{
return _authSettings.Realms.FirstOrDefault(r => r.Name == realmName)!;
}

private string GetToken()
{
var authorizationHeader = _httpContextAccessor.HttpContext!.Request.Headers.Authorization.FirstOrDefault();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
namespace Feijuca.Keycloak.MultiTenancy.Services
using Feijuca.Keycloak.MultiTenancy.Services.Models;

namespace Feijuca.Keycloak.MultiTenancy.Services
{
public interface IAuthService
{
string GetTenantFromToken();
string GetClientId();
Realm GetRealm(string realmName);
Guid GetUserFromToken();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public class AuthSettings
{
public string? ClientId { get; set; }
public string? ClientSecret { get; set; }
public string? Resource { get; set; }
public string? AuthServerUrl { get; set; }
public string? PolicyName { get; set; }
Expand Down

0 comments on commit b502e28

Please sign in to comment.