Skip to content

Commit 9ac2bfd

Browse files
committed
Added a Rake task for updating swagger-ui.
1 parent 5408f89 commit 9ac2bfd

File tree

18 files changed

+2654
-4081
lines changed

18 files changed

+2654
-4081
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,7 @@ GrapeSwaggerRails.options.authentication_proc: Proc.new{|request| # return a boo
9898
3. Commit your changes (`git commit -am 'Add some feature'`)
9999
4. Push to the branch (`git push origin my-new-feature`)
100100
5. Create new Pull Request
101+
102+
### Updating Swagger UI from Dist
103+
104+
To update Swagger UI from its [distribution](https://github.com/wordnik/swagger-ui), run 'bundle exec rake swagger_ui:dist:update'. Examine the changes carefully.

Rakefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
require "bundler/gem_tasks"
22

3+
Dir.glob('lib/tasks/*.rake').each do |r|
4+
import r
5+
end
6+
37
desc "Run tests within test-app."
48
task :tests do
59
path = File.expand_path('../test-app', __FILE__)
5.63 KB
Loading
Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
1-
// This is a manifest file that'll be compiled into application.js, which will include all the files
2-
// listed below.
3-
//
4-
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5-
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6-
//
7-
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8-
// the compiled file.
9-
//
10-
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
11-
// GO AFTER THE REQUIRES BELOW.
12-
//
131
//= require ./shred.bundle
142
//= require ./jquery-1.8.0.min
153
//= require ./jquery.slideto.min
@@ -19,6 +7,7 @@
197
//= require ./underscore-min
208
//= require ./backbone-min
219
//= require ./swagger
22-
//= require ./swagger-ui
10+
//= require ./swagger-ui.min
2311
//= require ./highlight.7.3.pack
12+
//= require ./swagger-oauth
2413
//= require ./base64
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
var appName;
2+
var popupMask;
3+
var popupDialog;
4+
var clientId;
5+
var realm;
6+
7+
function handleLogin() {
8+
var scopes = [];
9+
10+
if(window.swaggerUi.api.authSchemes
11+
&& window.swaggerUi.api.authSchemes.oauth2
12+
&& window.swaggerUi.api.authSchemes.oauth2.scopes) {
13+
scopes = window.swaggerUi.api.authSchemes.oauth2.scopes;
14+
}
15+
16+
if(window.swaggerUi.api
17+
&& window.swaggerUi.api.info) {
18+
appName = window.swaggerUi.api.info.title;
19+
}
20+
21+
if(popupDialog.length > 0)
22+
popupDialog = popupDialog.last();
23+
else {
24+
popupDialog = $(
25+
[
26+
'<div class="api-popup-dialog">',
27+
'<div class="api-popup-title">Select OAuth2.0 Scopes</div>',
28+
'<div class="api-popup-content">',
29+
'<p>Scopes are used to grant an application different levels of access to data on behalf of the end user. Each API may declare one or more scopes.',
30+
'<a href="#">Learn how to use</a>',
31+
'</p>',
32+
'<p><strong>' + appName + '</strong> API requires the following scopes. Select which ones you want to grant to Swagger UI.</p>',
33+
'<ul class="api-popup-scopes">',
34+
'</ul>',
35+
'<p class="error-msg"></p>',
36+
'<div class="api-popup-actions"><button class="api-popup-authbtn api-button green" type="button">Authorize</button><button class="api-popup-cancel api-button gray" type="button">Cancel</button></div>',
37+
'</div>',
38+
'</div>'].join(''));
39+
$(document.body).append(popupDialog);
40+
41+
popup = popupDialog.find('ul.api-popup-scopes').empty();
42+
for (i = 0; i < scopes.length; i ++) {
43+
scope = scopes[i];
44+
str = '<li><input type="checkbox" id="scope_' + i + '" scope="' + scope.scope + '"/>' + '<label for="scope_' + i + '">' + scope.scope;
45+
if (scope.description) {
46+
str += '<br/><span class="api-scope-desc">' + scope.description + '</span>';
47+
}
48+
str += '</label></li>';
49+
popup.append(str);
50+
}
51+
}
52+
53+
var $win = $(window),
54+
dw = $win.width(),
55+
dh = $win.height(),
56+
st = $win.scrollTop(),
57+
dlgWd = popupDialog.outerWidth(),
58+
dlgHt = popupDialog.outerHeight(),
59+
top = (dh -dlgHt)/2 + st,
60+
left = (dw - dlgWd)/2;
61+
62+
popupDialog.css({
63+
top: (top < 0? 0 : top) + 'px',
64+
left: (left < 0? 0 : left) + 'px'
65+
});
66+
67+
popupDialog.find('button.api-popup-cancel').click(function() {
68+
popupMask.hide();
69+
popupDialog.hide();
70+
});
71+
popupDialog.find('button.api-popup-authbtn').click(function() {
72+
popupMask.hide();
73+
popupDialog.hide();
74+
75+
var authSchemes = window.swaggerUi.api.authSchemes;
76+
var host = window.location;
77+
var pathname = location.pathname.substring(0, location.pathname.lastIndexOf("/"));
78+
var redirectUrl = host.protocol + '//' + host.host + pathname + "/o2c.html";
79+
var url = null;
80+
81+
for (var key in authSchemes) {
82+
if (authSchemes.hasOwnProperty(key)) {
83+
var o = authSchemes[key].grantTypes;
84+
for(var t in o) {
85+
if(o.hasOwnProperty(t) && t === 'implicit') {
86+
var dets = o[t];
87+
url = dets.loginEndpoint.url + "?response_type=token";
88+
window.swaggerUi.tokenName = dets.tokenName;
89+
}
90+
}
91+
}
92+
}
93+
var scopes = []
94+
var o = $('.api-popup-scopes').find('input:checked');
95+
96+
for(k =0; k < o.length; k++) {
97+
scopes.push($(o[k]).attr("scope"));
98+
}
99+
100+
window.enabledScopes=scopes;
101+
102+
url += '&redirect_uri=' + encodeURIComponent(redirectUrl);
103+
url += '&realm=' + encodeURIComponent(realm);
104+
url += '&client_id=' + encodeURIComponent(clientId);
105+
url += '&scope=' + encodeURIComponent(scopes);
106+
107+
window.open(url);
108+
});
109+
110+
popupMask.show();
111+
popupDialog.show();
112+
return;
113+
}
114+
115+
116+
function handleLogout() {
117+
for(key in window.authorizations.authz){
118+
window.authorizations.remove(key)
119+
}
120+
window.enabledScopes = null;
121+
$('.api-ic.ic-on').addClass('ic-off');
122+
$('.api-ic.ic-on').removeClass('ic-on');
123+
124+
// set the info box
125+
$('.api-ic.ic-warning').addClass('ic-error');
126+
$('.api-ic.ic-warning').removeClass('ic-warning');
127+
}
128+
129+
function initOAuth(opts) {
130+
var o = (opts||{});
131+
var errors = [];
132+
133+
appName = (o.appName||errors.push("missing appName"));
134+
popupMask = (o.popupMask||$('#api-common-mask'));
135+
popupDialog = (o.popupDialog||$('.api-popup-dialog'));
136+
clientId = (o.clientId||errors.push("missing client id"));
137+
realm = (o.realm||errors.push("missing realm"));
138+
139+
if(errors.length > 0){
140+
log("auth unable initialize oauth: " + errors);
141+
return;
142+
}
143+
144+
$('pre code').each(function(i, e) {hljs.highlightBlock(e)});
145+
$('.api-ic').click(function(s) {
146+
if($(s.target).hasClass('ic-off'))
147+
handleLogin();
148+
else {
149+
handleLogout();
150+
}
151+
false;
152+
});
153+
}
154+
155+
function onOAuthComplete(token) {
156+
if(token) {
157+
if(token.error) {
158+
var checkbox = $('input[type=checkbox],.secured')
159+
checkbox.each(function(pos){
160+
checkbox[pos].checked = false;
161+
});
162+
alert(token.error);
163+
}
164+
else {
165+
var b = token[window.swaggerUi.tokenName];
166+
if(b){
167+
// if all roles are satisfied
168+
var o = null;
169+
$.each($('.auth #api_information_panel'), function(k, v) {
170+
var children = v;
171+
if(children && children.childNodes) {
172+
var requiredScopes = [];
173+
$.each((children.childNodes), function (k1, v1){
174+
var inner = v1.innerHTML;
175+
if(inner)
176+
requiredScopes.push(inner);
177+
});
178+
var diff = [];
179+
for(var i=0; i < requiredScopes.length; i++) {
180+
var s = requiredScopes[i];
181+
if(window.enabledScopes && window.enabledScopes.indexOf(s) == -1) {
182+
diff.push(s);
183+
}
184+
}
185+
if(diff.length > 0){
186+
o = v.parentNode;
187+
$(o.parentNode).find('.api-ic.ic-on').addClass('ic-off');
188+
$(o.parentNode).find('.api-ic.ic-on').removeClass('ic-on');
189+
190+
// sorry, not all scopes are satisfied
191+
$(o).find('.api-ic').addClass('ic-warning');
192+
$(o).find('.api-ic').removeClass('ic-error');
193+
}
194+
else {
195+
o = v.parentNode;
196+
$(o.parentNode).find('.api-ic.ic-off').addClass('ic-on');
197+
$(o.parentNode).find('.api-ic.ic-off').removeClass('ic-off');
198+
199+
// all scopes are satisfied
200+
$(o).find('.api-ic').addClass('ic-info');
201+
$(o).find('.api-ic').removeClass('ic-warning');
202+
$(o).find('.api-ic').removeClass('ic-error');
203+
}
204+
}
205+
});
206+
207+
window.authorizations.add("oauth2", new ApiKeyAuthorization("Authorization", "Bearer " + b, "header"));
208+
}
209+
}
210+
}
211+
}

0 commit comments

Comments
 (0)