Module: Parse::MFA::UserExtension
- Extended by:
- ActiveSupport::Concern
- Included in:
- User
- Defined in:
- lib/parse/two_factor_auth/user_extension.rb
Overview
User extension module that adds MFA capabilities to Parse::User.
This module integrates with Parse Server's built-in MFA adapter, which stores MFA data in the user's authData.mfa field.
== Parse Server Configuration Required
Your Parse Server must have MFA enabled in the auth configuration:
{ auth: { mfa: { enabled: true, options: ["TOTP"], digits: 6, period: 30, algorithm: "SHA1" } } }
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#confirm_sms_mfa!(mobile:, token:) ⇒ Boolean
Confirm SMS MFA setup with the received code.
-
#disable_mfa!(current_token:) ⇒ Boolean
Disable MFA for this user.
-
#disable_mfa_admin!(*args, **kwargs) ⇒ Object
deprecated
Deprecated.
Use #disable_mfa_master_key! with an explicit
authorized_by:argument. The old name had no authorization gate and acted as a one-call IDOR primitive when invoked on an attacker-controlled user instance. -
#disable_mfa_master_key!(authorized_by:, admin_role: nil, allow_unverified: false) ⇒ Boolean
Disable MFA using the configured master key.
-
#login_with_mfa!(password, mfa_token = nil) ⇒ Boolean
Login this user instance with password and MFA token.
-
#mfa_enabled? ⇒ Boolean
Check if MFA is enabled for this user.
-
#mfa_provisioning_uri(secret, issuer: nil) ⇒ String
Generate a provisioning URI for this user.
-
#mfa_qr_code(secret, issuer: nil, format: :svg) ⇒ String
Generate a QR code for MFA setup.
-
#mfa_status ⇒ Symbol
Get the MFA status for this user.
-
#setup_mfa!(secret:, token:) ⇒ String?
Setup TOTP-based MFA for this user.
-
#setup_sms_mfa!(mobile:) ⇒ Boolean
Setup SMS-based MFA for this user.
Instance Method Details
#confirm_sms_mfa!(mobile:, token:) ⇒ Boolean
Confirm SMS MFA setup with the received code.
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/parse/two_factor_auth/user_extension.rb', line 254 def confirm_sms_mfa!(mobile:, token:) raise ArgumentError, "Mobile number is required" if mobile.blank? raise ArgumentError, "Token is required" if token.blank? # Use Parse::Phone for validation phone = mobile.is_a?(Parse::Phone) ? mobile : Parse::Phone.new(mobile) unless phone.valid? raise ArgumentError, "Invalid mobile number format. Must be E.164 format: +[country code][number] (e.g., +14155551234)" end mobile = phone.to_s # Use normalized E.164 format auth_data_payload = { mfa: { mobile: mobile, token: token, }, } response = client.update_user(id, { authData: auth_data_payload }, session_token: session_token) if response.error? if response.result.to_s.include?("Invalid MFA token") raise MFA::VerificationError, response.result.to_s end raise Parse::Client::ResponseError, response end # Refresh auth_data fetch true end |
#disable_mfa!(current_token:) ⇒ Boolean
Disable MFA for this user.
This requires a valid current MFA token (TOTP or recovery code) to verify the user's identity before disabling MFA.
The code check is performed by this SDK method, not by Parse Server.
The SDK first submits the code to confirm it is accepted, then sends
a separate authData: { mfa: nil } update. Parse Server accepts that
second update from any holder of the session token, with or without
the first step, so this method is not an enforcement boundary.
Protect MFA removal server-side, for example with a beforeSave
trigger on _User or a Cloud Code function that requires a fresh code
verified on the server. See Parse::MFA for the enforcement boundary.
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/parse/two_factor_auth/user_extension.rb', line 309 def disable_mfa!(current_token:) raise MFA::NotEnabledError, "MFA is not enabled for this user" unless mfa_enabled? raise ArgumentError, "Current token is required" if current_token.blank? # Parse Server's TOTP adapter exposes no first-class "disable via authData # update" path — its validateUpdate always re-runs setup, so a partial # mfa payload is rejected outright. Disabling is therefore a two-step: # # 1. Prove possession of the current code by submitting it as # `{ mfa: { old: <token> } }`. In the *update* context (unlike a # fresh login) the adapter validates that code against the stored # secret. A WRONG code fails at validateLogin ("Invalid MFA token"); # a CORRECT code passes validateLogin and is then blocked by the # re-setup requirement ("Invalid MFA data") — which is precisely the # signal that the code was accepted. (This re-entry of the current # code is the deliberate confirmation gate for turning MFA off.) # 2. Disable MFA by unlinking the provider with `{ mfa: nil }`. # # Step 1 is a check made by this SDK method only. Parse Server does # not link the two requests, so a session-token holder can send step 2 # directly; server-side protection is required (see YARD above). verify = client.update_user(id, { authData: { mfa: { old: current_token } } }, session_token: session_token) # Classify the two-step response POSITIVELY instead of treating # "anything that isn't success-or-one-magic-string" as a bad # token. The current code is ACCEPTED iff the server either # succeeds or rejects only the follow-on re-setup ("Invalid MFA # data") — that block fires AFTER validateLogin has already # accepted the code. A WRONG code fails earlier at validateLogin # ("Invalid MFA token"). Any OTHER error (transport, session, 5xx) # is a real fault surfaced as-is, not mislabeled a verification # failure. err = verify.error.to_s code_rejected = err.match?(/Invalid MFA token/i) code_accepted = verify.success? || err.match?(/Invalid MFA data/i) if code_rejected raise MFA::VerificationError, "Invalid MFA token" elsif !code_accepted raise Parse::Client::ResponseError, verify end response = client.update_user(id, { authData: { mfa: nil } }, session_token: session_token) raise Parse::Client::ResponseError, response if response.error? # CONFIRM the disable took effect from the SERVER's own view — a # positive post-condition rather than trusting the unlink response # alone. We must read the server directly here, NOT lean on the # in-memory #mfa_enabled? projection: Parse Server omits `authData` # entirely for a user with no providers, so once MFA is unlinked an # ordinary fetch carries no `authData` key at all and therefore can # never clear the `{ mfa: { status: "enabled" } }` value pinned at # enrollment. An enabled account's own (session-token) read returns # `authData.mfa`; a disabled one omits it — so an absent/mfa-less # authData on this trusted self-read is the authoritative signal. if mfa_enabled_on_server? raise MFA::VerificationError, "MFA disable did not take effect (still enabled after unlink)" end clear_local_mfa_projection! true end |
#disable_mfa_admin!(*args, **kwargs) ⇒ Object
Use #disable_mfa_master_key! with an explicit
authorized_by: argument. The old name had no authorization gate
and acted as a one-call IDOR primitive when invoked on an
attacker-controlled user instance.
474 475 476 477 478 |
# File 'lib/parse/two_factor_auth/user_extension.rb', line 474 def disable_mfa_admin!(*args, **kwargs) warn "[DEPRECATION] `disable_mfa_admin!` is deprecated; use " \ "`disable_mfa_master_key!(authorized_by: <admin user>)`." disable_mfa_master_key!(*args, **kwargs) end |
#disable_mfa_master_key!(authorized_by:, admin_role: nil, allow_unverified: false) ⇒ Boolean
Disable MFA using the configured master key. This bypasses MFA verification entirely, so the caller must prove (out-of-band) that the operator initiating the disable is authorized to do so.
The authorized_by: keyword is required and must be a
User (or Pointer to a User) representing the
operator performing the override. The caller is responsible for
verifying that operator's privileges (e.g. via a role check). An
optional admin_role: argument lets this method enforce a role
membership check on the operator using the existing role-hierarchy
support; when given, the operator must belong to the role (or any
of its child roles) or ForbiddenError is raised.
authorized_by: must be the authenticated operator, resolved from the
caller's own verified session (for example the current admin user of
the request after authentication). Never build it from an id or
pointer supplied in the request body or parameters; the role check
would then validate whichever user the requester names.
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 |
# File 'lib/parse/two_factor_auth/user_extension.rb', line 412 def disable_mfa_master_key!(authorized_by:, admin_role: nil, allow_unverified: false) operator = unless operator.is_a?(Parse::User) || (operator.is_a?(Parse::Pointer) && operator.parse_class == Parse::User.parse_class) raise ArgumentError, "disable_mfa_master_key! requires authorized_by: to be a Parse::User " \ "or Parse::Pointer to a User (got #{operator.class})" end if operator.respond_to?(:id) && operator.id.blank? raise ArgumentError, "authorized_by: User must be persisted (have an objectId)" end # FAIL CLOSED: this method bypasses MFA verification entirely via # the master key, so it refuses to run without SOME authorization # signal. Either supply an `admin_role:` for the library to verify, # or pass `allow_unverified: true` to deliberately assert that the # caller has already authorized the operator out-of-band. if admin_role.nil? && !allow_unverified raise MFA::ForbiddenError, "disable_mfa_master_key! refuses to bypass MFA without an authorization " \ "check: pass admin_role: to enforce role membership, or " \ "allow_unverified: true to explicitly accept caller-side authorization." end if admin_role role = admin_role.is_a?(Parse::Role) ? admin_role : Parse::Role.find_by_name(admin_role.to_s) if role.nil? raise MFA::ForbiddenError, "authorized_by user is not authorized: admin role " \ "#{admin_role.inspect} not found" end operator_id = operator.id = role.all_users.any? { |u| u.id == operator_id } unless raise MFA::ForbiddenError, "authorized_by user #{operator_id} is not a member of " \ "role #{role.name.inspect}" end end auth_data_payload = { mfa: nil } response = client.update_user(id, { authData: auth_data_payload }, use_master_key: true) if response.error? raise Parse::Client::ResponseError, response end # Refresh auth_data, then drop the in-memory MFA projection. As in # #disable_mfa!, a disabled user's read omits `authData`, so the # `{ mfa: { status: "enabled" } }` value pinned at enrollment won't # self-clear on fetch — clear it explicitly so #mfa_enabled? reports # the truth after a master-key disable. fetch clear_local_mfa_projection! true end |
#login_with_mfa!(password, mfa_token = nil) ⇒ Boolean
Login this user instance with password and MFA token.
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 |
# File 'lib/parse/two_factor_auth/user_extension.rb', line 491 def login_with_mfa!(password, mfa_token = nil) raise MFA::RequiredError, "MFA token is required for this account" if mfa_token.blank? response = client.login_with_mfa(username.to_s, password.to_s, mfa_token) unless response.success? raise MFA::VerificationError, response.error.to_s if MFA.invalid_token_response?(response) return false end # Self-fetch trust: the login response is this user's own row. self.class.with_authdata_trust { apply_attributes!(response.result) } session_token.present? rescue Parse::Client::ResponseError => e if e..include?("Missing additional authData") raise MFA::RequiredError, "MFA token is required for this account" elsif e..include?("Invalid MFA token") raise MFA::VerificationError, e. end raise end |
#mfa_enabled? ⇒ Boolean
Check if MFA is enabled for this user.
114 115 116 117 118 119 120 121 |
# File 'lib/parse/two_factor_auth/user_extension.rb', line 114 def mfa_enabled? return false unless auth_data.is_a?(Hash) return false unless auth_data["mfa"].is_a?(Hash) # Parse Server's afterFind returns { status: "enabled" } for enabled MFA mfa_data = auth_data["mfa"] mfa_data["status"] == "enabled" || mfa_data["secret"].present? || mfa_data["mobile"].present? end |
#mfa_provisioning_uri(secret, issuer: nil) ⇒ String
Generate a provisioning URI for this user.
Use this to create a QR code for the user to scan with their authenticator app.
522 523 524 525 |
# File 'lib/parse/two_factor_auth/user_extension.rb', line 522 def mfa_provisioning_uri(secret, issuer: nil) account_name = email.presence || username.presence || id MFA.provisioning_uri(secret, account_name, issuer: issuer) end |
#mfa_qr_code(secret, issuer: nil, format: :svg) ⇒ String
Generate a QR code for MFA setup.
538 539 540 541 |
# File 'lib/parse/two_factor_auth/user_extension.rb', line 538 def mfa_qr_code(secret, issuer: nil, format: :svg) account_name = email.presence || username.presence || id MFA.qr_code(secret, account_name, issuer: issuer, format: format) end |
#mfa_status ⇒ Symbol
Get the MFA status for this user.
126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/parse/two_factor_auth/user_extension.rb', line 126 def mfa_status return :unknown unless auth_data.is_a?(Hash) return :disabled unless auth_data["mfa"].is_a?(Hash) mfa_data = auth_data["mfa"] if mfa_data["status"] mfa_data["status"].to_sym elsif mfa_data["secret"].present? || mfa_data["mobile"].present? :enabled else :disabled end end |
#setup_mfa!(secret:, token:) ⇒ String?
Setup TOTP-based MFA for this user.
This sends the secret and verification token to Parse Server, which validates the TOTP and stores the secret securely.
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/parse/two_factor_auth/user_extension.rb', line 159 def setup_mfa!(secret:, token:) raise ArgumentError, "Secret is required" if secret.blank? raise ArgumentError, "Token is required" if token.blank? # Refresh authData from the server before gating on mfa_enabled? # so a stale in-memory user does not bypass the local guard. This # narrows the race window from "any time the user object is alive" # to "one round-trip" — it does not eliminate TOCTOU. Full # elimination requires the Parse Server MFA adapter to reject # re-setup when authData.mfa.status == "enabled". fetch if id.present? raise MFA::AlreadyEnabledError if mfa_enabled? # Validate secret length (Parse Server requires minimum 20 chars) if secret.length < 20 raise ArgumentError, "Secret must be at least 20 characters (got #{secret.length})" end auth_data_payload = { mfa: { secret: secret, token: token, }, } response = client.update_user(id, { authData: auth_data_payload }, session_token: session_token) if response.error? if response.result.to_s.include?("Invalid MFA") raise MFA::VerificationError, response.result.to_s end raise Parse::Client::ResponseError, response end # Parse Server returns recovery codes in the response recovery = response.result["recovery"] || response.result["authDataResponse"]&.dig("mfa", "recovery") # Refresh auth_data fetch recovery end |
#setup_sms_mfa!(mobile:) ⇒ Boolean
Setup SMS-based MFA for this user.
This initiates SMS MFA setup by registering the mobile number. Parse Server will send an SMS with a verification code.
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/parse/two_factor_auth/user_extension.rb', line 213 def setup_sms_mfa!(mobile:) raise ArgumentError, "Mobile number is required" if mobile.blank? # Use Parse::Phone for validation phone = mobile.is_a?(Parse::Phone) ? mobile : Parse::Phone.new(mobile) unless phone.valid? raise ArgumentError, "Invalid mobile number format. Must be E.164 format: +[country code][number] (e.g., +14155551234)" end mobile = phone.to_s # Use normalized E.164 format # Same TOCTOU narrowing as #setup_mfa!: refresh authData before # the guard so a stale in-memory user cannot bypass the check. # See #setup_mfa! for the residual-risk caveat. fetch if id.present? raise MFA::AlreadyEnabledError if mfa_enabled? auth_data_payload = { mfa: { mobile: mobile, }, } response = client.update_user(id, { authData: auth_data_payload }, session_token: session_token) if response.error? raise Parse::Client::ResponseError, response end true end |