New API endpoint to create user API tokens#5930
Conversation
kevinpapst
left a comment
There was a problem hiding this comment.
please address these issues and then we can discuss how to move forward
Makes the $token argument optional and generates a random token when omitted, removing the need for a separate static factory.
Replaces the inline authorization checks with the IsGranted attribute, matching the pattern used in ProfileController::createAccessToken.
|
@kevinpapst Done. Added |
|
Yes, I want the contribution to be functional. That's why I'd been applying your earlier feedback as it came in. This is my first PR to Kimai and I'm not familiar with the codebase. I'd been looking to use the API with your Cloud product, but saw that API passwords will be removed without a programmatic alternative for creating access tokens. So I opened a small PR to add the missing endpoint. I don't quite understand the tone of your last message, and I don't think it was necessary. The technical point is fair though, and I understand it. I've gone through the branch again. |
Please don't read what was never said. If you want to discuss a PR first, then please:
If you want me to maintain your code long term, I think its fair to ask you to put some effort into it upfront. |
Description
Adds a new API endpoint
POST /api/users/{id}/api-tokenthat creates a newAccessTokenfor the given user and returns the plain token in the response. This is the missing counterpart of the existingDELETE /api/users/api-token/{id}.Currently the only way to obtain an
AccessTokenis via the web UI. This blocks any automated provisioning flow that needs to create a Kimai user and immediately get a usable Bearer token for that user — for example, a SaaS integration that creates one Kimai user per customer. The legacyplainApiTokenfield onPOST /api/usersonly populates the deprecatedusers.api_tokencolumn used by theX-AUTH-USER/X-AUTH-TOKENheaders, which is not interchangeable withAuthorization: Bearer ….To guarantee that tokens generated via the API and via the web UI always follow the same algorithm, the random-token generation has been extracted into a new factory method
AccessToken::createForUser(User $user): self. BothProfileController::createAccessTokenand the newUserController::postApiTokennow use this single source of truth. TheAccessTokenconstructor signature(User $user, string $token)is left unchanged so that fixtures andResetTestCommand, which need predictable tokens, keep working as-is.Types of changes
Checklist
composer code-check)