Skip to content

auth-app: creating a token answers 200 instead of 201 #4098

Open

Description

@asaah262

POST /auth-app/tokens is meant to answer 201 Created, but always answers 200 OK. HandleCreate writes the response body first and calls w.WriteHeader(http.StatusCreated) only afterwards: by then the status has already been sent as 200, and the WriteHeader call has no effect.

HandleList has the same pattern twice: w.WriteHeader(http.StatusOK) after the body (no effect, but harmless), and w.WriteHeader(http.StatusInternalServerError) after a failed w.Write (no effect either: the client already got 200).

Steps to reproduce

Reproduced on main (d43947a209), with the auth-app service running (OC_ADD_RUN_SERVICES=auth-app) and basic auth enabled.

  1. curl -si -XPOST 'https://<host>/auth-app/tokens?expiry=72h' -u <user>:<password> | head -1

Expected

HTTP/2 201, as the code intends.

Actual

HTTP/2 200, with the new token in the body. The acceptance tests in tests/acceptance/features/apiAuthApp/token.feature expect, and get, 200 (lines 12, 41 and 111).

Where

services/auth-app/pkg/service/service.go: HandleCreate (the w.Write(b) followed by w.WriteHeader(http.StatusCreated)) and HandleList (the w.WriteHeader calls after w.Write).

Neither HandleCreate nor HandleList sets Content-Type: application/json, so their JSON bodies are sent as text/plain; charset=utf-8.

Fix

Two options, which differ in what clients see:

  1. Send 201 for real: call w.WriteHeader(http.StatusCreated) before w.Write(b), and change the three acceptance test expectations to 201. This is the correct status for a create, but changes the API's behaviour: a client that checks for exactly 200 would break. Within opencloud-eu only the acceptance tests do; web and the release tests accept any 2xx. Clients outside opencloud-eu can't be checked.
  2. Keep 200: remove the WriteHeader calls that have no effect, so that the code matches the behaviour. Nothing changes for clients.

In both cases, HandleList should drop its WriteHeader calls after w.Write, since a status can't be changed once the body has been written. Both handlers should also set Content-Type: application/json before writing the body.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions