Skip to content

Commit

Permalink
fix impersonated request user mismatch
Browse files Browse the repository at this point in the history
  • Loading branch information
2403905 committed Nov 12, 2024
1 parent 22d8025 commit d61d63b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
6 changes: 6 additions & 0 deletions changelog/unreleased/fix-auth-app.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Bugfix: Fix impersonated request user mismatch

We fixed a user id and name mismatch in the impersonated auth-app API request

https://github.com/owncloud/ocis/pull/10548
https://github.com/owncloud/ocis/issues/10292
23 changes: 18 additions & 5 deletions services/auth-app/pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"time"

Expand All @@ -25,6 +26,8 @@ import (
"google.golang.org/grpc/metadata"
)

var ErrBadRequest = errors.New("bad request")

// AuthAppToken represents an app token.
type AuthAppToken struct {
Token string `json:"token"`
Expand Down Expand Up @@ -97,8 +100,10 @@ func (a *AuthAppService) HandleCreate(w http.ResponseWriter, r *http.Request) {
}

label := "Generated via API"
cid := buildClientID(q.Get("userID"), q.Get("userName"))
if cid != "" {

// Impersonated request
userID, userName := q.Get("userID"), q.Get("userName")
if userID != "" || userName != "" {
if !a.cfg.AllowImpersonation {
sublog.Error().Msg("impersonation is not allowed")
http.Error(w, "impersonation is not allowed", http.StatusForbidden)
Expand All @@ -115,9 +120,13 @@ func (a *AuthAppService) HandleCreate(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusForbidden)
return
}
ctx, err = a.authenticateUser(cid, gwc)
ctx, err = a.authenticateUser(userID, userName, gwc)
if err != nil {
sublog.Error().Err(err).Msg("error authenticating user")
if errors.Is(err, ErrBadRequest) {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusInternalServerError)
return
}
Expand Down Expand Up @@ -241,11 +250,11 @@ func (a *AuthAppService) HandleDelete(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}

func (a *AuthAppService) authenticateUser(clientID string, gwc gateway.GatewayAPIClient) (context.Context, error) {
func (a *AuthAppService) authenticateUser(userID, userName string, gwc gateway.GatewayAPIClient) (context.Context, error) {
ctx := context.Background()
authRes, err := gwc.Authenticate(ctx, &gateway.AuthenticateRequest{
Type: "machine",
ClientId: clientID,
ClientId: buildClientID(userID, userName),
ClientSecret: a.cfg.MachineAuthAPIKey,
})
if err != nil {
Expand All @@ -256,6 +265,10 @@ func (a *AuthAppService) authenticateUser(clientID string, gwc gateway.GatewayAP
return nil, errors.New("error authenticating user: " + authRes.GetStatus().GetMessage())
}

if (userID != "" && authRes.GetUser().GetId().GetOpaqueId() != userID) || (userName != "" && authRes.GetUser().GetUsername() != userName) {
return nil, fmt.Errorf("requested user does not match authenticated user: userID:%s, userName:%s, %w", authRes.GetUser().GetId().GetOpaqueId(), authRes.GetUser().GetUsername(), ErrBadRequest)
}

ctx = ctxpkg.ContextSetUser(ctx, &userpb.User{Id: authRes.GetUser().GetId()})
return metadata.AppendToOutgoingContext(ctx, ctxpkg.TokenHeader, authRes.GetToken()), nil
}
Expand Down

0 comments on commit d61d63b

Please sign in to comment.