2020-06-19 11:19:09 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
2020-11-17 17:10:14 +01:00
|
|
|
"time"
|
2020-06-19 11:19:09 +02:00
|
|
|
|
2020-06-19 16:24:42 +02:00
|
|
|
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
|
2022-05-04 14:49:59 +02:00
|
|
|
"github.com/owncloud/ocis/v2/ocis-pkg/log"
|
2023-04-12 12:00:22 +02:00
|
|
|
"github.com/owncloud/ocis/v2/ocis-pkg/oidc"
|
2023-07-31 12:12:56 +02:00
|
|
|
policiessvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/policies/v0"
|
2023-03-22 15:21:57 +01:00
|
|
|
settingssvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/settings/v0"
|
2022-06-20 16:49:06 +02:00
|
|
|
"github.com/owncloud/ocis/v2/services/proxy/pkg/config"
|
2023-03-22 15:21:57 +01:00
|
|
|
"github.com/owncloud/ocis/v2/services/proxy/pkg/user/backend"
|
|
|
|
|
"github.com/owncloud/ocis/v2/services/proxy/pkg/userroles"
|
2025-03-04 16:02:36 +01:00
|
|
|
"github.com/owncloud/reva/v2/pkg/events"
|
|
|
|
|
"github.com/owncloud/reva/v2/pkg/rgrpc/todo/pool"
|
2024-02-26 16:08:03 +01:00
|
|
|
"go-micro.dev/v4/store"
|
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2020-06-19 11:19:09 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Option defines a single option function.
|
|
|
|
|
type Option func(o *Options)
|
|
|
|
|
|
|
|
|
|
// Options defines the available options for this package.
|
|
|
|
|
type Options struct {
|
|
|
|
|
// Logger to use for logging, must be set
|
|
|
|
|
Logger log.Logger
|
2021-07-21 18:34:34 +02:00
|
|
|
// PolicySelectorConfig for using the policy selector
|
|
|
|
|
PolicySelector config.PolicySelector
|
2020-11-16 13:42:23 +01:00
|
|
|
// HTTPClient to use for communication with the oidcAuth provider
|
2020-06-19 11:19:09 +02:00
|
|
|
HTTPClient *http.Client
|
2023-03-15 17:20:14 +01:00
|
|
|
// UserProvider backend to use for resolving User
|
2020-12-04 01:56:54 +01:00
|
|
|
UserProvider backend.UserBackend
|
2023-03-15 17:20:14 +01:00
|
|
|
// UserRoleAssigner to user for assign a users default role
|
|
|
|
|
UserRoleAssigner userroles.UserRoleAssigner
|
2020-08-27 15:45:44 +02:00
|
|
|
// SettingsRoleService for the roles API in settings
|
2022-01-12 15:10:15 +01:00
|
|
|
SettingsRoleService settingssvc.RoleService
|
2023-07-31 12:12:56 +02:00
|
|
|
// PoliciesProviderService for policy evaluation
|
|
|
|
|
PoliciesProviderService policiessvc.PoliciesProviderService
|
2024-02-01 10:07:44 +01:00
|
|
|
// OIDCClient to fetch user info and verify tokens, must be set for the oidc_auth middleware
|
2023-04-13 17:29:57 +02:00
|
|
|
OIDCClient oidc.OIDCClient
|
2020-11-16 13:42:23 +01:00
|
|
|
// OIDCIss is the oidcAuth-issuer
|
2020-07-31 22:57:19 +02:00
|
|
|
OIDCIss string
|
2023-06-08 12:41:04 +02:00
|
|
|
// RevaGatewaySelector to send requests to the reva gateway
|
|
|
|
|
RevaGatewaySelector pool.Selectable[gateway.GatewayAPIClient]
|
2020-08-20 10:54:26 +02:00
|
|
|
// PreSignedURLConfig to configure the middleware
|
|
|
|
|
PreSignedURLConfig config.PreSignedURL
|
2021-07-21 15:37:12 +00:00
|
|
|
// UserOIDCClaim to read from the oidc claims
|
2021-09-21 15:33:58 +02:00
|
|
|
UserOIDCClaim string
|
2021-07-21 15:37:12 +00:00
|
|
|
// UserCS3Claim to use when looking up a user in the CS3 API
|
2021-09-21 15:33:58 +02:00
|
|
|
UserCS3Claim string
|
2020-11-16 13:42:23 +01:00
|
|
|
// AutoprovisionAccounts when an accountResolver does not exist.
|
2020-10-05 11:25:02 +02:00
|
|
|
AutoprovisionAccounts bool
|
2020-09-29 13:30:55 +02:00
|
|
|
// EnableBasicAuth to allow basic auth
|
|
|
|
|
EnableBasicAuth bool
|
2025-03-12 08:55:22 +01:00
|
|
|
// AllowAppAuth specifies whether authentication using application tokens is permitted.
|
|
|
|
|
AllowAppAuth bool
|
2023-03-22 15:21:57 +01:00
|
|
|
// DefaultAccessTokenTTL is used to calculate the expiration when an access token has no expiration set
|
|
|
|
|
DefaultAccessTokenTTL time.Duration
|
2023-04-12 15:40:18 +02:00
|
|
|
// UserInfoCache sets the access token cache store
|
|
|
|
|
UserInfoCache store.Store
|
2026-05-15 16:53:05 +02:00
|
|
|
// MFAStore is used to persist verified MFA status so that non-OIDC
|
|
|
|
|
// requests (e.g. signed-URL archiver downloads) can inherit the status
|
|
|
|
|
// from the user's most recent OIDC-authenticated session.
|
|
|
|
|
MFAStore store.Store
|
2020-12-02 15:31:17 +01:00
|
|
|
// CredentialsByUserAgent sets the auth challenges on a per user-agent basis
|
|
|
|
|
CredentialsByUserAgent map[string]string
|
2022-07-20 17:45:43 +02:00
|
|
|
// AccessTokenVerifyMethod configures how access_tokens should be verified but the oidc_auth middleware.
|
|
|
|
|
// Possible values currently: "jwt" and "none"
|
|
|
|
|
AccessTokenVerifyMethod string
|
2022-07-22 15:01:25 +02:00
|
|
|
// JWKS sets the options for fetching the JWKS from the IDP
|
|
|
|
|
JWKS config.JWKS
|
2023-02-21 15:26:21 +01:00
|
|
|
// RoleQuotas hold userid:quota mappings. These will be used when provisioning new users.
|
|
|
|
|
// The users will get as much quota as is set for their role.
|
|
|
|
|
RoleQuotas map[string]uint64
|
2026-05-15 16:53:05 +02:00
|
|
|
// EnableVaultMode creates a new vault home for the user if it does not exist.
|
|
|
|
|
EnableVaultMode bool
|
2023-06-23 14:20:26 +02:00
|
|
|
// TraceProvider sets the tracing provider.
|
|
|
|
|
TraceProvider trace.TracerProvider
|
2023-08-23 10:36:05 +02:00
|
|
|
// SkipUserInfo prevents the oidc middleware from querying the userinfo endpoint and read any claims directly from the access token instead
|
2024-08-29 10:17:07 +02:00
|
|
|
SkipUserInfo bool
|
|
|
|
|
EventsPublisher events.Publisher
|
2025-04-30 14:27:50 +02:00
|
|
|
// Service Accounts
|
|
|
|
|
ServiceAccountID string
|
|
|
|
|
ServiceAccountSecret string
|
2025-11-26 14:10:23 +01:00
|
|
|
// Multi-Instance Options
|
|
|
|
|
MultiInstanceEnabled bool
|
|
|
|
|
InstanceID string
|
2026-05-06 16:15:32 +02:00
|
|
|
MasterID string
|
2025-11-26 14:10:23 +01:00
|
|
|
MemberClaim string
|
|
|
|
|
GuestClaim string
|
|
|
|
|
GuestRoleName string
|
2020-06-19 11:19:09 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-17 23:11:29 +02:00
|
|
|
// newOptions initializes the available default options.
|
2020-06-19 11:19:09 +02:00
|
|
|
func newOptions(opts ...Option) Options {
|
|
|
|
|
opt := Options{}
|
|
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
|
o(&opt)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return opt
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Logger provides a function to set the logger option.
|
|
|
|
|
func Logger(l log.Logger) Option {
|
|
|
|
|
return func(o *Options) {
|
|
|
|
|
o.Logger = l
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-21 18:34:34 +02:00
|
|
|
// PolicySelectorConfig provides a function to set the policy selector config option.
|
|
|
|
|
func PolicySelectorConfig(cfg config.PolicySelector) Option {
|
|
|
|
|
return func(o *Options) {
|
|
|
|
|
o.PolicySelector = cfg
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-19 11:19:09 +02:00
|
|
|
// HTTPClient provides a function to set the http client config option.
|
|
|
|
|
func HTTPClient(c *http.Client) Option {
|
|
|
|
|
return func(o *Options) {
|
|
|
|
|
o.HTTPClient = c
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-27 21:29:30 +02:00
|
|
|
// SettingsRoleService provides a function to set the role service option.
|
2022-01-12 15:10:15 +01:00
|
|
|
func SettingsRoleService(rc settingssvc.RoleService) Option {
|
2020-08-27 15:45:44 +02:00
|
|
|
return func(o *Options) {
|
|
|
|
|
o.SettingsRoleService = rc
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-31 12:12:56 +02:00
|
|
|
// PoliciesProviderService provides a function to set the policies provider option.
|
|
|
|
|
func PoliciesProviderService(pps policiessvc.PoliciesProviderService) Option {
|
|
|
|
|
return func(o *Options) {
|
|
|
|
|
o.PoliciesProviderService = pps
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// OIDCClient provides a function to set the oidc client option.
|
2023-04-13 17:29:57 +02:00
|
|
|
func OIDCClient(val oidc.OIDCClient) Option {
|
2020-06-19 11:19:09 +02:00
|
|
|
return func(o *Options) {
|
2023-04-12 12:00:22 +02:00
|
|
|
o.OIDCClient = val
|
2020-06-19 11:19:09 +02:00
|
|
|
}
|
|
|
|
|
}
|
2020-06-19 16:24:42 +02:00
|
|
|
|
2020-11-16 13:42:23 +01:00
|
|
|
// OIDCIss sets the oidcAuth issuer url
|
2020-07-31 22:57:19 +02:00
|
|
|
func OIDCIss(iss string) Option {
|
|
|
|
|
return func(o *Options) {
|
|
|
|
|
o.OIDCIss = iss
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-02 15:31:17 +01:00
|
|
|
// CredentialsByUserAgent sets UserAgentChallenges.
|
|
|
|
|
func CredentialsByUserAgent(v map[string]string) Option {
|
|
|
|
|
return func(o *Options) {
|
|
|
|
|
o.CredentialsByUserAgent = v
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-31 12:12:56 +02:00
|
|
|
// WithRevaGatewaySelector provides a function to set the reva gateway service selector option.
|
2023-06-08 12:41:04 +02:00
|
|
|
func WithRevaGatewaySelector(val pool.Selectable[gateway.GatewayAPIClient]) Option {
|
2020-06-19 16:24:42 +02:00
|
|
|
return func(o *Options) {
|
2023-06-08 12:41:04 +02:00
|
|
|
o.RevaGatewaySelector = val
|
2020-07-17 23:11:29 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-20 10:54:26 +02:00
|
|
|
// PreSignedURLConfig provides a function to set the PreSignedURL config
|
|
|
|
|
func PreSignedURLConfig(cfg config.PreSignedURL) Option {
|
|
|
|
|
return func(o *Options) {
|
|
|
|
|
o.PreSignedURLConfig = cfg
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-05 11:25:02 +02:00
|
|
|
|
2021-07-21 15:37:12 +00:00
|
|
|
// UserOIDCClaim provides a function to set the UserClaim config
|
|
|
|
|
func UserOIDCClaim(val string) Option {
|
|
|
|
|
return func(o *Options) {
|
|
|
|
|
o.UserOIDCClaim = val
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UserCS3Claim provides a function to set the UserClaimType config
|
|
|
|
|
func UserCS3Claim(val string) Option {
|
|
|
|
|
return func(o *Options) {
|
|
|
|
|
o.UserCS3Claim = val
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-05 11:25:02 +02:00
|
|
|
// AutoprovisionAccounts provides a function to set the AutoprovisionAccounts config
|
|
|
|
|
func AutoprovisionAccounts(val bool) Option {
|
|
|
|
|
return func(o *Options) {
|
|
|
|
|
o.AutoprovisionAccounts = val
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-29 13:30:55 +02:00
|
|
|
|
|
|
|
|
// EnableBasicAuth provides a function to set the EnableBasicAuth config
|
|
|
|
|
func EnableBasicAuth(enableBasicAuth bool) Option {
|
|
|
|
|
return func(o *Options) {
|
|
|
|
|
o.EnableBasicAuth = enableBasicAuth
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-17 17:10:14 +01:00
|
|
|
|
2025-03-12 08:55:22 +01:00
|
|
|
// AllowAppAuth provides a function to set the AllowAppAuth config
|
|
|
|
|
func AllowAppAuth(allowAppAuth bool) Option {
|
|
|
|
|
return func(o *Options) {
|
|
|
|
|
o.AllowAppAuth = allowAppAuth
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-22 15:21:57 +01:00
|
|
|
// DefaultAccessTokenTTL provides a function to set the DefaultAccessTokenTTL
|
|
|
|
|
func DefaultAccessTokenTTL(ttl time.Duration) Option {
|
2020-11-17 17:10:14 +01:00
|
|
|
return func(o *Options) {
|
2023-03-22 15:21:57 +01:00
|
|
|
o.DefaultAccessTokenTTL = ttl
|
2020-11-17 17:10:14 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 15:40:18 +02:00
|
|
|
// UserInfoCache provides a function to set the UserInfoCache
|
|
|
|
|
func UserInfoCache(val store.Store) Option {
|
2020-11-17 17:10:14 +01:00
|
|
|
return func(o *Options) {
|
2023-04-12 15:40:18 +02:00
|
|
|
o.UserInfoCache = val
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-15 16:53:05 +02:00
|
|
|
// MFAStore provides a function to set the MFA session store.
|
|
|
|
|
func MFAStore(val store.Store) Option {
|
|
|
|
|
return func(o *Options) {
|
|
|
|
|
o.MFAStore = val
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-04 01:56:54 +01:00
|
|
|
// UserProvider sets the accounts user provider
|
|
|
|
|
func UserProvider(up backend.UserBackend) Option {
|
|
|
|
|
return func(o *Options) {
|
|
|
|
|
o.UserProvider = up
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-20 17:45:43 +02:00
|
|
|
|
2023-03-15 17:20:14 +01:00
|
|
|
// UserRoleAssigner sets the mechanism for assigning the default user roles
|
|
|
|
|
func UserRoleAssigner(ra userroles.UserRoleAssigner) Option {
|
|
|
|
|
return func(o *Options) {
|
|
|
|
|
o.UserRoleAssigner = ra
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-20 17:45:43 +02:00
|
|
|
// AccessTokenVerifyMethod set the mechanism for access token verification
|
|
|
|
|
func AccessTokenVerifyMethod(method string) Option {
|
|
|
|
|
return func(o *Options) {
|
|
|
|
|
o.AccessTokenVerifyMethod = method
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-22 15:01:25 +02:00
|
|
|
|
2023-02-21 15:26:21 +01:00
|
|
|
// RoleQuotas sets the role quota mapping setting
|
|
|
|
|
func RoleQuotas(roleQuotas map[string]uint64) Option {
|
|
|
|
|
return func(o *Options) {
|
|
|
|
|
o.RoleQuotas = roleQuotas
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-23 14:20:26 +02:00
|
|
|
|
2026-05-15 16:53:05 +02:00
|
|
|
// EnableVaultMode sets the enable vault mode flag
|
|
|
|
|
func EnableVaultMode(enableVaultMode bool) Option {
|
|
|
|
|
return func(o *Options) {
|
|
|
|
|
o.EnableVaultMode = enableVaultMode
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-23 14:20:26 +02:00
|
|
|
// TraceProvider sets the tracing provider.
|
|
|
|
|
func TraceProvider(tp trace.TracerProvider) Option {
|
|
|
|
|
return func(o *Options) {
|
|
|
|
|
o.TraceProvider = tp
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-23 10:36:05 +02:00
|
|
|
|
|
|
|
|
// SkipUserInfo sets the skipUserInfo flag.
|
|
|
|
|
func SkipUserInfo(val bool) Option {
|
|
|
|
|
return func(o *Options) {
|
|
|
|
|
o.SkipUserInfo = val
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-29 10:17:07 +02:00
|
|
|
|
|
|
|
|
// EventsPublisher sets the events publisher.
|
|
|
|
|
func EventsPublisher(ep events.Publisher) Option {
|
|
|
|
|
return func(o *Options) {
|
|
|
|
|
o.EventsPublisher = ep
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-30 14:27:50 +02:00
|
|
|
|
|
|
|
|
// ServiceAccount sets the service account user
|
|
|
|
|
func ServiceAccount(id string, secret string) Option {
|
|
|
|
|
return func(o *Options) {
|
|
|
|
|
o.ServiceAccountID = id
|
|
|
|
|
o.ServiceAccountSecret = secret
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-26 14:10:23 +01:00
|
|
|
|
|
|
|
|
// MultiInstance sets muli-instance options
|
2026-05-06 16:15:32 +02:00
|
|
|
func MultiInstance(enabled bool, iid string, masterID string, memberClaim, guestClaim string, guestRole string) Option {
|
2025-11-26 14:10:23 +01:00
|
|
|
return func(o *Options) {
|
|
|
|
|
o.MultiInstanceEnabled = enabled
|
|
|
|
|
o.InstanceID = iid
|
2026-05-06 16:15:32 +02:00
|
|
|
o.MasterID = masterID
|
2025-11-26 14:10:23 +01:00
|
|
|
o.MemberClaim = memberClaim
|
|
|
|
|
o.GuestClaim = guestClaim
|
|
|
|
|
o.GuestRoleName = guestRole
|
|
|
|
|
}
|
|
|
|
|
}
|