Files
Michal Klos dc33f20fee feat: [OCISDEV-1411] don't logout users on a transient IdP userinfo failure (#12999)
* test: [OCISDEV-1411] regression test for userinfo-timeout 401

* fix: [OCISDEV-1411] don't logout users on a transient IdP userinfo failure

* fix: [OCISDEV-1411] don't logout users on a transient IdP userinfo failure

* feat: review

* feat: review

* feat: review
2026-09-29 09:37:48 +02:00

65 lines
2.0 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/ocis-pkg/oidc"
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]
}
// 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
}
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
}
r.Header.Set(revactx.TokenHeader, authenticateResponse.GetToken())
user := authenticateResponse.GetUser()
// fake oidc claims for the account resolver
claims := map[string]interface{}{
oidc.Iss: user.GetId().GetIdp(),
oidc.PreferredUsername: user.GetUsername(),
oidc.Email: user.GetMail(),
oidc.OwncloudUUID: user.GetId().GetOpaqueId(),
}
r = r.WithContext(oidc.NewContext(r.Context(), claims))
return r, nil
}