-
Notifications
You must be signed in to change notification settings - Fork 9k
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
Add possibility to loading a Swagger spec (.json/.yaml) that is protected by Basic auth. UI version 3.0 #2793
Comments
@ponelat did we end up exposing a method for doing that? |
Nope... ( I mean its possible, but ugly ).
Here are some ideas... Swagger({
url: '...',
swaggerAuthorizations: [ // Note, an array of Auths only applied to fetching swagger specs
Swagger.ApiKeyAuthorization('myQuery', 'foo', 'query'),
Swagger.QueryAuthorization('myQuery', 'bar'), // Better than ApiKeyAuthorization?
Swagger.PasswordAuthorization('fooUser', 'fooPaswsword')
]
}).then(...) Swagger({
url: '...',
swaggerProxy(req) {
req.headers.Authorization = 'bar'
// or
this.passwordAuth(req, 'fooUser', 'fooPassword') // Add a basic auth
return this.http(req) // Continue the request
// or
return // undefined, we'd infer that we should continued the request as above. In case the user forgets to
}
}).then(...) My favourite, ( ie: both of the above )...Swagger({
url: '...',
swaggerProxies: [ // Note, an array of functions, applied to fetching swagger specs
Swagger.PasswordAuthorization('fooUser', 'fooPaswsword'), // decorate request with this auth
...,
(req) => { // We can write our own decorators...
if(this.matchUrl(req.url, 'example.com') { // Might be a useful helper
req.headers['X-ThisIsASpecialRequest'] = true // Whatever
return Swagger.HeaderAuthorization('FooHeader', 'bar')(req) // we can also use the helpers.
}
}
],
tryItOutProxies: [ ... ] // Same for try-it-out-requests?
}).then(...) |
@ponelat I think @buunguyen had some thoughts on that. I guess the main question is, why change the behavior from 2.X? |
The key difference is that we were applying auth to both requests in the try-it-out and fetching the swagger specs ( from what I recall ). I think they should be separate. Bonus:Something not mentioned here but important, is having a literal representation of auths... so that they can be including the config file ( function aren't easily added to the config file, if even possible ). |
Right, but that I still fail to see why we need to break interface. We can possibly add, but the first step would be to support the original behavior. |
That's fine too. |
Sorry for a tangential question, but can we also have support for Authorization token scheme for fetching the API? I know it's been addressed in the past per #1503, but I don't see if it's still supported in the new version. Per your comments from above it would seem it's not, is that right? |
@dinvlad - that's the goal, yes. |
@webron Any indications on the priority of this issue? we are very interested on embedding swagger-ui into our project in the near term, but this is a blocker for us as all our swagger specs are protected by authentication. |
The |
I'm having kind of the same issue. I also had this issue with the api endpoint when executing them.
And when asked for the key, I copy the Is there a way to workaround with something like this for the swagger.json getting? |
I made the most horrible workaround but it worked for me and thats good enough for me now. Idea: Override System Fetch function to add extra header when getting Code: on index.html right after
|
Hi @martinpagesaal, I am also trying to adapt your workaround, but it fails to reload the UI. Can you please tell me the specific location where you included the above code? |
Hi @madhushib This is my full index.html
and this is part of my app.js
I hope this helps you! |
@martinpagesaal, i had been try your solution, it works well.
|
@ponelat Can we maybe see the ugly code until this issue is resolved? |
@TheCodeDestroyer An alternative is to change the download action, if its only the swagger spec you're concerned with. |
Setting
|
@danilobuerger Nice find, but judging by this comment (assuming I'm looking at the correct implementation) it sounds like // Wrap a http function ( there are otherways to do this, conisder this deprecated )
export function makeHttp(httpFn, preFetch, postFetch) {
postFetch = postFetch || (a => a)
preFetch = preFetch || (a => a) |
Yeah, I read that too. My hope is that this story will be done before its removed. |
I don't encourage the long-term use of a deprecated interface... but FWIW, we won't remove |
@webron is there a rough time schedule for implementation? We need access to protected API spec because our definitions are security-critical and thus not public. |
@CarstenRilke, no one is working on this at the moment, as our resources are limited and our main focus is OAS3 support right now. If you have the time, a pull request that fixes this would be appreciated. If you need this ASAP, please note that folks have mentioned workarounds to this issue upthread! |
@CarstenRilke - also, it's not that it's not doable with the current code - make sure you read the thread fully. |
It was pointed out to us that our request interceptors were not intercepting spec fetch. This has been fixed, and will be available this Friday when we release Swagger-UI again. Official solutionGiven user SwaggerUI({
url: "https://httpbin.org/basic-auth/myUser/myPassword",
requestInterceptor: (req) => {
if(req.loadSpec) {
var hash = btoa("myUser" + ":" + "myPassword") // base64
req.headers.Authorization = "Basic " + hash
}
return req
}
}) Basically, you define a requestInterceptor that attaches the basic auth information for you. Modify If you have questions about this solution, please open a new ticket. Thanks everyone! |
Thanks @shockey . Note that setting a // The spec URL
const url = "https://www.example.com/authorised-users-only/spec.json";
SwaggerUI({
url, // spec url
requestInterceptor: (req) => {
// Only set Authorization header if the request matches the spec URL
if (req.url === url) {
req.headers.Authorization = "Basic " + btoa("myUser" + ":" + "myPassword");
}
return req;
}
}) |
Thanks for pointing that out, @scottohara! You're correct, I overlooked that in my original example. There's an internal flag that's set on the request, |
@brianupskill as the ticket is closed, the request is pretty much lost (hard to track). Please file a new ticket. |
|
@HaleyWang please don't change things on the object you get back from |
I'm too doing the same things as @HaleyWang , Because I want to change the access token value dynamically. @shockey , How can I change the Authorization header dynamically after initializing the swagger UI ? I'm using Swagger/OpenAPI 2.0 , and swagger-ui version 3.22.2. |
@tmkasum and everyone else: I use code like const requestInterceptor = async req => {
req.headers['Authorization'] = 'Bearer ' + await getIdToken();
return req;
};
SwaggerUI({
url,
dom_id: '#swagger-ui',
requestInterceptor,
showMutatedRequest: true,
}); |
In swagger-ui 3.2x, to manually set Authorization header based on values entered in the authorize popup for basic authentication, we can use below code.
|
http://stackoverflow.com/questions/42995594/how-to-use-basic-auth-in-swagger-ui-v-3-0
The text was updated successfully, but these errors were encountered: