Add basic auth to power bi endpoint

This commit is contained in:
Michał Zieliski
2024-06-06 20:25:20 +02:00
parent a5c91c1e88
commit e19d6a8929
4 changed files with 21 additions and 5 deletions

View File

@@ -1,4 +1,5 @@
using System.Globalization;
using System.Text;
using DiunaBIWebAPI.dataImporters;
using Google.Apis.Sheets.v4;
using Microsoft.AspNetCore.Authorization;
@@ -96,7 +97,7 @@ namespace WebAPI.Controllers
}
}
[HttpGet]
[Route("getByNumber/{apiKey}/{number}")]
[Route("getForPowerBI/{apiKey}/{number}")]
public IActionResult GetByNumber(string apiKey, int number)
{
if (apiKey != configuration["apiKey"])
@@ -105,11 +106,23 @@ namespace WebAPI.Controllers
}
if (
!Request.Headers.TryGetValue("D-BI-AUTH", out var authKey)
|| authKey != configuration["authKey"])
!Request.Headers.TryGetValue("Authorization", out var authHeader))
{
return Unauthorized();
}
string[] credentialsArr = authHeader.ToString().Split(" ");
if (credentialsArr.Length != 2)
{
return Unauthorized();
}
var authValue = Encoding.UTF8.GetString(Convert.FromBase64String(credentialsArr[1]));
var username = authValue.Split(':')[0];
var password = authValue.Split(':')[1];
if (username != configuration["powerBI-user"] || password != configuration["powerBI-pass"])
{
return Unauthorized();
}
try
{
return Ok(db.Layers