implement auth middleware

This commit is contained in:
Jakob Schrettenbrunner
2017-07-06 20:49:36 +02:00
parent 260c9d70ab
commit 0bceb409e5
4 changed files with 233 additions and 7 deletions

View File

@@ -76,13 +76,16 @@ type Config struct {
// If set to <= 0 logs are kept forever
DeleteAfterDays int `mapstructure:"deleteAfterDays"`
} `mapstructure:"log"`
AuthKeys []string `mapstructure:"authKeys"`
}
var config *Config
func LoadConfiguration(path *string) error {
if path != nil {
viper.SetConfigFile(*path)
// LoadConfiguration loads the configuration from a specified file
func LoadConfiguration(path string) error {
if path != "" {
viper.SetConfigFile(path)
} else {
viper.AddConfigPath("./")
viper.SetConfigName("config")
@@ -109,3 +112,13 @@ func Get() *Config {
func setDefaults() {
}
// ContainsAuthKey checks wether the config contains a specified authentication key
func (c *Config) ContainsAuthKey(key string) bool {
for _, k := range c.AuthKeys {
if k == key {
return true
}
}
return false
}

View File

@@ -6,10 +6,22 @@ import (
"github.com/stretchr/testify/assert"
)
var configFile = "../config.example.json"
const configFile = "../config.example.json"
func TestLoadConfiguraiton(t *testing.T) {
err := LoadConfiguration(&configFile)
err := LoadConfiguration(configFile)
assert.Nil(t, err)
assert.Equal(t, Get().Web.ListenHost, "0.0.0.0")
}
func TestContainsAuthKey(t *testing.T) {
t.Run("key exists", func(t *testing.T) {
LoadConfiguration(configFile)
assert.True(t, Get().ContainsAuthKey("somekey"))
})
t.Run("key doesn't exist", func(t *testing.T) {
LoadConfiguration(configFile)
assert.False(t, Get().ContainsAuthKey("someotherkey"))
})
}