2016-10-10 22:00:27 +00:00
|
|
|
package uuid
|
|
|
|
|
2016-10-16 01:34:57 +00:00
|
|
|
import (
|
2016-10-25 15:20:06 +00:00
|
|
|
"context"
|
2016-10-16 01:34:57 +00:00
|
|
|
"time"
|
2016-10-10 22:00:27 +00:00
|
|
|
|
2016-10-20 23:01:14 +00:00
|
|
|
"github.com/influxdata/chronograf"
|
2016-10-16 01:34:57 +00:00
|
|
|
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{}
|
2016-10-10 22:00:27 +00:00
|
|
|
|
2016-10-16 01:34:57 +00:00
|
|
|
// Generate creates a UUID v4 string
|
2016-10-10 22:00:27 +00:00
|
|
|
func (i *V4) Generate() (string, error) {
|
2016-10-11 00:34:05 +00:00
|
|
|
return uuid.NewV4().String(), nil
|
2016-10-10 22:00:27 +00:00
|
|
|
}
|
2016-10-16 01:34:57 +00:00
|
|
|
|
2016-10-20 23:01:14 +00:00
|
|
|
// APIKey implements chronograf.Authenticator using V4
|
2016-10-16 01:34:57 +00:00
|
|
|
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 {
|
2016-10-16 01:34:57 +00:00
|
|
|
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) {
|
2016-10-16 01:34:57 +00:00
|
|
|
if key != k.Key {
|
2016-10-20 23:01:14 +00:00
|
|
|
return "", chronograf.ErrAuthentication
|
2016-10-16 01:34:57 +00:00
|
|
|
}
|
|
|
|
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) {
|
2016-10-16 01:34:57 +00:00
|
|
|
return k.Key, nil
|
|
|
|
}
|