Using the API is it possible find users by their sso username

AI-generated summary

Using the API to find users by their SSO username

The discussion revolves around finding users by their SSO username using the Discourse API. eriko explains the challenge of locating users when they have chosen a unique username within a Discourse instance.

eriko initially considers implementing a workaround, but later realizes it’s not an ideal solution. riking suggests using the /users/by-external/ endpoint, and eriko confirms that this approach works by appending .json to the URL, resulting in /users/by-external/#{sso_username}.json. This endpoint is not currently included in the API gem.

I can see in the api that once you have a user you can get their sso information. If you user are using sso you probably have a data source with this name. What you do not have is a source of the username they may have chosen in a discourse instance. This makes it very hard to find users. So it there a way to find a user by their sso username?

Since I moved to docker I have replacing some automation I that ran via adding it as a rake task. It added and disabled users. I do not want to do that anymore and would like the use the API to replace it.

In am thinking something like

 def show
  @user = User.find_by(username_lower: params[:id])
  if SiteSetting.enable_sso && !@user
    sso_record = SingleSignOnRecord.find_by(external_id: params[:id])
    if sso_record && user = sso_record.user
      @user = sso_record.user
    end
  end
  raise Discourse::NotFound unless @user
  render_serialized(@user, AdminDetailedUserSerializer, root: false)
end

The idea is that if sso is enable using the sso records as a back up. As I think this through the more I think this is a bad idea. Hmm how to get the user via their external_id?

/users/by-external/23

With a little tweak @riking that is it.
/users/by-external/#{sso_username}.json

The json part is needed as it will not do the search otherwise. This needs to be added to the api gem.