merged from auth_2.0 branch

This commit is contained in:
Tony Tam
2014-11-11 00:16:13 -08:00
parent 8e20a32344
commit 329772af4c
11 changed files with 168 additions and 355 deletions

79
dist/css/screen.css vendored
View File

@@ -1100,85 +1100,6 @@
.swagger-section .api-popup-actions { .swagger-section .api-popup-actions {
padding-top: 10px; padding-top: 10px;
} }
.auth {
text-align: right;
height: 15px;
float: right;
clear: both;
display: inline-block;
position: relative;
z-index: 3;
}
.auth_icon {
float: right;
}
.auth_container_2 {
visibility: visible;
position: absolute;
width: 250px;
margin-top: 26px;
float: left;
display: none;
border: solid 2px;
background: white;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
-o-border-radius: 4px;
-ms-border-radius: 4px;
-khtml-border-radius: 4px;
z-index: 2;
}
.auth_label {
text-align: left;
clear: left;
float: left;
padding-left: 10px;
width: 90px;
}
.auth_submit {
border-left: 1px;
border-right: 1px;
margin-top: 25px;
margin-bottom: 25px;
text-align: center;
}
.auth_button {
display: block;
float: right;
text-align: right;
}
.auth_submit_button {
display: block;
text-decoration: none;
font-weight: bold;
padding: 6px 8px;
font-size: 0.9em;
color: white;
float: right;
text-align: center;
background: #547f00;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
-o-border-radius: 4px;
-ms-border-radius: 4px;
-khtml-border-radius: 4px;
margin-top: 10px;
margin-bottom: 10px;
margin-right: 10px;
}
.auth_input {
float: left;
}
.authentication_container {
float: left;
display: block;
background: yellow;
}
.auth_button .auth_icon {
width: 25px;
height: 25px;
cursor: pointer;
}
.swagger-section .access { .swagger-section .access {
float: right; float: right;
} }

View File

