Skip to content

Commit 4459d1d

Browse files
committed
Get <img> with bearer token
1 parent fa1c56f commit 4459d1d

File tree

4 files changed

+153
-29
lines changed

4 files changed

+153
-29
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/********************************************************************************
2+
* Copyright (c) {2019 - 2024} Contributors to the Eclipse Foundation
3+
*
4+
* See the NOTICE file(s) distributed with this work for additional
5+
* information regarding copyright ownership.
6+
*
7+
* This program and the accompanying materials are made available under the
8+
* terms of the Apache License Version 2.0 which is available at
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* SPDX-License-Identifier: Apache-2.0
12+
********************************************************************************/
13+
14+
using System;
15+
using System.Linq;
16+
using System.Net;
17+
using System.Net.Http;
18+
using System.Net.Http.Headers;
19+
using System.Threading.Tasks;
20+
using Microsoft.AspNetCore.Authentication.BearerToken;
21+
using Microsoft.AspNetCore.Mvc;
22+
using Microsoft.IdentityModel.Tokens;
23+
24+
namespace AasxServerBlazor.Controller
25+
{
26+
[ApiController]
27+
public class ImageController : ControllerBase
28+
{
29+
[HttpGet]
30+
[Route("/blazor/image/{id}")]
31+
public async Task<IActionResult> GetImage(string id)
32+
{
33+
id = Base64UrlEncoder.Decode(id);
34+
35+
if (!id.StartsWith("$$"))
36+
{
37+
return NotFound();
38+
}
39+
40+
var split = id.Split("$$");
41+
id = split[1];
42+
43+
var bearer = "";
44+
var basicAuth = "";
45+
if (split.Length == 4)
46+
{
47+
switch (split[2])
48+
{
49+
case "bearer":
50+
bearer = split[3];
51+
break;
52+
case "basicauth":
53+
basicAuth = split[3];
54+
break;
55+
default:
56+
break;
57+
}
58+
}
59+
60+
if (id.Contains("?bearerHead="))
61+
{
62+
split = id.Split("?bearerHead=");
63+
id = split[0];
64+
bearer = split[1];
65+
}
66+
67+
var handler = new HttpClientHandler
68+
{
69+
DefaultProxyCredentials = CredentialCache.DefaultCredentials
70+
};
71+
72+
var client = new HttpClient(handler);
73+
74+
var request = new HttpRequestMessage(HttpMethod.Get, id);
75+
76+
if (bearer != "")
77+
{
78+
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", bearer);
79+
}
80+
if (basicAuth != "")
81+
{
82+
request.Headers.Authorization = new AuthenticationHeaderValue("Basic", basicAuth);
83+
}
84+
85+
var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
86+
87+
if (!response.IsSuccessStatusCode)
88+
{
89+
return StatusCode((int)response.StatusCode);
90+
}
91+
92+
var contentType = response.Content.Headers.ContentType?.ToString() ?? "image/jpeg";
93+
var stream = await response.Content.ReadAsStreamAsync();
94+
95+
return new FileStreamResult(stream, contentType);
96+
}
97+
}
98+
}

src/AasxServerBlazor/Pages/Pcf2.razor

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@
267267
// <img src=data:image;base64,@productImage class="h-16" style="max-width: @maxWidthPx; object-fit: contain;" />
268268
productImage = getImageLink(cs.credentials, node.productImage, node.envIndex);
269269
if (productImage != null)
270-
{
270+
{
271271
<img src=@productImage class="h-16" style="max-width: @maxWidthPx; object-fit: contain;"/>
272272
}
273273
}
@@ -663,6 +663,10 @@
663663
;
664664
}
665665
}
666+
else
667+
{
668+
;
669+
}
666670
}
667671
else
668672
{
@@ -767,7 +771,11 @@
767771
path = path.Replace("http://", "http://" + userPW + "@");
768772
}
769773
}
770-
774+
775+
if (path.Contains("?bearerHead="))
776+
{
777+
path = "/blazor/image/"+Base64UrlEncoder.Encode("$$"+path);
778+
}
771779
return path;
772780
}
773781
catch

