2022-03-29 09:53:19 +00:00
|
|
|
package authz
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-01-17 10:16:48 +00:00
|
|
|
"time"
|
2022-04-25 09:16:36 +00:00
|
|
|
|
|
|
|
"golang.org/x/text/language"
|
2024-02-28 08:55:54 +00:00
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/feature"
|
2022-03-29 09:53:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
emptyInstance = &instance{}
|
|
|
|
)
|
|
|
|
|
|
|
|
type Instance interface {
|
|
|
|
InstanceID() string
|
|
|
|
ProjectID() string
|
|
|
|
ConsoleClientID() string
|
2022-04-14 12:19:18 +00:00
|
|
|
ConsoleApplicationID() string
|
2022-04-12 14:20:17 +00:00
|
|
|
RequestedDomain() string
|
2022-04-25 09:16:36 +00:00
|
|
|
RequestedHost() string
|
|
|
|
DefaultLanguage() language.Tag
|
2022-06-03 12:30:39 +00:00
|
|
|
DefaultOrganisationID() string
|
2022-12-14 06:17:36 +00:00
|
|
|
SecurityPolicyAllowedOrigins() []string
|
2024-02-28 10:21:11 +00:00
|
|
|
EnableImpersonation() bool
|
2024-01-17 10:16:48 +00:00
|
|
|
Block() *bool
|
|
|
|
AuditLogRetention() *time.Duration
|
2024-02-28 08:55:54 +00:00
|
|
|
Features() feature.Features
|
2022-03-29 09:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type InstanceVerifier interface {
|
2023-02-15 01:52:11 +00:00
|
|
|
InstanceByHost(ctx context.Context, host string) (Instance, error)
|
|
|
|
InstanceByID(ctx context.Context) (Instance, error)
|
2022-03-29 09:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type instance struct {
|
2022-06-03 12:30:39 +00:00
|
|
|
id string
|
|
|
|
domain string
|
2022-04-25 09:16:36 +00:00
|
|
|
projectID string
|
|
|
|
appID string
|
|
|
|
clientID string
|
2022-06-03 12:30:39 +00:00
|
|
|
orgID string
|
2024-02-28 08:55:54 +00:00
|
|
|
features feature.Features
|
2022-03-29 09:53:19 +00:00
|
|
|
}
|
|
|
|
|
2024-01-17 10:16:48 +00:00
|
|
|
func (i *instance) Block() *bool {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *instance) AuditLogRetention() *time.Duration {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-29 09:53:19 +00:00
|
|
|
func (i *instance) InstanceID() string {
|
2022-06-03 12:30:39 +00:00
|
|
|
return i.id
|
2022-03-29 09:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *instance) ProjectID() string {
|
2022-04-25 09:16:36 +00:00
|
|
|
return i.projectID
|
2022-03-29 09:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *instance) ConsoleClientID() string {
|
2022-04-25 09:16:36 +00:00
|
|
|
return i.clientID
|
2022-03-29 09:53:19 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 12:19:18 +00:00
|
|
|
func (i *instance) ConsoleApplicationID() string {
|
2022-04-25 09:16:36 +00:00
|
|
|
return i.appID
|
2022-04-14 12:19:18 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 14:20:17 +00:00
|
|
|
func (i *instance) RequestedDomain() string {
|
2022-06-03 12:30:39 +00:00
|
|
|
return i.domain
|
2022-04-12 14:20:17 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 09:16:36 +00:00
|
|
|
func (i *instance) RequestedHost() string {
|
2022-06-03 12:30:39 +00:00
|
|
|
return i.domain
|
2022-04-25 09:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *instance) DefaultLanguage() language.Tag {
|
|
|
|
return language.Und
|
|
|
|
}
|
|
|
|
|
2022-06-03 12:30:39 +00:00
|
|
|
func (i *instance) DefaultOrganisationID() string {
|
|
|
|
return i.orgID
|
|
|
|
}
|
|
|
|
|
2022-12-14 06:17:36 +00:00
|
|
|
func (i *instance) SecurityPolicyAllowedOrigins() []string {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-28 10:21:11 +00:00
|
|
|
func (i *instance) EnableImpersonation() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-02-28 08:55:54 +00:00
|
|
|
func (i *instance) Features() feature.Features {
|
|
|
|
return i.features
|
|
|
|
}
|
|
|
|
|
2022-03-29 09:53:19 +00:00
|
|
|
func GetInstance(ctx context.Context) Instance {
|
|
|
|
instance, ok := ctx.Value(instanceKey).(Instance)
|
|
|
|
if !ok {
|
|
|
|
return emptyInstance
|
|
|
|
}
|
|
|
|
return instance
|
|
|
|
}
|
|
|
|
|
2024-02-28 08:55:54 +00:00
|
|
|
func GetFeatures(ctx context.Context) feature.Features {
|
|
|
|
return GetInstance(ctx).Features()
|
|
|
|
}
|
|
|
|
|
2022-03-29 09:53:19 +00:00
|
|
|
func WithInstance(ctx context.Context, instance Instance) context.Context {
|
|
|
|
return context.WithValue(ctx, instanceKey, instance)
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithInstanceID(ctx context.Context, id string) context.Context {
|
2022-06-03 12:30:39 +00:00
|
|
|
return context.WithValue(ctx, instanceKey, &instance{id: id})
|
2022-03-29 09:53:19 +00:00
|
|
|
}
|
2022-04-12 14:20:17 +00:00
|
|
|
|
|
|
|
func WithRequestedDomain(ctx context.Context, domain string) context.Context {
|
|
|
|
i, ok := ctx.Value(instanceKey).(*instance)
|
|
|
|
if !ok {
|
|
|
|
i = new(instance)
|
|
|
|
}
|
|
|
|
|
2022-06-03 12:30:39 +00:00
|
|
|
i.domain = domain
|
2022-04-12 14:20:17 +00:00
|
|
|
return context.WithValue(ctx, instanceKey, i)
|
|
|
|
}
|
2022-04-25 09:16:36 +00:00
|
|
|
|
|
|
|
func WithConsole(ctx context.Context, projectID, appID string) context.Context {
|
|
|
|
i, ok := ctx.Value(instanceKey).(*instance)
|
|
|
|
if !ok {
|
|
|
|
i = new(instance)
|
|
|
|
}
|
|
|
|
|
|
|
|
i.projectID = projectID
|
|
|
|
i.appID = appID
|
|
|
|
//i.clientID = clientID
|
|
|
|
return context.WithValue(ctx, instanceKey, i)
|
|
|
|
}
|
2024-02-28 08:55:54 +00:00
|
|
|
|
|
|
|
func WithFeatures(ctx context.Context, f feature.Features) context.Context {
|
|
|
|
i, ok := ctx.Value(instanceKey).(*instance)
|
|
|
|
if !ok {
|
|
|
|
i = new(instance)
|
|
|
|
}
|
|
|
|
i.features = f
|
|
|
|
return context.WithValue(ctx, instanceKey, i)
|
|
|
|
}
|