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