Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<AssemblyVersion>1.11.2</AssemblyVersion>
<Version>1.11.2</Version>
<PackageVersion>1.11.2</PackageVersion>
<AssemblyVersion>1.11.3</AssemblyVersion>
<Version>1.11.3</Version>
<PackageVersion>1.11.3</PackageVersion>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<FileVersion>1.11.2</FileVersion>
<FileVersion>1.11.3</FileVersion>
<Authors>kklldog</Authors>
<Company>kklldog</Company>
</PropertyGroup>
Expand Down
19 changes: 14 additions & 5 deletions src/AgileConfig.Server.Apisite/Controllers/api/ConfigController.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AgileConfig.Server.Apisite.Controllers.api.Models;
using AgileConfig.Server.Apisite.Controllers.api.Models;
using AgileConfig.Server.Apisite.Filters;
using AgileConfig.Server.Apisite.Metrics;
using AgileConfig.Server.Apisite.Models;
using AgileConfig.Server.Apisite.Models.Mapping;
using AgileConfig.Server.Common;
using AgileConfig.Server.Data.Entity;
using AgileConfig.Server.IService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

Expand Down Expand Up @@ -54,6 +55,14 @@ public async Task<ActionResult<List<ApiConfigVM>>> GetAppConfig(string appId, [F
{
ArgumentException.ThrowIfNullOrEmpty(appId);

var idInHeader = Encrypt.UnboxBasicAuth(HttpContext.Request).Item1;

if (appId != idInHeader)
{
await Response.WriteAsync("The AppId does not match the ID in Basic Authentication.");
return BadRequest();
}

var app = await _appService.GetAsync(appId);
if (!app.Enabled) return NotFound();

Expand Down
41 changes: 41 additions & 0 deletions src/AgileConfig.Server.Common/Encrypt.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using Microsoft.AspNetCore.Http;

namespace AgileConfig.Server.Common;

Expand All @@ -15,4 +17,43 @@ public static string Md5(string txt)
var hashBytes = Md5Instance.Value.ComputeHash(inputBytes);
return Convert.ToHexString(hashBytes);
}

public static (string, string) UnboxBasicAuth(HttpRequest httpRequest)
{
var authorization = httpRequest.Headers["Authorization"];
if (string.IsNullOrEmpty(authorization)) return ("", "");
var authStr = authorization.First();
// Remove the "Basic " prefix.
if (!authStr.StartsWith("Basic "))
{
return ("", "");
;
}

authStr = authStr.Substring(6, authStr.Length - 6);
byte[] base64Decode = null;
try
{
base64Decode = Convert.FromBase64String(authStr);
}
catch
{
return ("", "");
}

var base64Str = Encoding.UTF8.GetString(base64Decode);

if (string.IsNullOrEmpty(base64Str)) return ("", "");

var appId = "";
var sec = "";


var baseAuthArr = base64Str.Split(':');

if (baseAuthArr.Length > 0) appId = baseAuthArr[0];
if (baseAuthArr.Length > 1) sec = baseAuthArr[1];

return (appId, sec);
}
}
37 changes: 2 additions & 35 deletions src/AgileConfig.Server.Service/AppBasicAuthService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AgileConfig.Server.Common;
using AgileConfig.Server.IService;
using Microsoft.AspNetCore.Http;

Expand All @@ -23,41 +24,7 @@ public AppBasicAuthService(IAppService appService)
/// <returns>Tuple of Application ID and secret extracted from the header.</returns>
public (string, string) GetAppIdSecret(HttpRequest httpRequest)
{
var authorization = httpRequest.Headers["Authorization"];
if (string.IsNullOrEmpty(authorization)) return ("", "");
var authStr = authorization.First();
// Remove the "Basic " prefix.
if (!authStr.StartsWith("Basic "))
{
return ("", "");
;
}

authStr = authStr.Substring(6, authStr.Length - 6);
byte[] base64Decode = null;
try
{
base64Decode = Convert.FromBase64String(authStr);
}
catch
{
return ("", "");
}

var base64Str = Encoding.UTF8.GetString(base64Decode);

if (string.IsNullOrEmpty(base64Str)) return ("", "");

var appId = "";
var sec = "";


var baseAuthArr = base64Str.Split(':');

if (baseAuthArr.Length > 0) appId = baseAuthArr[0];
if (baseAuthArr.Length > 1) sec = baseAuthArr[1];

return (appId, sec);
return Encrypt.UnboxBasicAuth(httpRequest);
}

public async Task<bool> ValidAsync(HttpRequest httpRequest)
Expand Down
Loading