frostfs-s3-gw/auth/bearer-token.go
Evgeniy Kulikov 3b1fd968f8 [#25] Migrate auth package to NeoFS API v2
- upgrade to NeoFS API v2
- upgrade to new Authmate package

closes #25

Signed-off-by: Evgeniy Kulikov <kim@nspcc.ru>
2020-10-13 12:33:33 +03:00

30 lines
831 B
Go

package auth
import (
"context"
"github.com/nspcc-dev/neofs-api-go/pkg/token"
"github.com/pkg/errors"
)
type contextKey string
const bearerTokenContextKey contextKey = "bearer-token"
// GetBearerToken returns a bearer token embedded into a context or error, if any.
func GetBearerToken(ctx context.Context) (*token.BearerToken, error) {
bt := ctx.Value(bearerTokenContextKey)
if bt == nil {
return nil, errors.New("got nil bearer token")
}
v, ok := bt.(*token.BearerToken)
if !ok {
return nil, errors.Errorf("extracted unexpected type other than bearer token's: %T", v)
}
return v, nil
}
// SetBearerToken return a context with embedded bearer token.
func SetBearerToken(ctx context.Context, bearerToken *token.BearerToken) context.Context {
return context.WithValue(ctx, bearerTokenContextKey, bearerToken)
}