Skip to content
This repository has been archived by the owner on Feb 28, 2019. It is now read-only.

Commit

Permalink
Fix yaml tag for write whitelisted user ids (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
xichen2020 committed Nov 20, 2017
1 parent 625217b commit 5dc4c6a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
10 changes: 5 additions & 5 deletions auth/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ type authenticationConfig struct {
type authorizationConfig struct {
// This indicates whether reads should use a read whitelist.
ReadWhitelistEnabled bool `yaml:"readWhitelistEnabled,omitempty"`
// This indicates whether writes should use a write whitelist.
WriteWhitelistEnabled bool `yaml:"writeWhitelistEnabled,omitempty"`
// This is a list of users that are allowed to perform read operations.
ReadWhitelistedUserIDs []string `yaml:"readWhitelistedUserIDs,omitempty"`
// This indicates whether writes should use a write whitelist.
WriteWhitelistEnabled bool `yaml:"writeWhitelistEnabled,omitempty"`
// This is a list of users that are allowed to perform write operations.
WriteWhitelistedUserIDs []string `yaml:"readWhitelistedUserIDs,omitempty"`
WriteWhitelistedUserIDs []string `yaml:"writeWhitelistedUserIDs,omitempty"`
}

// NewSimpleAuth creates a new simple auth instance given using the provided config.
Expand All @@ -58,8 +58,8 @@ func (ac SimpleAuthConfig) NewSimpleAuth() HTTPAuthService {
},
authorization: simpleAuthorization{
readWhitelistEnabled: ac.Authorization.ReadWhitelistEnabled,
writeWhitelistEnabled: ac.Authorization.WriteWhitelistEnabled,
readWhitelistedUserIDs: ac.Authorization.ReadWhitelistedUserIDs,
writeWhitelistEnabled: ac.Authorization.WriteWhitelistEnabled,
writeWhitelistedUserIDs: ac.Authorization.WriteWhitelistedUserIDs,
},
}
Expand All @@ -83,8 +83,8 @@ func (a simpleAuthentication) authenticate(userID string) error {

type simpleAuthorization struct {
readWhitelistEnabled bool
writeWhitelistEnabled bool
readWhitelistedUserIDs []string
writeWhitelistEnabled bool
writeWhitelistedUserIDs []string
}

Expand Down
24 changes: 24 additions & 0 deletions auth/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"testing"

"github.com/stretchr/testify/require"
yaml "gopkg.in/yaml.v2"
)

var (
Expand All @@ -43,6 +44,29 @@ var (
}
)

func TestSimpleAuthConfigUnmarshal(t *testing.T) {
configStr := `
authentication:
userIDHeader: user-id
authorization:
readWhitelistEnabled: true
readWhitelistedUserIDs:
- foo
- bar
writeWhitelistEnabled: true
writeWhitelistedUserIDs:
- bar
- baz
`
var cfg SimpleAuthConfig
require.NoError(t, yaml.Unmarshal([]byte(configStr), &cfg))
require.Equal(t, "user-id", cfg.Authentication.UserIDHeader)
require.True(t, cfg.Authorization.ReadWhitelistEnabled)
require.Equal(t, []string{"foo", "bar"}, cfg.Authorization.ReadWhitelistedUserIDs)
require.True(t, cfg.Authorization.WriteWhitelistEnabled)
require.Equal(t, []string{"bar", "baz"}, cfg.Authorization.WriteWhitelistedUserIDs)
}

func TestNewSimpleAuth(t *testing.T) {
an := testConfig.NewSimpleAuth().(simpleAuth).authentication
az := testConfig.NewSimpleAuth().(simpleAuth).authorization
Expand Down

0 comments on commit 5dc4c6a

Please sign in to comment.