137 lines
4.6 KiB
C#
137 lines
4.6 KiB
C#
using Core;
|
|
using LiMan.APi.Data;
|
|
using LiMan.DB.DBModels;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Newtonsoft.Json;
|
|
using NLog;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace LiMan.APi.Controllers
|
|
{
|
|
/// <summary>
|
|
/// Controller livello APPLICAZIONE
|
|
/// </summary>
|
|
[Route("api/enroller")]
|
|
[ApiController]
|
|
public class EnrollerController : ControllerBase
|
|
{
|
|
#region Public Constructors
|
|
|
|
/// <summary>
|
|
/// Init generico
|
|
/// </summary>
|
|
/// <param name="DataService"></param>
|
|
public EnrollerController(ApiDataService DataService)
|
|
{
|
|
dataService = DataService;
|
|
Log.Info("Avviata classe ApplicazioneController");
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
/// <summary>
|
|
/// Recupera record di enroll di una richiesta x ricavarne ID licenza da applicare
|
|
/// </summary>
|
|
/// <param name="id">ID richiesta</param>
|
|
/// <param name="passcode">passcode associato</param>
|
|
/// <returns></returns>
|
|
[HttpGet("{id}")]
|
|
public async Task<EnrollRequestModel> Get(string id, int passcode)
|
|
{
|
|
string CodInst = "NA";
|
|
string CodApp = "Updater";
|
|
int reqId = 0;
|
|
int.TryParse(id, out reqId);
|
|
EnrollRequestModel reqRec = await dataService.EnrollReqGetById(reqId);
|
|
// solo se il passcode è corretto restituisco record, altrimenti fake one...
|
|
if (reqRec != null && reqRec.Passcode != passcode)
|
|
{
|
|
reqRec = new EnrollRequestModel() { IdReq = reqId };
|
|
}
|
|
await dataService.recordCall(CodInst, CodApp, $"GET:api/enroller/{id}");
|
|
return reqRec;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera record della licenza (mainKey) associata ad una richiesta date le sue info ID, passcode, ID licenza asegnata
|
|
/// </summary>
|
|
/// <param name="id">ID richiesta</param>
|
|
/// <param name="passcode">passcode associato</param>
|
|
/// <param name="idLic">ID licenza associato</param>
|
|
/// <returns></returns>
|
|
[HttpGet("getLicense/{id}")]
|
|
public async Task<LicenzaModel> GetLicData(string id, int passcode, int idLic)
|
|
{
|
|
string CodInst = "NA";
|
|
string CodApp = "Updater";
|
|
int reqId = 0;
|
|
int.TryParse(id, out reqId);
|
|
EnrollRequestModel reqRec = await dataService.EnrollReqGetById(reqId);
|
|
// init licenza non valida
|
|
LicenzaModel licRec = new LicenzaModel()
|
|
{
|
|
IdxLic = idLic,
|
|
Chiave = "",
|
|
CodInst = "NA",
|
|
CodApp = "None",
|
|
NumLicenze = 0,
|
|
Scadenza = DateTime.Today.AddYears(-1),
|
|
Payload = "",
|
|
Enigma = ""
|
|
};
|
|
// solo se sono corretti passcode e idLic corretto restituisco record, altrimenti fake one...
|
|
if (reqRec != null && reqRec.Passcode == passcode && reqRec.IdxLic == idLic)
|
|
{
|
|
licRec = await dataService.LicenzaById(idLic);
|
|
CodInst = licRec.CodInst;
|
|
CodApp = licRec.CodApp;
|
|
}
|
|
await dataService.recordCall(CodInst, CodApp, $"GET:api/enroller/getLicense/{id}");
|
|
return licRec;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Richiesta di un record con codice TOTP per l'enroll di un app client
|
|
/// </summary>
|
|
[HttpPost("getNewEnrollRec")]
|
|
public async Task<EnrollRequestModel> GetNewEnrollRec([FromBody] Dictionary<string, string> MachineInfo)
|
|
{
|
|
string CodInst = "NA";
|
|
string CodApp = "Updater";
|
|
var newRec = await dataService.EnrollReqCreate(MachineInfo);
|
|
await dataService.recordCall(CodInst, CodApp, $"GET:api/enroller/GetEnrollRec");
|
|
return newRec;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Protected Properties
|
|
|
|
/// <summary>
|
|
/// Dataservice x accesso DB
|
|
/// </summary>
|
|
protected ApiDataService dataService { get; set; }
|
|
|
|
#endregion Protected Properties
|
|
|
|
#region Private Fields
|
|
|
|
/// <summary>
|
|
/// Classe per logging
|
|
/// </summary>
|
|
private static Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
/// <summary>
|
|
/// Generatore pseudocasuale
|
|
/// </summary>
|
|
private Random rnd = new Random();
|
|
|
|
#endregion Private Fields
|
|
}
|
|
} |