Module: Parse::API::Users
- Included in:
- Client
- Defined in:
- lib/parse/api/users.rb
Overview
Defines the User class interface for the Parse REST API
Instance Method Summary collapse
-
#create_user(body, headers: {}, **opts) ⇒ Parse::Response
Create a new user.
-
#current_user(session_token, headers: {}, **opts) ⇒ Parse::Response
Find user matching this active session token.
-
#delete_user(id, headers: {}, **opts) ⇒ Parse::Response
Delete a User record given an objectId.
-
#fetch_user(id, headers: {}, **opts) ⇒ Parse::Response
Fetch a User for a given objectId.
-
#find_users(query = {}, headers: {}, **opts) ⇒ Parse::Response
Find users matching a set of constraints.
-
#login(username, password, headers: {}, **opts) ⇒ Parse::Response
Login a user.
-
#login_with_mfa(username, password, mfa_token, headers: {}, **opts) ⇒ Parse::Response
Login a user with MFA (Multi-Factor Authentication).
-
#logout(session_token, headers: {}, **opts) ⇒ Parse::Response
Logout a user by deleting the associated session.
-
#request_email_verification(email, headers: {}, **opts) ⇒ Parse::Response
Request that Parse Server (re)send the email-address verification email for a registered, not-yet-verified user.
-
#request_password_reset(email, headers: {}, **opts) ⇒ Parse::Response
Request a password reset for a registered email.
-
#set_service_auth_data(id, service_name, auth_data, headers: {}, **opts) ⇒ Parse::Response
Set the authentication service OAUth data for a user.
-
#signup(username, password, email = nil, body: {}, **opts) ⇒ Parse::Response
Signup a user given a username, password and, optionally, their email.
-
#update_user(id, body = {}, headers: {}, **opts) ⇒ Parse::Response
Update a User record given an objectId.
-
#verify_password(username, password, headers: {}, **opts) ⇒ Parse::Response
Verify a user's credentials against Parse Server without minting a session.
Instance Method Details
#create_user(body, headers: {}, **opts) ⇒ Parse::Response
Create a new user.
Sent WITHOUT the master key unless the caller passes
use_master_key: true. Parse Server does not mint a session token for
a master-key signup, so a master-keyed client used to get back a user
with no sessionToken (and User#upgrade_anonymous! then
failed). A master-key create also lets the request through _User
create CLPs and authData checks that a real signup must pass.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/parse/api/users.rb', line 75 def create_user(body, headers: {}, **opts) opts = opts.merge(use_master_key: false) unless opts[:use_master_key] == true headers = headers.merge({ Parse::Protocol::REVOCABLE_SESSION => "1" }) if opts[:session_token].present? headers = headers.merge({ Parse::Protocol::SESSION_TOKEN => opts[:session_token] }) elsif opts[:use_master_key] != true # No token given: send none, rather than letting an ambient # `with_session` token or a client-bound token make the caller the # `request.user` of someone else's signup. opts = opts.merge(session_token: "") end response = request :post, USER_PATH_PREFIX, body: body, headers: headers, opts: opts response.parse_class = Parse::Model::CLASS_USER response end |
#current_user(session_token, headers: {}, **opts) ⇒ Parse::Response
Find user matching this active session token.
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/parse/api/users.rb', line 49 def current_user(session_token, headers: {}, **opts) # The token argument is the whole point of this call, so it is passed # as the explicit per-call token: an ambient `Parse.with_session` # token or a client-bound token must never replace it (that resolved # the wrong user and poisoned the identity cache). A caller-supplied # `session_token:` in opts is ignored for the same reason. session_token = session_token.session_token if session_token.respond_to?(:session_token) opts = opts.merge(session_token: session_token.to_s, use_master_key: false) headers = headers.merge({ Parse::Protocol::SESSION_TOKEN => session_token.to_s }) response = request :get, "#{USER_PATH_PREFIX}/me", headers: headers, opts: opts response.parse_class = Parse::Model::CLASS_USER response end |
#delete_user(id, headers: {}, **opts) ⇒ Parse::Response
Delete a User record given an objectId.
126 127 128 129 130 131 |
# File 'lib/parse/api/users.rb', line 126 def delete_user(id, headers: {}, **opts) id = Parse::API::PathSegment.object_id!(id) response = request :delete, "#{USER_PATH_PREFIX}/#{id}", headers: headers, opts: opts invalidate_user_identity(id) if response.success? response end |
#fetch_user(id, headers: {}, **opts) ⇒ Parse::Response
Fetch a User for a given objectId.
28 29 30 31 |
# File 'lib/parse/api/users.rb', line 28 def fetch_user(id, headers: {}, **opts) id = Parse::API::PathSegment.object_id!(id) request :get, "#{USER_PATH_PREFIX}/#{id}", headers: headers, opts: opts end |
#find_users(query = {}, headers: {}, **opts) ⇒ Parse::Response
Find users matching a set of constraints.
38 39 40 41 42 |
# File 'lib/parse/api/users.rb', line 38 def find_users(query = {}, headers: {}, **opts) response = request :get, USER_PATH_PREFIX, query: query, headers: headers, opts: opts response.parse_class = Parse::Model::CLASS_USER response end |
#login(username, password, headers: {}, **opts) ⇒ Parse::Response
Login a user. Implements client-side rate limiting with exponential backoff after repeated failures to mitigate brute force attacks. Always sent without the master key and without any ambient or bound session token (see #unauthenticated_opts). A master-key login makes Parse Server skip the additional MFA check and SAVE the submitted authData, so it must never be sent from a master-keyed client.
198 199 200 201 202 203 204 205 206 |
# File 'lib/parse/api/users.rb', line 198 def login(username, password, headers: {}, **opts) check_login_rate_limit!(username) body = { username: username, password: password } headers = headers.merge({ Parse::Protocol::REVOCABLE_SESSION => "1" }) response = request :post, LOGIN_PATH, body: body, headers: headers, opts: unauthenticated_opts(opts) response.parse_class = Parse::Model::CLASS_USER track_login_attempt(username, response.success?) response end |
#login_with_mfa(username, password, mfa_token, headers: {}, **opts) ⇒ Parse::Response
Login a user with MFA (Multi-Factor Authentication).
This method handles Parse Server's MFA adapter which requires both standard credentials AND an MFA token when MFA is enabled for the user.
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/api/users.rb', line 222 def login_with_mfa(username, password, mfa_token, headers: {}, **opts) check_login_rate_limit!(username) # Parse Server expects authData to be sent with POST for MFA login body = { username: username, password: password, authData: { mfa: { token: mfa_token, }, }, } # Never with the master key: with it, Parse Server skips the MFA # validation and stores the submitted `authData.mfa` over the # account's enrolled TOTP secret, so any code "verifies" and MFA is # silently broken for the account from then on. headers = headers.merge({ Parse::Protocol::REVOCABLE_SESSION => "1" }) response = request :post, LOGIN_PATH, body: body, headers: headers, opts: unauthenticated_opts(opts) response.parse_class = Parse::Model::CLASS_USER track_login_attempt(username, response.success?) response end |
#logout(session_token, headers: {}, **opts) ⇒ Parse::Response
Logout a user by deleting the associated session.
291 292 293 294 295 296 297 298 299 300 |
# File 'lib/parse/api/users.rb', line 291 def logout(session_token, headers: {}, **opts) session_token = session_token.session_token if session_token.respond_to?(:session_token) headers = headers.merge({ Parse::Protocol::SESSION_TOKEN => session_token }) opts = opts.merge({ use_master_key: false, session_token: session_token }) response = request :post, LOGOUT_PATH, headers: headers, opts: opts # Forget the token in this client's identity plane so mongo-direct # reads see the revocation now rather than when the entry expires. invalidate_session_identity(session_token) if response.success? response end |
#request_email_verification(email, headers: {}, **opts) ⇒ Parse::Response
Request that Parse Server (re)send the email-address verification email
for a registered, not-yet-verified user. Requires the server to have an
email adapter and verifyUserEmails enabled; otherwise Parse Server
responds with an error. Rate-limited per email like password reset.
175 176 177 178 179 180 181 182 183 184 |
# File 'lib/parse/api/users.rb', line 175 def request_email_verification(email, headers: {}, **opts) rate_key = "emailverify:#{email}" check_login_rate_limit!(rate_key) body = { email: email } response = request :post, VERIFICATION_EMAIL_REQUEST, body: body, opts: unauthenticated_opts(opts), headers: headers # Indistinguishable found/not-found response, like password reset — count # every attempt toward backoff so probing can't reset the counter. track_login_attempt(rate_key, false) response end |
#request_password_reset(email, headers: {}, **opts) ⇒ Parse::Response
Request a password reset for a registered email.
Client-side rate limited on a per-email basis using the same
tracker that backs #login (entries are namespaced under a
pwreset: prefix so the two limiters don't collide on usernames
that happen to equal an email). Every request counts toward the
backoff — Parse Server's requestPasswordReset response does
not differentiate "email exists" from "email does not exist"
(and rightly so, to avoid account enumeration), so the SDK
cannot distinguish a legitimate retry from an attacker probing
for valid emails. The cap mirrors LOGIN_MAX_FAILURES: 5
requests within the rolling window before exponential backoff
kicks in and the limit clears via the same TTL-based cleanup.
152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/parse/api/users.rb', line 152 def request_password_reset(email, headers: {}, **opts) rate_key = "pwreset:#{email}" check_login_rate_limit!(rate_key) body = { email: email } response = request :post, REQUEST_PASSWORD_RESET, body: body, opts: unauthenticated_opts(opts), headers: headers # Always count the attempt as a "failure" for backoff purposes: # the response body is intentionally indistinguishable across # found/not-found emails, so we cannot reset the counter on # "success" without leaking that distinction to an attacker who # is probing. track_login_attempt(rate_key, false) response end |
#set_service_auth_data(id, service_name, auth_data, headers: {}, **opts) ⇒ Parse::Response
Set the authentication service OAUth data for a user. Deleting or unlinking is done by setting the authData of the service name to nil.
116 117 118 119 |
# File 'lib/parse/api/users.rb', line 116 def set_service_auth_data(id, service_name, auth_data, headers: {}, **opts) body = { authData: { service_name => auth_data } } update_user(id, body, headers: headers, **opts) end |
#signup(username, password, email = nil, body: {}, **opts) ⇒ Parse::Response
Signup a user given a username, password and, optionally, their email.
309 310 311 312 313 |
# File 'lib/parse/api/users.rb', line 309 def signup(username, password, email = nil, body: {}, **opts) body = body.merge({ username: username, password: password }) body[:email] = email || body[:email] create_user(body, **opts) end |
#update_user(id, body = {}, headers: {}, **opts) ⇒ Parse::Response
Update a User record given an objectId.
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/parse/api/users.rb', line 97 def update_user(id, body = {}, headers: {}, **opts) id = Parse::API::PathSegment.object_id!(id) response = request :put, "#{USER_PATH_PREFIX}/#{id}", body: body, headers: headers, opts: opts response.parse_class = Parse::Model::CLASS_USER # A password change revokes the user's other sessions server-side. if response.success? && body.is_a?(Hash) && (body.key?(:password) || body.key?("password")) invalidate_user_identity(id) end response end |
#verify_password(username, password, headers: {}, **opts) ⇒ Parse::Response
Verify a user's credentials against Parse Server without minting a session. This is the canonical step-up / re-authentication primitive: it confirms that the username + password combination is correct without producing a new session token on success.
Uses the POST /parse/verifyPassword endpoint (credentials in the request
BODY, mirroring login) rather than the GET form. Parse Server accepts
both (same handler, neither master-key gated; the POST variant landed in
7.1.0), but POST keeps the plaintext password out of the URL — and
therefore out of server access logs, reverse-proxy logs, the Referer
header, and the SDK's URL-keyed response cache.
On success Parse Server returns the user object (HTTP 200) with the same
shape as a login response (minus sessionToken). On failure it returns a
4xx with an error body, most commonly:
- code 101 (
ERROR_OBJECT_NOT_FOUND) for an unknown username or wrong password. - code 205 (
ERROR_EMAIL_NOT_FOUND) whenpreventLoginWithUnverifiedEmailis enabled and the account's email has not been verified.
Client-side rate limited per username using the SAME bucket as #login
(bare username, no namespace) — failures across both credential oracles
accumulate, so an attacker cannot bypass a login lockout by pivoting to
this endpoint. The trade-off: a run of failed step-up re-auth calls counts
toward (and can trigger) the primary login lockout for that username.
Client-side limiting is a convenience, not a boundary — the server is the
real control.
277 278 279 280 281 282 283 284 |
# File 'lib/parse/api/users.rb', line 277 def verify_password(username, password, headers: {}, **opts) check_login_rate_limit!(username) body = { username: username, password: password } response = request :post, VERIFY_PASSWORD_PATH, body: body, headers: headers, opts: unauthenticated_opts(opts) response.parse_class = Parse::Model::CLASS_USER track_login_attempt(username, response.success?) response end |