OpenAPI-Specification icon indicating copy to clipboard operation
OpenAPI-Specification copied to clipboard

Allow optional Authorizations

Open jacobweber opened this issue 12 years ago • 24 comments

This is an enhancement request, to allow optional authorizations. Not sure if this would be useful for others, but it would be for me.

In my case, you can access my APIs with no authorization, with OAuth, or with a set of two API keys. Depending on which you use, I may give you access to different data, but you still use the same APIs.

One way to handle this would be to allow a "required" boolean for each authorization in the API's Authorizations object.

If you did this, I could mark both of my oauth2 and apiKey authorizations as optional, which would allow you to select either or none of them. (It would also allow you to select both, but that's not really a problem.)

My case is also complicated by the fact that I need two parameters for my API key. If you wanted to treat this as a single Authorization, you'd have to allow multiple keynames for an apiKey. You could also treat it as two separate Authorizations, and just make them both optional. That's not ideal, since it wouldn't give you a way to indicate that they go together, but it would work.

jacobweber avatar Mar 23 '14 16:03 jacobweber

@jacobweber - in 2.0 you can specify several possible security requirements, and each of them can include multiple schemes.

So if you declare schema A, B and C you can have something like ( (A AND B) OR C ). Does that meet your proposal?

webron avatar Oct 14 '14 18:10 webron

Sounds like it would. Is that documented here? I couldn't quite find what you describe, although I haven't read it too closely yet.

jacobweber avatar Oct 14 '14 18:10 jacobweber

Yes. It is spread about several locations though. Perhaps I should create a section that clarifies it. Check out the documentation for https://github.com/wordnik/swagger-spec/blob/master/versions/2.0.md#swaggerSecurity and for https://github.com/wordnik/swagger-spec/blob/master/versions/2.0.md#securityRequirementObject.

webron avatar Oct 14 '14 18:10 webron

Will do, thanks. Yeah, an example would probably be helpful.

jacobweber avatar Oct 14 '14 18:10 jacobweber

Yeah, I need to add samples throughout the document (as you can obviously see). I was waiting to finalize the security part of it.

If you manage to read through it and see that it fits this issue, please close it.

webron avatar Oct 14 '14 18:10 webron

There are some examples already in swagger-spec/fixtures/v2.0/json/resources/securityExample.json:

What I think is missing is a “no security” option in securityDefinitions. Then you could have:

"security": {
  {"none": []},
  {"oauth": […]},
  {"token-1": []},
  {"token-2": []}
},
"securityDefinitions": {
  "none": {"type": "none", "description": "Publicly accessible (no authentication needed)."},
  "oauth": {…},
  "token-1": {…},
  "token-2": {…}
}

wking avatar Dec 06 '14 19:12 wking

This is an old ticket, and I believe the samples have been added to the spec.

webron avatar Mar 27 '16 21:03 webron

I still can't find an example of a "no security" option. Something like the {"none": []} example proposed by @wking

rkarodia avatar Apr 26 '16 09:04 rkarodia

@webron Also in need of a none security definition so when a token isn't provided responses are restricted to public content. Otherwise I need two endpoints for pretty much the same thing.

Should I raise a new ticket for this?

mikestead avatar May 02 '16 23:05 mikestead

@mikestead yup

webron avatar May 19 '16 05:05 webron

+1 Also in need of none security definition. @mikestead would you please copy a reference to the new ticket here when you create it?

rorisme avatar Aug 20 '16 17:08 rorisme

Was this functionality ever added? The ability to do as @rkarodia and @mikestead described with "no security" defaulting to public content is very needed

elzii avatar Apr 04 '17 20:04 elzii

same question here :)

rompic avatar Apr 26 '17 15:04 rompic

Everyone, you don't need a none definition. The following has the same effect:

"security": [
  {},
  {"oauth": […]},
  {"token-1": []},
  {"token-2": []}
]

webron avatar Apr 26 '17 15:04 webron

Thx for the fast feedback. Will look into It.

rompic avatar Apr 27 '17 04:04 rompic

But the swagger validator says the spec is not valid anymore ? How do you solve this in the end ?

iongion avatar Jun 03 '17 14:06 iongion

@iongion if it says it's invalid, then file a ticket on that project.

webron avatar Jun 05 '17 22:06 webron

Has this feature been somehow lost in 3.0? I want to specify an API, that can be called with and without auth and will return slightly different results depending on that.

BGehrels avatar Feb 21 '18 20:02 BGehrels

@BGehrels I am not aware of any conversations in the work on the V3 spec to change this behavior. I don't think this option was very clearly spelled out in the V2 spec and we didn't do a better job in the V3 spec. We definitely should fix that.

darrelmiller avatar Feb 22 '18 04:02 darrelmiller

Since this issue targets 2.0 and is closed, should i open a seperate issue for that? This issue is one of the first hits when one searches for "openapi optional auth", so it may be a good idea to leave a hint here how to accomplish this in the meantime. I would do it, but i still have no idea :-)

BGehrels avatar Feb 22 '18 10:02 BGehrels