Skip to content

fix: return 404 when creating an app token for an unknown user - #4097

Merged
asaah262 merged 1 commit into
opencloud-eu:mainfrom
asaah262:fix/857-auth-app-user-not-found
Nov 23, 2026
Merged

asaah262 merged 1 commit into
opencloud-eu:mainfrom
asaah262:fix/857-auth-app-user-not-found

Conversation

@asaah262

Copy link
Copy Markdown
Contributor

Description

When impersonation is enabled (AUTH_APP_ENABLE_IMPERSONATION=true), an admin can create an app token for another user with POST /auth-app/tokens?userName=… or ?userID=…. If that user doesn't exist, the endpoint answered 500 Internal Server Error with an empty body. It now answers 404 Not Found with user not found.

  • authenticateUser returns the new ErrUserNotFound when the machine authentication of the requested user ends with CODE_NOT_FOUND. The user lookup already reports a missing user that way, through the users provider and the gateway.
  • HandleCreate maps ErrUserNotFound to 404. All other errors behave as before: a mismatch between userID and userName stays 400, an unavailable user provider, a rejected machine auth or a transport error stay 500.
  • The check whether impersonation is allowed and whether the caller is an admin still comes first, so other users still get 403 and learn nothing about whether a user exists.

Related Issue

  • None

Motivation and Context

A missing user is a client error, not a server error. With 500 an admin, or a migration script that creates tokens for many users, can't tell a typo in a user name from a broken server.

How Has This Been Tested?

  • Test environment: macOS, Go tests, and the acceptance tests run locally against a locally built server (posix storage driver).
  • New Go test services/auth-app/pkg/service/service_test.go, the first unit test of the package. It calls HandleCreate through the service's router with a mocked gateway and a fake role service:
    • unknown user by userName and by userID: 404 with user not found. Both cases fail without the fix (500, empty body).
    • unchanged behaviour: user provider unavailable, machine auth rejected and gateway transport error (500), userID and userName of different users (400), caller without the account management permission and impersonation disabled (403, no user lookup), existing user (token created with the impersonation label).
  • go test -race ./services/auth-app/... passes. go vet, gofmt and git diff --check are clean.
  • New acceptance scenario in apiAuthApp/token.feature: "admin tries to create auth-app token for a user that does not exist". Against main it fails with HTTP status code 500 is not the expected value 404. With the fix, the whole feature passes (4 scenarios, 21 steps).
  • Not run locally: golangci-lint and the other acceptance suites.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Technical debt
  • Tests only (no source changes)

Checklist:

  • Code changes
  • Unit tests added
  • Acceptance tests added
  • Documentation added

AI assistance: Claude Code (claude-opus-5-5) wrote the first draft of the code, the tests and this description; I reviewed and adjusted them.

Assisted-by: Claude Code:claude-opus-5-5
@codacy-production

codacy-production Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 18 complexity

Metric Results
Complexity 18

View in Codacy

🟢 Coverage 100.00% diff coverage · +0.14% coverage variation

Metric Results
Coverage variation ✅ +0.14% coverage variation (-1.00%)
Diff coverage ✅ 100.00% diff coverage

View coverage diff in Codacy

Coverage variation details
Coverable lines Covered lines Coverage
Common ancestor commit (d43947a) 94350 28356 30.05%
Head commit (4111271) 94357 (+7) 28491 (+135) 30.19% (+0.14%)

Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: <coverage of head commit> - <coverage of common ancestor commit>

Diff coverage details
Coverable lines Covered lines Diff coverage
Pull request (#4097) 7 7 100.00%

Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: <covered lines added or modified>/<coverable lines added or modified> * 100%

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@asaah262
asaah262 marked this pull request as ready for review November 23, 2026 15:16
@asaah262
asaah262 requested a review from pbleser-oc November 23, 2026 15:16
@asaah262
asaah262 merged commit dc749a3 into opencloud-eu:main Nov 23, 2026
65 checks passed
@asaah262
asaah262 deleted the fix/857-auth-app-user-not-found branch November 23, 2026 18:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants