2020-11-16 13:42:23 +01:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
|
|
import (
|
2021-07-13 14:29:26 +02:00
|
|
|
"net/http"
|
|
|
|
|
|
2022-05-04 14:49:59 +02:00
|
|
|
"github.com/owncloud/ocis/v2/ocis-pkg/log"
|
2022-06-20 16:49:06 +02:00
|
|
|
"github.com/owncloud/ocis/v2/services/proxy/pkg/user/backend"
|
2026-10-09 15:37:34 +02:00
|
|
|
"github.com/owncloud/ocis/v2/services/proxy/pkg/userroles"
|
|
|
|
|
revactx "github.com/owncloud/reva/v2/pkg/ctx"
|
2020-11-16 13:42:23 +01:00
|
|
|
)
|
|
|
|
|
|
2022-08-16 12:47:44 +02:00
|
|
|
// BasicAuthenticator is the authenticator responsible for HTTP Basic authentication.
|
2022-08-04 17:38:55 +02:00
|
|
|
type BasicAuthenticator struct {
|
2026-10-09 15:37:34 +02:00
|
|
|
Logger log.Logger
|
|
|
|
|
UserProvider backend.UserBackend
|
|
|
|
|
UserRoleAssigner userroles.UserRoleAssigner
|
|
|
|
|
MultiInstanceEnabled bool
|
2022-08-04 17:38:55 +02:00
|
|
|
}
|
2020-11-26 13:52:24 +01:00
|
|
|
|
2022-08-22 14:15:13 +02:00
|
|
|
// Authenticate implements the authenticator interface to authenticate requests via basic auth.
|
2026-09-29 09:37:48 +02:00
|
|
|
func (m BasicAuthenticator) Authenticate(r *http.Request) (*http.Request, error) {
|
2024-05-21 09:51:25 +02:00
|
|
|
if isPublicPath(r.URL.Path) && isPublicWithShareToken(r) {
|
2022-08-04 17:38:55 +02:00
|
|
|
// The authentication of public path requests is handled by another authenticator.
|
|
|
|
|
// Since we can't guarantee the order of execution of the authenticators, we better
|
|
|
|
|
// implement an early return here for paths we can't authenticate in this authenticator.
|
2026-09-29 09:37:48 +02:00
|
|
|
return nil, ErrAuthenticationFailed
|
2020-11-18 12:48:31 +01:00
|
|
|
}
|
2020-11-16 13:42:23 +01:00
|
|
|
|
2026-10-09 15:37:34 +02:00
|
|
|
if m.MultiInstanceEnabled {
|
|
|
|
|
// Basic auth credentials carry no OIDC claims, so there is no way to
|
|
|
|
|
// verify tenant membership (OCIS_MULTI_INSTANCE_MEMBER_CLAIM/GUEST_CLAIM)
|
|
|
|
|
// for them. Reject rather than let the request bypass the tenant
|
|
|
|
|
// check in services/proxy/pkg/middleware/account_resolver.go's
|
|
|
|
|
// resolveUserType.
|
|
|
|
|
return nil, ErrAuthenticationFailed
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-08 15:49:22 +02:00
|
|
|
login, password, ok := r.BasicAuth()
|
2022-08-04 17:38:55 +02:00
|
|
|
if !ok {
|
2026-09-29 09:37:48 +02:00
|
|
|
return nil, ErrAuthenticationFailed
|
2020-11-26 10:33:46 +01:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 15:49:22 +02:00
|
|
|
user, _, err := m.UserProvider.Authenticate(r.Context(), login, password)
|
2022-08-04 17:38:55 +02:00
|
|
|
if err != nil {
|
2022-08-08 15:49:22 +02:00
|
|
|
m.Logger.Error().
|
|
|
|
|
Err(err).
|
|
|
|
|
Str("authenticator", "basic").
|
|
|
|
|
Str("path", r.URL.Path).
|
|
|
|
|
Msg("failed to authenticate request")
|
2026-09-29 09:37:48 +02:00
|
|
|
return nil, ErrAuthenticationFailed
|
2020-11-16 13:42:23 +01:00
|
|
|
}
|
2022-02-23 18:46:25 +01:00
|
|
|
|
2026-10-09 15:37:34 +02:00
|
|
|
user, err = m.UserRoleAssigner.ApplyUserRole(r.Context(), user)
|
|
|
|
|
if err != nil {
|
|
|
|
|
m.Logger.Error().
|
|
|
|
|
Err(err).
|
|
|
|
|
Str("authenticator", "basic").
|
|
|
|
|
Str("path", r.URL.Path).
|
|
|
|
|
Msg("could not apply user role")
|
|
|
|
|
return nil, ErrAuthenticationFailed
|
2022-02-23 18:46:25 +01:00
|
|
|
}
|
|
|
|
|
|
2022-08-08 15:49:22 +02:00
|
|
|
m.Logger.Debug().
|
|
|
|
|
Str("authenticator", "basic").
|
|
|
|
|
Str("path", r.URL.Path).
|
|
|
|
|
Msg("successfully authenticated request")
|
2026-10-09 15:37:34 +02:00
|
|
|
// the user is already authenticated, put it into the context directly
|
|
|
|
|
// instead of faking oidc claims for the account resolver to re-resolve.
|
|
|
|
|
return r.WithContext(revactx.ContextSetUser(r.Context(), user)), nil
|
2020-11-26 10:33:46 +01:00
|
|
|
}
|