Skip to content

Creating Token via API with Token authentication is not possible #21186

Description

@davidhiendl

Description

In trying to write a Drone Secrets Extension to allow Drone pipelines to obtain a per-pipeline Gitea API Token automatically I ran into 2 problems:

  1. Trying to create a token for a user does not work when using a token for authorization:
curl -X 'POST' \
  'https://gitea.test/api/v1/users/test123/tokens' \
  -H 'accept: application/json' \
  -H 'Authorization: token xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' \
  -H 'Content-Type: application/json' \
  -d '{
  "name": "drone_123123_2022-09-16-14-00"
}'

Response: {"message":"auth required","url":"https://gitea.test/api/swagger"}
Note: The token was created for an admin user account.

Edit: Forgot to mention the same problem seems to be present for listing and deleting tokens as well.

  1. Trying to create API tokens for different users via the API is not possible. In the following example I'm trying to create a token for the user "test123" (the user has been created already). However the token is created for my primary user (the one specified via basic auth credentials). This requires the sudo get param, the URL is however confusing because it also contains a username path param.
curl -X 'POST' \
  'https://gitea.test/api/v1/users/test123/tokens' \
  -H 'accept: application/json' \
  -u "admin:xxxxxxxx" \
  -H 'Content-Type: application/json' \
  -d '{
  "name": "drone_123123_2022-09-16-14-00"
}'

The reason appears to be that the controller uses the request context user instead of the one specified in the path parameter:

UID: ctx.Doer.ID,

Gitea Version

1.17.2

Can you reproduce the bug on the Gitea demo site?

Yes

Log Gist

No response

Screenshots

No response

Git Version

No response

Operating System

No response

How are you running Gitea?

  • official docker image
  • deployed via helm chart

Database

PostgreSQL

Metadata

Metadata

Assignees

No one assigned

    Labels

    type/questionIssue needs no code to be fixed, only a description on how to fix it yourself.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions