2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-10-09 15:39:14 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2020-10-06 11:40:31 +01:00
|
|
|
package consoleauth
|
2018-10-09 15:39:14 +01:00
|
|
|
|
2018-11-26 17:08:29 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
)
|
2018-10-09 15:39:14 +01:00
|
|
|
|
|
|
|
// The key type is unexported to prevent collisions with context keys defined in
|
|
|
|
// other packages.
|
2019-07-31 12:57:13 +01:00
|
|
|
type apikey struct{}
|
2018-10-09 15:39:14 +01:00
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// WithAPIKey creates context with api key.
|
2018-10-09 15:39:14 +01:00
|
|
|
func WithAPIKey(ctx context.Context, key []byte) context.Context {
|
2019-07-31 12:57:13 +01:00
|
|
|
return context.WithValue(ctx, apikey{}, key)
|
2018-10-09 15:39:14 +01:00
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// GetAPIKey returns api key from context is exists.
|
2018-10-09 15:39:14 +01:00
|
|
|
func GetAPIKey(ctx context.Context) ([]byte, bool) {
|
2019-07-31 12:57:13 +01:00
|
|
|
key, ok := ctx.Value(apikey{}).([]byte)
|
2018-10-09 15:39:14 +01:00
|
|
|
return key, ok
|
|
|
|
}
|