יצירת משתמש באופן פרוגרמטי עם מזהה משתמש OAuth2

סיכום מבוסס בינה מלאכותית

The discussion revolves around programmatically creating users in Discourse and linking them to OAuth2 accounts via the REST API. gnuletik seeks a way to insert rows into the user_associated_account table using the API to associate users with their OAuth2 provider IDs, enabling retrieval via GET /u/by-external/oauth2_basic/{external_uid} without requiring manual login. They note that while the OAuth2 Basic Plugin uses the Auth::ManagedAuthenticator system, the API lacks support for linking accounts during user creation. pfaffman suggests using the same email address as the OAuth2 provider, but gnuletik clarifies this doesn’t solve the need to retrieve users by their provider ID. gnuletik proposes extending the POST /users.json API to accept an associated_accounts field, similar to GitLab’s API, to directly link users to OAuth2 accounts during creation.

Is there a way to insert a row inside the user_associated_account table from the REST API ?

To give you some context, I need to be able to create users on our Discourse instance and link them to our OAuth2 provider.
When users logs in from the Discourse UI (through OAuth2 Basic Plugin), their account is linked and I can get their discourse user ID from their external UID with GET /u/by-external/oauth2_basic/{external_uid}.
However, when we create users from the REST API (POST /users.json), I see no way to link their account to their corresponding OAuth2 account (if they don’t manually log in on Discourse).

FYI, OAuth2 basic plugin implemented the new Auth::ManagedAuthenticator system : https://github.com/discourse/discourse-oauth2-basic/pull/21

What I tried :

  • I looked into the API docs and did not find any clue.
  • I found which API route was called to get the associated accounts from one user : GET /u/{username}/emails.json. But this route does not seems to support HTTP PUT method and does not expose the fields from the user_associated_accounts table (provider_name, provider_uid and user_id).
  • I also looked into the Discourse settings if anything was related to this issue without any success.
  • I checked on meta.discourse.org and did not found a solution.

Any help is greatly appreciated! Thanks :slight_smile:

As long as you create the user with the same email address that oauth has you don’t need to worry

Thanks for your feedback !
I’d like to not worry about it but I need to be able to get user from my provider ID ( GET /u/by-external/oauth2_basic/{external_uid}).
This is not possible until the user manually logs in through UI (I want to avoid that).

I would think of exposing the associated_accounts like this when creating a new user :

POST /users.json
{
  username: "something",
  email: "test@test.com",
  associated_accounts: [
    {
      provider_name: "oauth2_basic",
      provider_uid: "12345"
    }
  ]
}

This would be perfect !

Note: this is similar to the provider and extern_uid parameters of Gitlab Users API (Users API | GitLab Docs)