Skip to content

Commit

Permalink
FEAT: Adding new logic to use methods from IAuthService
Browse files Browse the repository at this point in the history
  • Loading branch information
fmattioli committed Jul 7, 2024
1 parent c615c64 commit e24b642
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
.AddEnvironmentVariables();

var applicationSettings = builder.Configuration.GetApplicationSettings(builder.Environment);
Console.WriteLine(applicationSettings);

builder.Services.AddSingleton<ISettings>(applicationSettings);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
"AuthSettings": {
"Realms": [
{
"Name": "smartconsig",
"Audience": "smartconsig-api",
"Issuer": "https://services-keycloak.ul0sru.easypanel.host/realms/smartconsig"
"Name": "",
"Audience": "",
"Issuer": ""
}
],
"ClientId": "smartconsig-api",
"Resource": "smartconsig-api",
"AuthServerUrl": "https://services-keycloak.ul0sru.easypanel.host",
"ClientSecret": "",
"ClientId": "",
"Resource": "",
"AuthServerUrl": "",
"PolicyName": "TokenManager",
"Roles": [
"TokenManager"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
using Feijuca.Keycloak.MultiTenancy.Services.Models;
using Feijuca.Keycloak.MultiTenancy.Services;
using Feijuca.Keycloak.MultiTenancy.Services.Models;
using Microsoft.Extensions.DependencyInjection;

using System;

using TokenManager.Domain.Interfaces;
using TokenManager.Infra.Data.Models;
using TokenManager.Infra.Data.Repositories;

namespace TokenManager.Infra.CrossCutting.Extensions
Expand All @@ -13,9 +18,20 @@ public static IServiceCollection AddRepositories(this IServiceCollection service
{
client.BaseAddress = new Uri(authSettings.AuthServerUrl!);
});


services.AddScoped<IUserRepository, UserRepository>();

var serviceProvider = services.BuildServiceProvider();
var authService = serviceProvider.GetRequiredService<IAuthService>();

var tokenCredentials = new TokenCredentials()
{
Client_Secret = authService.GetClientSecret(),
Client_Id = authService.GetClientId(),
ServerUrl = authService.GetServerUrl()
};

services.AddSingleton(tokenCredentials);
return services;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
namespace TokenManager.Domain.Entities
namespace TokenManager.Infra.Data.Models
{
public class TokenCredentials
{
public string ServerUrl { get; set; } = null!;
public string Grant_Type { get; set; } = null!;
public string Client_Secret { get; set; } = null!;
public string Client_Id { get; set; } = null!;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@
using TokenManager.Domain.Entities;
using TokenManager.Domain.Errors;
using TokenManager.Domain.Interfaces;
using TokenManager.Infra.Data.Models;

namespace TokenManager.Infra.Data.Repositories
{
public class UserRepository(IHttpClientFactory httpClientFactory, IAuthService authService) : IUserRepository
public class UserRepository(IHttpClientFactory httpClientFactory, IAuthService authService, TokenCredentials tokenCredentials) : IUserRepository
{
private readonly IHttpClientFactory _httpClientFactory = httpClientFactory;
private readonly IAuthService _authService = authService;
private readonly TokenCredentials tokenCredentials = new()
private readonly TokenCredentials _tokenCredentials = tokenCredentials;
private static readonly JsonSerializerSettings Settings = new()
{
Grant_Type = "client_credentials", //TODO: Create environmnet variable
Client_Secret = "qSGxtu0CFOmZ6Yzr5ntPK2iXppmKeerS", //TODO: Create environmnet variable
Client_Id = "smartconsig-api" //TODO: Create environmnet variable
ContractResolver = new CamelCasePropertyNamesContractResolver(),
NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore
};

public async Task<Result> CreateUserAsync(string tenant, User user)
Expand All @@ -37,13 +38,7 @@ public async Task<Result> CreateUserAsync(string tenant, User user)
.AppendPathSegment(tenant)
.AppendPathSegment("users");

var settings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore
};

var json = JsonConvert.SerializeObject(user, settings);
var json = JsonConvert.SerializeObject(user, Settings);
StringContent httpContent = new(json, Encoding.UTF8, "application/json");
var response = await client.PostAsync(url, httpContent);

Expand All @@ -63,11 +58,12 @@ public async Task<Result> CreateUserAsync(string tenant, User user)
public async Task<Result<TokenDetails>> GetAccessTokenAsync(string tenant)
{
var client = _httpClientFactory.CreateClient("KeycloakClient");

var requestData = new FormUrlEncodedContent(
[
new KeyValuePair<string, string>("grant_type", tokenCredentials.Grant_Type),
new KeyValuePair<string, string>("client_id", tokenCredentials.Client_Id),
new KeyValuePair<string, string>("client_secret", tokenCredentials.Client_Secret),
new KeyValuePair<string, string>("grant_type", "client_credentials"),
new KeyValuePair<string, string>("client_id", _tokenCredentials.Client_Id),
new KeyValuePair<string, string>("client_secret", _tokenCredentials.Client_Secret),
]);

var url = client.BaseAddress
Expand Down Expand Up @@ -97,8 +93,8 @@ public async Task<Result<TokenDetails>> LoginAsync(User user)
var requestData = new FormUrlEncodedContent(
[
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("client_id", tokenCredentials.Client_Id),
new KeyValuePair<string, string>("client_secret", tokenCredentials.Client_Secret),
new KeyValuePair<string, string>("client_id", _tokenCredentials.Client_Id),
new KeyValuePair<string, string>("client_secret", _tokenCredentials.Client_Secret),
new KeyValuePair<string, string>("username", user.Username!),
new KeyValuePair<string, string>("password", user.Password!),
new KeyValuePair<string, string>("scope", "tokenmanager-write tokenmanager-read"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Feijuca.Keycloak.MultiTenancy" Version="1.1.0" />
<PackageReference Include="Feijuca.Keycloak.MultiTenancy" Version="1.3.0" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit e24b642

Please sign in to comment.