2023-09-20 16:55:49 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GetCookieDomainFunc func(r *http.Request) (string, error)
|
|
|
|
|
|
|
|
func defaultGetCookieDomain(r *http.Request) (string, error) {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2024-05-02 09:42:56 +02:00
|
|
|
type DefaultUserOptions struct {
|
|
|
|
GetCookieDomain GetCookieDomainFunc
|
|
|
|
CookieDuration time.Duration
|
|
|
|
Tenant string
|
|
|
|
Entrypoint string
|
|
|
|
Role string
|
|
|
|
Issuer string
|
|
|
|
GetPreferredUsername func(r *http.Request) (string, error)
|
|
|
|
GetSubject func(r *http.Request) (string, error)
|
2023-09-20 16:55:49 +02:00
|
|
|
}
|
|
|
|
|
2024-05-02 09:42:56 +02:00
|
|
|
type DefaultUserOptionFunc func(opts *DefaultUserOptions)
|
|
|
|
|
|
|
|
func defaultUserOptions() *DefaultUserOptions {
|
|
|
|
return &DefaultUserOptions{
|
|
|
|
GetCookieDomain: defaultGetCookieDomain,
|
|
|
|
CookieDuration: 24 * time.Hour,
|
|
|
|
Tenant: "",
|
|
|
|
Entrypoint: "",
|
|
|
|
Role: "",
|
|
|
|
GetSubject: getAnonymousSubject,
|
|
|
|
GetPreferredUsername: getAnonymousPreferredUsername,
|
2023-09-20 16:55:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-02 09:42:56 +02:00
|
|
|
func WithCookieOptions(getCookieDomain GetCookieDomainFunc, duration time.Duration) DefaultUserOptionFunc {
|
|
|
|
return func(opts *DefaultUserOptions) {
|
2023-09-20 16:55:49 +02:00
|
|
|
opts.GetCookieDomain = getCookieDomain
|
|
|
|
opts.CookieDuration = duration
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-02 09:42:56 +02:00
|
|
|
func WithTenant(tenant string) DefaultUserOptionFunc {
|
|
|
|
return func(opts *DefaultUserOptions) {
|
2023-09-20 16:55:49 +02:00
|
|
|
opts.Tenant = tenant
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-02 09:42:56 +02:00
|
|
|
func WithEntrypoint(entrypoint string) DefaultUserOptionFunc {
|
|
|
|
return func(opts *DefaultUserOptions) {
|
2023-09-20 16:55:49 +02:00
|
|
|
opts.Entrypoint = entrypoint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-02 09:42:56 +02:00
|
|
|
func WithRole(role string) DefaultUserOptionFunc {
|
|
|
|
return func(opts *DefaultUserOptions) {
|
2023-09-20 16:55:49 +02:00
|
|
|
opts.Role = role
|
|
|
|
}
|
|
|
|
}
|