Module: Parse::MFA
- Defined in:
- lib/parse/two_factor_auth.rb,
lib/parse/two_factor_auth/user_extension.rb
Overview
Multi-Factor Authentication (MFA) support for Parse Server.
This module interfaces with Parse Server's built-in MFA adapter which supports TOTP (Time-based One-Time Password) and SMS-based authentication.
== Parse Server Configuration
MFA must be enabled in your Parse Server configuration:
{ auth: { mfa: { enabled: true, options: ["TOTP"], // or ["SMS", "TOTP"] digits: 6, period: 30, algorithm: "SHA1" } } }
== TOTP Setup Flow
- Generate a secret client-side using MFA.generate_secret
- Display QR code to user using MFA.provisioning_uri or MFA.qr_code
- User scans QR with authenticator app (Google Authenticator, Authy, etc.)
- User enters the 6-digit code from their app
- Call UserExtension#setup_mfa! with secret and token to enable MFA
- Store the recovery codes returned - user needs these for account recovery!
== Enforcement Boundary
MFA.verify, MFA.verify_timestamp and the SDK's self-disable flow (User#disable_mfa!) are client-side checks. They are not the enforcement boundary. Parse Server's MFA adapter enforces MFA at login, and that is the boundary.
Disabling MFA is an ordinary user update (authData: { mfa: nil }).
Anyone holding the user's session token can send that REST update
directly without going through the SDK. Protect it server-side, for
example with a beforeSave trigger on _User or a Cloud Code function
that refuses to clear authData.mfa unless a fresh code was verified on
the server for that request.
Defined Under Namespace
Modules: UserExtension Classes: AlreadyEnabledError, DependencyError, ForbiddenError, NotEnabledError, RequiredError, VerificationError
Constant Summary collapse
- DEFAULT_CONFIG =
Default configuration
{ issuer: "Parse App", digits: 6, period: 30, algorithm: "SHA1", secret_length: 20, # Minimum required by Parse Server }.freeze
Class Method Summary collapse
-
.build_login_auth_data(token:) ⇒ Hash
Build authData hash for MFA login.
-
.build_setup_auth_data(secret:, token:) ⇒ Hash
Build authData hash for MFA setup.
-
.build_sms_confirm_auth_data(mobile:, token:) ⇒ Hash
Build authData hash for SMS MFA confirmation.
-
.build_sms_setup_auth_data(mobile:) ⇒ Hash
Build authData hash for SMS MFA setup.
-
.config ⇒ Hash
Global MFA configuration.
-
.configure {|config| ... } ⇒ Object
Configure MFA settings.
-
.current_code(secret) ⇒ String
Get the current TOTP code (for testing/debugging).
-
.generate_secret(length: nil) ⇒ String
Generate a new TOTP secret for MFA setup.
-
.provisioning_uri(secret, account_name, issuer: nil) ⇒ String
Generate provisioning URI for authenticator apps.
-
.qr_code(secret, account_name, issuer: nil, format: :svg) ⇒ String
Generate a QR code for the authenticator app.
-
.rotp_available? ⇒ Boolean
Check if rotp gem is available.
-
.rqrcode_available? ⇒ Boolean
Check if rqrcode gem is available.
-
.totp(secret, issuer: nil) ⇒ ROTP::TOTP
Create a TOTP instance for verification.
-
.verify(secret, code, after: nil) ⇒ Boolean
Verify a TOTP code locally (for testing/validation before sending to server).
-
.verify_timestamp(secret, code, after: nil) ⇒ Integer?
Verify a TOTP code and return the start of the matching time step.
Class Method Details
.build_login_auth_data(token:) ⇒ Hash
Build authData hash for MFA login.
341 342 343 344 345 346 347 |
# File 'lib/parse/two_factor_auth.rb', line 341 def build_login_auth_data(token:) { mfa: { token: token, }, } end |
.build_setup_auth_data(secret:, token:) ⇒ Hash
Build authData hash for MFA setup.
328 329 330 331 332 333 334 335 |
# File 'lib/parse/two_factor_auth.rb', line 328 def build_setup_auth_data(secret:, token:) { mfa: { secret: secret, token: token, }, } end |
.build_sms_confirm_auth_data(mobile:, token:) ⇒ Hash
Build authData hash for SMS MFA confirmation.
366 367 368 369 370 371 372 373 |
# File 'lib/parse/two_factor_auth.rb', line 366 def build_sms_confirm_auth_data(mobile:, token:) { mfa: { mobile: mobile, token: token, }, } end |
.build_sms_setup_auth_data(mobile:) ⇒ Hash
Build authData hash for SMS MFA setup.
353 354 355 356 357 358 359 |
# File 'lib/parse/two_factor_auth.rb', line 353 def build_sms_setup_auth_data(mobile:) { mfa: { mobile: mobile, }, } end |
.config ⇒ Hash
Global MFA configuration
132 133 134 |
# File 'lib/parse/two_factor_auth.rb', line 132 def config @config ||= DEFAULT_CONFIG.dup end |
.configure {|config| ... } ⇒ Object
Configure MFA settings
142 143 144 145 |
# File 'lib/parse/two_factor_auth.rb', line 142 def configure yield config if block_given? config end |
.current_code(secret) ⇒ String
Get the current TOTP code (for testing/debugging).
270 271 272 273 |
# File 'lib/parse/two_factor_auth.rb', line 270 def current_code(secret) ensure_rotp! totp(secret).now end |
.generate_secret(length: nil) ⇒ String
Generate a new TOTP secret for MFA setup. The secret must be at least 20 characters (Parse Server requirement).
174 175 176 177 178 179 |
# File 'lib/parse/two_factor_auth.rb', line 174 def generate_secret(length: nil) ensure_rotp! length ||= config[:secret_length] length = [length, 20].max # Parse Server requires minimum 20 ROTP::Base32.random(length) end |
.provisioning_uri(secret, account_name, issuer: nil) ⇒ String
Generate provisioning URI for authenticator apps.
285 286 287 288 |
# File 'lib/parse/two_factor_auth.rb', line 285 def provisioning_uri(secret, account_name, issuer: nil) ensure_rotp! totp(secret, issuer: issuer).provisioning_uri(account_name) end |
.qr_code(secret, account_name, issuer: nil, format: :svg) ⇒ String
Generate a QR code for the authenticator app.
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/parse/two_factor_auth.rb', line 301 def qr_code(secret, account_name, issuer: nil, format: :svg) ensure_rqrcode! uri = provisioning_uri(secret, account_name, issuer: issuer) qr = RQRCode::QRCode.new(uri) case format when :svg qr.as_svg( color: "000", shape_rendering: "crispEdges", module_size: 4, standalone: true, ) when :png qr.as_png(size: 300) when :ascii qr.as_ansi else qr.as_svg end end |
.rotp_available? ⇒ Boolean
Check if rotp gem is available
149 150 151 152 153 154 |
# File 'lib/parse/two_factor_auth.rb', line 149 def rotp_available? require "rotp" true rescue LoadError false end |
.rqrcode_available? ⇒ Boolean
Check if rqrcode gem is available
158 159 160 161 162 163 |
# File 'lib/parse/two_factor_auth.rb', line 158 def rqrcode_available? require "rqrcode" true rescue LoadError false end |
.totp(secret, issuer: nil) ⇒ ROTP::TOTP
Create a TOTP instance for verification.
186 187 188 189 190 191 192 193 194 |
# File 'lib/parse/two_factor_auth.rb', line 186 def totp(secret, issuer: nil) ensure_rotp! ROTP::TOTP.new( secret, issuer: issuer || config[:issuer], interval: config[:period], digits: config[:digits], ) end |
.verify(secret, code, after: nil) ⇒ Boolean
Verify a TOTP code locally (for testing/validation before sending to server).
A code stays valid for its whole time step plus one step of drift on
each side, so without after: the same code verifies repeatedly for
roughly 90 seconds. That is fine for checking a code before sending it
to Parse Server, which tracks reuse itself. An application that uses
this method as its own second factor must store the timestamp of the
last accepted code per user (see verify_timestamp) and pass it back
as after: on the next attempt, or a captured code can be replayed.
Reading the stored timestamp, calling this method, and then writing
the new timestamp is not enough on its own: two concurrent requests
with the same code can both read the old value and both succeed.
Update the stored value atomically, as a compare-and-set keyed on the
previous timestamp (a conditional update that only matches when the
stored value still equals the one passed as after:), or record each
accepted step under a database unique constraint on user and time
step. Treat the code as accepted only when that write succeeds.
This method is a client-side check. Parse Server's MFA adapter, which enforces MFA at login, is the enforcement boundary (see Parse::MFA).
232 233 234 |
# File 'lib/parse/two_factor_auth.rb', line 232 def verify(secret, code, after: nil) !(secret, code, after: after).nil? end |
.verify_timestamp(secret, code, after: nil) ⇒ Integer?
Verify a TOTP code and return the start of the matching time step.
Store the returned value after a successful login and pass it as
after: on the next call to verify or verify_timestamp; codes from
that step or earlier are then refused. The store must be atomic
(compare-and-set on the previous value, or a unique constraint on user
and time step) so concurrent requests with the same code cannot both
succeed. See verify.
256 257 258 259 260 261 262 263 264 |
# File 'lib/parse/two_factor_auth.rb', line 256 def (secret, code, after: nil) return nil if secret.blank? || code.blank? ensure_rotp! after = normalize_after(after) drift_seconds = config[:period] totp(secret).verify(code.to_s, drift_behind: drift_seconds, drift_ahead: drift_seconds, after: after) end |