Skip to content

feat(idp): add built-in two-factor authentication (TOTP) to the builtin IDP - #4001

Open
davemathews12 wants to merge 4 commits into
opencloud-eu:mainfrom
davemathews12:idp-totp-mfa
Open

davemathews12 wants to merge 4 commits into
opencloud-eu:mainfrom
davemathews12:idp-totp-mfa

Conversation

@davemathews12

@davemathews12 davemathews12 commented Nov 15, 2026 •

Copy link
Copy Markdown

Description

Adds built-in two-factor authentication (TOTP, RFC 6238) to the builtin IDP. It is optional per user: only users who turn it on are asked for a code. It is available by default without configuration, IDP_MFA_ENABLED=false switches it off (then the unchanged lico LDAP backend is registered and no new routes are mounted).

  • Users: new page /signin/v1/mfa to set it up (password, QR code, confirm with a code, ten recovery codes), turn it off or get new recovery codes. The login page asks for a code after the password. Web, desktop and mobile clients all use this page.
  • Admins: opencloud idp mfa list and opencloud idp mfa disable <user>. Settings IDP_MFA_ENABLED, IDP_MFA_ISSUER, IDP_MFA_STORE*.

How it works:

  • services/idp/pkg/mfa holds TOTP (stdlib only), storage, the settings API and a wrapper around the lico identifier backend. The second factor is checked in the wrapped Logon, after the password. Without a code it fails closed.
  • The code is sent as a fourth element of the existing logon params. A rejected logon keeps lico's 204 and adds the header OpenCloud-MFA: required|invalid|locked, only after a correct password.
  • lico's LDAP bootstrap creates its backend internally, so pkg/backends/ldap/bootstrap is a copy with the wrapper added (like the cs3 bootstrap). Only used when the feature is on.
  • Secrets are stored AES-256-GCM encrypted with a key derived from IDP_ENCRYPTION_SECRET_FILE, recovery codes as hashes. Codes are single use, five wrong codes lock for five minutes.

Related Issue

Motivation and Context

Small deployments on the builtin IDP have no second factor without running an external IDP.

How Has This Been Tested?

  • test environment: Linux amd64, Go 1.26, opencloud server from this branch, demo users, ldap identity manager, nats-js-kv store
  • unit tests: go test -race ./services/idp/...
  • all of the following also with no MFA setting at all (default configuration)
  • HTTP (26 checks): login with and without 2FA incl. OIDC code flow and graph API, setup, wrong and reused codes, recovery codes, other users unaffected, basic auth does not bypass
  • restart persistence, lockout, admin reset, feature off and on again
  • Chrome via Playwright (20 checks): the full flow through the real pages, desktop and mobile width
  • native clients (13 checks): code flow with PKCE as OpenCloudDesktop, OpenCloudAndroid, OpenCloudIOS through the login page with device emulation, incl. consent, token, refresh

Not tested: the cs3 identity manager end to end, the real apps on real devices, scanning the QR code with a phone, several idp instances, the project CI, golangci-lint and acceptance tests.

Screenshots



Types of changes

  • New feature (non-breaking change which adds functionality)

Checklist:

  • Code changes
  • Unit tests added
  • Acceptance tests added
  • Documentation added

Notes for reviewers

  • Default on is the main thing to decide. With it on, the idp service needs to reach the store (default nats-js-kv on 127.0.0.1:9233) and logins fail closed when it cannot. Single-binary and compose deployments work as is; deployments that run idp separately need IDP_MFA_STORE_NODES. If you prefer opt-in, it is a one-line change of the default.
  • Opt-in per user only, no enforcement.
  • A companion change for opencloud-eu/web adds a "Two-factor authentication" row to the account settings that shows the state and links to the page (screenshot below). Without it the page is only reachable by its URL.
  • New UI strings have English defaults only, translation.pot was not regenerated.
  • New frontend dependency qrcode.react (ISC).
  • App tokens and basic auth are not covered by the second factor, see the README.

Users of the builtin IDP can protect their account with a time-based
one-time password (RFC 6238) from an authenticator app. It is optional
per user and available by default, IDP_MFA_ENABLED=false switches it
off.

- new page /signin/v1/mfa to set up, turn off and get recovery codes
- the login page asks for a code after the password
- the second factor is checked in a wrapper around the lico identifier
  backend, after the password was verified
- secrets are stored encrypted, recovery codes hashed
- `opencloud idp mfa list|disable` for administrators
@codacy-production

codacy-production Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Not up to standards ⛔

🔴 Issues 1 high

Alerts:
⚠ 1 issue (≤ 0 issues of at least minor severity)

Results:
1 new issue

Category Results
ErrorProne 1 high

View in Codacy

🟢 Metrics 448 complexity · -3 duplication

Metric Results
Complexity 448
Duplication -3

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

- return to the two-factor page after login via the router instead of
  a redirect built from props
- use function declarations and drop unused imports in the new page
- read the logon result header by its literal name
- document why SHA-1 (HMAC-SHA1, RFC 6238) and the configured CA file
  path are fine and silence the findings
@davemathews12

davemathews12 commented Nov 15, 2026 •

Copy link
Copy Markdown
Author

Hi! This is my first contribution here, so please tell me if I should do anything differently.

The server part is in this pull request, the matching switch for the account settings is in opencloud-eu/web#4471, and the idea is described in #3999.

I saw #1695 and the plan in #355 to embed Authelia. but I'm offering it as something that works with the current built-in IDP today.

I'm happy to make any changes you need to get this integrated, for example making it opt-in instead of on by default?, splitting it into smaller pull requests, or adding acceptance tests. Just let me know what you'd prefer. i really love this proyect!

ai disclosure: this was NOT vibe coded, but I used an LLM for some testing and reviewing

@davemathews12

Copy link
Copy Markdown
Author

Update: as suggested in #3999, I've also proposed the second factor step upstream in libregraph/lico#305. So there are two options now we can go now?:

  1. This pull request as it is. It works with the current lico, no upstream change needed.
  2. On top of lico#305, once a lico release has it. The copied lico bootstrap and the backend wrapper go away, about 450 lines less. Preview: davemathews12/opencloud-but-it-has-2fa@idp-totp-mfa...idp-totp-mfa-lico

Both use the same settings, setup page and account switch (opencloud-eu/web#4471). I'm happy to go with whichever you prefer. even a redesign, or well... anything, just ask me!

The lock after five wrong codes ended after five minutes and the count
started again, so somebody who has the password could keep on guessing
five codes every five minutes. Every further round of wrong codes now
doubles the lock, up to a day, until a correct code is entered.
The settings page keeps the view and the data only it shows in one
state, so they change in one update and a view never renders without
its data. The second step of the login page is a component of its own,
and a logon refused by the second factor is built in one place. The
password check moves next to the backend it uses.
@davemathews12

Copy link
Copy Markdown
Author

Pushed two follow-ups:

  • fix: after five wrong codes the lock now doubles with every further round of wrong codes (5 minutes, 10, 20, … up to a day) until a correct code is entered. Before, somebody who has the password could keep guessing five codes every five minutes.
  • refactor: simplified the 2FA settings and login screens, no change in behaviour.

The lico-based variant (branch idp-totp-mfa-lico) is updated to match libregraph/lico#305 as reworked after longsleep's feedback, so both options stay available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Built-in two-factor authentication (TOTP)

1 participant