Skip to content

Commit

Permalink
Fix issue with security schemes and libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
juancoen committed May 12, 2016
1 parent 5ca5b6d commit 7e6403c
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 10 deletions.
19 changes: 19 additions & 0 deletions dist/examples/libraries/security.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#%RAML 1.0 Library

securitySchemes:
oauth_2_0:
type: OAuth 2.0
describedBy:
headers:
Authorization:
type: string
queryParameters:
access_token:
type: string
responses:
404:
description: Unauthorized
settings:
authorizationUri: https://acme.com/login/oauth/authorize
accessTokenUri: https://acme.com/login/oauth/access_token
authorizationGrants: [ authorization_code ]
12 changes: 12 additions & 0 deletions dist/examples/library-security.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#%RAML 1.0
title: API with Library
version: 1

baseUri: http://localhost

uses:
security: libraries/security.raml

/test:
securedBy: [security.oauth_2_0]
post:
52 changes: 47 additions & 5 deletions dist/scripts/api-console.js
Original file line number Diff line number Diff line change
Expand Up @@ -1203,9 +1203,16 @@
delete $scope.types;
delete $rootScope.types;

inspectRaml(raml);

$timeout(function () {
var securitySchemes = raml.securitySchemes ? angular.copy(raml.securitySchemes) : [];
var librarySecuritySchemes = getSecuritySchemes();

if (securitySchemes || librarySecuritySchemes) {
raml.securitySchemes = securitySchemes.concat(librarySecuritySchemes);
}

inspectRaml(raml);

var types = raml.types ? angular.copy(raml.types) : [];
var libraryTypes = getLibraryTypes();

Expand Down Expand Up @@ -1293,6 +1300,26 @@

return result;
}

function getSecuritySchemes() {
var result = [];
if (raml.uses) {
Object.keys(raml.uses).forEach(function (usesKey) {
var usesSecuritySchemes = raml.uses[usesKey].securitySchemes;
if (usesSecuritySchemes) {
usesSecuritySchemes.forEach(function (aScheme) {
Object.keys(aScheme).forEach(function (schemaKey) {
var tempSchema = {};
tempSchema[usesKey + '.' + schemaKey] = aScheme[schemaKey];
result.push(tempSchema);
});
});
}
});
}

return result;
}
});
})();

Expand Down Expand Up @@ -2873,9 +2900,23 @@
];

/* jshint camelcase: false */
var authorizationGrants = $scope.$parent.securitySchemes.oauth_2_0.settings.authorizationGrants;
$scope.getOAuth2Settings = function () {
var result;
for (var securitySchemesKey in $scope.$parent.securitySchemes) {
if ($scope.$parent.securitySchemes.hasOwnProperty(securitySchemesKey)) {
if ($scope.$parent.securitySchemes[securitySchemesKey].type === 'OAuth 2.0') {
result = $scope.$parent.securitySchemes[securitySchemesKey].settings;
break;
}
}
}
return result;
};

var oauth2Settings = $scope.getOAuth2Settings();
var authorizationGrants = oauth2Settings.authorizationGrants;

$scope.scopes = $scope.$parent.securitySchemes.oauth_2_0.settings.scopes;
$scope.scopes = oauth2Settings.scopes;
$scope.credentials.scopes = {};

if (authorizationGrants) {
Expand All @@ -2890,6 +2931,7 @@
};
};


