Skip to content

2FA codes ​

A second factor for things that matter: bank transfers, a company laptop, the gang's crypto wallet. Your resource calls RequireCode(src, app); the player enters a rotating 6-digit code. Codes live in the Codes tab of the Passwords app and change every Config.Otp.period seconds (30).

2FA prompt

How it works ​

  1. The first RequireCode for an app enrols the character: np_passwords creates a random secret for this character and app (stored encrypted in np_pw_otp) and adds it to the Codes tab.
  2. On that first prompt, the code arrives by SMS in np_phone, from Config.Otp.smsFrom (Verify), when np_phone runs and SMS is on (Config.Otp.sms, or the app's own sms).
  3. From then on, the prompt offers the code From Codes: TAB fills it on the server, nothing is typed. The player can also read the code in the Codes tab and type it.

Codes are HMAC-SHA256 based (like TOTP): floor(time / period) is the counter, 6 digits. The current and the previous window are accepted, so a code typed right at the switch still works. A 2FA prompt allows 3 wrong codes, then it closes (reason tries).

Without np_phone

Enrolment still happens, but the first code can't reach the player (no SMS, no app). That prompt fails; every later prompt for the app fills from the Codes entry with TAB. If you want the first prompt to work without np_phone, call RequireCode once when the account is set up (for example at character creation) and ignore the result.

Register an app ​

server

In the config:

lua
Config.Otp.apps = {
  { id = 'fleeca', label = 'Fleeca Bank', icon = 'building-columns', tint = 'green' },
}

or from your resource:

lua
exports.np_passwords:RegisterOtpApp({
  id = 'mazebank',            -- up to 64 characters
  label = 'Maze Bank',        -- prompt title, Codes tab, SMS text
  icon = 'building-columns',  -- Font Awesome name, default 'shield-halved'
  tint = 'red',               -- Nimbus tile tint, default 'neutral'
  sms = false,                -- override Config.Otp.sms for this app
})

Apps registered from a resource live in memory: register them at every start. The enrolments (secrets) are persisted.

Ask for a code ​

lua
-- server, yields until the player answers (or Config.Prompt.timeout)
local ok = exports.np_passwords:RequireCode(src, 'mazebank', { reason = 'Transfer $75,000 to LS-48 1192' })
if not ok then return end

reason is the second line of the prompt (default Enter the 6-digit code). RequireCode returns false for an unknown app.

A client-initiated 2FA prompt works too, with the server check afterwards:

lua
-- client
local ok = exports.np_passwords:Prompt({ type = 'otp', app = 'mazebank', reason = 'Sign in' })
if ok then TriggerServerEvent('mazebank:signedIn') end
lua
-- server
RegisterNetEvent('mazebank:signedIn', function()
  if not exports.np_passwords:ConsumeUnlock(source, 'otp:mazebank') then return end
  -- …
end)

The same works inside your own NUI with <np-otp app="mazebank">.

Codes tab ​

Codes tab

One row per enrolled app with the current code and a ring for the seconds left (orange in the last 5 s). Tap a code to copy it. The codes are computed on the server and fetched again when they roll over. A 2FA unlock is logged (np_pw_log, lock id otp:<app>) and fires np_passwords:unlocked with method otp (typed) or autofill.

Configuration ​

Option Default Description
Config.Otp.period 30 Seconds per code
Config.Otp.sms true SMS on enrolment (editable in np_admin as 2FA SMS)
Config.Otp.smsFrom 'Verify' Sender name of the SMS
Config.Otp.apps Fleeca Bank Apps registered at start