Skip to content
This repository has been archived by the owner on Nov 13, 2022. It is now read-only.

oauth2 support #7

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ test/dummy/log/*.log
test/dummy/tmp/
test/dummy/.sass-cache
swagger_engine-*.gem
.idea/
Empty file.
22 changes: 11 additions & 11 deletions app/assets/javascripts/swagger_engine/application.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//= require 'swagger_engine/lib/jquery-1.8.0.min.js'
//= require 'swagger_engine/lib/jquery.slideto.min.js'
//= require 'swagger_engine/lib/jquery.wiggle.min.js'
//= require 'swagger_engine/lib/jquery.ba-bbq.min.js'
//= require 'swagger_engine/lib/handlebars-2.0.0.js'
//= require 'swagger_engine/lib/underscore-min.js'
//= require 'swagger_engine/lib/backbone-min.js'
//= require 'swagger_engine/swagger-ui.js'
//= require 'swagger_engine/lib/highlight.7.3.pack.js'
//= require 'swagger_engine/lib/marked.js'
//= require 'swagger_engine/lib/swagger-oauth.js'
//= require ./lib/jquery-1.8.0.min.js
//= require ./lib/jquery.slideto.min.js
//= require ./lib/jquery.wiggle.min.js
//= require ./lib/jquery.ba-bbq.min.js
//= require ./lib/handlebars-2.0.0.js
//= require ./lib/underscore-min.js
//= require ./lib/backbone-min.js
//= require ./lib/highlight.7.3.pack.js
//= require ./lib/marked.js
//= require ./lib/swagger-oauth.js
//= require ./swagger-ui.js
23 changes: 16 additions & 7 deletions app/assets/javascripts/swagger_engine/lib/swagger-oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ var clientId;
var realm;
var oauth2KeyName;
var redirect_uri;
var clientSecret;
var scopeSeparator;

function handleLogin() {
var scopes = [];
Expand Down Expand Up @@ -40,6 +42,7 @@ function handleLogin() {
appName = window.swaggerUi.api.info.title;
}

$('.api-popup-dialog').remove();
popupDialog = $(
[
'<div class="api-popup-dialog">',
Expand Down Expand Up @@ -97,7 +100,10 @@ function handleLogin() {
var authSchemes = window.swaggerUi.api.authSchemes;
var host = window.location;
var pathname = location.pathname.substring(0, location.pathname.lastIndexOf("/"));
var defaultRedirectUrl = host.protocol + '//' + host.host + pathname + '/o2c.html';
// MODIFIED FROM ORIGINAL LIBRARY >>> Probably a better way to do this.
// var defaultRedirectUrl = host.protocol + '//' + host.host + pathname + '/o2c.html';
var defaultRedirectUrl = host.protocol + '//' + host.host + pathname.replace('swaggers', 'callbacks');
// <<<<< END OF MODIFICATION >>>>
var redirectUrl = window.oAuthRedirectUrl || defaultRedirectUrl;
var url = null;

Expand Down Expand Up @@ -151,7 +157,7 @@ function handleLogin() {
url += '&redirect_uri=' + encodeURIComponent(redirectUrl);
url += '&realm=' + encodeURIComponent(realm);
url += '&client_id=' + encodeURIComponent(clientId);
url += '&scope=' + encodeURIComponent(scopes.join(' '));
url += '&scope=' + encodeURIComponent(scopes.join(scopeSeparator));
url += '&state=' + encodeURIComponent(state);

window.open(url);
Expand All @@ -164,8 +170,8 @@ function handleLogin() {


function handleLogout() {
for(key in window.authorizations.authz){
window.authorizations.remove(key)
for(key in window.swaggerUi.api.clientAuthorizations.authz){
window.swaggerUi.api.clientAuthorizations.remove(key)
}
window.enabledScopes = null;
$('.api-ic.ic-on').addClass('ic-off');
Expand All @@ -184,7 +190,9 @@ function initOAuth(opts) {
popupMask = (o.popupMask||$('#api-common-mask'));
popupDialog = (o.popupDialog||$('.api-popup-dialog'));
clientId = (o.clientId||errors.push('missing client id'));
clientSecret = (o.clientSecret||errors.push('missing client secret'));
realm = (o.realm||errors.push('missing realm'));
scopeSeparator = (o.scopeSeparator||' ');

if(errors.length > 0){
log('auth unable initialize oauth: ' + errors);
Expand All @@ -206,6 +214,7 @@ function initOAuth(opts) {
window.processOAuthCode = function processOAuthCode(data) {
var params = {
'client_id': clientId,
'client_secret': clientSecret,
'code': data.code,
'grant_type': 'authorization_code',
'redirect_uri': redirect_uri
Expand Down Expand Up @@ -240,7 +249,7 @@ window.onOAuthComplete = function onOAuthComplete(token) {
if(b){
// if all roles are satisfied
var o = null;
$.each($('.auth #api_information_panel'), function(k, v) {
$.each($('.auth .api-ic .api_information_panel'), function(k, v) {
var children = v;
if(children && children.childNodes) {
var requiredScopes = [];
Expand All @@ -257,7 +266,7 @@ window.onOAuthComplete = function onOAuthComplete(token) {
}
}
if(diff.length > 0){
o = v.parentNode;
o = v.parentNode.parentNode;
$(o.parentNode).find('.api-ic.ic-on').addClass('ic-off');
$(o.parentNode).find('.api-ic.ic-on').removeClass('ic-on');

Expand All @@ -266,7 +275,7 @@ window.onOAuthComplete = function onOAuthComplete(token) {
$(o).find('.api-ic').removeClass('ic-error');
}
else {
o = v.parentNode;
o = v.parentNode.parentNode;
$(o.parentNode).find('.api-ic.ic-off').addClass('ic-on');
$(o.parentNode).find('.api-ic.ic-off').removeClass('ic-off');

Expand Down
Loading