From 7e6403cc720a6287047796aec54961bb0ff8462d Mon Sep 17 00:00:00 2001 From: Juan Coen Date: Thu, 12 May 2016 16:25:57 -0300 Subject: [PATCH] Fix issue with security schemes and libraries --- dist/examples/libraries/security.raml | 19 ++++++++ dist/examples/library-security.raml | 12 +++++ dist/scripts/api-console.js | 52 +++++++++++++++++++-- src/app/directives/documentation.tpl.html | 2 +- src/app/directives/raml-console.js | 31 +++++++++++- src/app/security/oauth2.js | 19 +++++++- src/assets/examples/libraries/security.raml | 19 ++++++++ src/assets/examples/library-security.raml | 12 +++++ 8 files changed, 156 insertions(+), 10 deletions(-) create mode 100644 dist/examples/libraries/security.raml create mode 100644 dist/examples/library-security.raml create mode 100644 src/assets/examples/libraries/security.raml create mode 100644 src/assets/examples/library-security.raml diff --git a/dist/examples/libraries/security.raml b/dist/examples/libraries/security.raml new file mode 100644 index 000000000..28791103f --- /dev/null +++ b/dist/examples/libraries/security.raml @@ -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 ] diff --git a/dist/examples/library-security.raml b/dist/examples/library-security.raml new file mode 100644 index 000000000..ab8825f7d --- /dev/null +++ b/dist/examples/library-security.raml @@ -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: diff --git a/dist/scripts/api-console.js b/dist/scripts/api-console.js index 814e123c2..00d65508e 100644 --- a/dist/scripts/api-console.js +++ b/dist/scripts/api-console.js @@ -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(); @@ -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; + } }); })(); @@ -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) { @@ -2890,6 +2931,7 @@ }; }; + angular.module('RAML.Security') .directive('oauth2', RAML.Security.oauth2); })(); @@ -6472,7 +6514,7 @@ angular.module('ramlConsoleApp').run(['$templateCache', function($templateCache) "

Responses

\n" + "\n" + "
\n" + - "

{{code}}

\n" + + "

{{info.code}}

\n" + "

\n" + "
\n" + " \n" + diff --git a/src/app/directives/documentation.tpl.html b/src/app/directives/documentation.tpl.html index 10aca2166..4031d6f53 100644 --- a/src/app/directives/documentation.tpl.html +++ b/src/app/directives/documentation.tpl.html @@ -52,7 +52,7 @@

Query Parameters

Responses

-

{{code}}

+

{{info.code}}

diff --git a/src/app/directives/raml-console.js b/src/app/directives/raml-console.js index 57ab92b02..efb5cb0a8 100644 --- a/src/app/directives/raml-console.js +++ b/src/app/directives/raml-console.js @@ -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(); @@ -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; + } }); })(); diff --git a/src/app/security/oauth2.js b/src/app/security/oauth2.js index ab91d099c..9741ce641 100644 --- a/src/app/security/oauth2.js +++ b/src/app/security/oauth2.js @@ -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) { @@ -72,6 +86,7 @@ }; }; + angular.module('RAML.Security') .directive('oauth2', RAML.Security.oauth2); })(); diff --git a/src/assets/examples/libraries/security.raml b/src/assets/examples/libraries/security.raml new file mode 100644 index 000000000..28791103f --- /dev/null +++ b/src/assets/examples/libraries/security.raml @@ -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 ] diff --git a/src/assets/examples/library-security.raml b/src/assets/examples/library-security.raml new file mode 100644 index 000000000..ab8825f7d --- /dev/null +++ b/src/assets/examples/library-security.raml @@ -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: