Closed skulidropek closed 1 year ago
I was able to fix it for myself in this way: I just added the JWT authorization code so that it would accept my custom header
services.AddAuthentication(o =>
{
o.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
o.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(o =>
{
o.RequireHttpsMetadata = false;
o.SaveToken = true;
o.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters()
{
ValidateIssuerSigningKey = true,
ValidateIssuer = false,
ValidateAudience = false,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(JwtTokenHandler.JWT_SECURITY_KEY))
};
o.Events = new JwtBearerEvents
{
OnMessageReceived = context =>
{
string authorization = context.Request.Headers["X-MyApp-Authorization"];
if (string.IsNullOrEmpty(authorization))
{
context.NoResult();
return Task.CompletedTask;
}
if (authorization.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase))
{
context.Token = authorization.Substring("Bearer ".Length).Trim();
}
if (string.IsNullOrEmpty(context.Token))
{
context.NoResult();
return Task.CompletedTask;
}
return Task.CompletedTask;
}
};
});
My config:
{
"DownstreamPathTemplate": "/{everything}",
"DownstreamScheme": "https",
"DownstreamHostAndPorts": [
{
"Host": "api.openai.com",
"Port": 443
}
],
"UpstreamHeaderTransform": {
"Authorization": "Bearer OpenAIToken"
},
"UpstreamPathTemplate": "/openai/{everything}",
"UpstreamHttpMethod": [ "GET", "POST", "PUT", "DELETE" ],
"AuthenticationOptions": {
"AuthenticationProviderKey": "Bearer",
"AllowedScopes": []
},
//"RouteClaimsRequirement": {
// "Role": "Administrator"
//},
"SwaggerKey": "openai"
}
Dear Skuli,
You should understand that before making any requests through gateway app, you must check direct connection to downstream service from client apps. If the direct connection works then you are able to try to route this service traffic via gateway, applying correct configuration, for sure.
@skulidropek commented on Sep 25 I just added the JWT authorization code so that it would accept my custom header
Definitely! As I said, you have to check direct connection first.
And your header is X-MyApp-Authorization
and your value is Bearer bla-bla-bla
.
But I cannot get it, why do you parse header value? Client app could forward token value only bla-bla-bla
and you need not such complex design of authorization setup as AddJwtBearer callback, with custom parsing of the header.
Truly speaking, it is a bit long and complicated.
I guess, this question-issue can be closed, right? Going to close...
@skulidropek Next time, please open questions in Discussions space aka Q&A category!
Hello everyone. I have been struggling with this problem for a long time. I implemented authorization for Ocelot using Bearer token and everything works. But it stops working when I try to use DownstreamHeaderTransform or UpstreamHeaderTransform
If I use Upstream, then it simply replaces my token for authorization, which I transmit, which is logical in principle, because I'm trying to influence the upper header. But when I use the Downstream nothing works, even if I remove the authorization from the Downstream nothing works, which is strange. I just want to pass a custom authorization token for Downstream
I constantly get a similar response from the server. Although my Downstream is added to the headers, it is never read by the OpenAI server
My upstream token is added to RequestMessage
I have already read this documentation several times but have not found a solution to my problem https://ocelot.readthedocs.io/en/latest/features/headerstransformation.html
The message that the server returns to me Request finished HTTP/1.1 POST http://DOMAIN/openai/v1/chat/completions application/json;+charset=utf-8 - - 401 198 application/json;+charset=utf-8 171.8120ms
My config: