Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow admins to create user API tokens #2541

Closed

Conversation

theopolis
Copy link

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.

Copy link

codecov bot commented May 22, 2024

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 ⚠️
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     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ColdHeat
Copy link
Member

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?

@theopolis
Copy link
Author

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 theopolis closed this May 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants