JaimeStill / decentralized-staffing

PoC for structuring microservice apps and automating their infrastructure in Azure
0 stars 0 forks source link

Cross-Service Token Ledger System #15

Open JaimeStill opened 1 year ago

JaimeStill commented 1 year ago

Before a staffing package can be submitted, the origin service will have to register with the Staffing service, which will issue a Registration token:

public class Registration
{
    public Guid Id { get; set; }
    public string Name { get; set; }
}

public class RegistrationService
{
        readonly AppDbContext db;

        public RegistrationService(AppDbContext db)
        {
            this.db = db;
        }

        public async Task<StaffingRegistration> RegisterService(string service)
        {
            Registration registration = new()
            {
                Id = GenerateGuid(),
                Name = service
            };

            await db.Registrations.AddAsync(registration);
            await db.SaveChangesAsync();
        }

        async Task<Guid> GenerateGuid()
        {
            Guid guid = Guid.NewGuid();

            while(await GuidExists(guid))
                guid = Guid.NewGuid();

            return guid;
        }

        async Task<bool> GuidExists(Guid guid) =>
            await db.Registrations.AnyAsync(x => x.Id == guid);
}

[Route("api/[controller]")]
public class RegistrationController : Controller
{
    [HttpPost("[action]/{service}")]
    public async Task<IActionResult> RegisterService([FromRoute]string service) =>
        Ok(await registrationSvc.RegisterService(service));
}

Once registered, all cross-service API endpoints will require the registration ID to be provided in the endpoint route. This will allow the staffing service to track issued tokens across services by associating them with a registration ID.

Tokens that can be issued by the staffing service: