Added authentication

This commit is contained in:
2026-06-21 20:10:02 +02:00
parent efe63211ed
commit a50be1ec0b
50 changed files with 2596 additions and 328 deletions
+106 -1
View File
@@ -4,6 +4,8 @@ using EonaCat.DoxaApi.Models;
namespace EonaCat.DoxaApi.Interop
{
// This file is part of the EonaCat project(s) which is released under the Apache License.
// See the LICENSE file or go to https://EonaCat.com/license for full license details.
public static class OpenApiExporter
{
public static JsonObject Export(ApiDocument doc)
@@ -53,6 +55,24 @@ namespace EonaCat.DoxaApi.Interop
root["components"] = new JsonObject { ["schemas"] = schemas };
}
if (doc.SecuritySchemes.Count > 0)
{
var schemes = new JsonObject();
foreach (var (id, scheme) in doc.SecuritySchemes)
{
schemes[id] = SecuritySchemeToOpenApi(scheme);
}
if (root["components"] is JsonObject componentsObj)
{
componentsObj["securitySchemes"] = schemes;
}
else
{
root["components"] = new JsonObject { ["securitySchemes"] = schemes };
}
}
return root;
}
@@ -161,9 +181,94 @@ namespace EonaCat.DoxaApi.Interop
}
op["responses"] = responses;
if (endpoint.Security.Count > 0)
{
var security = new JsonArray();
foreach (var schemeId in endpoint.Security)
{
security.Add(new JsonObject { [schemeId] = new JsonArray() });
}
op["security"] = security;
}
return op;
}
private static JsonObject SecuritySchemeToOpenApi(SecuritySchemeModel scheme)
{
var obj = new JsonObject { ["type"] = scheme.Type };
if (scheme.Description is not null)
{
obj["description"] = scheme.Description;
}
switch (scheme.Type)
{
case "apiKey":
obj["name"] = scheme.Name ?? "X-API-Key";
obj["in"] = scheme.In ?? "header";
break;
case "http":
obj["scheme"] = scheme.Scheme ?? "bearer";
if (scheme.BearerFormat is not null)
{
obj["bearerFormat"] = scheme.BearerFormat;
}
break;
case "oauth2":
var flows = new JsonObject();
if (scheme.Flows?.ClientCredentials is OAuthFlowModel cc)
{
flows["clientCredentials"] = OAuthFlowToOpenApi(cc);
}
if (scheme.Flows?.AuthorizationCode is OAuthFlowModel ac)
{
flows["authorizationCode"] = OAuthFlowToOpenApi(ac);
}
if (scheme.Flows?.Password is OAuthFlowModel pw)
{
flows["password"] = OAuthFlowToOpenApi(pw);
}
if (scheme.Flows?.Implicit is OAuthFlowModel im)
{
flows["implicit"] = OAuthFlowToOpenApi(im);
}
obj["flows"] = flows;
break;
}
return obj;
}
private static JsonObject OAuthFlowToOpenApi(OAuthFlowModel flow)
{
var obj = new JsonObject();
if (flow.AuthorizationUrl is not null)
{
obj["authorizationUrl"] = flow.AuthorizationUrl;
}
if (flow.TokenUrl is not null)
{
obj["tokenUrl"] = flow.TokenUrl;
}
if (flow.RefreshUrl is not null)
{
obj["refreshUrl"] = flow.RefreshUrl;
}
var scopes = new JsonObject();
foreach (var (k, v) in flow.Scopes)
{
scopes[k] = v;
}
obj["scopes"] = scopes;
return obj;
}
private static JsonObject SchemaToOpenApi(SchemaModel schema)
{
@@ -269,4 +374,4 @@ namespace EonaCat.DoxaApi.Interop
_ => "Response"
};
}
}
}