Skip to content

Commit

Permalink
Added support for @x- attributes on security definition (#971)
Browse files Browse the repository at this point in the history
* added support for @x- attributes on security definition
  • Loading branch information
pmorelli92 authored Aug 11, 2021
1 parent 505d4f1 commit fd504eb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
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

0 comments on commit fd504eb

Please sign in to comment.