-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5f0543c
commit 6671cbb
Showing
20 changed files
with
424 additions
and
68 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package auth | ||
|
||
import "encoding/base64" | ||
|
||
type HTTPBasicCredentials struct { | ||
Username string `json:"username" yaml:"username"` | ||
Password string `json:"password" yaml:"password"` | ||
} | ||
|
||
func NewHTTPBasicCredentials(username string, password string) *HTTPBasicCredentials { | ||
return &HTTPBasicCredentials{ | ||
Username: username, | ||
Password: password, | ||
} | ||
} | ||
|
||
func (credentials *HTTPBasicCredentials) GetUsername() string { | ||
return credentials.Username | ||
} | ||
|
||
func (credentials *HTTPBasicCredentials) GetPassword() string { | ||
return credentials.Password | ||
} | ||
|
||
func (credentials *HTTPBasicCredentials) Encode() string { | ||
return base64.StdEncoding.EncodeToString([]byte(credentials.GetUsername() + ":" + credentials.GetPassword())) | ||
} | ||
|
||
func NewAuthorizationBasicSecurityScheme(name string, credentials *HTTPBasicCredentials) (*SecurityScheme, error) { | ||
in := InHeader | ||
securityScheme, err := NewSecurityScheme(name, nil, HttpType, BasicScheme, &in, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if credentials != nil { | ||
err = securityScheme.SetValidValue(credentials) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return securityScheme, nil | ||
} | ||
|
||
func MustNewAuthorizationBasicSecurityScheme(name string, credentials *HTTPBasicCredentials) *SecurityScheme { | ||
securityScheme, err := NewAuthorizationBasicSecurityScheme(name, credentials) | ||
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,47 @@ | ||
package auth_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cerberauth/vulnapi/internal/auth" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewAuthorizationBasicSecurityScheme(t *testing.T) { | ||
name := "basic" | ||
basicCredentials := auth.NewHTTPBasicCredentials("admin", "password") | ||
|
||
securityScheme, err := auth.NewAuthorizationBasicSecurityScheme(name, basicCredentials) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, auth.HttpType, securityScheme.GetType()) | ||
assert.Equal(t, auth.BasicScheme, securityScheme.GetScheme()) | ||
assert.Equal(t, auth.InHeader, *securityScheme.GetIn()) | ||
assert.Equal(t, name, securityScheme.GetName()) | ||
assert.Equal(t, basicCredentials, securityScheme.GetValidValue()) | ||
assert.Equal(t, nil, securityScheme.GetAttackValue()) | ||
} | ||
|
||
func TestNewAuthorizationBasicSecurityScheme_WhenNilValue(t *testing.T) { | ||
name := "basic" | ||
|
||
securityScheme, err := auth.NewAuthorizationBasicSecurityScheme(name, nil) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, nil, securityScheme.GetValidValue()) | ||
assert.Equal(t, nil, securityScheme.GetAttackValue()) | ||
} | ||
|
||
func TestMustNewAuthorizationBasicSecurityScheme(t *testing.T) { | ||
name := "basic" | ||
basicCredentials := auth.NewHTTPBasicCredentials("admin", "password") | ||
|
||
securityScheme := auth.MustNewAuthorizationBasicSecurityScheme(name, basicCredentials) | ||
|
||
assert.Equal(t, auth.HttpType, securityScheme.GetType()) | ||
assert.Equal(t, auth.BasicScheme, securityScheme.GetScheme()) | ||
assert.Equal(t, auth.InHeader, *securityScheme.GetIn()) | ||
assert.Equal(t, name, securityScheme.GetName()) | ||
assert.Equal(t, basicCredentials, securityScheme.GetValidValue()) | ||
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
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
Oops, something went wrong.