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
{
///
/// Controller livello APPLICAZIONE
///
[Route("api/enroller")]
[ApiController]
public class EnrollerController : ControllerBase
{
#region Public Constructors
///
/// Init generico
///
///
public EnrollerController(ApiDataService DataService)
{
dataService = DataService;
Log.Info("Avviata classe ApplicazioneController");
}
#endregion Public Constructors
#region Public Methods
///
/// Recupera record di enroll di una richiesta x ricavarne ID licenza da applicare
///
/// ID richiesta
/// passcode associato
///
[HttpGet("{id}")]
public async Task 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;
}
///
/// Recupera record della licenza (mainKey) associata ad una richiesta date le sue info ID, passcode, ID licenza asegnata
///
/// ID richiesta
/// passcode associato
/// ID licenza associato
///
[HttpGet("getLicense/{id}")]
public async Task 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;
}
///
/// Richiesta di un record con codice TOTP per l'enroll di un app client
///
[HttpPost("getNewEnrollRec")]
public async Task GetNewEnrollRec([FromBody] Dictionary 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
///
/// Dataservice x accesso DB
///
protected ApiDataService dataService { get; set; }
#endregion Protected Properties
#region Private Fields
///
/// Classe per logging
///
private static Logger Log = LogManager.GetCurrentClassLogger();
///
/// Generatore pseudocasuale
///
private Random rnd = new Random();
#endregion Private Fields
}
}