Page MenuHomePhabricator

Provide resonable method for bots to authenticate to Toolhub API
Closed, ResolvedPublic

Description

The main authentication flow for Toolhub is an OAuth 2.0 handshake with metawiki. This is an interactive flow that is meant for human interaction.

The Toolhub API has a secondary authentication flow in the form of an OAuth 2.0 handshake with Toolhub itself. This is also an interactive flow that is meant for human interaction, but it generates tokens which can be renewed without human intervention. It is suited for use by a web service which is providing some value added interaction with the write API actions.

A third currently unsatisfied use case is a Toolhub API authentication method that can be used to run a bot as the bot developer's own user. Essentially the need is for a https://www.mediawiki.org/wiki/Manual:Bot_passwords or https://www.mediawiki.org/wiki/OAuth/Owner-only_consumers workalike method which can be used by bot operators.

Event Timeline

The built-in TokenAuthentication system of Django REST Framework might be the easiest solution to implement for this. I think we would only need to:

  • Add rest_framework.authtoken to INSTALLED_APPS
  • Add rest_framework.authentication.TokenAuthentication to REST_FRAMEWORK["DEFAULT_AUTHENTICATION_CLASSES"]
  • Expose self-service token management (create, revoke) via the Toolhub API
  • Expose token management API via the Toolhub UI. This feels like it could fit in the "Developer settings" screen as new tab.

To use this, a bot developer would use the Toolhub UI to authenticate and then generate a token. This token would then be directly used via an Authorization header when making authenticate API requests.

Another option would be to implement a client credentials grant workflow with the existing django-oauth-toolkit integration. This flow would require a similar amount of implementation work for the Toolhub backend however as it requires exposing a new type of grant registration via the API and UI. It would also require the implementing bots to add an initial step of exchanging the OAuth client id and secret for a Bearer token via an API call before then using the Bearer token via an Authorization header to make authenticated API requests.

Change 713369 had a related patch set uploaded (by BryanDavis; author: Bryan Davis):

[wikimedia/toolhub@main] api: Add endpoints for managing owner-only authtokens

https://gerrit.wikimedia.org/r/713369

Change 713561 had a related patch set uploaded (by BryanDavis; author: Bryan Davis):

[wikimedia/toolhub@main] ui: Add tab to DeveloperSettings for managing authentication token

https://gerrit.wikimedia.org/r/713561

bd808 moved this task from Backlog to Review on the Toolhub board.

Change 713369 merged by jenkins-bot:

[wikimedia/toolhub@main] api: Add endpoints for managing owner-only authtokens

https://gerrit.wikimedia.org/r/713369

Change 713561 merged by jenkins-bot:

[wikimedia/toolhub@main] ui: Add tab to DeveloperSettings for managing authentication token

https://gerrit.wikimedia.org/r/713561