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

Add @securitydefinitions.bearer (#1100) #1821

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const (
secImplicitAttr = "@securitydefinitions.oauth2.implicit"
secPasswordAttr = "@securitydefinitions.oauth2.password"
secAccessCodeAttr = "@securitydefinitions.oauth2.accesscode"
secBearerAttr = "@securitydefinitions.bearer"
tosAttr = "@termsofservice"
extDocsDescAttr = "@externaldocs.description"
extDocsURLAttr = "@externaldocs.url"
Expand Down
20 changes: 18 additions & 2 deletions parserv3.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (p *Parser) parseGeneralAPIInfoV3(comments []string) error {
}

tag.Spec.ExternalDocs.Spec.Description = value
case secBasicAttr, secAPIKeyAttr, secApplicationAttr, secImplicitAttr, secPasswordAttr, secAccessCodeAttr:
case secBasicAttr, secAPIKeyAttr, secApplicationAttr, secImplicitAttr, secPasswordAttr, secAccessCodeAttr, secBearerAttr:
key, scheme, err := parseSecAttributesV3(attribute, comments, &line)
if err != nil {
return err
Expand Down Expand Up @@ -331,9 +331,11 @@ func parseSecAttributesV3(context string, lines []string, index *int) (string, *
descriptionAttr = "@description"
tokenURL = "@tokenurl"
authorizationURL = "@authorizationurl"
bearerFormat = "@bearerformat"
)

var search []string
var optionalSearсh []string

attribute := strings.ToLower(FieldsByAnySpace(lines[*index], 2)[0])
switch attribute {
Expand All @@ -351,6 +353,8 @@ func parseSecAttributesV3(context string, lines []string, index *int) (string, *
search = []string{authorizationURL, in}
case secAccessCodeAttr:
search = []string{tokenURL, authorizationURL, in}
case secBearerAttr:
optionalSearсh = []string{bearerFormat}
}

// For the first line we get the attributes in the context parameter, so we skip to the next one
Expand Down Expand Up @@ -380,6 +384,13 @@ func parseSecAttributesV3(context string, lines []string, index *int) (string, *
}
}

for _, optFindTerm := range optionalSearсh {
if securityAttr == optFindTerm {
attrMap[securityAttr] = value
break
}
}

isExists, err := isExistsScope(securityAttr)
if err != nil {
return "", nil, err
Expand Down Expand Up @@ -408,7 +419,7 @@ func parseSecAttributesV3(context string, lines []string, index *int) (string, *
}
}

if len(attrMap) != len(search) {
if len(attrMap) < len(search) {
return "", nil, fmt.Errorf("%s is %v required", context, search)
}

Expand Down Expand Up @@ -460,6 +471,11 @@ func parseSecAttributesV3(context string, lines []string, index *int) (string, *
scheme.Flows.Spec.AuthorizationCode = spec.NewOAuthFlow()
scheme.Flows.Spec.AuthorizationCode.Spec.AuthorizationURL = attrMap[authorizationURL]
scheme.Flows.Spec.AuthorizationCode.Spec.TokenURL = attrMap[tokenURL]

case secBearerAttr:
scheme.Type = "http"
scheme.Scheme = "bearer"
scheme.BearerFormat = attrMap[bearerFormat]
}

scheme.Description = description
Expand Down
6 changes: 5 additions & 1 deletion parserv3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func TestParserParseGeneralApiInfoV3(t *testing.T) {
assert.Equal(t, "OpenAPI", p.openAPI.ExternalDocs.Spec.Description)
assert.Equal(t, "https://swagger.io/resources/open-api", p.openAPI.ExternalDocs.Spec.URL)

assert.Equal(t, 6, len(p.openAPI.Components.Spec.SecuritySchemes))
assert.Equal(t, 7, len(p.openAPI.Components.Spec.SecuritySchemes))

security := p.openAPI.Components.Spec.SecuritySchemes
assert.Equal(t, "basic", security["basic"].Spec.Spec.Scheme)
Expand Down Expand Up @@ -164,6 +164,10 @@ func TestParserParseGeneralApiInfoV3(t *testing.T) {
assert.Equal(t, "oauth2", security["OAuth2AccessCode"].Spec.Spec.Type)
assert.Equal(t, "header", security["OAuth2AccessCode"].Spec.Spec.In)
assert.Equal(t, "https://example.com/oauth/token", security["OAuth2AccessCode"].Spec.Spec.Flows.Spec.AuthorizationCode.Spec.TokenURL)

assert.Equal(t, "http", security["BearerToken"].Spec.Spec.Type)
assert.Equal(t, "bearer", security["BearerToken"].Spec.Spec.Scheme)
assert.Equal(t, "JWT", security["BearerToken"].Spec.Spec.BearerFormat)
}

func TestParser_ParseGeneralApiInfoExtensionsV3(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions testdata/v3/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ package main
// @in header
// @name name

// @securitydefinitions.bearer BearerToken
// @bearerFormat JWT

// @externalDocs.description OpenAPI
// @externalDocs.url https://swagger.io/resources/open-api

Expand Down
Loading