src/AasxServerStandardBib/Credentials.cs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,11 @@ public static bool get(AasxTaskService aasxTaskService, List<AasxCredentialsEntr
186186
qp.Add("bearer=" + cList[i].bearer);
187187
result = true;
188188
break;
189+
case "bearerHead":
190+
bearerCheckAndInit(cList[i], aasxTaskService);
191+
qp.Add("bearerHead=" + cList[i].bearer);
192+
result = true;
193+
break;
189194
case "querypara":
190195
if (cList[i].parameters.Count == 2)
191196
{
@@ -230,24 +235,24 @@ public static bool get(AasxTaskService aasxTaskService, List<AasxCredentialsEntr
230235

231236
public static bool keep(List<AasxCredentialsEntry> cList, string urlPath, bool blazor = false)
232237
{
233-
bool result = false;
238+
var result = false;
234239

235-
for (int i = 0; i < cList.Count; i++)
236-
{
237-
int lenPrefix = cList[i].urlPrefix.Length;
238-
int lenUrl = urlPath.Length;
239-
if (lenPrefix <= lenUrl)
240-
{
241-
string u = urlPath.Substring(0, lenPrefix);
242-
if (cList[i].urlPrefix == "*" || u == cList[i].urlPrefix)
243-
{
244-
switch (cList[i].type)
245-
{
246-
case "keeppath":
247-
result = true;
248-
break;
240+
for (var i = 0; i < cList.Count; i++)
241+
{
242+
switch (cList[i].type.ToLower())
243+
{
244+
case "keeppath":
245+
var lenPrefix = cList[i].urlPrefix.Length;
246+
var lenUrl = urlPath.Length;
247+
if (lenPrefix <= lenUrl)
248+
{
249+
var u = urlPath.Substring(0, lenPrefix);
250+
if (cList[i].urlPrefix == "*" || u == cList[i].urlPrefix)
251+
{
252+
result = true;
253+
}
249254
}
250-
}
255+
break;
251256
}
252257
}
253258
return result;

src/IO.Swagger.Registry.Lib.V3/Services/RegistryInitializerService.cs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -584,15 +584,19 @@ public async Task InitRegistry(List<AasxCredentialsEntry> cList, DateTime timest
584584
var success = false;
585585
var external = false;
586586
string idEncoded;
587-
var endpoint = sd.Endpoints?[0].ProtocolInformation?.Href;
588-
var s1 = endpoint?.Split("/shells/");
589-
if (s1 != null && s1.Length == 2)
590-
{
591-
var s2 = s1[1].Split("/submodels/");
592-
if (s2.Length == 2)
593-
{
594-
idEncoded = s2[1].Replace("/submodel/", "");
595-
endpoint = s1[0] + "/submodels/" + idEncoded;
587+
var endpoint = sd.Endpoints?[0].ProtocolInformation?.Href;
588+
589+
if (endpoint != null && !AasxCredentials.keep(cList, endpoint, false))
590+
{
591+
var s1 = endpoint?.Split("/shells/");
592+
if (s1 != null && s1.Length == 2)
593+
{
594+
var s2 = s1[1].Split("/submodels/");
595+
if (s2.Length == 2)
596+
{
597+
idEncoded = s2[1].Replace("/submodel/", "");
598+
endpoint = s1[0] + "/submodels/" + idEncoded;
599+
}
596600
}
597601
}
598602

@@ -606,8 +610,17 @@ public async Task InitRegistry(List<AasxCredentialsEntry> cList, DateTime timest
606610
}
607611

608612
if (queryPara != "")
609-
{
610-
queryPara = "?" + queryPara;
613+
{
614+
if (queryPara.StartsWith("bearerHead="))
615+
{
616+
var bearer = queryPara.Replace("bearerHead=", "");
617+
queryPara = "";
618+
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", bearer);
619+
}
620+
else
621+
{
622+
queryPara = "?" + queryPara;
623+
}
611624
}
612625

613626
if (userPW != "")

0 commit comments

Comments
 (0)