Added authentication
This commit is contained in:
@@ -3,6 +3,9 @@ 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 SwaggerExporter
|
||||
{
|
||||
public static JsonObject Export(ApiDocument doc)
|
||||
@@ -57,6 +60,17 @@ namespace EonaCat.DoxaApi.Interop
|
||||
root["definitions"] = definitions;
|
||||
}
|
||||
|
||||
if (doc.SecuritySchemes.Count > 0)
|
||||
{
|
||||
var defs = new JsonObject();
|
||||
foreach (var (id, scheme) in doc.SecuritySchemes)
|
||||
{
|
||||
defs[id] = SecuritySchemeToSwagger(scheme);
|
||||
}
|
||||
|
||||
root["securityDefinitions"] = defs;
|
||||
}
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
@@ -157,6 +171,16 @@ 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;
|
||||
}
|
||||
|
||||
@@ -285,6 +309,65 @@ namespace EonaCat.DoxaApi.Interop
|
||||
return obj;
|
||||
}
|
||||
|
||||
private static JsonObject SecuritySchemeToSwagger(SecuritySchemeModel scheme)
|
||||
{
|
||||
var obj = new JsonObject();
|
||||
|
||||
if (scheme.Type == "apiKey")
|
||||
{
|
||||
obj["type"] = "apiKey";
|
||||
obj["name"] = scheme.Name ?? "X-API-Key";
|
||||
obj["in"] = scheme.In ?? "header";
|
||||
}
|
||||
else if (scheme.Type == "http" && scheme.Scheme == "basic")
|
||||
{
|
||||
obj["type"] = "basic";
|
||||
}
|
||||
else if (scheme.Type == "http" && scheme.Scheme == "bearer")
|
||||
{
|
||||
// Swagger 2.0 has no native bearer type; represent as an apiKey-style
|
||||
// Authorization header for maximum tool compatibility.
|
||||
obj["type"] = "apiKey";
|
||||
obj["name"] = "Authorization";
|
||||
obj["in"] = "header";
|
||||
}
|
||||
else if (scheme.Type == "oauth2")
|
||||
{
|
||||
obj["type"] = "oauth2";
|
||||
var flow = scheme.Flows?.ClientCredentials;
|
||||
if (flow is not null)
|
||||
{
|
||||
obj["flow"] = "application";
|
||||
obj["tokenUrl"] = flow.TokenUrl;
|
||||
var scopes = new JsonObject();
|
||||
foreach (var (k, v) in flow.Scopes)
|
||||
{
|
||||
scopes[k] = v;
|
||||
}
|
||||
obj["scopes"] = scopes;
|
||||
}
|
||||
else if (scheme.Flows?.AuthorizationCode is OAuthFlowModel ac)
|
||||
{
|
||||
obj["flow"] = "accessCode";
|
||||
obj["authorizationUrl"] = ac.AuthorizationUrl;
|
||||
obj["tokenUrl"] = ac.TokenUrl;
|
||||
var scopes = new JsonObject();
|
||||
foreach (var (k, v) in ac.Scopes)
|
||||
{
|
||||
scopes[k] = v;
|
||||
}
|
||||
obj["scopes"] = scopes;
|
||||
}
|
||||
}
|
||||
|
||||
if (scheme.Description is not null)
|
||||
{
|
||||
obj["description"] = scheme.Description;
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
private static string ToSwaggerPath(string path)
|
||||
=> path.StartsWith('/') ? path : "/" + path;
|
||||
|
||||
@@ -301,4 +384,4 @@ namespace EonaCat.DoxaApi.Interop
|
||||
_ => "Response"
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user