Skip to content

Commit aae93a3

Browse files
author
gormanfletcher
committed
Parse Server 6641: document the new /loginAs REST endpoint
1 parent 589b58d commit aae93a3

File tree

2 files changed

+59
-11
lines changed

2 files changed

+59
-11
lines changed

_includes/rest/users.md

+58-10
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ There are three `emailVerified` states to consider:
111111
2. `false` - at the time the `User` object was last refreshed, the user had not confirmed his or her email address. If `emailVerified` is `false`, consider refreshing the `User` object.
112112
3. _missing_ - the `User` was created when email verification was off or the `User` does not have an `email`.
113113

114-
You can request a verification email to be sent by sending a POST request to <code class="highlighter-rouge"><span class="custom-parse-server-mount">/parse/</span>verificationEmailRequest</code> with `email` in the body of the request:
114+
You can request a verification email to be sent by sending a POST request to <code class="highlighter-rouge"><span class="custom-parse-server-mount">/parse/</span>verificationEmailRequest</code> with `email` in the body of the request:
115115

116116
<div class="language-toggle">
117117
<pre><code class="bash">
@@ -171,7 +171,6 @@ print result
171171

172172
If successful, the response body is an empty JSON object.
173173

174-
175174
## Retrieving Users
176175

177176
You can also retrieve the contents of a user object by sending a GET request to the URL returned in the location header when it was created. For example, to retrieve the user created above:
@@ -234,7 +233,7 @@ print result
234233
</code></pre>
235234
</div>
236235

237-
The response matches the JSON object above for retrieving users. If the session token is not valid, an error object is returned:
236+
The response matches the JSON object above for retrieving users. If the session token is not valid, an error object is returned:
238237

