adding swagger-ui
[cdmlib.git] / cdmlib-remote-webapp / src / main / webapp / swagger-ui / lib / swagger-oauth.js
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 redirectUrl = host.protocol + '//' + host.host + "/o2c.html";
78 var url = null;
79
80 var p = window.swaggerUi.api.authSchemes;
81 for (var key in p) {
82 if (p.hasOwnProperty(key)) {
83 var o = p[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("key", new ApiKeyAuthorization("Authorization", "Bearer " + b, "header"));
208 }
209 }
210 }
211 }