chronograf/uuid/v4.go

45 lines
926 B
Go
Raw Normal View History

package uuid
import (
2016-10-25 15:20:06 +00:00
"context"
"time"
2016-10-20 23:01:14 +00:00
"github.com/influxdata/chronograf"
uuid "github.com/satori/go.uuid"
)
2016-10-20 23:01:14 +00:00
// V4 implements chronograf.ID
2016-10-11 00:34:05 +00:00
type V4 struct{}
// Generate creates a UUID v4 string
func (i *V4) Generate() (string, error) {
2016-10-11 00:34:05 +00:00
return uuid.NewV4().String(), nil
}
2016-10-20 23:01:14 +00:00
// APIKey implements chronograf.Authenticator using V4
type APIKey struct {
Key string
}
// NewAPIKey creates an APIKey with a UUID v4 Key
2016-10-20 23:01:14 +00:00
func NewAPIKey() chronograf.Authenticator {
v4 := V4{}
key, _ := v4.Generate()
return &APIKey{
Key: key,
}
}
// Authenticate checks the key against the UUID v4 key
2016-10-20 23:01:14 +00:00
func (k *APIKey) Authenticate(ctx context.Context, key string) (chronograf.Principal, error) {
if key != k.Key {
2016-10-20 23:01:14 +00:00
return "", chronograf.ErrAuthentication
}
return "admin", nil
}
// Token returns the UUID v4 key
2016-10-20 23:01:14 +00:00
func (k *APIKey) Token(context.Context, chronograf.Principal, time.Duration) (string, error) {
return k.Key, nil
}