MDL-42993 auth: Allow spaces in usernames for external authentication

Only when $CFG->extendedusernamechars is set to true.
Also included 2 trim statements where small issues were found with reading
external usernames in that  were not filtered for trailing whitespaces.