2016-05-30 16:08:09 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/imdario/mergo"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-06-03 04:11:50 +02:00
|
|
|
// DefaultBasicAuthRealm is "Authorization Required"
|
2016-05-31 10:05:42 +02:00
|
|
|
DefaultBasicAuthRealm = "Authorization Required"
|
|
|
|
// DefaultBasicAuthContextKey is the "auth"
|
|
|
|
// this key is used to do context.Set("auth", theUsernameFromBasicAuth)
|
2016-05-30 16:08:09 +02:00
|
|
|
DefaultBasicAuthContextKey = "auth"
|
|
|
|
)
|
|
|
|
|
2016-05-31 10:05:42 +02:00
|
|
|
// BasicAuth the configs for the basicauth middleware
|
2016-05-30 16:08:09 +02:00
|
|
|
type BasicAuth struct {
|
|
|
|
// Users a map of login and the value (username/password)
|
|
|
|
Users map[string]string
|
|
|
|
// Realm http://tools.ietf.org/html/rfc2617#section-1.2. Default is "Authorization Required"
|
|
|
|
Realm string
|
|
|
|
// ContextKey the key for ctx.GetString(...). Default is 'auth'
|
|
|
|
ContextKey string
|
|
|
|
// Expires expiration duration, default is 0 never expires
|
|
|
|
Expires time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultBasicAuth returns the default configs for the BasicAuth middleware
|
|
|
|
func DefaultBasicAuth() BasicAuth {
|
|
|
|
return BasicAuth{make(map[string]string), DefaultBasicAuthRealm, DefaultBasicAuthContextKey, 0}
|
|
|
|
}
|
|
|
|
|
2016-05-31 10:05:42 +02:00
|
|
|
// MergeSingle merges the default with the given config and returns the result
|
2016-05-30 16:08:09 +02:00
|
|
|
func (c BasicAuth) MergeSingle(cfg BasicAuth) (config BasicAuth) {
|
|
|
|
|
|
|
|
config = cfg
|
|
|
|
mergo.Merge(&config, c)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|