Files

77 lines
2.7 KiB
Go
Raw Permalink Normal View History

2024-05-06 12:48:49 +02:00
package middleware
import (
"net/http"
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
cs3rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
2024-05-06 12:48:49 +02:00
"github.com/owncloud/ocis/v2/ocis-pkg/log"
"github.com/owncloud/ocis/v2/services/proxy/pkg/userroles"
2025-03-04 16:02:36 +01:00
revactx "github.com/owncloud/reva/v2/pkg/ctx"
"github.com/owncloud/reva/v2/pkg/rgrpc/todo/pool"
2024-05-06 12:48:49 +02:00
)
// AppAuthAuthenticator defines the app auth authenticator
type AppAuthAuthenticator struct {
Logger log.Logger
RevaGatewaySelector pool.Selectable[gateway.GatewayAPIClient]
UserRoleAssigner userroles.UserRoleAssigner
MultiInstanceEnabled bool
2024-05-06 12:48:49 +02:00
}
// Authenticate implements the authenticator interface to authenticate requests via app auth.
func (m AppAuthAuthenticator) Authenticate(r *http.Request) (*http.Request, error) {
2024-05-06 12:48:49 +02:00
if isPublicPath(r.URL.Path) {
// 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.
return nil, ErrAuthenticationFailed
2024-05-06 12:48:49 +02:00
}
if m.MultiInstanceEnabled {
// App passwords 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
}
2024-05-06 12:48:49 +02:00
username, password, ok := r.BasicAuth()
if !ok {
return nil, ErrAuthenticationFailed
2024-05-06 12:48:49 +02:00
}
next, err := m.RevaGatewaySelector.Next()
if err != nil {
return nil, ErrAuthenticationFailed
2024-05-06 12:48:49 +02:00
}
2024-05-06 12:48:49 +02:00
authenticateResponse, err := next.Authenticate(r.Context(), &gateway.AuthenticateRequest{
Type: "appauth",
ClientId: username,
ClientSecret: password,
})
if err != nil {
return nil, ErrAuthenticationFailed
2024-05-06 12:48:49 +02:00
}
if authenticateResponse.GetStatus().GetCode() != cs3rpc.Code_CODE_OK {
// TODO: log???
return nil, ErrAuthenticationFailed
}
user, err := m.UserRoleAssigner.ApplyUserRole(r.Context(), authenticateResponse.GetUser())
if err != nil {
m.Logger.Error().Err(err).Msg("could not apply user role")
return nil, ErrAuthenticationFailed
}
// only mutate the request once we know we're returning success, so a
// failed authentication attempt can't leave a stale access token behind
// for a later authenticator in the chain to build on.
r.Header.Set(revactx.TokenHeader, authenticateResponse.GetToken())
// the user is already authenticated, put it into the context directly
// instead of faking oidc claims for the account resolver to re-resolve.
r = r.WithContext(revactx.ContextSetUser(r.Context(), user))
return r, nil
2024-05-06 12:48:49 +02:00
}