Programmatically create user with OAuth2 user ID

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 : Move to ManagedAuthenticator by angusmcleod · Pull Request #21 · discourse/discourse-oauth2-basic · GitHub

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

1 Like

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)