make multi tenancy based on namespace and ID of account (#85)

* make multi tenancy based on namespace and ID of account not just namespace

* fix tests
This commit is contained in:
Dominic Wong
2021-04-21 15:09:38 +01:00
committed by GitHub
parent 4361ef6d90
commit 7ae45b522e
13 changed files with 68 additions and 47 deletions

View File

@@ -4,11 +4,12 @@ import (
"fmt"
"time"
"github.com/micro/micro/v3/service/auth"
"github.com/micro/micro/v3/service/errors"
"github.com/micro/micro/v3/service/events"
gorm2 "github.com/micro/services/pkg/gorm"
"github.com/nats-io/nats-streaming-server/util"
"gorm.io/gorm"
)
var (
@@ -26,18 +27,21 @@ type Token struct {
Token string `gorm:"primaryKey"`
Topic string
ExpiresAt time.Time
Namespace string
}
type Streams struct {
DB *gorm.DB
gorm2.Helper
Events events.Stream
Time func() time.Time
}
// fmtTopic returns a topic string with namespace prefix
func fmtTopic(ns, topic string) string {
return fmt.Sprintf("%s.%s", ns, topic)
func fmtTopic(acc *auth.Account, topic string) string {
owner := acc.Metadata["apikey_owner"]
if len(owner) == 0 {
owner = acc.ID
}
return fmt.Sprintf("%s.%s.%s", acc.Issuer, owner, topic)
}
// validateTopicInput validates that topic is alphanumeric