@@ -389,6 +389,9 @@ SwaggerClient.prototype.buildFromSpec = function(response) {
this.produces = response.produces; this.produces = response.produces;
this.securityDefinitions = response.securityDefinitions; this.securityDefinitions = response.securityDefinitions;
// legacy support
this.authSchemes = response.securityDefinitions;
var location = this.parseUri(this.url); var location = this.parseUri(this.url);
if(typeof this.schemes === 'undefined' || this.schemes.length === 0) { if(typeof this.schemes === 'undefined' || this.schemes.length === 0) {
this.scheme = location.scheme; this.scheme = location.scheme;

View File

@@ -7,10 +7,29 @@ var realm;
function handleLogin() { function handleLogin() {
var scopes = []; var scopes = [];
if(window.swaggerUi.api.authSchemes var auths = window.swaggerUi.api.authSchemes || window.swaggerUi.api.securityDefinitions;
&& window.swaggerUi.api.authSchemes.oauth2 if(auths) {
&& window.swaggerUi.api.authSchemes.oauth2.scopes) { var key;
scopes = window.swaggerUi.api.authSchemes.oauth2.scopes; var defs = auths;
for(key in defs) {
var auth = defs[key];
if(auth.type === 'oauth2' && auth.scopes) {
var scope;
if(Array.isArray(auth.scopes)) {
// 1.2 support
var i;
for(i = 0; i < auth.scopes.length; i++) {
scopes.push(auth.scopes[i]);
}
}
else {
// 2.0 support
for(scope in auth.scopes) {
scopes.push({scope: scope, description: auth.scopes[scope]});
}
}
}
}
} }
if(window.swaggerUi.api if(window.swaggerUi.api
@@ -18,36 +37,32 @@ function handleLogin() {
appName = window.swaggerUi.api.info.title; appName = window.swaggerUi.api.info.title;
} }
if(popupDialog.length > 0) popupDialog = $(
popupDialog = popupDialog.last(); [
else { '<div class="api-popup-dialog">',
popupDialog = $( '<div class="api-popup-title">Select OAuth2.0 Scopes</div>',
[ '<div class="api-popup-content">',
'<div class="api-popup-dialog">', '<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.',
'<div class="api-popup-title">Select OAuth2.0 Scopes</div>', '<a href="#">Learn how to use</a>',
'<div class="api-popup-content">', '</p>',
'<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.', '<p><strong>' + appName + '</strong> API requires the following scopes. Select which ones you want to grant to Swagger UI.</p>',
'<a href="#">Learn how to use</a>', '<ul class="api-popup-scopes">',
'</p>', '</ul>',
'<p><strong>' + appName + '</strong> API requires the following scopes. Select which ones you want to grant to Swagger UI.</p>', '<p class="error-msg"></p>',
'<ul class="api-popup-scopes">', '<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>',
'</ul>', '</div>',
'<p class="error-msg"></p>', '</div>'].join(''));
'<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>', $(document.body).append(popupDialog);
'</div>',
'</div>'].join(''));
$(document.body).append(popupDialog);
popup = popupDialog.find('ul.api-popup-scopes').empty(); popup = popupDialog.find('ul.api-popup-scopes').empty();
for (i = 0; i < scopes.length; i ++) { for (i = 0; i < scopes.length; i ++) {
scope = scopes[i]; scope = scopes[i];
str = '<li><input type="checkbox" id="scope_' + i + '" scope="' + scope.scope + '"/>' + '<label for="scope_' + i + '">' + scope.scope; str = '<li><input type="checkbox" id="scope_' + i + '" scope="' + scope.scope + '"/>' + '<label for="scope_' + i + '">' + scope.scope;
if (scope.description) { if (scope.description) {
str += '<br/><span class="api-scope-desc">' + scope.description + '</span>'; str += '<br/><span class="api-scope-desc">' + scope.description + '</span>';
}
str += '</label></li>';
popup.append(str);
} }
str += '</label></li>';
popup.append(str);
} }
var $win = $(window), var $win = $(window),
@@ -67,7 +82,10 @@ function handleLogin() {
popupDialog.find('button.api-popup-cancel').click(function() { popupDialog.find('button.api-popup-cancel').click(function() {
popupMask.hide(); popupMask.hide();
popupDialog.hide(); popupDialog.hide();
popupDialog.empty();
popupDialog = [];
}); });
popupDialog.find('button.api-popup-authbtn').click(function() { popupDialog.find('button.api-popup-authbtn').click(function() {
popupMask.hide(); popupMask.hide();
popupDialog.hide(); popupDialog.hide();
@@ -75,17 +93,26 @@ function handleLogin() {
var authSchemes = window.swaggerUi.api.authSchemes; var authSchemes = window.swaggerUi.api.authSchemes;
var host = window.location; var host = window.location;
var pathname = location.pathname.substring(0, location.pathname.lastIndexOf("/")); var pathname = location.pathname.substring(0, location.pathname.lastIndexOf("/"));
var redirectUrl = host.protocol + '//' + host.host + pathname + "/o2c.html"; var redirectUrl = host.protocol + '//' + host.host + pathname + '/o2c.html';
var url = null; var url = null;
for (var key in authSchemes) { for (var key in authSchemes) {
if (authSchemes.hasOwnProperty(key)) { if (authSchemes.hasOwnProperty(key)) {
var o = authSchemes[key].grantTypes; if(authSchemes[key].type === 'oauth2' && authSchemes[key].flow === 'implicit') {
for(var t in o) { var dets = authSchemes[key];
if(o.hasOwnProperty(t) && t === 'implicit') { url = dets.authorizationUrl + '?response_type=token';
var dets = o[t]; window.swaggerUi.tokenName = dets.tokenUrl || 'access_token';
url = dets.loginEndpoint.url + "?response_type=token"; }
window.swaggerUi.tokenName = dets.tokenName; else if(authSchemes[key].grantTypes) {
// 1.2 support
var o = authSchemes[key].grantTypes;
for(var t in o) {
if(o.hasOwnProperty(t) && t === 'implicit') {
var dets = o[t];
var ep = dets.loginEndpoint.url;
url = dets.loginEndpoint.url + '?response_type=token';
window.swaggerUi.tokenName = dets.tokenName;
}
} }
} }
} }
@@ -94,7 +121,7 @@ function handleLogin() {
var o = $('.api-popup-scopes').find('input:checked'); var o = $('.api-popup-scopes').find('input:checked');
for(k =0; k < o.length; k++) { for(k =0; k < o.length; k++) {
scopes.push($(o[k]).attr("scope")); scopes.push($(o[k]).attr('scope'));
} }
window.enabledScopes=scopes; window.enabledScopes=scopes;
@@ -130,14 +157,14 @@ function initOAuth(opts) {
var o = (opts||{}); var o = (opts||{});
var errors = []; var errors = [];
appName = (o.appName||errors.push("missing appName")); appName = (o.appName||errors.push('missing appName'));
popupMask = (o.popupMask||$('#api-common-mask')); popupMask = (o.popupMask||$('#api-common-mask'));
popupDialog = (o.popupDialog||$('.api-popup-dialog')); popupDialog = (o.popupDialog||$('.api-popup-dialog'));
clientId = (o.clientId||errors.push("missing client id")); clientId = (o.clientId||errors.push('missing client id'));
realm = (o.realm||errors.push("missing realm")); realm = (o.realm||errors.push('missing realm'));
if(errors.length > 0){ if(errors.length > 0){
log("auth unable initialize oauth: " + errors); log('auth unable initialize oauth: ' + errors);
return; return;
} }
@@ -203,8 +230,7 @@ function onOAuthComplete(token) {
} }
} }
}); });
window.authorizations.add('oauth2', new ApiKeyAuthorization('Authorization', 'Bearer ' + b, 'header'));
window.authorizations.add("oauth2", new ApiKeyAuthorization("Authorization", "Bearer " + b, "header"));
} }
} }
} }

28
dist/lib/swagger.js vendored
View File

@@ -1,5 +1,5 @@
// swagger.js // swagger.js
// version 2.0.41 // version 2.0.42
(function () { (function () {
@@ -974,16 +974,22 @@
var queryParams = ""; var queryParams = "";
for (var i = 0; i < params.length; i++) { for (var i = 0; i < params.length; i++) {
var param = params[i]; var param = params[i];
if (param.paramType === 'query') { if (queryParams !== '')
if (args[param.name] !== undefined) { queryParams += '&';
if (queryParams !== '') if (Array.isArray(param)) {
queryParams += "&"; var j;
queryParams += encodeURIComponent(param.name) + '=' + encodeURIComponent(args[param.name]); var output = '';
} for(j = 0; j < param.length; j++) {
if(j > 0)
output += ',';
output += encodeURIComponent(param[j]);
}
queryParams += encodeURIComponent(param.name) + '=' + output;
}
else {
queryParams += encodeURIComponent(param.name) + '=' + encodeURIComponent(args[param.name]);
} }
} }
if ((queryParams != null) && queryParams.length > 0)
url += '?' + queryParams;
return url; return url;
}; };
@@ -1477,8 +1483,8 @@
data: response.content.data data: response.content.data
}; };
var contentType = (response._headers["content-type"] || response._headers["Content-Type"] || null) var headers = response._headers.normalized || response._headers;
var contentType = (headers["content-type"] || headers["Content-Type"] || null)
if (contentType != null) { if (contentType != null) {
if (contentType.indexOf("application/json") == 0 || contentType.indexOf("+json") > 0) { if (contentType.indexOf("application/json") == 0 || contentType.indexOf("+json") > 0) {
if (response.content.data && response.content.data !== "") if (response.content.data && response.content.data !== "")

1
dist/swagger-ui.js vendored
View File

@@ -1782,7 +1782,6 @@ helpers = this.merge(helpers, Handlebars.helpers); data = data || {};
this.model.description = this.model.description.replace(/(?:\r\n|\r|\n)/g, '<br />'); this.model.description = this.model.description.replace(/(?:\r\n|\r|\n)/g, '<br />');
} }
this.model.oauth = null; this.model.oauth = null;
log(this.model.authorizations);
if (this.model.authorizations) { if (this.model.authorizations) {
if (Array.isArray(this.model.authorizations)) { if (Array.isArray(this.model.authorizations)) {
_ref5 = this.model.authorizations; _ref5 = this.model.authorizations;

File diff suppressed because one or more lines are too long

View File

@@ -389,6 +389,9 @@ SwaggerClient.prototype.buildFromSpec = function(response) {
this.produces = response.produces; this.produces = response.produces;
this.securityDefinitions = response.securityDefinitions; this.securityDefinitions = response.securityDefinitions;
// legacy support
this.authSchemes = response.securityDefinitions;
var location = this.parseUri(this.url); var location = this.parseUri(this.url);
if(typeof this.schemes === 'undefined' || this.schemes.length === 0) { if(typeof this.schemes === 'undefined' || this.schemes.length === 0) {
this.scheme = location.scheme; this.scheme = location.scheme;

View File

@@ -7,10 +7,29 @@ var realm;
function handleLogin() { function handleLogin() {
var scopes = []; var scopes = [];
if(window.swaggerUi.api.authSchemes var auths = window.swaggerUi.api.authSchemes || window.swaggerUi.api.securityDefinitions;
&& window.swaggerUi.api.authSchemes.oauth2 if(auths) {
&& window.swaggerUi.api.authSchemes.oauth2.scopes) { var key;
scopes = window.swaggerUi.api.authSchemes.oauth2.scopes; var defs = auths;
for(key in defs) {
var auth = defs[key];
if(auth.type === 'oauth2' && auth.scopes) {
var scope;
if(Array.isArray(auth.scopes)) {
// 1.2 support
var i;
for(i = 0; i < auth.scopes.length; i++) {
scopes.push(auth.scopes[i]);
}
}
else {
// 2.0 support
for(scope in auth.scopes) {
scopes.push({scope: scope, description: auth.scopes[scope]});
}
}
}
}
} }
if(window.swaggerUi.api if(window.swaggerUi.api
@@ -18,36 +37,32 @@ function handleLogin() {
appName = window.swaggerUi.api.info.title; appName = window.swaggerUi.api.info.title;
} }
if(popupDialog.length > 0) popupDialog = $(
popupDialog = popupDialog.last(); [
else { '<div class="api-popup-dialog">',
popupDialog = $( '<div class="api-popup-title">Select OAuth2.0 Scopes</div>',
[ '<div class="api-popup-content">',
'<div class="api-popup-dialog">', '<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.',
'<div class="api-popup-title">Select OAuth2.0 Scopes</div>', '<a href="#">Learn how to use</a>',
'<div class="api-popup-content">', '</p>',
'<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.', '<p><strong>' + appName + '</strong> API requires the following scopes. Select which ones you want to grant to Swagger UI.</p>',
'<a href="#">Learn how to use</a>', '<ul class="api-popup-scopes">',
'</p>', '</ul>',
'<p><strong>' + appName + '</strong> API requires the following scopes. Select which ones you want to grant to Swagger UI.</p>', '<p class="error-msg"></p>',
'<ul class="api-popup-scopes">', '<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>',
'</ul>', '</div>',
'<p class="error-msg"></p>', '</div>'].join(''));
'<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>', $(document.body).append(popupDialog);
'</div>',
'</div>'].join(''));
$(document.body).append(popupDialog);
popup = popupDialog.find('ul.api-popup-scopes').empty(); popup = popupDialog.find('ul.api-popup-scopes').empty();
for (i = 0; i < scopes.length; i ++) { for (i = 0; i < scopes.length; i ++) {
scope = scopes[i]; scope = scopes[i];
str = '<li><input type="checkbox" id="scope_' + i + '" scope="' + scope.scope + '"/>' + '<label for="scope_' + i + '">' + scope.scope; str = '<li><input type="checkbox" id="scope_' + i + '" scope="' + scope.scope + '"/>' + '<label for="scope_' + i + '">' + scope.scope;
if (scope.description) { if (scope.description) {
str += '<br/><span class="api-scope-desc">' + scope.description + '</span>'; str += '<br/><span class="api-scope-desc">' + scope.description + '</span>';
}
str += '</label></li>';
popup.append(str);
} }
str += '</label></li>';
popup.append(str);
} }
var $win = $(window), var $win = $(window),
@@ -67,7 +82,10 @@ function handleLogin() {
popupDialog.find('button.api-popup-cancel').click(function() { popupDialog.find('button.api-popup-cancel').click(function() {
popupMask.hide(); popupMask.hide();
popupDialog.hide(); popupDialog.hide();
popupDialog.empty();
popupDialog = [];
}); });
popupDialog.find('button.api-popup-authbtn').click(function() { popupDialog.find('button.api-popup-authbtn').click(function() {
popupMask.hide(); popupMask.hide();
popupDialog.hide(); popupDialog.hide();
@@ -75,17 +93,26 @@ function handleLogin() {
var authSchemes = window.swaggerUi.api.authSchemes; var authSchemes = window.swaggerUi.api.authSchemes;
var host = window.location; var host = window.location;
var pathname = location.pathname.substring(0, location.pathname.lastIndexOf("/")); var pathname = location.pathname.substring(0, location.pathname.lastIndexOf("/"));
var redirectUrl = host.protocol + '//' + host.host + pathname + "/o2c.html"; var redirectUrl = host.protocol + '//' + host.host + pathname + '/o2c.html';
var url = null; var url = null;
for (var key in authSchemes) { for (var key in authSchemes) {
if (authSchemes.hasOwnProperty(key)) { if (authSchemes.hasOwnProperty(key)) {
var o = authSchemes[key].grantTypes; if(authSchemes[key].type === 'oauth2' && authSchemes[key].flow === 'implicit') {
for(var t in o) { var dets = authSchemes[key];
if(o.hasOwnProperty(t) && t === 'implicit') { url = dets.authorizationUrl + '?response_type=token';
var dets = o[t]; window.swaggerUi.tokenName = dets.tokenUrl || 'access_token';
url = dets.loginEndpoint.url + "?response_type=token"; }
window.swaggerUi.tokenName = dets.tokenName; else if(authSchemes[key].grantTypes) {
// 1.2 support
var o = authSchemes[key].grantTypes;
for(var t in o) {
if(o.hasOwnProperty(t) && t === 'implicit') {
var dets = o[t];
var ep = dets.loginEndpoint.url;
url = dets.loginEndpoint.url + '?response_type=token';
window.swaggerUi.tokenName = dets.tokenName;
}
} }
} }
} }
@@ -94,7 +121,7 @@ function handleLogin() {
var o = $('.api-popup-scopes').find('input:checked'); var o = $('.api-popup-scopes').find('input:checked');
for(k =0; k < o.length; k++) { for(k =0; k < o.length; k++) {
scopes.push($(o[k]).attr("scope")); scopes.push($(o[k]).attr('scope'));
} }
window.enabledScopes=scopes; window.enabledScopes=scopes;
@@ -130,14 +157,14 @@ function initOAuth(opts) {
var o = (opts||{}); var o = (opts||{});
var errors = []; var errors = [];
appName = (o.appName||errors.push("missing appName")); appName = (o.appName||errors.push('missing appName'));
popupMask = (o.popupMask||$('#api-common-mask')); popupMask = (o.popupMask||$('#api-common-mask'));
popupDialog = (o.popupDialog||$('.api-popup-dialog')); popupDialog = (o.popupDialog||$('.api-popup-dialog'));
clientId = (o.clientId||errors.push("missing client id")); clientId = (o.clientId||errors.push('missing client id'));
realm = (o.realm||errors.push("missing realm")); realm = (o.realm||errors.push('missing realm'));
if(errors.length > 0){ if(errors.length > 0){
log("auth unable initialize oauth: " + errors); log('auth unable initialize oauth: ' + errors);
return; return;
} }
@@ -203,8 +230,7 @@ function onOAuthComplete(token) {
} }
} }
}); });
window.authorizations.add('oauth2', new ApiKeyAuthorization('Authorization', 'Bearer ' + b, 'header'));
window.authorizations.add("oauth2", new ApiKeyAuthorization("Authorization", "Bearer " + b, "header"));
} }
} }
} }

View File

@@ -52,7 +52,6 @@ class OperationView extends Backbone.View
if @model.description if @model.description
@model.description = @model.description.replace(/(?:\r\n|\r|\n)/g, '<br />') @model.description = @model.description.replace(/(?:\r\n|\r|\n)/g, '<br />')
@model.oauth = null @model.oauth = null
log @model.authorizations
if @model.authorizations if @model.authorizations
if Array.isArray @model.authorizations if Array.isArray @model.authorizations
for auths in @model.authorizations for auths in @model.authorizations

View File

@@ -1100,85 +1100,6 @@
.swagger-section .api-popup-actions { .swagger-section .api-popup-actions {
padding-top: 10px; padding-top: 10px;
} }
.auth {
text-align: right;
height: 15px;
float: right;
clear: both;
display: inline-block;
position: relative;
z-index: 3;
}
.auth_icon {
float: right;
}
.auth_container_2 {
visibility: visible;
position: absolute;
width: 250px;
margin-top: 26px;
float: left;
display: none;
border: solid 2px;
background: white;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
-o-border-radius: 4px;
-ms-border-radius: 4px;
-khtml-border-radius: 4px;
z-index: 2;
}
.auth_label {
text-align: left;
clear: left;
float: left;
padding-left: 10px;
width: 90px;
}
.auth_submit {
border-left: 1px;
border-right: 1px;
margin-top: 25px;
margin-bottom: 25px;
text-align: center;
}
.auth_button {
display: block;
float: right;
text-align: right;
}
.auth_submit_button {
display: block;
text-decoration: none;
font-weight: bold;
padding: 6px 8px;
font-size: 0.9em;
color: white;
float: right;
text-align: center;
background: #547f00;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
-o-border-radius: 4px;
-ms-border-radius: 4px;
-khtml-border-radius: 4px;
margin-top: 10px;
margin-bottom: 10px;
margin-right: 10px;
}
.auth_input {
float: left;
}
.authentication_container {
float: left;
display: block;
background: yellow;
}
.auth_button .auth_icon {
width: 25px;
height: 25px;
cursor: pointer;
}
.swagger-section .access { .swagger-section .access {
float: right; float: right;
} }

View File

@@ -81,94 +81,3 @@
} }
} }
.auth {
text-align: right;
height: 15px;
float: right;
clear: both;
display: inline-block;
position: relative;
z-index: 3;
}
.auth_icon {
float: right;
}
.auth_container_2 {
visibility: visible;
position: absolute;
width: 250px;
float:left;
margin-top: 26px;
float: left;
display: none;
border: solid 2px;
background: white;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
-o-border-radius: 4px;
-ms-border-radius: 4px;
-khtml-border-radius: 4px;
z-index: 2;
}
.auth_label {
text-align: left;
clear: left;
float: left;
padding-left: 10px;
width: 90px;
}
.auth_submit {
border-left: 1px;
border-right: 1px;
margin-top: 25px;
margin-bottom: 25px;
text-align: center;
}
.auth_button {
display: block;
float: right;
text-align: right;
}
.auth_submit_button {
display: block;
text-decoration: none;
font-weight: bold;
padding: 6px 8px;
font-size: 0.9em;
color: white;
float: right;
text-align: center;
background: #547f00;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
-o-border-radius: 4px;
-ms-border-radius: 4px;
-khtml-border-radius: 4px;
margin-top: 10px;
margin-bottom: 10px;
margin-right: 10px;
}
.auth_input {
float: left;
}
.authentication_container {
float: left;
display: block;
background: yellow;
}
.auth_button .auth_icon {
width: 25px;
height: 25px;
cursor: pointer;
}