mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-16 04:54:42 +00:00
* fixup streams stuff * use cache instead of pg in streams * hash out issuer streams test * fix compile error
42 lines
1.0 KiB
Go
42 lines
1.0 KiB
Go
package handler
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/micro/micro/v3/service/auth"
|
|
"github.com/micro/micro/v3/service/errors"
|
|
"github.com/micro/micro/v3/service/logger"
|
|
pb "github.com/micro/services/streams/proto"
|
|
)
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
// 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),
|
|
}
|
|
|
|
if err := s.Cache.Put(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")
|
|
}
|
|
|
|
// return the token in the response
|
|
rsp.Token = t.Token
|
|
return nil
|
|
}
|