Backup codes for Moodle multi-factor authentication

Moodle's MFA has no backup codes, so every lost phone is an admin reset. Our free factor_backupcodes gives users ten one-time codes to sign in.

Drawing of a paper card with a list of short lines next to a large blue key.

Short answer: Moodle has had multi-factor authentication built in since version 4.3, with authenticator apps, security keys, email and SMS. It has no backup codes. When a student loses the phone that holds their authenticator app, often on the day of an exam, only a site administrator can let them back in. Our free plugin, factor_backupcodes, adds the standard fix: ten one-time codes each user saves when they set up MFA.

This is the twenty-first plugin in our series: one plugin for each Moodle plugin type, each one teaching DevLMS Studio, our AI plugin builder, how that type works. We checked the Moodle plugins directory first: it lists no MFA factors at all, only the ones built into Moodle.

How it works for users

  1. In Preferences > Multi-factor authentication preferences, they choose Set up backup codes.
  2. Moodle shows ten codes like k7m3q-x9wdp, once. They download them as a text file, keep them in a password manager or print them, tick “I have saved these codes somewhere safe” and save.
  3. When they cannot use their usual method, the sign-in page offers Use a backup code. Each code works once.

They see how many codes are left on their preferences page, are warned when they use the last one, and can make a new set at any time, which stops the old one. It supports Moodle 4.5 LTS to 5.3. The code is on GitHub.

Security choices

  • Only one-way hashes are stored, the same way Moodle stores passwords. A copy of the database does not reveal the codes.
  • A code is removed in the same step that accepts it, so it cannot be used twice. We checked this the hard way: with the removal taken out on purpose, two tests fail.
  • Wrong codes count towards Moodle’s MFA lockout, like a wrong authenticator code.
  • Codes have no i, l, o, 0 or 1, and capitals, spaces and the hyphen are ignored when typing, so people can read them off paper without mistakes.

How an MFA factor works

From Moodle 5.3’s own authenticator app factor:

  1. A factor is one class with two forms: one to set it up in the user’s preferences, one to check it at sign-in.
  2. Each factor has a weight; a user is through when the factors they passed add up to 100. A backup code with weight 100 is enough on its own.
  3. The sign-in page asks one factor first and offers the others as buttons, which is where “Use a backup code” appears.

The browser test does the whole journey: set up the codes, sign out, sign in with one of them through Moodle’s MFA page, and see “9 backup codes left”. It passes on Moodle 4.5 to 5.3, after we fixed two differences in 4.5’s sign-in page that only showed up there.

Need a plugin like this for your own Moodle?

Describe it in DevLMS Studio and watch it built and tested in a real Moodle, or ask our team for a fixed price.

Open DevLMS Studio