Documentation
¶
Overview ¶
Authentication and Authorization Middleware
Middleware for IDs
Index ¶
- Constants
- Variables
- func AccountIDFromContext(ctx context.Context) string
- func AccountPublicKeyFromContext(ctx context.Context) string
- func ClaimsFromContext(ctx context.Context) *user.UserClaims
- func EndpointKinds() []string
- func InKind(kind string) bool
- func NoAuthFromContext(ctx context.Context) bool
- func UserIDFromContext(ctx context.Context) string
- func ValidBasicAuthFromContext(ctx context.Context) bool
- func ValidTokenFromContext(ctx context.Context) bool
- type Auth
- func (auth *Auth) AddTrustedAccount(id string)
- func (auth *Auth) Basic(h http.HandlerFunc) http.HandlerFunc
- func (auth *Auth) BasicOrBearer(h http.HandlerFunc) http.HandlerFunc
- func (auth *Auth) Bearer(h http.HandlerFunc) http.HandlerFunc
- func (auth *Auth) IsPublicKeyTrusted(publicKey string) bool
- func (auth *Auth) RequireAdmin(adminAccountID string, h http.HandlerFunc) http.HandlerFunc
- func (auth *Auth) RequireRole(adminAccountID string, role string, h http.HandlerFunc) http.HandlerFunc
- func (auth *Auth) RequireSuperAdmin(adminAccountID string, h http.HandlerFunc) http.HandlerFunc
- func (auth *Auth) SetStore(st IdentityStore)
- type AuthConfig
- type AuthContextKey
- type EndpointHealth
- type ID
- type IDConfig
- type IDContextKey
- type IdentityStore
- type ServiceEndpoint
- type ServiceEndpoints
- type TenantCreatedNotification
- type TenantDeletedNotification
- type TenantUpdatedNotification
Constants ¶
const ( SubjectTenantCreated = "tenant.notify.created" SubjectTenantUpdated = "tenant.notify.updated" SubjectTenantDeleted = "tenant.notify.deleted" )
NATS subjects published by the admin service for tenant lifecycle events. Subscribers (e.g., media-server) react to these to keep tenant-scoped resources in sync with the admin account store.
Variables ¶
var (
ENDPOINT_KINDS = []string{
"login_service",
"admin_service",
"proxy_service",
"messenger_service",
"blackbox_service",
"media_service",
"queue_service",
"controller_service",
"host_service",
"comms_service",
"proxy",
}
)
Functions ¶
func ClaimsFromContext ¶
func ClaimsFromContext(ctx context.Context) *user.UserClaims
ClaimsFromContext returns the validated user claims attached by the Bearer middleware, or nil if the request was not bearer-authenticated. The claims are stored as *user.UserClaims (see Bearer/BasicOrBearer), so this must assert that concrete type.
func EndpointKinds ¶
func EndpointKinds() []string
func NoAuthFromContext ¶
If notoken is attached to the context, there was no token attached to the request.
func ValidBasicAuthFromContext ¶
Is the user valid - from Basis Authentication with email:password.
func ValidTokenFromContext ¶
Does the request have a valid token?
Types ¶
type Auth ¶
type Auth struct {
// contains filtered or unexported fields
}
Store is optional when middleware is used by distributed service API endpoint - validates JWTs issued by trusted accounts. Authorization using JWT claims.
func NewAuth ¶
func NewAuth(authConfig AuthConfig) *Auth
NewAuth returns a *Auth. It returns a pointer (not a value) because Auth contains a sync.RWMutex and must not be copied.
func (*Auth) AddTrustedAccount ¶
func (*Auth) BasicOrBearer ¶
func (auth *Auth) BasicOrBearer(h http.HandlerFunc) http.HandlerFunc
Basic -or- Bearer
func (*Auth) Bearer ¶
func (auth *Auth) Bearer(h http.HandlerFunc) http.HandlerFunc
Bearer Auth Only
func (*Auth) IsPublicKeyTrusted ¶
func (*Auth) RequireAdmin ¶
func (auth *Auth) RequireAdmin(adminAccountID string, h http.HandlerFunc) http.HandlerFunc
RequireAdmin wraps a Bearer-authenticated handler and allows users from the ADMIN account (super admins) or users with the administrator role in any account. Returns 403 for all other users.
func (*Auth) RequireRole ¶
func (auth *Auth) RequireRole(adminAccountID string, role string, h http.HandlerFunc) http.HandlerFunc
RequireRole wraps a Bearer-authenticated handler and allows users from the ADMIN account (super admins) or users with the specified role in any account. Uses user.HasRole which accepts both singular and legacy plural role names. Returns 403 for all other users.
func (*Auth) RequireSuperAdmin ¶
func (auth *Auth) RequireSuperAdmin(adminAccountID string, h http.HandlerFunc) http.HandlerFunc
RequireSuperAdmin wraps a Bearer-authenticated handler and only allows users from the ADMIN account. Returns 403 for all other users.
func (*Auth) SetStore ¶
func (auth *Auth) SetStore(st IdentityStore)
SetStore attaches an admin Store handle to the auth middleware so RequireRole can look up users by their NATS public key and check their roles. Used by services that share a process with the admin server (the media server, for example) and need to gate handlers by the user's role on their own tenant account, not just by whether the caller is in the super-admin ADMIN account.
type AuthConfig ¶
type AuthConfig struct {
BasicAuth bool
Store IdentityStore
Logger *logger.Logger
TrustedAccounts []string
}
type AuthContextKey ¶
type AuthContextKey struct {
Key string
}
type EndpointHealth ¶
type EndpointHealth struct {
}
type IDContextKey ¶
type IDContextKey struct {
Key string
}
type IdentityStore ¶
type IdentityStore interface {
GetUserByEmail(email string) (*user.User, string, error)
GetUserByPublicKey(accountID, publicKey string) (*user.User, error)
GetActivity(accountID, activityID string) (user.Activity, error)
UpdateActivity(accountID string, a user.Activity) error
PutActivity(accountID string, activity user.Activity) error
ValidateUserPassword(accountID, userID, password string) (bool, error)
}
IdentityStore is the slice of the admin store that the Auth and ID middleware need: password validation and lockout accounting for basic auth, and user lookup for role checks. *admin.Store satisfies it. Services that only validate bearer tokens issued by trusted accounts do not need one at all.
type ServiceEndpoint ¶
type ServiceEndpoint struct {
Kind string `json:"kind"`
URL string `json:"url"`
Health EndpointHealth `json:"health"`
ProxyHost string `json:"proxy_host,omitempty"`
ProxyPort int `json:"proxy_port,omitempty"`
}
func NewServiceEndpoint ¶
func NewServiceEndpoint(kind string, url string) ServiceEndpoint
type ServiceEndpoints ¶
type ServiceEndpoints []ServiceEndpoint
func (*ServiceEndpoints) AddEndpoint ¶
func (serviceEndpoints *ServiceEndpoints) AddEndpoint(kind string, url string) error
func (*ServiceEndpoints) ByKind ¶
func (serviceEndpoints *ServiceEndpoints) ByKind(kind string) *[]ServiceEndpoint
type TenantCreatedNotification ¶
type TenantCreatedNotification struct {
AccountID string `json:"accountId"`
AccountName string `json:"accountName"`
Timestamp int64 `json:"timestamp"`
}
TenantCreatedNotification is published when a new account is created.
type TenantDeletedNotification ¶
type TenantDeletedNotification struct {
AccountID string `json:"accountId"`
AccountName string `json:"accountName"`
Timestamp int64 `json:"timestamp"`
}
TenantDeletedNotification is published when an account is deleted. AccountName carries the last-known name since the account record is gone by the time subscribers receive this.
type TenantUpdatedNotification ¶
type TenantUpdatedNotification struct {
AccountID string `json:"accountId"`
AccountName string `json:"accountName"`
PreviousName string `json:"previousName,omitempty"`
Timestamp int64 `json:"timestamp"`
}
TenantUpdatedNotification is published when an account's mutable fields (name, description, etc.) change. PreviousName is set when a rename occurred so subscribers can map old → new resources.