Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-69555

Make duration of QR login and auto-login time between requests configurable

    XMLWordPrintable

Details

    • MOODLE_310_STABLE, MOODLE_311_STABLE, MOODLE_39_STABLE, MOODLE_400_STABLE
    • MOODLE_400_STABLE
    • MDL-69555-master
    • Hide
      Prerequisite
      1. Moodle site configured to use https (e.g. using ngrok with $CFG->sslproxy set to true)
      2. Mobile device with a QR code reader app
      3. As the site admin, ensure that Mobile services are enabled in Site administration > Advanced features
      4. As the site admin, ensure that the Mobile app > Mobile app authentication > QR code access setting is sett to QR code with automatic login
      Automatic login via QR code as a non-admin user
      1. Now, login into the Moodle site with a non-admin user account
      2. Go to the user profile page.
      3. Confirm that you can see the "View QR code" button in the "Mobile app" section of your profile page.
      4. Confirm that the message above the "View QR code" button says at the end "The QR code will expire in 10 mins."
      5. Click the "View QR code" button and scan the QR code with a phone
      6. Copy the scanned text result somewhere on your computer
      7. Open the console and execute this curl request replacing SITE_URL and YOUR_QR_LOGIN_KEY with the value of the qrlogin parameter from the scanned text and USERID with the id of the user the code was generated for.

         curl 'SITE_URL/lib/ajax/service.php' -A "MoodleMobile" --data-binary '[{"index":0,"methodname":"tool_mobile_get_tokens_for_qr_login","args":{"qrloginkey": "YOUR_QR_LOGIN_KEY", "userid": "USERID"}}]' | python -m "json.tool"
        

      8. Confirm that:
        • In the response to the curl request you see three fields: token, privatetoken and warnings
        • Warning is empty
        • token and privatetoken are not empty
      9. Open the console and execute this new curl request (let's call this "core_webservice_get_site_info"), replacing the SITE_URL and TOKEN with value of the token field from the previous curl request

        curl 'SITE_URL/webservice/rest/server.php?moodlewsrestformat=json' --data 'wsfunction=core_webservice_get_site_info&wstoken=TOKEN' | python -m "json.tool"
        

      10. Confirm that you see information about the site (site name, version) among other fields in the response.
      Set a custom " QR authentication key duration " value
      1. As the site admin, set the Mobile app > Mobile app authentication > QR code access setting to 10 seconds
      2. Now, login into the Moodle site with a non-admin user account
      3. Go to the user profile page.
      4. Confirm that you can see the "View QR code" button in the "Mobile app" section of your profile page.
      5. Confirm that the message above the "View QR code" button says at the end "The QR code will expire in 10 seconds."
      6. Click the "View QR code" button and scan the QR code with a phone
      7. Copy the scanned text result somewhere on your computer
      8. Wait for at least 10 seconds so we are sure that the code has expired
      9. Open the console and execute this curl request replacing SITE_URL and YOUR_QR_LOGIN_KEY with the value of the qrlogin parameter from the scanned text and USERID with the id of the user the code was generated for.

         curl 'SITE_URL/lib/ajax/service.php' -A "MoodleMobile" --data-binary '[{"index":0,"methodname":"tool_mobile_get_tokens_for_qr_login","args":{"qrloginkey": "YOUR_QR_LOGIN_KEY", "userid": "USERID"}}]' | python -m "json.tool"
        

      10. Confirm that:
      11. The curl response is an exception with a message "Expired key"
      Test auto-login minimum time between requests for keys
      1. As a Moodle admin now go to Site administration > Mobile app > Mobile authentication
      2. Set the field "Minimum time between auto-login requests" to a value of "30 minutes", and Save changes
      3. Open a new incognito tab in the browser and get a normal token and private token via this URL (replace U with username and P with the user password): https://SITE_URL/login/token.php?username=U&password=P&service=moodle_mobile_app
      4. Now, we are going to get an auto-login key that is valid for 30 minutes, we need to call this WS via curl command, please, replace the token and privatetoken values with the ones from step 3.

        curl 'https://SITE_URL/webservice/rest/server.php?moodlewsrestformat=json' --data 'privatetoken=PRIVATETOKEN&wsfunction=tool_mobile_get_autologin_key&wstoken=WSTOKEN' --header 'user-agent: MoodleMobile'

      5. Confirm that
        • You get as response a key and the autologin url
      6. Point your browser incognito tab to the autologin url, replacing your key and userid: https://SITE_URL/admin/tool/mobile/autologin.php?key=KEY&userid=USERID
      7. Check that you are successfully logged in.
      8. Log out from Moodle site and close the incognito tab
      9. Now, repeat the same CURL request from step 2, confirm that:
        • You get a "autologinkeygenerationlockout" errorcode as part of a general excepttion
      10. As a Moodle admin now go to Site administration > Mobile app > Mobile authentication
      11. Set the field "Minimum time between auto-login requests" to a value of "1 minutes", and Save changes
      12. Repeat CURL request from step 2, confirm that
        • You get as response a key and the autologin url
      13. Wait exactly 20 seconds, repeat the request again and confirm that:
        • You get a "autologinkeygenerationlockout" error
      14. Wait 40 seconds, repeat the curl request again and confirm that:
        • You get as response a key and the autologin url
      Show
      Prerequisite Moodle site configured to use https (e.g. using ngrok with $CFG->sslproxy set to true) Mobile device with a QR code reader app As the site admin, ensure that Mobile services are enabled in Site administration > Advanced features As the site admin, ensure that the Mobile app > Mobile app authentication > QR code access setting is sett to QR code with automatic login Automatic login via QR code as a non-admin user Now, login into the Moodle site with a non-admin user account Go to the user profile page. Confirm that you can see the "View QR code" button in the " Mobile app " section of your profile page. Confirm that the message above the "View QR code" button says at the end "The QR code will expire in 10 mins." Click the "View QR code" button and scan the QR code with a phone Copy the scanned text result somewhere on your computer Open the console and execute this curl request replacing SITE_URL and YOUR_QR_LOGIN_KEY with the value of the qrlogin parameter from the scanned text and USERID with the id of the user the code was generated for. curl 'SITE_URL/lib/ajax/service.php' -A "MoodleMobile" --data-binary '[{"index":0,"methodname":"tool_mobile_get_tokens_for_qr_login","args":{"qrloginkey": "YOUR_QR_LOGIN_KEY", "userid": "USERID"}}]' | python -m "json.tool" Confirm that: In the response to the curl request you see three fields: token, privatetoken and warnings Warning is empty token and privatetoken are not empty Open the console and execute this new curl request (let's call this " core_webservice_get_site_info "), replacing the SITE_URL and TOKEN with value of the token field from the previous curl request curl 'SITE_URL/webservice/rest/server.php?moodlewsrestformat=json' --data 'wsfunction=core_webservice_get_site_info&wstoken=TOKEN' | python -m "json.tool" Confirm that you see information about the site (site name, version) among other fields in the response. Set a custom " QR authentication key duration " value As the site admin, set the Mobile app > Mobile app authentication > QR code access setting to 10 seconds Now, login into the Moodle site with a non-admin user account Go to the user profile page. Confirm that you can see the "View QR code" button in the " Mobile app " section of your profile page. Confirm that the message above the "View QR code" button says at the end "The QR code will expire in 10 seconds." Click the "View QR code" button and scan the QR code with a phone Copy the scanned text result somewhere on your computer Wait for at least 10 seconds so we are sure that the code has expired Open the console and execute this curl request replacing SITE_URL and YOUR_QR_LOGIN_KEY with the value of the qrlogin parameter from the scanned text and USERID with the id of the user the code was generated for. curl 'SITE_URL/lib/ajax/service.php' -A "MoodleMobile" --data-binary '[{"index":0,"methodname":"tool_mobile_get_tokens_for_qr_login","args":{"qrloginkey": "YOUR_QR_LOGIN_KEY", "userid": "USERID"}}]' | python -m "json.tool" Confirm that: The curl response is an exception with a message "Expired key" Test auto-login minimum time between requests for keys As a Moodle admin now go to Site administration > Mobile app > Mobile authentication Set the field "Minimum time between auto-login requests" to a value of "30 minutes", and Save changes Open a new incognito tab in the browser and get a normal token and private token via this URL (replace U with username and P with the user password): https://SITE_URL/login/token.php?username=U&password=P&service=moodle_mobile_app Now, we are going to get an auto-login key that is valid for 30 minutes, we need to call this WS via curl command, please, replace the token and privatetoken values with the ones from step 3. curl 'https://SITE_URL/webservice/rest/server.php?moodlewsrestformat=json' --data 'privatetoken=PRIVATETOKEN&wsfunction=tool_mobile_get_autologin_key&wstoken=WSTOKEN' --header 'user-agent: MoodleMobile' Confirm that You get as response a key and the autologin url Point your browser incognito tab to the autologin url, replacing your key and userid: https://SITE_URL/admin/tool/mobile/autologin.php?key=KEY&userid=USERID Check that you are successfully logged in. Log out from Moodle site and close the incognito tab Now, repeat the same CURL request from step 2, confirm that: You get a "autologinkeygenerationlockout" errorcode as part of a general excepttion As a Moodle admin now go to Site administration > Mobile app > Mobile authentication Set the field "Minimum time between auto-login requests" to a value of "1 minutes", and Save changes Repeat CURL request from step 2, confirm that You get as response a key and the autologin url Wait exactly 20 seconds, repeat the request again and confirm that: You get a "autologinkeygenerationlockout" error Wait 40 seconds, repeat the curl request again and confirm that: You get as response a key and the autologin url

    Description

      QR login time

      The  time period of duration for QR code login is hard coded and set to 600 seconds.  That makes sense and is in most cases a very good setting.  In some cases it makes sense to set this time period to a much longer time .

      Feedback from a Moodle Partner:

      I had  the discussion with teachers from primary schools that work from, class 1 when pupils can't read or write.  They have the wish that they can set the time period for a QR code to six months and print the QR code for each pupil.

      This is not secure like a period of 600 seconds. But I see the point from their perspective.

      We could solve this when we make the period configurable by admins.

      Auto-login time between requests

      Apart from the previous but related, we have detected that in recent Moodle versions it is really difficult to have a consistent auto-login in the app (when we perform an SSO between app and web to embed Moodle LMS content in the app).
      This is caused by some Android and especially iOS additional cookies security restrictions. The only way to fix this will be allowing site admins to relax the security time between auto-login requests (auto-login is quite secure right now, it implements different levels of protection, see tool_mobile_external::get_autologin_key()). The time between requests restriction was mostly there to avoid clients being able to abuse generating auto-login keys when it wasn't something really necessary until now.

      Attachments

        Issue Links

          Activity

            People

              jleyva Juan Leyva
              ralfh Ralf Hilgenstock
              Dani Palou Dani Palou
              Shamim Rezaie Shamim Rezaie
              Angelia Dela Cruz Angelia Dela Cruz
              Adrian Greeve, David Woloszyn, Huong Nguyen, Jake Dallimore, Meirza, Michael Hawkins, Raquel Ortega, Safat Shahin, Stevani Andolo
              Votes:
              0 Vote for this issue
              Watchers:
              7 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                19/Apr/22

                Time Tracking

                  Estimated:
                  Original Estimate - 0 minutes
                  0m
                  Remaining:
                  Remaining Estimate - 0 minutes
                  0m
                  Logged:
                  Time Spent - 5 hours, 15 minutes
                  5h 15m