2025-09-11 16:09:11 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
|
|
import (
|
2026-10-12 10:28:49 +02:00
|
|
|
"context"
|
2025-09-11 16:09:11 +02:00
|
|
|
"net/http"
|
2026-10-12 10:28:49 +02:00
|
|
|
"strconv"
|
2026-06-19 10:43:41 +02:00
|
|
|
"strings"
|
2026-05-15 16:53:05 +02:00
|
|
|
"time"
|
|
|
|
|
|
2026-06-19 10:43:41 +02:00
|
|
|
"github.com/owncloud/reva/v2/pkg/autoprop"
|
2026-05-15 16:53:05 +02:00
|
|
|
revactx "github.com/owncloud/reva/v2/pkg/ctx"
|
|
|
|
|
microstore "go-micro.dev/v4/store"
|
2025-09-11 16:09:11 +02:00
|
|
|
|
|
|
|
|
"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"
|
|
|
|
|
)
|
|
|
|
|
|
2026-05-21 15:32:26 +02:00
|
|
|
const defaultMFASessionDuration = 3600
|
2026-05-15 16:53:05 +02:00
|
|
|
|
2026-10-12 10:28:49 +02:00
|
|
|
// mfaSessionDuration returns the configured MFA session duration or the default.
|
|
|
|
|
func mfaSessionDuration(cfg config.MFAConfig) time.Duration {
|
|
|
|
|
if cfg.SessionDuration <= 0 {
|
|
|
|
|
return defaultMFASessionDuration * time.Second
|
|
|
|
|
}
|
|
|
|
|
return time.Duration(cfg.SessionDuration) * time.Second
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MultiFactor returns a middleware that checks requests for mfa.
|
|
|
|
|
//
|
|
|
|
|
// It must run after the AccountResolver, which adds the user to the context.
|
|
|
|
|
// The user is needed to store and to read the MFA status.
|
2025-09-11 16:09:11 +02:00
|
|
|
func MultiFactor(cfg config.MFAConfig, opts ...Option) func(next http.Handler) http.Handler {
|
|
|
|
|
options := newOptions(opts...)
|
|
|
|
|
logger := options.Logger
|
|
|
|
|
|
|
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
|
return &MultiFactorAuthentication{
|
2026-05-21 15:32:26 +02:00
|
|
|
next: next,
|
|
|
|
|
logger: logger,
|
|
|
|
|
enabled: cfg.Enabled,
|
|
|
|
|
authLevelNames: cfg.AuthLevelNames,
|
|
|
|
|
store: options.MFAStore,
|
2026-10-12 10:28:49 +02:00
|
|
|
sessionDuration: mfaSessionDuration(cfg),
|
2025-09-11 16:09:11 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MultiFactorAuthentication is a authenticator that checks for mfa on specific paths
|
|
|
|
|
type MultiFactorAuthentication struct {
|
2026-05-21 15:32:26 +02:00
|
|
|
next http.Handler
|
|
|
|
|
logger log.Logger
|
|
|
|
|
enabled bool
|
|
|
|
|
authLevelNames []string
|
|
|
|
|
sessionDuration time.Duration
|
2026-10-12 10:28:49 +02:00
|
|
|
// store holds the time of the last request with MFA of a user, so that requests
|
|
|
|
|
// without claims (e.g. signed-URL downloads) can inherit the MFA status. Nil when
|
|
|
|
|
// no store is configured.
|
2026-05-15 16:53:05 +02:00
|
|
|
store microstore.Store
|
2025-09-11 16:09:11 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-17 12:03:10 +02:00
|
|
|
// ServeHTTP adds the mfa header if the request contains a valid mfa token
|
2025-09-16 12:39:04 +02:00
|
|
|
func (m MultiFactorAuthentication) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
2026-06-19 10:43:41 +02:00
|
|
|
// 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)
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-09-16 12:39:04 +02:00
|
|
|
|
|
|
|
|
if !m.enabled {
|
2025-09-18 16:17:04 +02:00
|
|
|
// 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.
|
2026-06-19 10:43:41 +02:00
|
|
|
req = req.WithContext(revactx.SetMFA(req.Context()))
|
|
|
|
|
m.next.ServeHTTP(w, req)
|
2025-09-11 16:09:11 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-16 12:39:04 +02:00
|
|
|
// overwrite the mfa header to avoid passing on wrong information
|
2026-06-19 10:43:41 +02:00
|
|
|
ctx := revactx.RemoveMFA(req.Context())
|
2025-09-11 16:09:11 +02:00
|
|
|
|
2026-06-19 10:43:41 +02:00
|
|
|
claims := oidc.FromContext(ctx)
|
2025-09-11 16:09:11 +02:00
|
|
|
|
2026-05-15 16:53:05 +02:00
|
|
|
if claims == nil {
|
2026-10-12 10:28:49 +02:00
|
|
|
// No OIDC claims, e.g. the request has a signed URL. MFA cannot be determined
|
2026-05-15 16:53:05 +02:00
|
|
|
// from claims directly.
|
|
|
|
|
//
|
2026-10-12 10:28:49 +02:00
|
|
|
// Fall back to the stored MFA status from the most recent request with MFA
|
|
|
|
|
// of the user. This allows, for example, a signed-URL archiver download to
|
|
|
|
|
// succeed when the user has recently proven MFA in their browser session.
|
|
|
|
|
if m.readMFAFromStore(ctx) {
|
|
|
|
|
ctx = revactx.SetMFA(ctx)
|
2026-05-15 16:53:05 +02:00
|
|
|
}
|
2026-06-19 10:43:41 +02:00
|
|
|
|
|
|
|
|
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
|
2026-05-15 16:53:05 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-16 12:39:04 +02:00
|
|
|
// acr is a standard OIDC claim.
|
|
|
|
|
value, err := oidc.ReadStringClaim("acr", claims)
|
|
|
|
|
if err != nil {
|
2026-05-15 16:53:05 +02:00
|
|
|
m.logger.Debug().Str("path", req.URL.Path).Interface("required", m.authLevelNames).Err(err).Msg("acr claim not set in access token")
|
2026-06-19 10:43:41 +02:00
|
|
|
} else if !m.containsMFA(value) {
|
2025-09-16 12:39:04 +02:00
|
|
|
m.logger.Debug().Str("acr", value).Str("url", req.URL.Path).Msg("accessing path without mfa")
|
2026-06-19 10:43:41 +02:00
|
|
|
} else {
|
|
|
|
|
m.logger.Debug().Str("acr", value).Str("url", req.URL.Path).Msg("mfa authenticated")
|
|
|
|
|
ctx = revactx.SetMFA(ctx)
|
2026-10-12 10:28:49 +02:00
|
|
|
m.writeMFAToStore(ctx)
|
2026-05-15 16:53:05 +02:00
|
|
|
}
|
2026-06-19 10:43:41 +02:00
|
|
|
|
|
|
|
|
// 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))
|
2026-05-15 16:53:05 +02:00
|
|
|
}
|
|
|
|
|
|
2026-10-12 10:28:49 +02:00
|
|
|
// readMFAFromStore checks if the user had MFA in an OIDC request within the session duration.
|
|
|
|
|
// The age is checked here because stores like nats-js-kv ignore the expiry of a record.
|
|
|
|
|
func (m MultiFactorAuthentication) readMFAFromStore(ctx context.Context) bool {
|
|
|
|
|
userID := mfaUserID(ctx)
|
|
|
|
|
if m.store == nil || userID == "" {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2026-05-15 16:53:05 +02:00
|
|
|
records, err := m.store.Read(key(userID))
|
|
|
|
|
if err != nil || len(records) == 0 {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2026-10-12 10:28:49 +02:00
|
|
|
verifiedAt, err := strconv.ParseInt(string(records[0].Value), 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return time.Since(time.Unix(verifiedAt, 0)) < m.sessionDuration
|
2026-05-15 16:53:05 +02:00
|
|
|
}
|
|
|
|
|
|
2026-10-12 10:28:49 +02:00
|
|
|
// writeMFAToStore stores the time of the last request with MFA of the user, so that requests
|
|
|
|
|
// without claims (e.g. signed-URL downloads) can inherit the MFA status, see readMFAFromStore.
|
|
|
|
|
func (m MultiFactorAuthentication) writeMFAToStore(ctx context.Context) {
|
|
|
|
|
userID := mfaUserID(ctx)
|
|
|
|
|
if m.store == nil || userID == "" {
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-05-15 16:53:05 +02:00
|
|
|
if err := m.store.Write(µstore.Record{
|
2026-10-12 10:28:49 +02:00
|
|
|
Key: key(userID),
|
|
|
|
|
Value: []byte(strconv.FormatInt(time.Now().Unix(), 10)),
|
|
|
|
|
// redis honors the expiry, nats-js-kv uses the TTL of the bucket instead
|
2026-05-21 15:32:26 +02:00
|
|
|
Expiry: m.sessionDuration,
|
2026-05-15 16:53:05 +02:00
|
|
|
}); err != nil {
|
|
|
|
|
m.logger.Error().Err(err).Str("userID", userID).Msg("failed to write MFA status to store")
|
|
|
|
|
}
|
2025-09-11 16:09:11 +02:00
|
|
|
}
|
2025-09-17 12:03:10 +02:00
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
}
|
2026-05-15 16:53:05 +02:00
|
|
|
|
2026-10-12 10:28:49 +02:00
|
|
|
// mfaUserID returns the id of the user in the context, or "" if there is no user.
|
|
|
|
|
func mfaUserID(ctx context.Context) string {
|
|
|
|
|
u, _ := revactx.ContextGetUser(ctx)
|
|
|
|
|
return u.GetId().GetOpaqueId()
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-15 16:53:05 +02:00
|
|
|
func key(userID string) string {
|
|
|
|
|
return "mfa:" + userID
|
|
|
|
|
}
|