mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-14 20:14:47 +00:00
Multitenant streams api (#72)
This commit is contained in:
@@ -4,17 +4,29 @@ 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,
|
||||
Namespace: acc.Issuer,
|
||||
}
|
||||
if err := s.DB.Create(&t).Error; err != nil {
|
||||
logger.Errorf("Error creating token in store: %v", err)
|
||||
|
||||
Reference in New Issue
Block a user