Authorization query parameter not being added to the requests.
I've been working on the develop_2.0 branch and today switched to the master. I see the problem in both versions.
I have my own authorization "token" that I'm trying to inject as a query parameter:
window.authorizations.add("key", new ApiKeyAuthorization("access_token", currentToken, "query"));
The token does not show up in any of the requests and they are properly rejected as being unauthorized. If I add a couple of lines of code, the problem goes away. In swagger-client.js I re-applied the authorizations just before executing a request:
...
//Re-apply authorizations just before executing the request
var e = (typeof window !== 'undefined' ? window : exports);
e.authorizations.apply(obj);
new SwaggerHttp().execute(obj);
}
Is this a valid issue or am I doing something wrong?
There are a couple different things that can cause this. Does your spec specify if the key should be applied? For instance:
authorizations: { }
means "no authorizations required", meaning, don't apply the headers here. Can you share your swagger json spec?
I'm using a 'pure' 2.0 spec and have not specified any authorizations or security related requirements. I just wanted the UI to append the query parameter token as noted which it does if I add the extra lines of code.
{
"swagger": 2.0,
"info": {
"title": "REST API Documentation and Client",
"description": "Dynamic client that you can use to exercise the API and try it out.",
"version": "1.0.0",
"license": {
"name": "TBA"
}
},
"host": "some.host.com",
"schemes": [
"http",
"https"
],
"basePath": "/locate",
"produces": [
"application/json"
],
"paths": {
"/{realmId}/locations/{resourceId}": {
"get": {
"summary": "Retrieve a Location",
"description": "Retrieve a single specific record associated with the resourceId.",
"parameters": [
{
"name": "realmId",
"in": "path",
"description": "The realm under which this resource is managed.",
"required": true,
"type": "string"
},
{
"name": "resourceId",
"in": "path",
"description": "The unique id associated with this specific resource.",
"required": true,
"type": "string"
}
],
"tags": [
"Locations"
],
"responses": {
"200": {
"description": "Returns a single, specific record uniquely identified by the resourceId",
"schema": {
"$ref": "Location"
}
},
"400": {
"description": "Invalid request. Parameters may be missing or incorrect.",
"schema": {
"$ref": "Error"
}
},
"401": {
"description": "Unable to perform operation due to invalid permission.",
"schema": {
"$ref": "Error"
}
},
"404": {
"description": "Could not find the resource associated with this id.",
"schema": {
"$ref": "Error"
}
},
"500": {
"description": "Server error.",
"schema": {
"$ref": "Error"
}
},
"default": {
"description": "Unexpected error",
"schema": {
"$ref": "Error"
}
}
}
},
"post": {
"summary": "Store a Location",
"description": "Save a specific Location using the provided resourceId as the key.",
"parameters": [
{
"name": "realmId",
"in": "path",
"description": "The realm under which this resource is managed.",
"required": true,
"type": "string"
},
{
"name": "resourceId",
"in": "path",
"description": "The unique id associated with this specific resource.",
"required": true,
"type": "string"
},
{
"name": "body",
"in": "body",
"description": "The resource to be saved.",
"required": true,
"schema": {
"$ref": "Location"
}
}
],
"tags": [
"Locations"
],
"responses": {
"201": {
"description": "Returns a unique resource id URI in the Location: header as well as in the body of the response.",
"schema": {
"$ref": "Location"
}
},
"400": {
"description": "Invalid request. Parameters may be missing or incorrect.",
"schema": {
"$ref": "Error"
}
},
"401": {
"description": "Unable to perform operation due to invalid permission.",
"schema": {
"$ref": "Error"
}
},
"404": {
"description": "Could not find the resource associated with this id.",
"schema": {
"$ref": "Error"
}
},
"500": {
"description": "Server error.",
"schema": {
"$ref": "Error"
}
},
"default": {
"description": "Unexpected error",
"schema": {
"$ref": "Error"
}
}
}
},
"delete": {
"summary": "Remove a Location",
"description": "Delete a specific Location associated with the resourceId.",
"parameters": [
{
"name": "realmId",
"in": "path",
"description": "The realm under which this resource is managed.",
"required": true,
"type": "string"
},
{
"name": "resourceId",
"in": "path",
"description": "The unique id associated with this specific resource.",
"required": true,
"type": "string"
}
],
"tags": [
"Locations"
],
"responses": {
"204": {
"description": "Indicates that the resource was successfully deleted."
},
"400": {
"description": "Invalid request. Parameters may be missing or incorrect.",
"schema": {
"$ref": "Error"
}
},
"401": {
"description": "Unable to perform operation due to invalid permission.",
"schema": {
"$ref": "Error"
}
},
"404": {
"description": "Could not find the resource associated with this id.",
"schema": {
"$ref": "Error"
}
},
"500": {
"description": "Server error.",
"schema": {
"$ref": "Error"
}
},
"default": {
"description": "Unexpected error",
"schema": {
"$ref": "Error"
}
}
}
}
}
},
"definitions": {
"Position": {
"description": "A single geographic point",
"type": "array",
"minItems": 2,
"items": [
{
"description": "Longitude (-180 to 180)",
"type": "number",
"minimum": -180,
"maximum": 180
},
{
"description": "Latitude (-90 to 90)",
"type": "number",
"minimum": -90,
"maximum": 90
}
]
},
"Location": {
"description": "A Location represents a users location information (longitude, latitude) in GeoJSON format. It also includes related metadata and optional, custom properties.",
"type": "object",
"required": ["location"],
"properties": {
"label": {
"description": "Human friendly name for this resource.",
"type": "string"
},
"location": {
"description": "The GeoJSON record for this resource.",
"type": "object",
"properties": {
"geometry": {
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": ["Point"],
"default": "Point"
},
"coordinates": {
"$ref": "Position"
}
}
},
"properties": {
"type": "object",
"description": "Optional, additional properties of the resource.",
"properties": {
"altitude": {
"description": "The altitude of the Location in meters.",
"type": "number"
}
}
}
}
}
}
},
"Locations": {
"type": "array",
"items": {
"$ref": "Location"
}
}
}
}
Thanks for sharing that. I'll update the ticket shortly after looking into the issue.
please give master a shot
Some light, preliminary testing seems to show it working now. Thanks.
Some requests of my REST API are protected using oauth2. When needed, I click on the On/Off button in Swagger, user is redirected to a login page, credentials are sent to the web service which returns an access_token, which goes to Swagger's o2c.html before arriving in swagger-oauth.js, method onOAuthComplete(token):
window.authorizations.add("oauth2", new ApiKeyAuthorization("Authorization", "Bearer " + b, "header"));
where b is the access token. So far so good!
When I execute a GET request, for instance, how can I ensure that the access token has been correctly sent ? When I look at the Chrome console, I cannot find this information as well as the value of the access token sent (I don't find more information in Firefox/Firebug).
Request Headers (view source) Accept:/ Accept-Encoding:gzip,deflate,sdch Accept-Language:en-US,en;q=0.8 Access-Control-Request-Headers:accept, authorization, content-type Access-Control-Request-Method:GET Cache-Control:no-cache Connection:keep-alive DNT:1 Host:localhost:8080 Origin:http://localhost Pragma:no-cache Referer:http://localhost/swagger/ User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36
Thanks
@tschaffter perhaps your operations are not declaring that they require authorization? Is this a statically-generated swagger doc? Please share more info and perhaps open a new ticked with a sample of what your spec looks like
So according to you we would see the value of the authorization header somewhere in the above "Request Headers" if it was sent?
I get the following exception when I click on "Try it out!" (at that time Swagger has the access token). XMLHttpRequest cannot load http://localhost:8080/rest-oauth2/v1.0/events/1. Request header field Authorization is not allowed by Access-Control-Allow-Headers.
When the issue here is a separate matter linked to my CORS filter, I thought that the authorization header was correctly sent.
From these observations:
- Can I conclude that the authorization header has been sent?
- If yes, where can I see the value of the authorization header effectively sent?
If some information are still missing to answer the above interrogations, I'll open a new ticket with more comprehensive information. :)