Skip to content

Security requirements not being serialized? #374

@baywet

Description

@baywet

Hi everyone,
As I'm fairly new playing with this library I'm not whether I found a bug or if I'm missing something.
I'm currently building a brand new API, this api relies on aspnet core, ef core, odata core. Previously this API was not secured in any way and I had Open API definitions working fine.
I added an additional project/service to handle all identity/authentication/autorisation concerns. This is implemented using Identity Server v4. I also added the bearer validation in my API. All of that works fine.
Now I'm trying to add the authentication information to my open api definition (and reflect it in the swashbuckle UI) so developers using that API know that they need to get tokens and where before trying to call the endpoints.
The issue I'm facing the current code is that although I'm adding security requirements in the code, that doesn't get reflected in the serialized json or yaml.
Here is the code I implemented to generate my definitions. Thanks for the help!

 var authServiceBaseUrl = "https://myidentityserverinstance";
            var apiEndpoint = "https://theapiurl";
var oDataBuilder = new ODataConventionModelBuilder(provider); // provider is my IServiceProvider
            oDataBuilder.EntitySet<DataPoint>($"{nameof(DataPoint)}s");
            var model = oDataBuilder.GetEdmModel();
            oDataBuilder.ValidateModel(model);
            var document = model.ConvertToOpenApi();
// the addtion for auth starts here
            document.SecurityRequirements = new List<OpenApiSecurityRequirement>
            {
                new OpenApiSecurityRequirement
                {
                    {
                        new OpenApiSecurityScheme()
                        {
                            OpenIdConnectUrl = new Uri($"{authServiceBaseUrl}/.well-known/openid-configuration"),
                            BearerFormat = "JWT",
                            Scheme = "bearer",
                            In = ParameterLocation.Header,
                            Name = "Authorization",
                            Type = SecuritySchemeType.OpenIdConnect,
                            Flows = new OpenApiOAuthFlows
                            {
                                Implicit = new OpenApiOAuthFlow
                                {
                                    AuthorizationUrl = new Uri($"{authServiceBaseUrl}/connect/authorize"),
                                    TokenUrl = new Uri($"{authServiceBaseUrl}/connect/token"),
                                    Scopes = new Dictionary<string, string>
                                    {
                                        {"datapoints.read", "Read DataPoints" },
                                    }
                                }
                            }
                        },
                        new List<string>
                        {
                            "datapoints.read",
                        }
                    }
                }
            };
// the addition for auth ends here
            var outputYAML = document.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_0);
            return Content(outputYAML, new MediaTypeHeaderValue("text/vnd.yaml"));

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions