Files
ocis/services/proxy/pkg/middleware/app_auth.go
Roman PerekhodandClaude Sonnet 5 2e377bde67 fix: [OCISDEV-1476] fix AuthApp requests failing with a custom PROXY_… (#13078) (#13107)
* fix: [OCISDEV-1476] fix AuthApp requests failing with a custom PROXY_USER_OIDC_CLAIM

* fix: [OCISDEV-1476] map ErrAccountNotFound to 401 in verifyUser

AccountResolver.verifyUser only mapped ErrAccountDisabled to 401 before
falling through to a generic 500. The identity backend excludes disabled
accounts from by-username lookups and returns ErrAccountNotFound instead,
so a disabled user authenticating via AppAuth or basic auth got a 500
instead of a 401, caught by the acceptance test suite.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-09 13:37:34 +00:00

78 lines
2.7 KiB
Go

package middleware
import (
"net/http"
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
cs3rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
"github.com/owncloud/ocis/v2/ocis-pkg/log"
"github.com/owncloud/ocis/v2/services/proxy/pkg/userroles"
revactx "github.com/owncloud/reva/v2/pkg/ctx"
"github.com/owncloud/reva/v2/pkg/rgrpc/todo/pool"
)
// AppAuthAuthenticator defines the app auth authenticator
type AppAuthAuthenticator struct {
Logger log.Logger
RevaGatewaySelector pool.Selectable[gateway.GatewayAPIClient]
UserRoleAssigner userroles.UserRoleAssigner
MultiInstanceEnabled bool
}
// Authenticate implements the authenticator interface to authenticate requests via app auth.
func (m AppAuthAuthenticator) Authenticate(r *http.Request) (*http.Request, error) {
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
}
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
}
username, password, ok := r.BasicAuth()
if !ok {
return nil, ErrAuthenticationFailed
}
next, err := m.RevaGatewaySelector.Next()
if err != nil {
return nil, ErrAuthenticationFailed
}
authenticateResponse, err := next.Authenticate(r.Context(), &gateway.AuthenticateRequest{
Type: "appauth",
ClientId: username,
ClientSecret: password,
})
if err != nil {
return nil, ErrAuthenticationFailed
}
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
}