I have OAuth 2 custom service set up with the following user field mappings:
|External field name||Internal field name|
Creation of Moodle account on authentication via OAuth 2 is allowed.
The problem is that idnumber and institution fields are not filled for new users, but userinfo endpoint responds with correct values for these fields.
I've looked through Moodle code and found out that only few fields of those we can map are saved.
Let's start from \auth_oauth2\auth::complete_login function. It's called after the user is redirected to oauth2callback.php and authorization code is upgraded to access token.
First, it calls $client->get_userinfo which returns an array (key = internal field name, value = field value from userinfo endpoint response). Mapping is done correctly there. In my case I see idnumber and institution keys with expected values.
Later, depending on 'Require e-mail confirmation' option, one of two functions called:
Both has the same code which creates the user.
After that linked account record is created and only user picture could be updated later.
This explains why some fields are not saved after OAuth login.