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

Added support for @x- attributes on security definition #971

Merged
merged 3 commits into from
Aug 11, 2021
Merged
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
24 changes: 19 additions & 5 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,15 +387,28 @@ func (parser *Parser) ParseGeneralAPIInfo(mainAPIFile string) error {
return err
}
securityMap[value] = securitySchemeOAuth2AccessToken(attrMap["@authorizationurl"], attrMap["@tokenurl"], scopes, extensions)
case "@x-tokenname":
// ignore this
break
case "@query.collection.format":
parser.collectionFormatInQuery = value
default:
prefixExtension := "@x-"
if len(attribute) > 5 { // Prefix extension + 1 char + 1 space + 1 char
if attribute[:len(prefixExtension)] == prefixExtension {

extExistsInSecurityDef := false
// for each security definition
for _, v := range securityMap {
// check if extension exists
_, extExistsInSecurityDef = v.VendorExtensible.Extensions.GetString(attribute[1:])
// if it exists in at least one, then we stop iterating
if extExistsInSecurityDef {
break
}
}
// if it is present on security def, don't add it again
if extExistsInSecurityDef {
break
}

var valueJSON interface{}
split := strings.SplitAfter(commentLine, attribute+" ")
if len(split) < 2 {
Expand Down Expand Up @@ -462,8 +475,9 @@ func extractSecurityAttribute(context string, search []string, lines []string) (
}
scopes[scopScheme] = v[len(securityAttr):]
}
if securityAttr == "@x-tokenname" {
extensions["x-tokenName"] = strings.TrimSpace(v[len(securityAttr):])
if strings.HasPrefix(securityAttr, "@x-") {
// Add the custom attribute without the @
extensions[securityAttr[1:]] = strings.TrimSpace(v[len(securityAttr):])
}
// next securityDefinitions
if strings.Index(securityAttr, "@securitydefinitions.") == 0 {
Expand Down
5 changes: 3 additions & 2 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func TestParser_ParseGeneralApiInfo(t *testing.T) {
"scopes": {
"admin": " Grants read and write access to administrative information"
},
"x-tokenName": "id_token"
"x-tokenname": "id_token"
},
"OAuth2Application": {
"type": "oauth2",
Expand All @@ -108,7 +108,8 @@ func TestParser_ParseGeneralApiInfo(t *testing.T) {
"scopes": {
"admin": " Grants read and write access to administrative information",
"write": " Grants write access"
}
},
"x-google-audiences": "some_audience.google.com"
},
"OAuth2Password": {
"type": "oauth2",
Expand Down
1 change: 1 addition & 0 deletions testdata/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ package main
// @authorizationurl https://example.com/oauth/authorize
// @scope.write Grants write access
// @scope.admin Grants read and write access to administrative information
// @x-google-audiences some_audience.google.com

// @securitydefinitions.oauth2.password OAuth2Password
// @tokenUrl https://example.com/oauth/token
Expand Down