Skip to content

Commit 77aac78

Browse files
author
agile.zhou
committed
Merge branch 'master' into publish
2 parents 4f4f1fe + a766175 commit 77aac78

7 files changed

Lines changed: 97 additions & 56 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# Change log
22
------------------------------
3-
[Unreleased]
4-
* Introduced dynamic role management with CRUD APIs and UI allowing custom permission assignments.
3+
[1.11.2]
4+
* Fixbug #228
5+
6+
[1.11.1]
7+
* Role base access control
58

69
[1.10.0]
710
* Use publish timeline virtual id to compare the version between client. To enable this feature the client should use version >=1.8.0 .

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/AgileConfig.Server.Common.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
<ItemGroup>
88
<FrameworkReference Include="Microsoft.AspNetCore.App" />
9-
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="10.0.0" />
10-
<PackageReference Include="Microsoft.Extensions.Logging" Version="10.0.0" />
119
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
1210
</ItemGroup>
1311

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)

test/ApiSiteTests/TestApiConfigController.cs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
using System.Collections.Generic;
2-
using System.Threading.Tasks;
31
using AgileConfig.Server.Apisite.Controllers;
42
using AgileConfig.Server.Apisite.Controllers.api.Models;
53
using AgileConfig.Server.Apisite.Metrics;
64
using AgileConfig.Server.Apisite.Models;
75
using AgileConfig.Server.Common.EventBus;
86
using AgileConfig.Server.Data.Entity;
97
using AgileConfig.Server.IService;
8+
using Microsoft.AspNetCore.Http;
109
using Microsoft.AspNetCore.Mvc;
1110
using Microsoft.Extensions.Caching.Memory;
1211
using Microsoft.VisualStudio.TestTools.UnitTesting;
1312
using Moq;
13+
using System.Collections.Generic;
14+
using System.Threading.Tasks;
1415

1516
namespace ApiSiteTests;
1617

1718
[TestClass]
1819
public class TestApiConfigController
1920
{
2021
[TestMethod]
21-
public async Task TestGet()
22+
public async Task GetAppConfig_WithValidApp_ReturnsConfigs()
2223
{
2324
App newApp()
2425
{
@@ -55,8 +56,6 @@ List<Config> newConfigs()
5556
}
5657

5758
var configService = new Mock<IConfigService>();
58-
//configService.Setup(s => s.GetPublishedConfigsAsync("001"))
59-
// .ReturnsAsync(newConfigs);
6059
configService.Setup(s => s.GetPublishedConfigsByAppIdWithInheritance(It.IsAny<string>(), It.IsAny<string>()))
6160
.ReturnsAsync(newConfigs);
6261

@@ -65,20 +64,32 @@ List<Config> newConfigs()
6564
var eventBus = new Mock<ITinyEventBus>();
6665
var meterService = new Mock<IMeterService>();
6766

67+
var httpContext = new DefaultHttpContext();
68+
httpContext.Request.Headers["Authorization"] = "Basic MDAxOjE=";
69+
6870
var ctrl = new AgileConfig.Server.Apisite.Controllers.api.ConfigController(
6971
configService.Object,
7072
appService.Object,
7173
memoryCache,
7274
meterService.Object,
7375
new ConfigController(configService.Object, appService.Object, userSErvice.Object, eventBus.Object)
7476
);
77+
ctrl.ControllerContext = new ControllerContext
78+
{
79+
HttpContext = httpContext
80+
};
81+
7582
var act = await ctrl.GetAppConfig("001", new EnvString { Value = "DEV" });
7683

7784
Assert.IsNotNull(act);
7885
Assert.IsNotNull(act.Value);
7986
Assert.IsInstanceOfType(act.Value, typeof(List<ApiConfigVM>));
8087
Assert.AreEqual(2, act.Value.Count);
88+
}
8189

90+
[TestMethod]
91+
public async Task GetAppConfig_WithDisabledApp_ReturnsNotFound()
92+
{
8293
App newApp1()
8394
{
8495
return new App
@@ -87,17 +98,29 @@ App newApp1()
8798
};
8899
}
89100

90-
appService = new Mock<IAppService>();
101+
var appService = new Mock<IAppService>();
91102
appService.Setup(s => s.GetAsync(It.IsAny<string>())).ReturnsAsync(newApp1);
103+
var configService = new Mock<IConfigService>();
104+
IMemoryCache memoryCache = null;
105+
var userSErvice = new Mock<IUserService>();
106+
var eventBus = new Mock<ITinyEventBus>();
107+
var meterService = new Mock<IMeterService>();
108+
var httpContext = new DefaultHttpContext();
109+
httpContext.Request.Headers["Authorization"] = "Basic MDAxOjE=";
92110

93-
ctrl = new AgileConfig.Server.Apisite.Controllers.api.ConfigController(
111+
var ctrl = new AgileConfig.Server.Apisite.Controllers.api.ConfigController(
94112
configService.Object,
95113
appService.Object,
96114
memoryCache,
97115
meterService.Object,
98116
new ConfigController(configService.Object, appService.Object, userSErvice.Object, eventBus.Object)
99117
);
100-
act = await ctrl.GetAppConfig("001", new EnvString { Value = "DEV" });
118+
ctrl.ControllerContext = new ControllerContext
119+
{
120+
HttpContext = httpContext
121+
};
122+
123+
var act = await ctrl.GetAppConfig("001", new EnvString { Value = "DEV" });
101124

102125
Assert.IsNotNull(act);
103126
Assert.IsNull(act.Value);

0 commit comments

Comments
 (0)