Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve OAuth2 scopes management #104

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
6 changes: 6 additions & 0 deletions src/models/Auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
11 changes: 9 additions & 2 deletions src/parsers/paw/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 9 additions & 2 deletions src/parsers/paw/__tests__/Parser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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'
})
])
})
])

Expand Down
17 changes: 13 additions & 4 deletions src/parsers/postman/v2/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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(
Expand All @@ -623,9 +634,7 @@ export default class PostmanParser {
accessTokenUrl: this._referenceEnvironmentVariable(
auth.tokenUrl
),
scopes: [ this._referenceEnvironmentVariable(
auth.scope
) ]
scopes: this._extractOAuth2Scopes(auth.scope)
})
}

Expand Down
21 changes: 15 additions & 6 deletions src/parsers/raml/v0.8/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -757,6 +757,16 @@ 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) {
let flowMap = {
code: 'accessCode',
Expand All @@ -780,11 +790,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
Expand Down
45 changes: 36 additions & 9 deletions src/parsers/raml/v0.8/__tests__/Parser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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' ]
}
]
)
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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, '')

Expand Down
12 changes: 11 additions & 1 deletion src/parsers/swagger/v2.0/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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,
Expand Down
22 changes: 21 additions & 1 deletion src/parsers/swagger/v2.0/__tests__/Parser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 3 additions & 1 deletion src/serializers/paw/Serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(' ')
}
)
}
Expand Down
11 changes: 9 additions & 2 deletions src/serializers/paw/__tests__/Serializer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion src/serializers/raml/Serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
27 changes: 23 additions & 4 deletions src/serializers/raml/__tests__/Serializer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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'
})
])
})
])
}),
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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'
})
])
})
])
Expand Down
Loading