Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to remove Top level document security #2304

Closed
dbartumeu opened this issue Jul 9, 2019 · 17 comments · Fixed by #2305
Closed

How to remove Top level document security #2304

dbartumeu opened this issue Jul 9, 2019 · 17 comments · Fixed by #2305

Comments

@dbartumeu
Copy link

I'm adding JWT authorization to the document:

 document.AddSecurity("JWT", 
    Enumerable.Empty<string>(),
    new OpenApiSecurityScheme {
    Type = OpenApiSecuritySchemeType.ApiKey,
    Name = "Authorization",
    In = OpenApiSecurityApiKeyLocation.Header,
    Description = "Type into the textbox: Bearer {your JWT token}."
});

document.OperationProcessors.Add(
    new AspNetCoreOperationSecurityScopeProcessor("JWT")
        //new OperationSecurityScopeProcessor("JWT")
);

and for some reason in the swagger.json it appends the security to the whole document. Ex:

...
"security": [
    {
      "JWT": []
    }
  ]

is there a way to remove that section?

@RicoSuter
Copy link
Owner

I think that this "security" section is always needed if "securitySchemes" are defined, no?

@dbartumeu
Copy link
Author

I just want to put some context:
Values Endpoints don't need authorization at all. As you can see I have a security schemes definition and those endpoints remains without security.
s1

If I add the security section it affects the entire doc. As you can see in the image.
s2

The only way to remove the security icon is append securtity: [] in all endpoints definition. I dont know if there is a way to specify that, because i tried with [AllowAnonymous] and didn't work;

@RicoSuter
Copy link
Owner

Did you add AspNetCoreOperationSecurityScopeProcessor?

@RicoSuter
Copy link
Owner

I think you can also add the appender instead of AddSecurity without scopeNames

settings.DocumentProcessors.Add(new SecurityDefinitionAppender(name, scopeNames, swaggerSecurityScheme));

@dbartumeu
Copy link
Author

I changed the code to:

document.DocumentProcessors.Add(
    new SecurityDefinitionAppender("JWT", Enumerable.Empty<string>(),
        new OpenApiSecurityScheme
    {
        Type = OpenApiSecuritySchemeType.ApiKey,
        Name = "Authorization",
        In = OpenApiSecurityApiKeyLocation.Header,
        Description = "Type into the textbox: Bearer {your JWT token}."
}));

and still getting the same issue.

@RicoSuter
Copy link
Owner

Remove the second enumerable parameter

@dbartumeu
Copy link
Author

you mean like this?

document.DocumentProcessors.Add(
    new SecurityDefinitionAppender("JWT",
    new OpenApiSecurityScheme
    {
        Type = OpenApiSecuritySchemeType.ApiKey,
        Name = "Authorization",
        In = OpenApiSecurityApiKeyLocation.Header,
        Description = "Type into the textbox: Bearer {your JWT token}."
    })
);

@RicoSuter
Copy link
Owner

Yep, this way “security” should not be generated (not sure if this is correct though)

@dbartumeu
Copy link
Author

Yes you are right but now is not generating security for controllers that require authentication

@dbartumeu
Copy link
Author

And now i got it. Here is the final code:

document.OperationProcessors.Add(new AspNetCoreOperationSecurityScopeProcessor("JWT"));

document.DocumentProcessors.Add(
  new SecurityDefinitionAppender("JWT",
	new OpenApiSecurityScheme
	{
	  Type = OpenApiSecuritySchemeType.ApiKey,
	  Name = "Authorization",
	  In = OpenApiSecurityApiKeyLocation.Header,
	  Description = "Type into the textbox: Bearer {your JWT token}."
	})
);

@RicoSuter
Copy link
Owner

And this works as expected?

@dbartumeu
Copy link
Author

yes, but i have a warning because the overload is obsolete.

@RicoSuter
Copy link
Owner

Ok, then both overloads are valid and obsolete has to be removed, right?

@RicoSuter
Copy link
Owner

Will create a pr for review tomorrow

@dbartumeu
Copy link
Author

yes that's right. thank you so much for your help.

@RicoSuter
Copy link
Owner

Created PR: #2305
What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants