Creating users via SSO fails with blank username


(bryanlarsen) #1

User.suggest_name assumes email, and crashes if is not blank but doesn’t contain an “@”.

However, DiscourseSingleSignOn#match_email_or_create_user calls it with User.suggest_name(name || username || email)

NoMethodError (undefined method `gsub' for nil:NilClass):
  app/models/user.rb:133:in `suggest_name'
  app/models/discourse_single_sign_on.rb:82:in `match_email_or_create_user'
  app/models/discourse_single_sign_on.rb:49:in `lookup_or_create_user'
  app/controllers/session_controller.rb:33:in `sso_login'
  lib/middleware/anonymous_cache.rb:117:in `call'
  config/initializers/quiet_logger.rb:10:in `call_with_quiet_assets'
  config/initializers/silence_logger.rb:24:in `call'
  lib/middleware/unicorn_oobgc.rb:95:in `process_client'

(Sam Saffron) #2

This seems to be suggesting name, email and username are all nil. Look at /logs what params are being send in?


(Jeff Atwood) #3

Can SSO provide better feedback in this failure case @sam? Better errors = less support requests.


(Sam Saffron) #4

First let’s confirm what the issue is, hence /logs


(bryanlarsen) #5

/logs are useless since the request parameters are hashed with my private secret

nils passed in are fine, the first line handles that. It crashes when the email argument doesn’t contain a @ (which is quite a common scenario when passing in name || username || email as DiscourseSingleSignOn does.

  def self.suggest_name(email)
    return "" unless email
    name = email.split(/[@\+]/)[0]
    name = name.gsub(".", " ")   # this line crashes
    name.titleize
  end

(Sam Saffron) #6

I guess it fails if for some reason a username / name is the empty string

Will get that fixed. Nothing to do with containing a @ or not.

[15] pry(main)> "".split(/[@\+]/)[0]
=> nil
[16] pry(main)> "hello".split(/[@\+]/)[0]
=> "hello"
[17] pry(main)> "hello@world".split(/[@\+]/)[0]
=> "hello"

(Sam Saffron) #7

Fixed via:


(Sam Saffron) #8

This topic was automatically closed after 24 hours. New replies are no longer allowed.