From 87903d61e9766305e74833e2332689ffef3d6762 Mon Sep 17 00:00:00 2001 From: JonathanMontane Date: Fri, 25 Nov 2016 17:04:58 +0100 Subject: [PATCH 1/9] added OAuth2Scope object to model --- src/models/Auth.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/models/Auth.js b/src/models/Auth.js index 37d5ca5..090beff 100644 --- a/src/models/Auth.js +++ b/src/models/Auth.js @@ -82,6 +82,12 @@ export class OAuth1Auth extends Immutable.Record({ authorizationUri: null }) { } +export class OAuth2Scope extends Immutable.Record({ + name: null, + description: null, + value: null +}) { } + export class OAuth2Auth extends Immutable.Record({ _model: new Model({ name: 'oauth-2.auth.models', From 8f39f6352dda74e2facd5e3969167ccfba1d21d5 Mon Sep 17 00:00:00 2001 From: JonathanMontane Date: Fri, 25 Nov 2016 17:06:29 +0100 Subject: [PATCH 2/9] added support for OAuth2Scope to paw parser --- src/parsers/paw/Parser.js | 11 +++++++++-- src/parsers/paw/__tests__/Parser-test.js | 11 +++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/parsers/paw/Parser.js b/src/parsers/paw/Parser.js index 111f03c..3f9ea47 100644 --- a/src/parsers/paw/Parser.js +++ b/src/parsers/paw/Parser.js @@ -12,7 +12,7 @@ import URL from '../../models/URL' import Request from '../../models/Request' import Constraint from '../../models/Constraint' import Group from '../../models/Group' -import Auth from '../../models/Auth' +import Auth, { OAuth2Scope } from '../../models/Auth' import Reference from '../../models/references/Reference' import ReferenceContainer from '../../models/references/Container' import JSONSchemaReference from '../../models/references/JSONSchema' @@ -1026,11 +1026,18 @@ export default class PawParser { 2: 'application', 3: 'password' } - let scopes = (oauth2.scope || '').split(' ') + let scopes = (oauth2.scope || '').split(/[\s,;]/) if (scopes.length === 1 && scopes[0] === '') { scopes = null } + else { + scopes = scopes.map(scope => { + return new OAuth2Scope({ + value: scope + }) + }) + } let auth = new Auth.OAuth2({ flow: grantMap[oauth2.grantType] || null, diff --git a/src/parsers/paw/__tests__/Parser-test.js b/src/parsers/paw/__tests__/Parser-test.js index b6e5fbc..9e47677 100644 --- a/src/parsers/paw/__tests__/Parser-test.js +++ b/src/parsers/paw/__tests__/Parser-test.js @@ -16,7 +16,7 @@ import Context, { import Group from '../../../models/Group' import Constraint from '../../../models/Constraint' -import Auth from '../../../models/Auth' +import Auth, { OAuth2Scope } from '../../../models/Auth' import Request from '../../../models/Request' import URL from '../../../models/URL' @@ -1847,7 +1847,14 @@ export class TestPawParser extends UnitTest { flow: 'implicit', authorizationUrl: 'w;oeifhwe', tokenUrl: 'h2oiufh23', - scopes: [ 'read:any', 'write:self' ] + scopes: new Immutable.List([ + new OAuth2Scope({ + value: 'read:any' + }), + new OAuth2Scope({ + value: 'write:self' + }) + ]) }) ]) From aedd84da637cf429b6a877d9411bb52cc18da239 Mon Sep 17 00:00:00 2001 From: JonathanMontane Date: Fri, 25 Nov 2016 17:06:47 +0100 Subject: [PATCH 3/9] added support for OAuth2Scope to postman parser --- src/parsers/postman/v2/Parser.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/parsers/postman/v2/Parser.js b/src/parsers/postman/v2/Parser.js index 60925ec..ff5e739 100644 --- a/src/parsers/postman/v2/Parser.js +++ b/src/parsers/postman/v2/Parser.js @@ -18,7 +18,7 @@ import URL from '../../../models/URL' import Group from '../../../models/Group' import Request from '../../../models/Request' -import Auth from '../../../models/Auth' +import Auth, { OAuth2Scope } from '../../../models/Auth' export default class PostmanParser { static format = 'postman' @@ -615,6 +615,17 @@ export default class PostmanParser { }) } + _extractOAuth2Scopes(_scope) { + let scopes = (_scope || '').split(/[\s,;]/) + scopes = scopes.map(scope => { + return new OAuth2Scope({ + value: this._referenceEnvironmentVariable(scope) + }) + }) + + return new Immutable.List(scopes) + } + _extractOAuth2(auth) { return new Auth.OAuth1({ authorizationUrl: this._referenceEnvironmentVariable( @@ -623,9 +634,7 @@ export default class PostmanParser { accessTokenUrl: this._referenceEnvironmentVariable( auth.tokenUrl ), - scopes: [ this._referenceEnvironmentVariable( - auth.scope - ) ] + scopes: this._extractOAuth2Scopes(auth.scope) }) } From 713e169d8d085b4e254a0142c0df26753c77f164 Mon Sep 17 00:00:00 2001 From: JonathanMontane Date: Fri, 25 Nov 2016 17:07:06 +0100 Subject: [PATCH 4/9] added support for OAuth2Scope to raml parser --- src/parsers/raml/v0.8/Parser.js | 22 ++++++--- .../raml/v0.8/__tests__/Parser-test.js | 45 +++++++++++++++---- 2 files changed, 52 insertions(+), 15 deletions(-) diff --git a/src/parsers/raml/v0.8/Parser.js b/src/parsers/raml/v0.8/Parser.js index 73e18a0..5f9bc17 100644 --- a/src/parsers/raml/v0.8/Parser.js +++ b/src/parsers/raml/v0.8/Parser.js @@ -22,7 +22,7 @@ import ExoticReference from '../../../models/references/Exotic' import JSONSchemaReference from '../../../models/references/JSONSchema' import Constraint from '../../../models/Constraint' -import Auth from '../../../models/Auth' +import Auth, { OAuth2Scope } from '../../../models/Auth' import ShimmingFileReader from '../FileReader' @@ -757,7 +757,18 @@ export default class RAMLParser { return auths } + _extractOAuth2Scopes(scopes) { + const _scopes = scopes.map(scope => { + return new OAuth2Scope({ + value: scope + }) + }) + + return new Immutable.List(_scopes) + } + _extractOAuth2Auth(raml, authName = null, security, params) { + console.error('---------', security, params) let flowMap = { code: 'accessCode', token: 'implicit', @@ -780,11 +791,10 @@ export default class RAMLParser { _params.accessTokenUri || security.settings.accessTokenUri || null, - scopes: - new Immutable.List( - _params.scopes || - security.settings.scopes || [] - ) + scopes: this._extractOAuth2Scopes( + _params.scopes || + security.settings.scopes || [] + ) }) return auth diff --git a/src/parsers/raml/v0.8/__tests__/Parser-test.js b/src/parsers/raml/v0.8/__tests__/Parser-test.js index bc19ccb..dde4acc 100644 --- a/src/parsers/raml/v0.8/__tests__/Parser-test.js +++ b/src/parsers/raml/v0.8/__tests__/Parser-test.js @@ -13,7 +13,7 @@ import { } from '../../../../mocks/PawMocks' import Constraint from '../../../../models/Constraint' -import Auth from '../../../../models/Auth' +import Auth, { OAuth2Scope } from '../../../../models/Auth' import Context, { Body, @@ -1181,21 +1181,18 @@ export class TestRAMLParser extends UnitTest { ] } } + const expected = new Immutable.List([ 12 ]) const result = parser._extractAuth(raml, req) this.assertEqual(expected, result) this.assertEqual(parser.spy._extractOAuth2Auth.count, 1) this.assertEqual( - parser.spy._extractOAuth2Auth.calls[0], + parser.spy._extractOAuth2Auth.calls[0].slice(2), [ - raml, - 'oauth_2_0', scheme, { - scopes: [ - 'ADMINISTRATOR' - ] + scopes: [ 'ADMINISTRATOR' ] } ] ) @@ -1309,6 +1306,26 @@ export class TestRAMLParser extends UnitTest { ) } + @targets('_extractOAuth2Scopes') + testExtractOAuth2Scopes() { + const [ parser ] = this.__init() + + const scopes = [ 'code', 'token' ] + + const expected = new Immutable.List([ + new OAuth2Scope({ + value: 'code' + }), + new OAuth2Scope({ + value: 'token' + }) + ]) + + const result = parser._extractOAuth2Scopes(scopes) + + this.assertEqual(result, expected) + } + @targets('_extractOAuth2Auth') testExtractOAuth2Auth() { const [ parser, raml ] = this.__init('large-raml') @@ -1360,7 +1377,11 @@ export class TestRAMLParser extends UnitTest { flow: 'accessCode', authorizationUrl: 'https://www.box.com/api/oauth2/authorize', tokenUrl: 'https://www.box.com/api/oauth2/token', - scopes: new Immutable.List(params.scopes) + scopes: new Immutable.List([ + new OAuth2Scope({ + value: 'ADMINISTRATOR' + }) + ]) }) const result = parser._extractOAuth2Auth( @@ -2428,7 +2449,13 @@ export class TestRAMLParser extends UnitTest { } __init(file) { - let raml = this.__loadRAMLObject(file) + let raml + if (!file) { + raml = null + } + else { + raml = this.__loadRAMLObject(file) + } let parser = new RAMLParser() let mockedParser = new ClassMock(parser, '') From 03fba4efed0ec1f75036ed179e5dc65a96d9bc56 Mon Sep 17 00:00:00 2001 From: JonathanMontane Date: Fri, 25 Nov 2016 17:07:26 +0100 Subject: [PATCH 5/9] added support for OAuth2Scope to swagger parser --- src/parsers/swagger/v2.0/Parser.js | 12 +++++++++- .../swagger/v2.0/__tests__/Parser-test.js | 22 ++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/parsers/swagger/v2.0/Parser.js b/src/parsers/swagger/v2.0/Parser.js index d374e5d..e3dc4d9 100644 --- a/src/parsers/swagger/v2.0/Parser.js +++ b/src/parsers/swagger/v2.0/Parser.js @@ -18,7 +18,7 @@ import { import Group from '../../../models/Group' import Request from '../../../models/Request' -import Auth from '../../../models/Auth' +import Auth, { OAuth2Scope } from '../../../models/Auth' import URL from '../../../models/URL' import Item from '../../../models/Item' @@ -333,6 +333,16 @@ export default class SwaggerParser { }) } + _setOAuth2Scopes(scopes) { + const _scopes = (scopes || []).map(scope => { + return new OAuth2Scope({ + value: scope + }) + }) + + return _scopes + } + _setOAuth2Auth(authName = null, definition) { return new Auth.OAuth2({ authName, diff --git a/src/parsers/swagger/v2.0/__tests__/Parser-test.js b/src/parsers/swagger/v2.0/__tests__/Parser-test.js index ba64cf5..1dbbb21 100644 --- a/src/parsers/swagger/v2.0/__tests__/Parser-test.js +++ b/src/parsers/swagger/v2.0/__tests__/Parser-test.js @@ -20,7 +20,7 @@ import { import Group from '../../../../models/Group' import Constraint from '../../../../models/Constraint' -import Auth from '../../../../models/Auth' +import Auth, { OAuth2Scope } from '../../../../models/Auth' import Request from '../../../../models/Request' import URL from '../../../../models/URL' import Item from '../../../../models/Item' @@ -1460,6 +1460,26 @@ export class TestSwaggerParser extends UnitTest { this.assertEqual(expected, result) } + @targets('_setOAuth2Scopes') + testSetOAuth2Scopes() { + const parser = this.__init() + + const scopes = [ 'read:any', 'write:own' ] + + const expected = new Immutable.List([ + new OAuth2Scope({ + value: 'read:any' + }), + new OAuth2Scope({ + value: 'write:own' + }) + ]) + + const result = parser._setOAuth2Scopes(scopes) + + this.assertEqual(result, expected) + } + @targets('_setOAuth2Auth') testSetOAuth2AuthWithSimpleDefinition() { const parser = this.__init() From 8391ad1fec384208ed309368a11996d62cd90121 Mon Sep 17 00:00:00 2001 From: JonathanMontane Date: Fri, 25 Nov 2016 17:07:58 +0100 Subject: [PATCH 6/9] added support for OAuth2Scope to paw serializer --- src/serializers/paw/Serializer.js | 4 +++- src/serializers/paw/__tests__/Serializer-test.js | 11 +++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/serializers/paw/Serializer.js b/src/serializers/paw/Serializer.js index e312f22..22f69ca 100644 --- a/src/serializers/paw/Serializer.js +++ b/src/serializers/paw/Serializer.js @@ -831,7 +831,9 @@ export default class PawSerializer { accessTokenUrl: this._toDynamicString( auth.get('tokenUrl') || '', true, 'auth' ), - scope: (auth.get('scopes') || []).join(' ') + scope: (auth.get('scopes') || []) + .map(scope => scope.get('value')) + .join(' ') } ) } diff --git a/src/serializers/paw/__tests__/Serializer-test.js b/src/serializers/paw/__tests__/Serializer-test.js index ef23650..8e16cb7 100644 --- a/src/serializers/paw/__tests__/Serializer-test.js +++ b/src/serializers/paw/__tests__/Serializer-test.js @@ -13,7 +13,7 @@ import ExoticReference from '../../../models/references/Exotic' import Request from '../../../models/Request' import Constraint from '../../../models/Constraint' import URL from '../../../models/URL' -import Auth from '../../../models/Auth' +import Auth, { OAuth2Scope } from '../../../models/Auth' import PawEnvironment from '../../../models/environments/PawEnvironment' import ContextResolver from '../../../resolvers/ContextResolver' @@ -826,7 +826,14 @@ export class TestPawSerializer extends UnitTest { flow: 'implicit', authorizationUrl: 'fakeurl.com/oauth2', tokenUrl: 'fakeurl.com/oauth2/access-token', - scopes: [ 'user:write', 'user:read' ] + scopes: new Immutable.List([ + new OAuth2Scope({ + value: 'user:write' + }), + new OAuth2Scope({ + value: 'user:read' + }) + ]) }) let dv = importer._setOAuth2Auth(auth) From 8fb2f94ac17d5cf32ef71c93348b8a004b07312c Mon Sep 17 00:00:00 2001 From: JonathanMontane Date: Fri, 25 Nov 2016 17:08:56 +0100 Subject: [PATCH 7/9] added support for OAuth2Scope to raml serializer --- src/serializers/raml/Serializer.js | 4 ++- .../raml/__tests__/Serializer-test.js | 27 ++++++++++++++++--- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/serializers/raml/Serializer.js b/src/serializers/raml/Serializer.js index 92ccc4a..ca07f17 100644 --- a/src/serializers/raml/Serializer.js +++ b/src/serializers/raml/Serializer.js @@ -876,7 +876,9 @@ export default class RAMLSerializer extends BaseSerializer { if (scopes && scopes.size > 0) { content = {} content.oauth_2_0 = { - scopes: scopes.toJS() + scopes: (scopes || []).map(scope => { + return scope.get('value') + }) } } else { diff --git a/src/serializers/raml/__tests__/Serializer-test.js b/src/serializers/raml/__tests__/Serializer-test.js index ad9c2a7..7e33dee 100644 --- a/src/serializers/raml/__tests__/Serializer-test.js +++ b/src/serializers/raml/__tests__/Serializer-test.js @@ -13,7 +13,7 @@ import { Info, Contact, License } from '../../../models/Utils' -import Auth from '../../../models/Auth' +import Auth, { OAuth2Scope } from '../../../models/Auth' import Constraint from '../../../models/Constraint' import URL from '../../../models/URL' import Request from '../../../models/Request' @@ -735,7 +735,14 @@ export class TestRAMLSerializer extends UnitTest { flow: 'implicit', authorizationUrl: 'api.com/oauth2/authorize', tokenUrl: 'api.com/oauth2/token', - scopes: Immutable.List([ 'read:any', 'write:own' ]) + scopes: Immutable.List([ + new OAuth2Scope({ + value: 'read:any' + }), + new OAuth2Scope({ + value: 'write:own' + }) + ]) }) ]) }), @@ -804,7 +811,14 @@ export class TestRAMLSerializer extends UnitTest { flow: 'implicit', authorizationUrl: 'api.com/oauth2/authorize', tokenUrl: 'api.com/oauth2/token', - scopes: Immutable.List([ 'read:any', 'write:own' ]) + scopes: Immutable.List([ + new OAuth2Scope({ + value: 'read:any' + }), + new OAuth2Scope({ + value: 'write:own' + }) + ]) }) let expected = { @@ -1785,7 +1799,12 @@ export class TestRAMLSerializer extends UnitTest { const input = new Immutable.List([ new Auth.OAuth2({ scopes: new Immutable.List([ - 'read:any', 'write:self' + new OAuth2Scope({ + value: 'read:any' + }), + new OAuth2Scope({ + value: 'write:self' + }) ]) }) ]) From 67b444c6b2c49c461bedc005a3fce42888dce747 Mon Sep 17 00:00:00 2001 From: JonathanMontane Date: Fri, 25 Nov 2016 17:09:08 +0100 Subject: [PATCH 8/9] added support for OAuth2Scope to swagger serializer --- src/serializers/swagger/Serializer.js | 3 +-- src/serializers/swagger/__tests__/Serializer-test.js | 11 +++++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/serializers/swagger/Serializer.js b/src/serializers/swagger/Serializer.js index 2813c14..ae0d21a 100644 --- a/src/serializers/swagger/Serializer.js +++ b/src/serializers/swagger/Serializer.js @@ -798,7 +798,7 @@ export default class SwaggerSerializer extends BaseSerializer { } _formatOAuth2Auth(context, auth) { - let scopes = auth.get('scopes') + let scopes = (auth.get('scopes') || []).map(scope => scope.get('value')) const name = auth.get('authName') || 'oauth_2_auth' let _definition = { @@ -827,7 +827,6 @@ export default class SwaggerSerializer extends BaseSerializer { let scopeDescriptions = {} let security if (scopes) { - scopes = Array.isArray(scopes) ? scopes : scopes.toJS() security = {} security[name] = scopes diff --git a/src/serializers/swagger/__tests__/Serializer-test.js b/src/serializers/swagger/__tests__/Serializer-test.js index 9aeb1d3..e50ea1f 100644 --- a/src/serializers/swagger/__tests__/Serializer-test.js +++ b/src/serializers/swagger/__tests__/Serializer-test.js @@ -24,7 +24,7 @@ import { import Constraint from '../../../models/Constraint' -import Auth from '../../../models/Auth' +import Auth, { OAuth2Scope } from '../../../models/Auth' import URL from '../../../models/URL' import Request from '../../../models/Request' @@ -1277,7 +1277,14 @@ export class TestSwaggerSerializer extends UnitTest { authorizationUrl: 'test.com/auth', tokenUrl: 'test.com/token', flow: 'implicit', - scopes: new Immutable.List([ 'write:self', 'read:any' ]) + scopes: new Immutable.List([ + new OAuth2Scope({ + value: 'write:self' + }), + new OAuth2Scope({ + value: 'read:any' + }) + ]) }) const expected = [ From 511457d3ad4a1ac9a2124a6e3e0727863b0c9a47 Mon Sep 17 00:00:00 2001 From: JonathanMontane Date: Fri, 25 Nov 2016 17:13:33 +0100 Subject: [PATCH 9/9] linting --- src/parsers/raml/v0.8/Parser.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/parsers/raml/v0.8/Parser.js b/src/parsers/raml/v0.8/Parser.js index 5f9bc17..6985ca1 100644 --- a/src/parsers/raml/v0.8/Parser.js +++ b/src/parsers/raml/v0.8/Parser.js @@ -768,7 +768,6 @@ export default class RAMLParser { } _extractOAuth2Auth(raml, authName = null, security, params) { - console.error('---------', security, params) let flowMap = { code: 'accessCode', token: 'implicit',