Skip to content

Commit b7dadb5

Browse files
authored
Merge pull request #231 from dotnetcore/bugfix/fix-230
Fixbug #230
2 parents 151ea3f + af23bb5 commit b7dadb5

File tree

4 files changed

+61
-44
lines changed

4 files changed

+61
-44
lines changed

src/AgileConfig.Server.Apisite/AgileConfig.Server.Apisite.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
<PropertyGroup>
44
<TargetFramework>net10.0</TargetFramework>
55
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
6-
<AssemblyVersion>1.11.2</AssemblyVersion>
7-
<Version>1.11.2</Version>
8-
<PackageVersion>1.11.2</PackageVersion>
6+
<AssemblyVersion>1.11.3</AssemblyVersion>
7+
<Version>1.11.3</Version>
8+
<PackageVersion>1.11.3</PackageVersion>
99
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
10-
<FileVersion>1.11.2</FileVersion>
10+
<FileVersion>1.11.3</FileVersion>
1111
<Authors>kklldog</Authors>
1212
<Company>kklldog</Company>
1313
</PropertyGroup>

src/AgileConfig.Server.Apisite/Controllers/api/ConfigController.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
5-
using AgileConfig.Server.Apisite.Controllers.api.Models;
1+
using AgileConfig.Server.Apisite.Controllers.api.Models;
62
using AgileConfig.Server.Apisite.Filters;
73
using AgileConfig.Server.Apisite.Metrics;
84
using AgileConfig.Server.Apisite.Models;
95
using AgileConfig.Server.Apisite.Models.Mapping;
6+
using AgileConfig.Server.Common;
107
using AgileConfig.Server.Data.Entity;
118
using AgileConfig.Server.IService;
129
using Microsoft.AspNetCore.Http;
1310
using Microsoft.AspNetCore.Mvc;
1411
using Microsoft.Extensions.Caching.Memory;
12+
using System;
13+
using System.Collections.Generic;
14+
using System.Linq;
15+
using System.Threading.Tasks;
1516

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

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

58+
var idInHeader = Encrypt.UnboxBasicAuth(HttpContext.Request).Item1;
59+
60+
if (appId != idInHeader)
61+
{
62+
await Response.WriteAsync("The AppId does not match the ID in Basic Authentication.");
63+
return BadRequest();
64+
}
65+
5766
var app = await _appService.GetAsync(appId);
5867
if (!app.Enabled) return NotFound();
5968

src/AgileConfig.Server.Common/Encrypt.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
2+
using System.Linq;
23
using System.Security.Cryptography;
34
using System.Text;
45
using System.Threading;
6+
using Microsoft.AspNetCore.Http;
57

68
namespace AgileConfig.Server.Common;
79

@@ -15,4 +17,43 @@ public static string Md5(string txt)
1517
var hashBytes = Md5Instance.Value.ComputeHash(inputBytes);
1618
return Convert.ToHexString(hashBytes);
1719
}
20+
21+
public static (string, string) UnboxBasicAuth(HttpRequest httpRequest)
22+
{
23+
var authorization = httpRequest.Headers["Authorization"];
24+
if (string.IsNullOrEmpty(authorization)) return ("", "");
25+
var authStr = authorization.First();
26+
// Remove the "Basic " prefix.
27+
if (!authStr.StartsWith("Basic "))
28+
{
29+
return ("", "");
30+
;
31+
}
32+
33+
authStr = authStr.Substring(6, authStr.Length - 6);
34+
byte[] base64Decode = null;
35+
try
36+
{
37+
base64Decode = Convert.FromBase64String(authStr);
38+
}
39+
catch
40+
{
41+
return ("", "");
42+
}
43+
44+
var base64Str = Encoding.UTF8.GetString(base64Decode);
45+
46+
if (string.IsNullOrEmpty(base64Str)) return ("", "");
47+
48+
var appId = "";
49+
var sec = "";
50+
51+
52+
var baseAuthArr = base64Str.Split(':');
53+
54+
if (baseAuthArr.Length > 0) appId = baseAuthArr[0];
55+
if (baseAuthArr.Length > 1) sec = baseAuthArr[1];
56+
57+
return (appId, sec);
58+
}
1859
}

src/AgileConfig.Server.Service/AppBasicAuthService.cs

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Linq;
33
using System.Text;
44
using System.Threading.Tasks;
5+
using AgileConfig.Server.Common;
56
using AgileConfig.Server.IService;
67
using Microsoft.AspNetCore.Http;
78

@@ -23,41 +24,7 @@ public AppBasicAuthService(IAppService appService)
2324
/// <returns>Tuple of Application ID and secret extracted from the header.</returns>
2425
public (string, string) GetAppIdSecret(HttpRequest httpRequest)
2526
{
26-
var authorization = httpRequest.Headers["Authorization"];
27-
if (string.IsNullOrEmpty(authorization)) return ("", "");
28-
var authStr = authorization.First();
29-
// Remove the "Basic " prefix.
30-
if (!authStr.StartsWith("Basic "))
31-
{
32-
return ("", "");
33-
;
34-
}
35-
36-
authStr = authStr.Substring(6, authStr.Length - 6);
37-
byte[] base64Decode = null;
38-
try
39-
{
40-
base64Decode = Convert.FromBase64String(authStr);
41-
}
42-
catch
43-
{
44-
return ("", "");
45-
}
46-
47-
var base64Str = Encoding.UTF8.GetString(base64Decode);
48-
49-
if (string.IsNullOrEmpty(base64Str)) return ("", "");
50-
51-
var appId = "";
52-
var sec = "";
53-
54-
55-
var baseAuthArr = base64Str.Split(':');
56-
57-
if (baseAuthArr.Length > 0) appId = baseAuthArr[0];
58-
if (baseAuthArr.Length > 1) sec = baseAuthArr[1];
59-
60-
return (appId, sec);
27+
return Encrypt.UnboxBasicAuth(httpRequest);
6128
}
6229

6330
public async Task<bool> ValidAsync(HttpRequest httpRequest)

0 commit comments

Comments
 (0)