From ac62b8b35bb9f52e5875709cc3251ab355aa4956 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Sun, 2 May 2021 09:57:23 +0100 Subject: [PATCH] remove auth from token --- streams/handler/handler.go | 4 ---- streams/handler/subscribe.go | 2 +- streams/handler/token.go | 14 +++++++------- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/streams/handler/handler.go b/streams/handler/handler.go index 510d011..04c060e 100644 --- a/streams/handler/handler.go +++ b/streams/handler/handler.go @@ -35,10 +35,6 @@ type Streams struct { Time func() time.Time } -func (t *Token) Key() string { - return fmt.Sprintf("%s:%s", t.Account, t.Token) -} - func getAccount(acc *auth.Account) string { owner := acc.Metadata["apikey_owner"] if len(owner) == 0 { diff --git a/streams/handler/subscribe.go b/streams/handler/subscribe.go index 786baf1..d9d9dcc 100644 --- a/streams/handler/subscribe.go +++ b/streams/handler/subscribe.go @@ -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 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 } else if err != nil { logger.Errorf("Error reading token from store: %v", err) diff --git a/streams/handler/token.go b/streams/handler/token.go index 0da1c84..42467bb 100644 --- a/streams/handler/token.go +++ b/streams/handler/token.go @@ -11,26 +11,26 @@ import ( ) 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 err := validateTopicInput(req.Topic); err != nil { return err } } + var account string + if acc, ok := auth.AccountFromContext(ctx); ok { + account = getAccount(acc) + } + // construct the token and write it to the database t := Token{ Token: uuid.New().String(), ExpiresAt: s.Time().Add(TokenTTL), 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) return errors.InternalServerError("DATABASE_ERROR", "Error writing token to database") }