swagui 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,218 @@
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 location = window.location;
77
+ var locationUrl = location.protocol + '//' + location.host + location.pathname;
78
+ var redirectUrl = locationUrl.replace("index.html","").concat("/o2c.html").replace("//o2c.html","/o2c.html");
79
+ var url = null;
80
+
81
+ var p = window.swaggerUi.api.authSchemes;
82
+ for (var key in p) {
83
+ if (p.hasOwnProperty(key)) {
84
+ var o = p[key].grantTypes;
85
+ for(var t in o) {
86
+ if(o.hasOwnProperty(t) && t === 'implicit') {
87
+ var dets = o[t];
88
+ url = dets.loginEndpoint.url + "?response_type=token";
89
+ window.swaggerUi.tokenName = dets.tokenName;
90
+ }
91
+ }
92
+ }
93
+ }
94
+ var scopes = [];
95
+ var scopeForUrl='';
96
+ var o = $('.api-popup-scopes').find('input:checked');
97
+
98
+ for(var k =0; k < o.length; k++) {
99
+ scopes.push($(o[k]).attr("scope"));
100
+ if(k > 0){
101
+ scopeForUrl+=' ';
102
+ }
103
+ scopeForUrl+=$(o[k]).attr("scope");
104
+ }
105
+
106
+ window.enabledScopes=scopes;
107
+
108
+
109
+ url += '&redirect_uri=' + encodeURIComponent(redirectUrl);
110
+ url += '&realm=' + encodeURIComponent(realm);
111
+ url += '&client_id=' + encodeURIComponent(clientId);
112
+ url += '&scope=' + encodeURIComponent(scopeForUrl);
113
+
114
+ window.open(url);
115
+ });
116
+ }
117
+ popupMask.show();
118
+ popupDialog.show();
119
+ return;
120
+ }
121
+
122
+
123
+ function handleLogout() {
124
+ for(key in window.authorizations.authz){
125
+ window.authorizations.remove(key)
126
+ }
127
+ window.enabledScopes = null;
128
+ $('.api-ic.ic-on').addClass('ic-off');
129
+ $('.api-ic.ic-on').removeClass('ic-on');
130
+
131
+ // set the info box
132
+ $('.api-ic.ic-warning').addClass('ic-error');
133
+ $('.api-ic.ic-warning').removeClass('ic-warning');
134
+ }
135
+
136
+ function initOAuth(opts) {
137
+ var o = (opts||{});
138
+ var errors = [];
139
+
140
+ appName = (o.appName||errors.push("missing appName"));
141
+ popupMask = (o.popupMask||$('#api-common-mask'));
142
+ popupDialog = (o.popupDialog||$('.api-popup-dialog'));
143
+ clientId = (o.clientId||errors.push("missing client id"));
144
+ realm = (o.realm||errors.push("missing realm"));
145
+
146
+ if(errors.length > 0){
147
+ log("auth unable initialize oauth: " + errors);
148
+ return;
149
+ }
150
+
151
+ $('pre code').each(function(i, e) {hljs.highlightBlock(e)});
152
+ $('.api-ic').click(function(s) {
153
+ if($(s.target).hasClass('ic-off'))
154
+ handleLogin();
155
+ else {
156
+ handleLogout();
157
+ }
158
+ false;
159
+ });
160
+ }
161
+
162
+ function onOAuthComplete(token) {
163
+ if(token) {
164
+ if(token.error) {
165
+ var checkbox = $('input[type=checkbox],.secured')
166
+ checkbox.each(function(pos){
167
+ checkbox[pos].checked = false;
168
+ });
169
+ alert(token.error);
170
+ }
171
+ else {
172
+ var b = token[window.swaggerUi.tokenName];
173
+ if(b){
174
+ // if all roles are satisfied
175
+ var o = null;
176
+ $.each($('.auth #api_information_panel'), function(k, v) {
177
+ var children = v;
178
+ if(children && children.childNodes) {
179
+ var requiredScopes = [];
180
+ $.each((children.childNodes), function (k1, v1){
181
+ var inner = v1.innerHTML;
182
+ if(inner)
183
+ requiredScopes.push(inner);
184
+ });
185
+ var diff = [];
186
+ for(var i=0; i < requiredScopes.length; i++) {
187
+ var s = requiredScopes[i];
188
+ if(window.enabledScopes && window.enabledScopes.indexOf(s) == -1) {
189
+ diff.push(s);
190
+ }
191
+ }
192
+ if(diff.length > 0){
193
+ o = v.parentNode;
194
+ $(o.parentNode).find('.api-ic.ic-on').addClass('ic-off');
195
+ $(o.parentNode).find('.api-ic.ic-on').removeClass('ic-on');
196
+
197
+ // sorry, not all scopes are satisfied
198
+ $(o).find('.api-ic').addClass('ic-warning');
199
+ $(o).find('.api-ic').removeClass('ic-error');
200
+ }
201
+ else {
202
+ o = v.parentNode;
203
+ $(o.parentNode).find('.api-ic.ic-off').addClass('ic-on');
204
+ $(o.parentNode).find('.api-ic.ic-off').removeClass('ic-off');
205
+
206
+ // all scopes are satisfied
207
+ $(o).find('.api-ic').addClass('ic-info');
208
+ $(o).find('.api-ic').removeClass('ic-warning');
209
+ $(o).find('.api-ic').removeClass('ic-error');
210
+ }
211
+ }
212
+ });
213
+
214
+ window.authorizations.add("key", new ApiKeyAuthorization("Authorization", "Bearer " + b, "header"));
215
+ }
216
+ }
217
+ }
218
+ }