CTFd icon indicating copy to clipboard operation
CTFd copied to clipboard

Allow admins to create user API tokens

Open theopolis opened this issue 1 year ago • 2 comments

Hi folks, I am curious what you think about this feature. It adds functionality to the the POST /api/v1/tokens endpoint to optionally accept a user_id request parameter. If the caller is an admin, then this input user_id is the target for the API token creation.

The use case here is that we have automation that auto-registers users and provides them an API-key-only for interacting with CTFd (no UI). Thus we need a way to create tokens on their behalf.

Heads up that I want to test this thoroughly, and right now I am opening a PR to get feedback on direction/approach/applicability.

theopolis avatar May 21 '24 17:05 theopolis

Codecov Report

Attention: Patch coverage is 25.00000% with 3 lines in your changes are missing coverage. Please review.

Project coverage is 87.85%. Comparing base (162bca6) to head (03c9163). Report is 9 commits behind head on master.

Files Patch % Lines
CTFd/api/v1/tokens.py 25.00% 3 Missing :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2541      +/-   ##
==========================================
+ Coverage   87.57%   87.85%   +0.28%     
==========================================
  Files         155      155              
  Lines        9650     9659       +9     
==========================================
+ Hits         8451     8486      +35     
+ Misses       1199     1173      -26     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar May 22 '24 02:05 codecov[bot]

Hi Ted!

If you have command line access you can do this via the shell similarly to how manage.py works. You import CTFd, create the app and use with app.app_context() and you can import the Users & Tokens models and create the tokens that you need.

Something sort of like the following:

from CTFd import create_app
from CTFd.models import db, Users, Tokens

app = create_app()
with app.app_context():
    t = Tokens()  # You will need to add the arguments here
    db.session.add(t)
    db.session.commit()

You could also create a plugin if you need it exposed via HTTP.

I see the benefit of this capability but codifying it in the API means that we are blessing that admins have the ability to become users. And other admins as well.

It's not that this isn't currently possible (resetting the user's password, using XSS) but I think there's some kind of better implementation. Maybe low privilege token, maybe service accounts, maybe this is behind a configuration but I don't know for sure.

Does creating the secondary script (perhaps it could be a simple HTTP service) or a plugin work for you?

ColdHeat avatar May 22 '24 03:05 ColdHeat

Hey Kevin! Thanks for the great response, that's exactly what I wanted to know. I definitely understand wanting to preserve the expectations of admin control over users.

I will go with the plugin route for my use-case.

theopolis avatar May 22 '24 16:05 theopolis