Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"openapi-schema-to-json-schema": "^2.2.0",
"ora": "^3.0.0",
"path-to-regexp": "^2.4.0",
"slice-ansi": "^1.0.0",
"slice-ansi": "^2.0.0",
"statuses": "^1.5.0",
"string-width": "^2.1.1",
"strip-ansi": "^5.0.0",
Expand Down
48 changes: 46 additions & 2 deletions src/core/spec/start/normalize/params/security.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,40 @@ const IN_TO_LOCATION = require('./in_to_location')
// Normalize OpenAPI security request parameters into specification-agnostic
// format
const getSecParams = function({
spec: { securityDefinitions, security: apiSecurity = [] },
spec: {
securityDefinitions,
security: apiSecurity = [],
components: {
securitySchemes: secSchemes
}
},
operation: { security = apiSecurity },
}) {
const secRefs = getSecRefs({ security })
const secDef = getSecDefs({ securityDefinitions, securitySchemes: secSchemes})
const secParams = secRefs.map(([secName, scopes]) =>
getSecParam({ secName, scopes, securityDefinitions }),
getSecParam({ secName, scopes, securityDefinitions: secDef }),
)
const secParamsA = Object.assign({}, ...secParams)
return secParamsA
}

const getSecDefs = function({ securityDefinitions, securitySchemes }) {
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't a good solution for checking 2.0 and 3.0 specs. Just throwing up the most basic PR here.

// Check if either 2.0 or 3.0 exist
if ((securityDefinitions === undefined) && (securitySchemes === undefined)) {
throw new TestOpenApiError(
`Could not find OpenAPI 2 'securityDefinitions' or OpenAPI 3 'components.securitySchemes' from the spec root`
)
}

// Check for 2.0
if (securityDefinitions === undefined) {
return securitySchemes
}

return securityDefinitions
}

const getSecRefs = function({ security }) {
const securityA = security.map(Object.entries)
const securityB = [].concat(...securityA)
Expand Down Expand Up @@ -62,10 +85,31 @@ const getDefApiKey = function({ name, in: paramIn }) {
return { [key]: { type: 'string', optional: true } }
}

const getDefHttpKey = function({ scheme }) {
const scheme_type = HTTP_SCHEME_TYPES[scheme]

if (scheme_type === undefined) {
throw new TestOpenApiError(
`Other HTTP schemes defined by RFC 7235 not yet supported`,
)
}

const location = IN_TO_LOCATION['header']
const key = locationToKey({ name: 'authorization', location})

return { [key]: { type: 'string', optional: false } }
}

const SECURITY_DEFS = {
apiKey: getDefApiKey,
http: getDefHttpKey
}

const HTTP_SCHEME_TYPES = Object.freeze({
"basic": 1,
"bearer": 2
})

module.exports = {
getSecParams,
}