Files
Juan Pablo Villafañez 4945eb6833 Autoprop (#12386)
* feat: metadata autopropagation

* feat: autoprop support for MFA / vault mode

* fix: replace some http clients with reva ones

* chore: required lib update

* fix: adjust unit tests and generate new mocks

* fix: apply autopropagation to all regular graph routes

* chore: updated reva dependency

* fix: return the right decorator for the thumbnails

* fix: remove autopropagation headers in the proxy
2026-06-19 10:43:41 +02:00

155 lines
5.1 KiB
Go

package middleware
import (
"net/http"
"strings"
"time"
"github.com/owncloud/reva/v2/pkg/autoprop"
revactx "github.com/owncloud/reva/v2/pkg/ctx"
microstore "go-micro.dev/v4/store"
"github.com/owncloud/ocis/v2/ocis-pkg/log"
"github.com/owncloud/ocis/v2/ocis-pkg/oidc"
"github.com/owncloud/ocis/v2/services/proxy/pkg/config"
)
const defaultMFASessionDuration = 3600
// MultiFactor returns a middleware that checks requests for mfa
func MultiFactor(cfg config.MFAConfig, opts ...Option) func(next http.Handler) http.Handler {
options := newOptions(opts...)
logger := options.Logger
sessionDuration := cfg.SessionDuration
if sessionDuration <= 0 {
sessionDuration = defaultMFASessionDuration
}
return func(next http.Handler) http.Handler {
return &MultiFactorAuthentication{
next: next,
logger: logger,
enabled: cfg.Enabled,
authLevelNames: cfg.AuthLevelNames,
store: options.MFAStore,
sessionDuration: time.Duration(sessionDuration) * time.Second,
}
}
}
// MultiFactorAuthentication is a authenticator that checks for mfa on specific paths
type MultiFactorAuthentication struct {
next http.Handler
logger log.Logger
enabled bool
authLevelNames []string
sessionDuration time.Duration
// store persists verified MFA status so that non-OIDC requests (e.g.
// signed-URL archiver downloads) can inherit it from the user's most
// recent OIDC session. Nil when no store is configured.
store microstore.Store
}
// ServeHTTP adds the mfa header if the request contains a valid mfa token
func (m MultiFactorAuthentication) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// Incoming requests must not have autopropagation headers
for key, _ := range req.Header {
if strings.HasPrefix(key, autoprop.HTTPAutoPropPrefix) || strings.HasPrefix(key, autoprop.MicroAutoPropPrefix) {
req.Header.Del(key)
}
}
if !m.enabled {
// if mfa is disabled we always set the header to true.
// this allows all other services to assume mfa is always active.
// this should reduce code and configuration complexity in other services.
req = req.WithContext(revactx.SetMFA(req.Context()))
m.next.ServeHTTP(w, req)
return
}
// overwrite the mfa header to avoid passing on wrong information
ctx := revactx.RemoveMFA(req.Context())
claims := oidc.FromContext(ctx)
if claims == nil {
// No OIDC claims — request was authenticated via a non-OIDC method
// (e.g. signed URL, basic auth, app token). MFA cannot be determined
// from claims directly.
//
// Fall back to the persisted MFA status from the user's most recent
// OIDC-authenticated session. This allows, for example, a signed-URL
// archiver download to succeed when the user has recently proven MFA
// in their browser session.
if m.store != nil {
if u, ok := revactx.ContextGetUser(ctx); ok && u.GetId().GetOpaqueId() != "" {
if m.readMFAFromStore(u.GetId().GetOpaqueId()) {
ctx = revactx.SetMFA(ctx)
}
}
}
m.logger.Debug().Str("path", req.URL.Path).Bool("mfaStatus", revactx.HasMFA(ctx)).Msg("no OIDC claims in context")
m.next.ServeHTTP(w, req.WithContext(ctx)) // ensure the request has the right context
return
}
// acr is a standard OIDC claim.
value, err := oidc.ReadStringClaim("acr", claims)
if err != nil {
m.logger.Debug().Str("path", req.URL.Path).Interface("required", m.authLevelNames).Err(err).Msg("acr claim not set in access token")
} else if !m.containsMFA(value) {
m.logger.Debug().Str("acr", value).Str("url", req.URL.Path).Msg("accessing path without mfa")
} else {
m.logger.Debug().Str("acr", value).Str("url", req.URL.Path).Msg("mfa authenticated")
ctx = revactx.SetMFA(ctx)
// Persist the verified MFA status so that subsequent non-OIDC requests
// (e.g. signed-URL archiver downloads) can inherit it. The entry is
// refreshed on every successful OIDC MFA verification and expires after
// the configured session duration if no further OIDC requests are made.
if m.store != nil {
if u, ok := revactx.ContextGetUser(ctx); ok && u.GetId().GetOpaqueId() != "" {
m.writeMFAToStore(u.GetId().GetOpaqueId())
}
}
}
// MFA status will only be true if the acr claim contains the proper value,
// otherwise it wll be false (removed from the context early)
m.next.ServeHTTP(w, req.WithContext(ctx))
}
func (m MultiFactorAuthentication) readMFAFromStore(userID string) bool {
records, err := m.store.Read(key(userID))
if err != nil || len(records) == 0 {
return false
}
return string(records[0].Value) == "true"
}
func (m MultiFactorAuthentication) writeMFAToStore(userID string) {
if err := m.store.Write(&microstore.Record{
Key: key(userID),
Value: []byte("true"),
Expiry: m.sessionDuration,
}); err != nil {
m.logger.Error().Err(err).Str("userID", userID).Msg("failed to write MFA status to store")
}
}
// containsMFA checks if the given value is in the list of authentication level names
func (m MultiFactorAuthentication) containsMFA(value string) bool {
for _, v := range m.authLevelNames {
if v == value {
return true
}
}
return false
}
func key(userID string) string {
return "mfa:" + userID
}