Module: Parse::MFA::UserExtension::ClassMethods
- Defined in:
- lib/parse/two_factor_auth/user_extension.rb
Overview
Class methods added to Parse::User
Instance Method Summary collapse
-
#login_with_mfa(username, password, mfa_token) ⇒ User?
Login a user with username, password, and MFA token.
-
#mfa_required?(username) ⇒ Boolean
Check if a user requires MFA for login.
Instance Method Details
#login_with_mfa(username, password, mfa_token) ⇒ User?
Login a user with username, password, and MFA token.
This method handles the Parse Server MFA "additional" policy, which requires both standard credentials AND an MFA token.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/parse/two_factor_auth/user_extension.rb', line 50 def login_with_mfa(username, password, mfa_token) # Validate only: the login body sends the username exactly as given. mfa_username!(username) raise MFA::RequiredError, "MFA token is required" if mfa_token.blank? # `client.login_with_mfa` always goes out without the master key # (see Parse::API::Users#login_with_mfa): with it, Parse Server # would skip this verification and overwrite the enrolled secret. response = client.login_with_mfa(username, password, mfa_token) unless response.success? raise MFA::VerificationError, response.error.to_s if MFA.invalid_token_response?(response) return nil end # Self-fetch trust: an MFA login returns the authenticating # user's own row, so authData here is legitimately theirs. Parse::User.with_authdata_trust { Parse::User.build(response.result) } rescue Parse::Client::ResponseError => e if e..include?("Invalid MFA token") || e..include?("Missing additional authData") raise MFA::VerificationError, e. end raise end |
#mfa_required?(username) ⇒ Boolean
Check if a user requires MFA for login.
This queries the user's authData.mfa status using the afterFind hook which returns { status: "enabled" } or { status: "disabled" }.
89 90 91 92 93 94 95 |
# File 'lib/parse/two_factor_auth/user_extension.rb', line 89 def mfa_required?(username) username = mfa_username!(username) user = where(username: username).first return false unless user user.mfa_enabled? end |