mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-23 15:51:24 +00:00
remove auth from token
This commit is contained in:
@@ -35,10 +35,6 @@ type Streams struct {
|
|||||||
Time func() time.Time
|
Time func() time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Token) Key() string {
|
|
||||||
return fmt.Sprintf("%s:%s", t.Account, t.Token)
|
|
||||||
}
|
|
||||||
|
|
||||||
func getAccount(acc *auth.Account) string {
|
func getAccount(acc *auth.Account) string {
|
||||||
owner := acc.Metadata["apikey_owner"]
|
owner := acc.Metadata["apikey_owner"]
|
||||||
if len(owner) == 0 {
|
if len(owner) == 0 {
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ func (s *Streams) Subscribe(ctx context.Context, req *pb.SubscribeRequest, strea
|
|||||||
|
|
||||||
// find the token and check to see if it has expired
|
// find the token and check to see if it has expired
|
||||||
var token Token
|
var token Token
|
||||||
if err := s.Cache.Get(req.Token, &token); err == store.ErrNotFound {
|
if err := s.Cache.Get("token:"+req.Token, &token); err == store.ErrNotFound {
|
||||||
return ErrInvalidToken
|
return ErrInvalidToken
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
logger.Errorf("Error reading token from store: %v", err)
|
logger.Errorf("Error reading token from store: %v", err)
|
||||||
|
|||||||
@@ -11,26 +11,26 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (s *Streams) Token(ctx context.Context, req *pb.TokenRequest, rsp *pb.TokenResponse) error {
|
func (s *Streams) Token(ctx context.Context, req *pb.TokenRequest, rsp *pb.TokenResponse) error {
|
||||||
acc, ok := auth.AccountFromContext(ctx)
|
|
||||||
if !ok {
|
|
||||||
return errors.Unauthorized("UNAUTHORIZED", "Unauthorized")
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(req.Topic) > 0 {
|
if len(req.Topic) > 0 {
|
||||||
if err := validateTopicInput(req.Topic); err != nil {
|
if err := validateTopicInput(req.Topic); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var account string
|
||||||
|
if acc, ok := auth.AccountFromContext(ctx); ok {
|
||||||
|
account = getAccount(acc)
|
||||||
|
}
|
||||||
|
|
||||||
// construct the token and write it to the database
|
// construct the token and write it to the database
|
||||||
t := Token{
|
t := Token{
|
||||||
Token: uuid.New().String(),
|
Token: uuid.New().String(),
|
||||||
ExpiresAt: s.Time().Add(TokenTTL),
|
ExpiresAt: s.Time().Add(TokenTTL),
|
||||||
Topic: req.Topic,
|
Topic: req.Topic,
|
||||||
Account: getAccount(acc),
|
Account: account,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := s.Cache.Put(t.Token, t, t.ExpiresAt); err != nil {
|
if err := s.Cache.Put("token:"+t.Token, t, t.ExpiresAt); err != nil {
|
||||||
logger.Errorf("Error creating token in store: %v", err)
|
logger.Errorf("Error creating token in store: %v", err)
|
||||||
return errors.InternalServerError("DATABASE_ERROR", "Error writing token to database")
|
return errors.InternalServerError("DATABASE_ERROR", "Error writing token to database")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user