-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve and add support for more openapi security schemes
- Loading branch information
1 parent
c03793a
commit 209e04b
Showing
88 changed files
with
1,542 additions
and
1,440 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package auth | ||
|
||
func NewAPIKeySecurityScheme(name string, in SchemeIn, value *string) (*SecurityScheme, error) { | ||
tokenFormat := NoneTokenFormat | ||
securityScheme, err := NewSecurityScheme(name, nil, ApiKey, NoneScheme, &in, &tokenFormat) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if value != nil && *value != "" { | ||
err = securityScheme.SetValidValue(*value) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return securityScheme, nil | ||
} | ||
|
||
func MustNewAPIKeySecurityScheme(name string, in SchemeIn, value *string) *SecurityScheme { | ||
securityScheme, err := NewAPIKeySecurityScheme(name, in, value) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return securityScheme | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package auth_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cerberauth/vulnapi/internal/auth" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewAPIKeySecurityScheme(t *testing.T) { | ||
name := "token" | ||
value := "abc123" | ||
tokenFormat := auth.NoneTokenFormat | ||
|
||
securityScheme, err := auth.NewAPIKeySecurityScheme(name, auth.InHeader, &value) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, auth.ApiKey, securityScheme.GetType()) | ||
assert.Equal(t, auth.NoneScheme, securityScheme.GetScheme()) | ||
assert.Equal(t, auth.InHeader, *securityScheme.GetIn()) | ||
assert.Equal(t, &tokenFormat, securityScheme.GetTokenFormat()) | ||
assert.Equal(t, name, securityScheme.GetName()) | ||
assert.Equal(t, value, securityScheme.GetValidValue().(string)) | ||
assert.Equal(t, nil, securityScheme.GetAttackValue()) | ||
} | ||
|
||
func TestTestNewAPIKeySecurityScheme_WhenNilValue(t *testing.T) { | ||
name := "token" | ||
|
||
securityScheme, err := auth.NewAPIKeySecurityScheme(name, auth.InHeader, nil) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, nil, securityScheme.GetValidValue()) | ||
assert.Equal(t, nil, securityScheme.GetAttackValue()) | ||
} | ||
|
||
func TestNewAuthorizationBearerSecurityScheme_WhenInCooke(t *testing.T) { | ||
name := "token" | ||
value := "abc123" | ||
|
||
securityScheme, err := auth.NewAPIKeySecurityScheme(name, auth.InQuery, &value) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, auth.InQuery, *securityScheme.GetIn()) | ||
} | ||
|
||
func TestMustNewAPIKeySecurityScheme(t *testing.T) { | ||
name := "token" | ||
value := "abc123" | ||
tokenFormat := auth.NoneTokenFormat | ||
|
||
securityScheme := auth.MustNewAPIKeySecurityScheme(name, auth.InHeader, &value) | ||
|
||
assert.Equal(t, auth.ApiKey, securityScheme.GetType()) | ||
assert.Equal(t, auth.NoneScheme, securityScheme.GetScheme()) | ||
assert.Equal(t, auth.InHeader, *securityScheme.GetIn()) | ||
assert.Equal(t, &tokenFormat, securityScheme.GetTokenFormat()) | ||
assert.Equal(t, name, securityScheme.GetName()) | ||
assert.Equal(t, value, securityScheme.GetValidValue().(string)) | ||
assert.Equal(t, nil, securityScheme.GetAttackValue()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,41 @@ | ||
package auth | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"github.com/cerberauth/vulnapi/jwt" | ||
) | ||
|
||
type BearerSecurityScheme struct { | ||
Type Type `json:"type" yaml:"type"` | ||
Scheme SchemeName `json:"scheme" yaml:"scheme"` | ||
In SchemeIn `json:"in" yaml:"in"` | ||
Name string `json:"name" yaml:"name"` | ||
ValidValue *string `json:"-" yaml:"-"` | ||
AttackValue string `json:"-" yaml:"-"` | ||
} | ||
|
||
var _ SecurityScheme = (*BearerSecurityScheme)(nil) | ||
|
||
func NewAuthorizationBearerSecurityScheme(name string, value *string) *BearerSecurityScheme { | ||
return &BearerSecurityScheme{ | ||
Type: HttpType, | ||
Scheme: BearerScheme, | ||
In: InHeader, | ||
Name: name, | ||
ValidValue: value, | ||
AttackValue: "", | ||
} | ||
} | ||
|
||
func (ss *BearerSecurityScheme) GetType() Type { | ||
return ss.Type | ||
} | ||
|
||
func (ss *BearerSecurityScheme) GetScheme() SchemeName { | ||
return ss.Scheme | ||
} | ||
|
||
func (ss *BearerSecurityScheme) GetIn() *SchemeIn { | ||
return &ss.In | ||
} | ||
|
||
func (ss *BearerSecurityScheme) GetName() string { | ||
return ss.Name | ||
} | ||
|
||
func (ss *BearerSecurityScheme) GetHeaders() http.Header { | ||
header := http.Header{} | ||
attackValue := ss.GetAttackValue().(string) | ||
if attackValue == "" && ss.HasValidValue() { | ||
attackValue = ss.GetValidValue().(string) | ||
func NewAuthorizationBearerSecurityScheme(name string, value *string) (*SecurityScheme, error) { | ||
in := InHeader | ||
securityScheme, err := NewSecurityScheme(name, nil, HttpType, BearerScheme, &in, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if attackValue != "" { | ||
header.Set(AuthorizationHeader, fmt.Sprintf("%s %s", BearerPrefix, attackValue)) | ||
if value != nil && *value != "" { | ||
err = securityScheme.SetValidValue(*value) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var tokenFormat TokenFormat | ||
if jwt.IsJWT(*value) { | ||
tokenFormat = JWTTokenFormat | ||
} else { | ||
tokenFormat = NoneTokenFormat | ||
} | ||
if err = securityScheme.SetTokenFormat(tokenFormat); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return header | ||
} | ||
|
||
func (ss *BearerSecurityScheme) GetCookies() []*http.Cookie { | ||
return []*http.Cookie{} | ||
} | ||
|
||
func (ss *BearerSecurityScheme) HasValidValue() bool { | ||
return ss.ValidValue != nil && *ss.ValidValue != "" | ||
return securityScheme, nil | ||
} | ||
|
||
func (ss *BearerSecurityScheme) GetValidValue() interface{} { | ||
if !ss.HasValidValue() { | ||
return nil | ||
func MustNewAuthorizationBearerSecurityScheme(name string, value *string) *SecurityScheme { | ||
securityScheme, err := NewAuthorizationBearerSecurityScheme(name, value) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return *ss.ValidValue | ||
} | ||
|
||
func (ss *BearerSecurityScheme) GetValidValueWriter() interface{} { | ||
return nil | ||
} | ||
|
||
func (ss *BearerSecurityScheme) SetAttackValue(v interface{}) { | ||
ss.AttackValue = v.(string) | ||
} | ||
|
||
func (ss *BearerSecurityScheme) GetAttackValue() interface{} { | ||
return ss.AttackValue | ||
return securityScheme | ||
} |
Oops, something went wrong.