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
+3
View File
@@ -3,6 +3,9 @@ using System.Text.Json;
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 DoxaApiImporter
{
public static ApiDocument Import(string json)
+171 -7
View File
@@ -4,9 +4,11 @@ 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 OpenApiImporter
{
public static ApiDocument Import(string json)
{
var node = JsonNode.Parse(json) ?? throw new ArgumentException("Input is not valid JSON.");
@@ -77,6 +79,17 @@ namespace EonaCat.DoxaApi.Interop
}
}
if (root["components"]?["securitySchemes"] is JsonObject compSecuritySchemes)
{
foreach (var (id, schemeNode) in compSecuritySchemes)
{
if (schemeNode is JsonObject schemeObj)
{
doc.SecuritySchemes[id] = ParseSecurityScheme3(id, schemeObj);
}
}
}
var groups = new Dictionary<string, ApiGroup>(StringComparer.OrdinalIgnoreCase);
if (root["paths"] is JsonObject paths)
@@ -130,19 +143,20 @@ namespace EonaCat.DoxaApi.Interop
Method = method,
Path = path,
Deprecated = op["deprecated"]?.GetValue<bool>() ?? false,
Tags = ParseStringArray(op["tags"])
Tags = ParseStringArray(op["tags"]),
Security = ParseSecurityArray(op["security"])
};
if (op["parameters"] is JsonArray parameters)
if (op["parameters"] is JsonArray opParameters)
{
foreach (var p in parameters)
foreach (var p in opParameters)
{
if (p is not JsonObject param)
{
continue;
}
var schema = p["schema"] is JsonNode schemaNode
var schema = param["schema"] is JsonNode schemaNode
? ParseSchema3(schemaNode)
: new SchemaModel { Type = "string" };
@@ -326,6 +340,17 @@ namespace EonaCat.DoxaApi.Interop
}
}
if (root["securityDefinitions"] is JsonObject secDefs)
{
foreach (var (id, defNode) in secDefs)
{
if (defNode is JsonObject defObj)
{
doc.SecuritySchemes[id] = ParseSecurityScheme2(id, defObj);
}
}
}
var groups = new Dictionary<string, ApiGroup>(StringComparer.OrdinalIgnoreCase);
if (root["paths"] is JsonObject paths)
@@ -379,7 +404,8 @@ namespace EonaCat.DoxaApi.Interop
Method = method,
Path = path,
Deprecated = op["deprecated"]?.GetValue<bool>() ?? false,
Tags = ParseStringArray(op["tags"])
Tags = ParseStringArray(op["tags"]),
Security = ParseSecurityArray(op["security"])
};
if (op["parameters"] is JsonArray parameters)
@@ -567,6 +593,144 @@ namespace EonaCat.DoxaApi.Interop
return ("application/json", null);
}
private static List<string> ParseSecurityArray(JsonNode? node)
{
var list = new List<string>();
if (node is not JsonArray arr)
{
return list;
}
foreach (var requirement in arr)
{
if (requirement is JsonObject reqObj)
{
foreach (var (schemeId, _) in reqObj)
{
list.Add(schemeId);
}
}
}
return list;
}
private static SecuritySchemeModel ParseSecurityScheme2(string id, JsonObject obj)
{
var type = obj["type"]?.GetValue<string>() ?? "apiKey";
var scheme = new SecuritySchemeModel
{
Id = id,
Description = obj["description"]?.GetValue<string>()
};
if (type == "basic")
{
scheme.Type = "http";
scheme.Scheme = "basic";
}
else if (type == "apiKey")
{
scheme.Type = "apiKey";
scheme.Name = obj["name"]?.GetValue<string>();
scheme.In = obj["in"]?.GetValue<string>();
}
else if (type == "oauth2")
{
scheme.Type = "oauth2";
var flow = obj["flow"]?.GetValue<string>();
var flowModel = new OAuthFlowModel
{
AuthorizationUrl = obj["authorizationUrl"]?.GetValue<string>(),
TokenUrl = obj["tokenUrl"]?.GetValue<string>()
};
if (obj["scopes"] is JsonObject scopesObj)
{
foreach (var (k, v) in scopesObj)
{
flowModel.Scopes[k] = v?.GetValue<string>() ?? "";
}
}
scheme.Flows = flow switch
{
"application" => new OAuthFlowsModel { ClientCredentials = flowModel },
"accessCode" => new OAuthFlowsModel { AuthorizationCode = flowModel },
"password" => new OAuthFlowsModel { Password = flowModel },
_ => new OAuthFlowsModel { Implicit = flowModel }
};
}
else
{
scheme.Type = type;
}
return scheme;
}
private static SecuritySchemeModel ParseSecurityScheme3(string id, JsonObject obj)
{
var scheme = new SecuritySchemeModel
{
Id = id,
Type = obj["type"]?.GetValue<string>() ?? "apiKey",
Description = obj["description"]?.GetValue<string>()
};
switch (scheme.Type)
{
case "apiKey":
scheme.Name = obj["name"]?.GetValue<string>();
scheme.In = obj["in"]?.GetValue<string>();
break;
case "http":
scheme.Scheme = obj["scheme"]?.GetValue<string>();
scheme.BearerFormat = obj["bearerFormat"]?.GetValue<string>();
break;
case "oauth2":
if (obj["flows"] is JsonObject flowsObj)
{
scheme.Flows = new OAuthFlowsModel
{
ClientCredentials = ParseOAuthFlow3(flowsObj["clientCredentials"] as JsonObject),
AuthorizationCode = ParseOAuthFlow3(flowsObj["authorizationCode"] as JsonObject),
Password = ParseOAuthFlow3(flowsObj["password"] as JsonObject),
Implicit = ParseOAuthFlow3(flowsObj["implicit"] as JsonObject)
};
}
break;
}
return scheme;
}
private static OAuthFlowModel? ParseOAuthFlow3(JsonObject? obj)
{
if (obj is null)
{
return null;
}
var flow = new OAuthFlowModel
{
AuthorizationUrl = obj["authorizationUrl"]?.GetValue<string>(),
TokenUrl = obj["tokenUrl"]?.GetValue<string>(),
RefreshUrl = obj["refreshUrl"]?.GetValue<string>()
};
if (obj["scopes"] is JsonObject scopesObj)
{
foreach (var (k, v) in scopesObj)
{
flow.Scopes[k] = v?.GetValue<string>() ?? "";
}
}
return flow;
}
private static List<string> ParseStringArray(JsonNode? node)
{
var list = new List<string>();
@@ -600,4 +764,4 @@ namespace EonaCat.DoxaApi.Interop
private static string SanitizePath(string path)
=> path.Trim('/').Replace('/', '_').Replace('{', '_').Replace('}', '_');
}
}
}