239238
```json
240239
{
@@ -335,7 +334,6 @@ The return value is a JSON object that contains a `results` field with a JSON ar
335334

336335
All of the options for queries that work for regular objects also work for user objects, so check the section on [Querying Objects](#basic-queries) for more details.
337336

338-
339337
## Deleting Users
340338

341339
To delete a user from the Parse Cloud, send a DELETE request to its URL. You must provide the `X-Parse-Session-Token` header to authenticate. For example:
@@ -366,7 +364,7 @@ print result
366364

367365
Parse allows you to link your users with services like Twitter and Facebook, enabling your users to sign up or log into your application using their existing identities. This is accomplished through the sign-up and update REST endpoints by providing authentication data for the service you wish to link to a user in the `authData` field. Once your user is associated with a service, the `authData` for the service will be stored with the user and is retrievable by logging in.
368366

369-
`authData` is a JSON object with keys for each linked service containing the data below. In each case, you are responsible for completing the authentication flow (e.g. OAuth 1.0a) to obtain the information the the service requires for linking.
367+
`authData` is a JSON object with keys for each linked service containing the data below. In each case, you are responsible for completing the authentication flow (e.g. OAuth 1.0a) to obtain the information the the service requires for linking.
370368

371369
### Facebook `authData`
372370

@@ -411,7 +409,7 @@ Learn more about [Twitter login](https://dev.twitter.com/docs/auth/implementing-
411409

412410
### Signing Up and Logging In
413411

414-
Signing a user up with a linked service and logging them in with that service uses the same POST request, in which the `authData` for the user is specified. For example, to sign up or log in with a user's Twitter account:
412+
Signing a user up with a linked service and logging them in with that service uses the same POST request, in which the `authData` for the user is specified. For example, to sign up or log in with a user's Twitter account:
415413

416414
<div class="language-toggle">
417415
<pre><code class="bash">
@@ -460,7 +458,7 @@ print result
460458
</code></pre>
461459
</div>
462460

463-
Parse then verifies that the provided `authData` is valid and checks to see if a user is already associated with this data. If so, it returns a status code of `200 OK` and the details (including a `sessionToken` for the user):
461+
Parse then verifies that the provided `authData` is valid and checks to see if a user is already associated with this data. If so, it returns a status code of `200 OK` and the details (including a `sessionToken` for the user):
464462

465463
<pre><code class="javascript">
466464
Status: 200 OK
@@ -496,7 +494,7 @@ Status: 201 Created
496494
Location: <span class="custom-parse-server-protocol">https</span>://<span class="custom-parse-server-url">YOUR.PARSE-SERVER.HERE</span><span class="custom-parse-server-mount">/parse/</span>users/uMz0YZeAqc
497495
</code></pre>
498496

499-
The body of the response will contain the `objectId`, `createdAt`, `sessionToken`, and an automatically-generated unique `username`. For example:
497+
The body of the response will contain the `objectId`, `createdAt`, `sessionToken`, and an automatically-generated unique `username`. For example:
500498

501499
```json
502500
{
@@ -509,7 +507,7 @@ The body of the response will contain the `objectId`, `createdAt`, `sessionToken
509507

510508
### Linking
511509

512-
Linking an existing user with a service like Facebook or Twitter uses a PUT request to associate `authData` with the user. For example, linking a user with a Facebook account would use a request like this:
510+
Linking an existing user with a service like Facebook or Twitter uses a PUT request to associate `authData` with the user. For example, linking a user with a Facebook account would use a request like this:
513511

514512
<div class="language-toggle">
515513
<pre><code class="bash">
@@ -556,7 +554,7 @@ After linking your user to a service, you can authenticate them using matching `
556554

557555
### Unlinking
558556

559-
Unlinking an existing user with a service also uses a PUT request to clear `authData` from the user by setting the `authData` for the service to `null`. For example, unlinking a user with a Facebook account would use a request like this:
557+
Unlinking an existing user with a service also uses a PUT request to clear `authData` from the user by setting the `authData` for the service to `null`. For example, unlinking a user with a Facebook account would use a request like this:
560558

561559
<div class="language-toggle">
562560
<pre><code class="bash">
@@ -613,3 +611,53 @@ For example, if you want the user with id `"3KmCvT7Zsb"` to have read and write
613611
```
614612

615613
If you want to access your data ignoring all ACLs, you can use the master key provided on the Dashboard. Instead of the `X-Parse-REST-API-Key` header, set the `X-Parse-Master-Key` header. For backward compatibility, you can also do master-level authentication using HTTP Basic Auth, passing the application id as the username and the master key as the password. For security, the master key should not be distributed to end users, but if you are running code in a trusted environment, feel free to use the master key for authentication.
614+
615+
## Impersonating a user
616+
617+
An application may have use cases where an operator or automated system needs to
618+
take action on a user's behalf, under the user's authority, without access to
619+
the user's credentials. The Parse REST API supports these use cases with the
620+
`/loginAs` endpoint. This endpoint takes a `userId` parameter, and must be
621+
called using the master key. It will create a session for the given user ID, and
622+
will return the same response format as the `/login` endpoint. The new session
623+
will have a `createdWith` value of `create`, and an `authProvider` value of
624+
`override`.
625+
626+
**The `/loginAs` endpoint performs no identity verification** - any client with
627+
the master key can log in as any user. You are strongly encouraged to apply
628+
careful security around any systems that call the `/loginAs` endpoint.
629+
630+
<div class="language-toggle">
631+
<pre><code class="bash">
632+
curl -X GET \
633+
-H "X-Parse-Application-Id: <span class="custom-parse-server-appid">${APPLICATION_ID}</span>" \
634+
-H "X-Parse-REST-API-Key: <span class="custom-parse-server-restapikey">${REST_API_KEY}</span>" \
635+
-H "X-Parse-Master-Key: ${MASTER_KEY}" \
636+
-H "X-Parse-Revocable-Session: 1" \
637+
-G \
638+
--data-urlencode 'userId=abc123' \
639+
<span class="custom-parse-server-protocol">https</span>://<span class="custom-parse-server-url">YOUR.PARSE-SERVER.HERE</span><span class="custom-parse-server-mount">/parse/</span>loginAs
640+
</code></pre>
641+
<pre><code class="python">
642+
import json,httplib,urllib
643+
connection = httplib.HTTPSConnection('<span class="custom-parse-server-url">YOUR.PARSE-SERVER.HERE</span>', 443)
644+
params = urllib.urlencode({"userId":"abc123"})
645+
connection.connect()
646+
connection.request('GET', '<span class="custom-parse-server-mount">/parse/</span>loginAs?%s' % params, '', {
647+
"X-Parse-Application-Id": "<span class="custom-parse-server-appid">${APPLICATION_ID}</span>",
648+
"X-Parse-REST-API-Key": "<span class="custom-parse-server-restapikey">${REST_API_KEY}</span>",
649+
"X-Parse-Master-Key": "${MASTER_KEY}",
650+
"X-Parse-Revocable-Session": "1"
651+
})
652+
result = json.loads(connection.getresponse().read())
653+
print result
654+
</code></pre>
655+
</div>
656+
657+
At this time, the `/loginAs` endpoint does not run the `beforeLogin` or
658+
`afterLogin` hooks that would be invoked when calling the `/login` endpoint.
659+
660+
Since the master key must always be supplied with any request to `/loginAs`,
661+
this action will always succeed if the supplied user id exists in the database.
662+
The endpoint does not honor account lockouts - developers must check for that
663+
manually if appropriate for their use case.

assets/js/bundle.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)