angular.module('RAML.Security')
.directive('oauth2', RAML.Security.oauth2);
})();
Expand Down Expand Up @@ -6472,7 +6514,7 @@ angular.module('ramlConsoleApp').run(['$templateCache', function($templateCache)
" <h4 class=\"raml-console-resource-heading-a\">Responses</h4>\n" +
"\n" +
" <div class=\"raml-console-resource-param\" ng-repeat=\"(code, info) in documentationSchemeSelected.describedBy.responses\">\n" +
" <h4 class=\"raml-console-resource-param-heading\">{{code}}</h4>\n" +
" <h4 class=\"raml-console-resource-param-heading\">{{info.code}}</h4>\n" +
" <p markdown=\"info.description\" class=\"raml-console-marked-content\"></p>\n" +
" </div>\n" +
" </section>\n" +
Expand Down
2 changes: 1 addition & 1 deletion src/app/directives/documentation.tpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ <h4 class="raml-console-resource-heading-a">Query Parameters</h4>
<h4 class="raml-console-resource-heading-a">Responses</h4>

<div class="raml-console-resource-param" ng-repeat="(code, info) in documentationSchemeSelected.describedBy.responses">
<h4 class="raml-console-resource-param-heading">{{code}}</h4>
<h4 class="raml-console-resource-param-heading">{{info.code}}</h4>
<p markdown="info.description" class="raml-console-marked-content"></p>
</div>
</section>
Expand Down
31 changes: 29 additions & 2 deletions src/app/directives/raml-console.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,16 @@
delete $scope.types;
delete $rootScope.types;

inspectRaml(raml);

$timeout(function () {
var securitySchemes = raml.securitySchemes ? angular.copy(raml.securitySchemes) : [];
var librarySecuritySchemes = getSecuritySchemes();

if (securitySchemes || librarySecuritySchemes) {
raml.securitySchemes = securitySchemes.concat(librarySecuritySchemes);
}

inspectRaml(raml);

var types = raml.types ? angular.copy(raml.types) : [];
var libraryTypes = getLibraryTypes();

Expand Down Expand Up @@ -152,6 +159,26 @@

return result;
}

function getSecuritySchemes() {
var result = [];
if (raml.uses) {
Object.keys(raml.uses).forEach(function (usesKey) {
var usesSecuritySchemes = raml.uses[usesKey].securitySchemes;
if (usesSecuritySchemes) {
usesSecuritySchemes.forEach(function (aScheme) {
Object.keys(aScheme).forEach(function (schemaKey) {
var tempSchema = {};
tempSchema[usesKey + '.' + schemaKey] = aScheme[schemaKey];
result.push(tempSchema);
});
});
}
});
}

return result;
}
});
})();

Expand Down
19 changes: 17 additions & 2 deletions src/app/security/oauth2.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,23 @@
];

/* jshint camelcase: false */
var authorizationGrants = $scope.$parent.securitySchemes.oauth_2_0.settings.authorizationGrants;
$scope.getOAuth2Settings = function () {
var result;
for (var securitySchemesKey in $scope.$parent.securitySchemes) {
if ($scope.$parent.securitySchemes.hasOwnProperty(securitySchemesKey)) {
if ($scope.$parent.securitySchemes[securitySchemesKey].type === 'OAuth 2.0') {
result = $scope.$parent.securitySchemes[securitySchemesKey].settings;
break;
}
}
}
return result;
};

$scope.scopes = $scope.$parent.securitySchemes.oauth_2_0.settings.scopes;
var oauth2Settings = $scope.getOAuth2Settings();
var authorizationGrants = oauth2Settings.authorizationGrants;

$scope.scopes = oauth2Settings.scopes;
$scope.credentials.scopes = {};

if (authorizationGrants) {
Expand All @@ -72,6 +86,7 @@
};
};


angular.module('RAML.Security')
.directive('oauth2', RAML.Security.oauth2);
})();
19 changes: 19 additions & 0 deletions src/assets/examples/libraries/security.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#%RAML 1.0 Library

securitySchemes:
oauth_2_0:
type: OAuth 2.0
describedBy:
headers:
Authorization:
type: string
queryParameters:
access_token:
type: string
responses:
404:
description: Unauthorized
settings:
authorizationUri: https://acme.com/login/oauth/authorize
accessTokenUri: https://acme.com/login/oauth/access_token
authorizationGrants: [ authorization_code ]
12 changes: 12 additions & 0 deletions src/assets/examples/library-security.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#%RAML 1.0
title: API with Library
version: 1

baseUri: http://localhost

uses:
security: libraries/security.raml

/test:
securedBy: [security.oauth_2_0]
post:

0 comments on commit 7e6403c

Please sign in to comment.