Skip to content

Commit

Permalink
fix: basic_auth authenticator is not responsible for the request any …
Browse files Browse the repository at this point in the history
…more if the Authorization header does not contain Basic Auth schema (#107)
  • Loading branch information
dadrus authored Jul 23, 2022
1 parent 6129f10 commit 96136ef
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
20 changes: 10 additions & 10 deletions internal/pipeline/authenticators/basic_auth_authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ type basicAuthAuthenticator struct {
}

func newBasicAuthAuthenticator(rawConfig map[string]any) (*basicAuthAuthenticator, error) {
type _config struct {
type Config struct {
UserID string `mapstructure:"user_id"`
Password string `mapstructure:"password"`
}

var conf _config
var conf Config

if err := decodeConfig(rawConfig, &conf); err != nil {
return nil, errorchain.
Expand Down Expand Up @@ -85,19 +85,19 @@ func (a *basicAuthAuthenticator) Execute(ctx heimdall.Context) (*subject.Subject

headerValue := ctx.RequestHeader("Authorization")
if len(headerValue) == 0 {
return nil, errorchain.NewWithMessage(heimdall.ErrAuthentication, "no Authorization header received").
CausedBy(heimdall.ErrArgument)
return nil, errorchain.NewWithMessage(heimdall.ErrAuthentication,
"no Authorization header received").CausedBy(heimdall.ErrArgument)
}

schemeAndValue := strings.Split(headerValue, " ")
if len(schemeAndValue) != basicAuthSchemeAttributes {
return nil, errorchain.
NewWithMessage(heimdall.ErrAuthentication, "unexpected value in the Authorization header")
return nil, errorchain.NewWithMessage(heimdall.ErrAuthentication,
"unexpected value in the Authorization header").CausedBy(heimdall.ErrArgument)
}

if schemeAndValue[0] != "Basic" {
return nil, errorchain.
NewWithMessagef(heimdall.ErrAuthentication, "unexpected authentication scheme: %s", schemeAndValue[0])
return nil, errorchain.NewWithMessagef(heimdall.ErrAuthentication,
"unexpected authentication scheme: %s", schemeAndValue[0]).CausedBy(heimdall.ErrArgument)
}

res, err := base64.StdEncoding.DecodeString(schemeAndValue[1])
Expand Down Expand Up @@ -137,12 +137,12 @@ func (a *basicAuthAuthenticator) WithConfig(rawConfig map[string]any) (Authentic
return a, nil
}

type _config struct {
type Config struct {
UserID string `mapstructure:"user_id"`
Password string `mapstructure:"password"`
}

var conf _config
var conf Config

if err := decodeConfig(rawConfig, &conf); err != nil {
return nil, errorchain.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ password: bar`))

assert.Error(t, err)
assert.ErrorIs(t, err, heimdall.ErrAuthentication)
assert.ErrorIs(t, err, heimdall.ErrArgument)
assert.Contains(t, err.Error(), "unexpected value")

assert.Nil(t, sub)
Expand All @@ -300,6 +301,7 @@ password: bar`))

assert.Error(t, err)
assert.ErrorIs(t, err, heimdall.ErrAuthentication)
assert.ErrorIs(t, err, heimdall.ErrArgument)
assert.Contains(t, err.Error(), "unexpected authentication scheme")

assert.Nil(t, sub)
Expand All @@ -317,6 +319,7 @@ password: bar`))

assert.Error(t, err)
assert.ErrorIs(t, err, heimdall.ErrAuthentication)
assert.NotErrorIs(t, err, heimdall.ErrArgument)
assert.Contains(t, err.Error(), "failed to decode")

assert.Nil(t, sub)
Expand All @@ -335,6 +338,7 @@ password: bar`))

assert.Error(t, err)
assert.ErrorIs(t, err, heimdall.ErrAuthentication)
assert.NotErrorIs(t, err, heimdall.ErrArgument)
assert.Contains(t, err.Error(), "malformed user-id - password")

assert.Nil(t, sub)
Expand All @@ -353,6 +357,7 @@ password: bar`))

assert.Error(t, err)
assert.ErrorIs(t, err, heimdall.ErrAuthentication)
assert.NotErrorIs(t, err, heimdall.ErrArgument)
assert.Contains(t, err.Error(), "invalid user credentials")

assert.Nil(t, sub)
Expand All @@ -371,6 +376,7 @@ password: bar`))

assert.Error(t, err)
assert.ErrorIs(t, err, heimdall.ErrAuthentication)
assert.NotErrorIs(t, err, heimdall.ErrArgument)
assert.Contains(t, err.Error(), "invalid user credentials")

assert.Nil(t, sub)
Expand Down

0 comments on commit 96136ef

Please sign in to comment.