Committed by
Gerrit Code Review
Bumpup swagger ui to 2.1.4 to fix the issues in 2.1.0
This commit resolves some issues on generating REST API request, such as incorrect content-type generation for http DELETE request. Change-Id: I70bf18247f95ccf22b1fd77bc48ce9977ff57212
Showing
5 changed files
with
89 additions
and
34 deletions
... | @@ -21,6 +21,7 @@ | ... | @@ -21,6 +21,7 @@ |
21 | <script src='lib/highlight.7.3.pack.js' type='text/javascript'></script> | 21 | <script src='lib/highlight.7.3.pack.js' type='text/javascript'></script> |
22 | <script src='lib/marked.js' type='text/javascript'></script> | 22 | <script src='lib/marked.js' type='text/javascript'></script> |
23 | <script src='lib/swagger-oauth.js' type='text/javascript'></script> | 23 | <script src='lib/swagger-oauth.js' type='text/javascript'></script> |
24 | + <script src='lib/jsoneditor.min.js' type='text/javascript'></script> | ||
24 | 25 | ||
25 | <script type="text/javascript"> | 26 | <script type="text/javascript"> |
26 | $(function () { | 27 | $(function () { | ... | ... |
This diff could not be displayed because it is too large.
... | @@ -3,8 +3,10 @@ var popupMask; | ... | @@ -3,8 +3,10 @@ var popupMask; |
3 | var popupDialog; | 3 | var popupDialog; |
4 | var clientId; | 4 | var clientId; |
5 | var realm; | 5 | var realm; |
6 | -var oauth2KeyName; | ||
7 | var redirect_uri; | 6 | var redirect_uri; |
7 | +var clientSecret; | ||
8 | +var scopeSeparator; | ||
9 | +var additionalQueryStringParams; | ||
8 | 10 | ||
9 | function handleLogin() { | 11 | function handleLogin() { |
10 | var scopes = []; | 12 | var scopes = []; |
... | @@ -16,7 +18,6 @@ function handleLogin() { | ... | @@ -16,7 +18,6 @@ function handleLogin() { |
16 | for(key in defs) { | 18 | for(key in defs) { |
17 | var auth = defs[key]; | 19 | var auth = defs[key]; |
18 | if(auth.type === 'oauth2' && auth.scopes) { | 20 | if(auth.type === 'oauth2' && auth.scopes) { |
19 | - oauth2KeyName = key; | ||
20 | var scope; | 21 | var scope; |
21 | if(Array.isArray(auth.scopes)) { | 22 | if(Array.isArray(auth.scopes)) { |
22 | // 1.2 support | 23 | // 1.2 support |
... | @@ -28,7 +29,7 @@ function handleLogin() { | ... | @@ -28,7 +29,7 @@ function handleLogin() { |
28 | else { | 29 | else { |
29 | // 2.0 support | 30 | // 2.0 support |
30 | for(scope in auth.scopes) { | 31 | for(scope in auth.scopes) { |
31 | - scopes.push({scope: scope, description: auth.scopes[scope]}); | 32 | + scopes.push({scope: scope, description: auth.scopes[scope], OAuthSchemeKey: key}); |
32 | } | 33 | } |
33 | } | 34 | } |
34 | } | 35 | } |
... | @@ -40,6 +41,7 @@ function handleLogin() { | ... | @@ -40,6 +41,7 @@ function handleLogin() { |
40 | appName = window.swaggerUi.api.info.title; | 41 | appName = window.swaggerUi.api.info.title; |
41 | } | 42 | } |
42 | 43 | ||
44 | + $('.api-popup-dialog').remove(); | ||
43 | popupDialog = $( | 45 | popupDialog = $( |
44 | [ | 46 | [ |
45 | '<div class="api-popup-dialog">', | 47 | '<div class="api-popup-dialog">', |
... | @@ -57,12 +59,16 @@ function handleLogin() { | ... | @@ -57,12 +59,16 @@ function handleLogin() { |
57 | '</div>'].join('')); | 59 | '</div>'].join('')); |
58 | $(document.body).append(popupDialog); | 60 | $(document.body).append(popupDialog); |
59 | 61 | ||
62 | + //TODO: only display applicable scopes (will need to pass them into handleLogin) | ||
60 | popup = popupDialog.find('ul.api-popup-scopes').empty(); | 63 | popup = popupDialog.find('ul.api-popup-scopes').empty(); |
61 | for (i = 0; i < scopes.length; i ++) { | 64 | for (i = 0; i < scopes.length; i ++) { |
62 | scope = scopes[i]; | 65 | scope = scopes[i]; |
63 | - str = '<li><input type="checkbox" id="scope_' + i + '" scope="' + scope.scope + '"/>' + '<label for="scope_' + i + '">' + scope.scope; | 66 | + str = '<li><input type="checkbox" id="scope_' + i + '" scope="' + scope.scope + '"' +'" oauthtype="' + scope.OAuthSchemeKey +'"/>' + '<label for="scope_' + i + '">' + scope.scope ; |
64 | if (scope.description) { | 67 | if (scope.description) { |
65 | - str += '<br/><span class="api-scope-desc">' + scope.description + '</span>'; | 68 | + if ($.map(auths, function(n, i) { return i; }).length > 1) //if we have more than one scheme, display schemes |
69 | + str += '<br/><span class="api-scope-desc">' + scope.description + ' ('+ scope.OAuthSchemeKey+')' +'</span>'; | ||
70 | + else | ||
71 | + str += '<br/><span class="api-scope-desc">' + scope.description + '</span>'; | ||
66 | } | 72 | } |
67 | str += '</label></li>'; | 73 | str += '</label></li>'; |
68 | popup.append(str); | 74 | popup.append(str); |
... | @@ -100,9 +106,25 @@ function handleLogin() { | ... | @@ -100,9 +106,25 @@ function handleLogin() { |
100 | var defaultRedirectUrl = host.protocol + '//' + host.host + pathname + '/o2c.html'; | 106 | var defaultRedirectUrl = host.protocol + '//' + host.host + pathname + '/o2c.html'; |
101 | var redirectUrl = window.oAuthRedirectUrl || defaultRedirectUrl; | 107 | var redirectUrl = window.oAuthRedirectUrl || defaultRedirectUrl; |
102 | var url = null; | 108 | var url = null; |
103 | - | 109 | + var scopes = [] |
104 | - for (var key in authSchemes) { | 110 | + var o = popup.find('input:checked'); |
105 | - if (authSchemes.hasOwnProperty(key)) { | 111 | + var OAuthSchemeKeys = []; |
112 | + var state; | ||
113 | + for(k =0; k < o.length; k++) { | ||
114 | + var scope = $(o[k]).attr('scope'); | ||
115 | + if (scopes.indexOf(scope) === -1) | ||
116 | + scopes.push(scope); | ||
117 | + var OAuthSchemeKey = $(o[k]).attr('oauthtype'); | ||
118 | + if (OAuthSchemeKeys.indexOf(OAuthSchemeKey) === -1) | ||
119 | + OAuthSchemeKeys.push(OAuthSchemeKey); | ||
120 | + } | ||
121 | + | ||
122 | + //TODO: merge not replace if scheme is different from any existing | ||
123 | + //(needs to be aware of schemes to do so correctly) | ||
124 | + window.enabledScopes=scopes; | ||
125 | + | ||
126 | + for (var key in authSchemes) { | ||
127 | + if (authSchemes.hasOwnProperty(key) && OAuthSchemeKeys.indexOf(key) != -1) { //only look at keys that match this scope. | ||
106 | var flow = authSchemes[key].flow; | 128 | var flow = authSchemes[key].flow; |
107 | 129 | ||
108 | if(authSchemes[key].type === 'oauth2' && flow && (flow === 'implicit' || flow === 'accessCode')) { | 130 | if(authSchemes[key].type === 'oauth2' && flow && (flow === 'implicit' || flow === 'accessCode')) { |
... | @@ -110,7 +132,14 @@ function handleLogin() { | ... | @@ -110,7 +132,14 @@ function handleLogin() { |
110 | url = dets.authorizationUrl + '?response_type=' + (flow === 'implicit' ? 'token' : 'code'); | 132 | url = dets.authorizationUrl + '?response_type=' + (flow === 'implicit' ? 'token' : 'code'); |
111 | window.swaggerUi.tokenName = dets.tokenName || 'access_token'; | 133 | window.swaggerUi.tokenName = dets.tokenName || 'access_token'; |
112 | window.swaggerUi.tokenUrl = (flow === 'accessCode' ? dets.tokenUrl : null); | 134 | window.swaggerUi.tokenUrl = (flow === 'accessCode' ? dets.tokenUrl : null); |
135 | + state = key; | ||
113 | } | 136 | } |
137 | + else if(authSchemes[key].type === 'oauth2' && flow && (flow === 'application')) { | ||
138 | + var dets = authSchemes[key]; | ||
139 | + window.swaggerUi.tokenName = dets.tokenName || 'access_token'; | ||
140 | + clientCredentialsFlow(scopes, dets.tokenUrl, key); | ||
141 | + return; | ||
142 | + } | ||
114 | else if(authSchemes[key].grantTypes) { | 143 | else if(authSchemes[key].grantTypes) { |
115 | // 1.2 support | 144 | // 1.2 support |
116 | var o = authSchemes[key].grantTypes; | 145 | var o = authSchemes[key].grantTypes; |
... | @@ -131,28 +160,17 @@ function handleLogin() { | ... | @@ -131,28 +160,17 @@ function handleLogin() { |
131 | } | 160 | } |
132 | } | 161 | } |
133 | } | 162 | } |
134 | - var scopes = [] | ||
135 | - var o = $('.api-popup-scopes').find('input:checked'); | ||
136 | - | ||
137 | - for(k =0; k < o.length; k++) { | ||
138 | - var scope = $(o[k]).attr('scope'); | ||
139 | - | ||
140 | - if (scopes.indexOf(scope) === -1) | ||
141 | - scopes.push(scope); | ||
142 | - } | ||
143 | - | ||
144 | - // Implicit auth recommends a state parameter. | ||
145 | - var state = Math.random (); | ||
146 | - | ||
147 | - window.enabledScopes=scopes; | ||
148 | 163 | ||
149 | redirect_uri = redirectUrl; | 164 | redirect_uri = redirectUrl; |
150 | 165 | ||
151 | url += '&redirect_uri=' + encodeURIComponent(redirectUrl); | 166 | url += '&redirect_uri=' + encodeURIComponent(redirectUrl); |
152 | url += '&realm=' + encodeURIComponent(realm); | 167 | url += '&realm=' + encodeURIComponent(realm); |
153 | url += '&client_id=' + encodeURIComponent(clientId); | 168 | url += '&client_id=' + encodeURIComponent(clientId); |
154 | - url += '&scope=' + encodeURIComponent(scopes.join(' ')); | 169 | + url += '&scope=' + encodeURIComponent(scopes.join(scopeSeparator)); |
155 | url += '&state=' + encodeURIComponent(state); | 170 | url += '&state=' + encodeURIComponent(state); |
171 | + for (var key in additionalQueryStringParams) { | ||
172 | + url += '&' + key + '=' + encodeURIComponent(additionalQueryStringParams[key]); | ||
173 | + } | ||
156 | 174 | ||
157 | window.open(url); | 175 | window.open(url); |
158 | }); | 176 | }); |
... | @@ -164,8 +182,8 @@ function handleLogin() { | ... | @@ -164,8 +182,8 @@ function handleLogin() { |
164 | 182 | ||
165 | 183 | ||
166 | function handleLogout() { | 184 | function handleLogout() { |
167 | - for(key in window.authorizations.authz){ | 185 | + for(key in window.swaggerUi.api.clientAuthorizations.authz){ |
168 | - window.authorizations.remove(key) | 186 | + window.swaggerUi.api.clientAuthorizations.remove(key) |
169 | } | 187 | } |
170 | window.enabledScopes = null; | 188 | window.enabledScopes = null; |
171 | $('.api-ic.ic-on').addClass('ic-off'); | 189 | $('.api-ic.ic-on').addClass('ic-off'); |
... | @@ -184,7 +202,10 @@ function initOAuth(opts) { | ... | @@ -184,7 +202,10 @@ function initOAuth(opts) { |
184 | popupMask = (o.popupMask||$('#api-common-mask')); | 202 | popupMask = (o.popupMask||$('#api-common-mask')); |
185 | popupDialog = (o.popupDialog||$('.api-popup-dialog')); | 203 | popupDialog = (o.popupDialog||$('.api-popup-dialog')); |
186 | clientId = (o.clientId||errors.push('missing client id')); | 204 | clientId = (o.clientId||errors.push('missing client id')); |
205 | + clientSecret = (o.clientSecret||null); | ||
187 | realm = (o.realm||errors.push('missing realm')); | 206 | realm = (o.realm||errors.push('missing realm')); |
207 | + scopeSeparator = (o.scopeSeparator||' '); | ||
208 | + additionalQueryStringParams = (o.additionalQueryStringParams||{}); | ||
188 | 209 | ||
189 | if(errors.length > 0){ | 210 | if(errors.length > 0){ |
190 | log('auth unable initialize oauth: ' + errors); | 211 | log('auth unable initialize oauth: ' + errors); |
... | @@ -203,13 +224,43 @@ function initOAuth(opts) { | ... | @@ -203,13 +224,43 @@ function initOAuth(opts) { |
203 | }); | 224 | }); |
204 | } | 225 | } |
205 | 226 | ||
227 | +function clientCredentialsFlow(scopes, tokenUrl, OAuthSchemeKey) { | ||
228 | + var params = { | ||
229 | + 'client_id': clientId, | ||
230 | + 'client_secret': clientSecret, | ||
231 | + 'scope': scopes.join(' '), | ||
232 | + 'grant_type': 'client_credentials' | ||
233 | + } | ||
234 | + $.ajax( | ||
235 | + { | ||
236 | + url : tokenUrl, | ||
237 | + type: "POST", | ||
238 | + data: params, | ||
239 | + success:function(data, textStatus, jqXHR) | ||
240 | + { | ||
241 | + onOAuthComplete(data,OAuthSchemeKey); | ||
242 | + }, | ||
243 | + error: function(jqXHR, textStatus, errorThrown) | ||
244 | + { | ||
245 | + onOAuthComplete(""); | ||
246 | + } | ||
247 | + }); | ||
248 | + | ||
249 | + } | ||
250 | + | ||
206 | window.processOAuthCode = function processOAuthCode(data) { | 251 | window.processOAuthCode = function processOAuthCode(data) { |
252 | + var OAuthSchemeKey = data.state; | ||
207 | var params = { | 253 | var params = { |
208 | 'client_id': clientId, | 254 | 'client_id': clientId, |
209 | 'code': data.code, | 255 | 'code': data.code, |
210 | 'grant_type': 'authorization_code', | 256 | 'grant_type': 'authorization_code', |
211 | 'redirect_uri': redirect_uri | 257 | 'redirect_uri': redirect_uri |
258 | + }; | ||
259 | + | ||
260 | + if (clientSecret) { | ||
261 | + params.client_secret = clientSecret; | ||
212 | } | 262 | } |
263 | + | ||
213 | $.ajax( | 264 | $.ajax( |
214 | { | 265 | { |
215 | url : window.swaggerUi.tokenUrl, | 266 | url : window.swaggerUi.tokenUrl, |
... | @@ -217,16 +268,16 @@ window.processOAuthCode = function processOAuthCode(data) { | ... | @@ -217,16 +268,16 @@ window.processOAuthCode = function processOAuthCode(data) { |
217 | data: params, | 268 | data: params, |
218 | success:function(data, textStatus, jqXHR) | 269 | success:function(data, textStatus, jqXHR) |
219 | { | 270 | { |
220 | - onOAuthComplete(data); | 271 | + onOAuthComplete(data, OAuthSchemeKey); |
221 | }, | 272 | }, |
222 | error: function(jqXHR, textStatus, errorThrown) | 273 | error: function(jqXHR, textStatus, errorThrown) |
223 | { | 274 | { |
224 | onOAuthComplete(""); | 275 | onOAuthComplete(""); |
225 | } | 276 | } |
226 | }); | 277 | }); |
227 | -} | 278 | +}; |
228 | 279 | ||
229 | -window.onOAuthComplete = function onOAuthComplete(token) { | 280 | +window.onOAuthComplete = function onOAuthComplete(token,OAuthSchemeKey) { |
230 | if(token) { | 281 | if(token) { |
231 | if(token.error) { | 282 | if(token.error) { |
232 | var checkbox = $('input[type=checkbox],.secured') | 283 | var checkbox = $('input[type=checkbox],.secured') |
... | @@ -236,11 +287,14 @@ window.onOAuthComplete = function onOAuthComplete(token) { | ... | @@ -236,11 +287,14 @@ window.onOAuthComplete = function onOAuthComplete(token) { |
236 | alert(token.error); | 287 | alert(token.error); |
237 | } | 288 | } |
238 | else { | 289 | else { |
239 | - var b = token[window.swaggerUi.tokenName]; | 290 | + var b = token[window.swaggerUi.tokenName]; |
291 | + if (!OAuthSchemeKey){ | ||
292 | + OAuthSchemeKey = token.state; | ||
293 | + } | ||
240 | if(b){ | 294 | if(b){ |
241 | // if all roles are satisfied | 295 | // if all roles are satisfied |
242 | var o = null; | 296 | var o = null; |
243 | - $.each($('.auth #api_information_panel'), function(k, v) { | 297 | + $.each($('.auth .api-ic .api_information_panel'), function(k, v) { |
244 | var children = v; | 298 | var children = v; |
245 | if(children && children.childNodes) { | 299 | if(children && children.childNodes) { |
246 | var requiredScopes = []; | 300 | var requiredScopes = []; |
... | @@ -257,7 +311,7 @@ window.onOAuthComplete = function onOAuthComplete(token) { | ... | @@ -257,7 +311,7 @@ window.onOAuthComplete = function onOAuthComplete(token) { |
257 | } | 311 | } |
258 | } | 312 | } |
259 | if(diff.length > 0){ | 313 | if(diff.length > 0){ |
260 | - o = v.parentNode; | 314 | + o = v.parentNode.parentNode; |
261 | $(o.parentNode).find('.api-ic.ic-on').addClass('ic-off'); | 315 | $(o.parentNode).find('.api-ic.ic-on').addClass('ic-off'); |
262 | $(o.parentNode).find('.api-ic.ic-on').removeClass('ic-on'); | 316 | $(o.parentNode).find('.api-ic.ic-on').removeClass('ic-on'); |
263 | 317 | ||
... | @@ -266,7 +320,7 @@ window.onOAuthComplete = function onOAuthComplete(token) { | ... | @@ -266,7 +320,7 @@ window.onOAuthComplete = function onOAuthComplete(token) { |
266 | $(o).find('.api-ic').removeClass('ic-error'); | 320 | $(o).find('.api-ic').removeClass('ic-error'); |
267 | } | 321 | } |
268 | else { | 322 | else { |
269 | - o = v.parentNode; | 323 | + o = v.parentNode.parentNode; |
270 | $(o.parentNode).find('.api-ic.ic-off').addClass('ic-on'); | 324 | $(o.parentNode).find('.api-ic.ic-off').addClass('ic-on'); |
271 | $(o.parentNode).find('.api-ic.ic-off').removeClass('ic-off'); | 325 | $(o.parentNode).find('.api-ic.ic-off').removeClass('ic-off'); |
272 | 326 | ||
... | @@ -277,8 +331,8 @@ window.onOAuthComplete = function onOAuthComplete(token) { | ... | @@ -277,8 +331,8 @@ window.onOAuthComplete = function onOAuthComplete(token) { |
277 | } | 331 | } |
278 | } | 332 | } |
279 | }); | 333 | }); |
280 | - window.swaggerUi.api.clientAuthorizations.add(oauth2KeyName, new SwaggerClient.ApiKeyAuthorization('Authorization', 'Bearer ' + b, 'header')); | 334 | + window.swaggerUi.api.clientAuthorizations.add(OAuthSchemeKey, new SwaggerClient.ApiKeyAuthorization('Authorization', 'Bearer ' + b, 'header')); |
281 | } | 335 | } |
282 | } | 336 | } |
283 | } | 337 | } |
284 | -} | 338 | +}; | ... | ... |
This diff is collapsed. Click to expand it.
This diff could not be displayed because it is too large.
-
Please register or login to post a comment