Skip to content

Commit 72fdf23

Browse files
committed
Optional token exchange in server
1 parent fa934b9 commit 72fdf23

File tree

5 files changed

+475
-354
lines changed

5 files changed

+475
-354
lines changed

examples/TokenTool/TokenTool.Core/TokenTool.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,8 @@ public async Task RunAsync(MyApp.IOConsole ioConsole)
345345
"",
346346
"basyx",
347347
"assetfox",
348-
"factory-x"
348+
"factory-x",
349+
"fa3st"
349350
];
350351
for (var i = 0; i < configUrlList.Count; i++)
351352
{

src/AasSecurity/SecurityService.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,57 @@ private string HandleBearerToken(string? bearerToken, ref string user, ref bool
474474
valid = true;
475475
}
476476
if (!valid)
477+
{
478+
var tokenExchange1 = System.Environment.GetEnvironmentVariable("TOKENEXCHANGE1");
479+
var tokenExchange2 = System.Environment.GetEnvironmentVariable("TOKENEXCHANGE2");
480+
if (!string.IsNullOrEmpty(tokenExchange1) && !string.IsNullOrEmpty(tokenExchange2)
481+
&& iss == tokenExchange1)
482+
{
483+
var handlerExchange = new HttpClientHandler { DefaultProxyCredentials = CredentialCache.DefaultCredentials };
484+
var client = new HttpClient(handlerExchange);
485+
486+
JsonDocument doc;
487+
var parameters = new Dictionary<string, string>
488+
{
489+
{ "grant_type", "urn:ietf:params:oauth:grant-type:token-exchange" },
490+
{ "subject_token_type", "urn:ietf:params:oauth:token-type:jwt" },
491+
{ "requested_token_type", "urn:ietf:params:oauth:token-type:access_token" },
492+
{ "subject_token", bearerToken },
493+
{ "audience", "fa3st" }
494+
};
495+
var request = new HttpRequestMessage(HttpMethod.Post, $"{tokenExchange2}/token")
496+
{
497+
Content = new FormUrlEncodedContent(parameters)
498+
};
499+
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
500+
501+
var response = client.SendAsync(request);
502+
var content = response.GetAwaiter().GetResult().Content.ContentToString();
503+
504+
bearerToken = "";
505+
jwtSecurityToken = null;
506+
doc = JsonDocument.Parse(content);
507+
if (doc.RootElement.TryGetProperty("access_token", out var tokenElement))
508+
{
509+
bearerToken = tokenElement.GetString();
510+
Console.WriteLine("token exchange " + bearerToken);
511+
jwtSecurityToken = handler.ReadJwtToken(bearerToken);
512+
513+
iss = "";
514+
issClaim = jwtSecurityToken.Claims.Where(c => c.Type == "iss");
515+
if (issClaim.Any())
516+
{
517+
iss = issClaim.First().Value;
518+
}
519+
}
520+
if (jwtSecurityToken == null)
521+
{
522+
user = "";
523+
return "";
524+
}
525+
}
526+
}
527+
if (!valid)
477528
{
478529
if (jwtSecurityToken.Header.TryGetValue("kid", out _))
479530
{

src/AasSecurity/SecuritySettingsForServerParser.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -217,29 +217,32 @@ private static void ParseAuthenticationServer(AdminShellPackageEnv env, Submodel
217217
.Select(sp => (string)sp)
218218
.FirstOrDefault()
219219
})
220-
.FirstOrDefault()
220+
.ToList()
221221
})
222222
.ToList();
223223

224224
foreach (var provider in providers)
225225
{
226-
var serverName = provider?.Service?.Name;
227-
Console.WriteLine(" serverName: " + serverName);
226+
foreach (var service in provider?.Service)
227+
{
228+
var serverName = service?.Name;
229+
Console.WriteLine(" serverName: " + serverName);
228230

229-
var domain = provider?.Domain;
230-
Console.WriteLine(" domain: " + domain);
231+
var domain = provider?.Domain;
232+
Console.WriteLine(" domain: " + domain);
231233

232-
var jwks = provider?.Service?.SupplyPoint;
233-
Console.WriteLine(" jwks: " + jwks);
234+
var jwks = service?.SupplyPoint;
235+
Console.WriteLine(" jwks: " + jwks);
234236

235-
var kid = "";
237+
var kid = "";
236238

237-
GlobalSecurityVariables.ServerCertificates.Add(null);
238-
GlobalSecurityVariables.ServerCertFileNames.Add("");
239-
GlobalSecurityVariables.ServerCertFileNames.Add(serverName + ".cer");
240-
GlobalSecurityVariables.ServerDomain.Add(domain);
241-
GlobalSecurityVariables.ServerJwksUrl.Add(jwks);
242-
GlobalSecurityVariables.ServerKid.Add(kid);
239+
GlobalSecurityVariables.ServerCertificates.Add(null);
240+
GlobalSecurityVariables.ServerCertFileNames.Add("");
241+
GlobalSecurityVariables.ServerCertFileNames.Add(serverName + ".cer");
242+
GlobalSecurityVariables.ServerDomain.Add(domain);
243+
GlobalSecurityVariables.ServerJwksUrl.Add(jwks);
244+
GlobalSecurityVariables.ServerKid.Add(kid);
245+
}
243246
}
244247
}
245248
if (authServer == null || authServer.Value == null)

src/AasxServerBlazor/Properties/launchSettings.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
},
1111
"AasxServerBlazor": {
1212
"commandName": "Project",
13-
"commandLineArgs": "--with-db --start-index 0 --secret-string-api 1234 --data-path \"C:\\Users\\ee924l\\Documents\\aas\\mqtt\" --edit --external-blazor http://localhost:5001",
13+
"commandLineArgs": "--with-db --start-index 1000 --secret-string-api 1234 --data-path \"C:\\Development\\p5\" --edit --external-blazor http://localhost:5001",
1414
"launchBrowser": true,
1515
"environmentVariables": {
1616
"ASPNETCORE_ENVIRONMENT": "Development",
@@ -19,7 +19,8 @@
1919
"Kestrel__Endpoints__Http__Url": "http://localhost:5001",
2020
"EVENT2": "http://localhost:5002/submodels/aHR0cHM6Ly9pNGQuZGUvVC8zMjA5NTEwL3N1Ym1vZGVsL05hbWVwbGF0ZS9yZWNlaXZlcg/events/EventElement1",
2121
"AASX_MQTT": "1",
22-
"REGISTRYOFREGISTRY": "https://plugfest3.aas-voyager.com/registry-descriptors"
22+
"TOKENEXCHANGE1": "https://iam-security-training.com/consumer/sts",
23+
"TOKENEXCHANGE2": "https://iam-security-training.com/provider/sts"
2324
},
2425
"applicationUrl": "http://localhost:5001",
2526
"jsWebView2Debugging": true

0 commit comments

Comments
 (0)