Skip to content

Commit 9457566

Browse files
kai-morichtim-lai
andauthored
fix(oauth2): only display scopes relevant for current endpoint (#8229)
* 'available authorization' popup: only show oauth2 scopes relevant for current endpoint (issue #8219) * unit tests for oauth2 scope filter Co-authored-by: Kai Morich <[email protected]> Co-authored-by: Tim Lai <[email protected]>
1 parent 9546375 commit 9457566

File tree

2 files changed

+117
-2
lines changed

2 files changed

+117
-2
lines changed

src/core/plugins/auth/selectors.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,28 @@ export const getDefinitionsByNames = ( state, securities ) => ( { specSelectors
6161

6262
export const definitionsForRequirements = (state, securities = List()) => ({ authSelectors }) => {
6363
const allDefinitions = authSelectors.definitionsToAuthorize() || List()
64-
return allDefinitions.filter((def) => {
65-
return securities.some(sec => sec.get(def.keySeq().first()))
64+
let result = List()
65+
allDefinitions.forEach( (definition) => {
66+
let security = securities.find(sec => sec.get(definition.keySeq().first()))
67+
if ( security ) {
68+
definition.forEach( (props, name) => {
69+
if ( props.get("type") === "oauth2" ) {
70+
const securityScopes = security.get(name)
71+
let definitionScopes = props.get("scopes")
72+
if( List.isList(securityScopes) && Map.isMap(definitionScopes) ) {
73+
definitionScopes.keySeq().forEach( (key) => {
74+
if ( !securityScopes.contains(key) ) {
75+
definitionScopes = definitionScopes.delete(key)
76+
}
77+
})
78+
definition = definition.set(name, props.set("scopes", definitionScopes))
79+
}
80+
}
81+
})
82+
result = result.push(definition)
83+
}
6684
})
85+
return result
6786
}
6887

6988
export const authorized = createSelector(

test/unit/core/plugins/auth/selectors.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,100 @@ describe("auth plugin - selectors", () => {
129129
expect(res.toJS()).toEqual([])
130130
})
131131
})
132+
133+
it("should return only security definitions used by the endpoint", () => {
134+
const securityDefinitions = {
135+
"used": {
136+
"type": "http",
137+
"scheme": "basic",
138+
},
139+
"unused": {
140+
"type": "http",
141+
"scheme": "basic",
142+
}
143+
}
144+
145+
const system = {
146+
authSelectors: {
147+
definitionsToAuthorize() {
148+
return fromJS([
149+
{
150+
"used": securityDefinitions["used"]
151+
},
152+
{
153+
"unused": securityDefinitions["unused"]
154+
},
155+
])
156+
}
157+
}
158+
}
159+
160+
const securities = fromJS([
161+
{
162+
"used": [],
163+
"undefined": [],
164+
}
165+
])
166+
167+
const res = definitionsForRequirements({}, securities)(system)
168+
169+
expect(res.toJS()).toEqual([
170+
{
171+
"used": securityDefinitions["used"]
172+
}
173+
])
174+
})
175+
176+
it("should return only oauth scopes used by the endpoint", () => {
177+
const securityDefinitions = {
178+
"oauth2": {
179+
"type": "oauth2",
180+
"flow": "clientCredentials",
181+
"tokenUrl": "https://api.testserver.com/oauth2/token/",
182+
"scopes": {
183+
"used": "foo",
184+
"unused": "bar"
185+
}
186+
},
187+
"other": {
188+
"type": "apiKey",
189+
"name": "api_key",
190+
"in": "header"
191+
}
192+
193+
}
194+
195+
const system = {
196+
authSelectors: {
197+
definitionsToAuthorize() {
198+
return fromJS([
199+
{
200+
"oauth2": securityDefinitions["oauth2"],
201+
"other": securityDefinitions["other"],
202+
},
203+
])
204+
}
205+
}
206+
}
207+
208+
const securities = fromJS([
209+
{
210+
"oauth2": ["used", "undefined"],
211+
"other": [],
212+
}
213+
])
214+
215+
let expectedOauth2Definitions = {...securityDefinitions["oauth2"]}
216+
expectedOauth2Definitions["scopes"] = {"used": "foo"}
217+
218+
const res = definitionsForRequirements({}, securities)(system)
219+
220+
expect(res.toJS()).toEqual([
221+
{
222+
"oauth2": expectedOauth2Definitions,
223+
"other": securityDefinitions["other"]
224+
}
225+
])
226+
})
227+
132228
})

0 commit comments

Comments
 (0)