Edit Page

User Management

🔧 Configuration

Sets localhost:8080 with admin:secret
Values are saved in your browser

Introduction

This section provides instructions on how to create, update and delete users with the default authenticator mongoRealAuthenticator.

It also shows how to manage permissions with the default authorizer mongoAclAuthorizer.

Note
The mongoRealAuthenticator is configured to utilize the "/users" collection as its default storage location for user documents. Similarly, the mongoAclAuthorizer employs the "/acl" collection as its default repository for storing permissions.

Before running the example requests

The following examples assume RESTHeart running on the localhost with the default configuration: the database restheart is bound to / and the user admin exists with default password secret.

User document

With the default configuration, a user is represented as follows:

{
    "_id": "username",
    "roles": [ "list", "of", "roles" ],
    "password": "secret"
}
Tip
mongoRealAuthenticator can be configured to use different properties for the username, roles an password. Check mongoRealAuthenticator for more information.

Get existing users

cURL

curl -i -X GET [RESTHEART-URL]/users -H "Authorization: Basic [BASIC-AUTH]" \

HTTPie

http GET [RESTHEART-URL]/users Authorization:"Basic [BASIC-AUTH]"

JavaScript

const username = 'your-username';
const password = 'your-password';
const credentials = btoa(`${username}:${password}`);

fetch('[RESTHEART-URL]/users', {
    method: 'GET',
    headers: {
        'Authorization': `Basic ${credentials}`
    }
})
.then(response => response.json())
.then(data => {
    console.log('Retrieved users:', data);
})
.catch(error => console.error('Error:', error));
[
  {
    "_id": "admin",
    "roles": [
      "admin"
    ],
    "_etag": {
      "$oid": "5d2edb155883c050065d6a8a"
    }
  }
]
Note
The password is always hidden on GET requests.
Note
For security reasons, it not possible to use the filter query parameter on the password field; the following request is forbidden and will cause an error: GET /users?filter={"password":{"$regex":"^a.*"}}

Create a user

cURL

curl -i -X POST [RESTHEART-URL]/users \
  -H "Authorization: Basic [BASIC-AUTH]" \
  -H "Content-Type: application/json" \
  -d '{
    "_id": "foo",
    "roles": [ "user" ],
    "password": "secret"
}'

HTTPie

http POST [RESTHEART-URL]/users \
  Authorization:"Basic [BASIC-AUTH]" \
  Content-Type:application/json \
  _id="foo" \
  roles:='["user"]' \
  password="secret"

JavaScript

const username = 'your-username';
const password = 'your-password';
const credentials = btoa(`${username}:${password}`);

fetch('[RESTHEART-URL]/users', {
    method: 'POST',
    headers: {
        'Authorization': `Basic ${credentials}`,
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({
        "_id": "foo",
        "roles": [ "user" ],
        "password": "secret"
    })
})
.then(response => {
    if (response.ok) {
        console.log('User created successfully');
    } else {
        console.error('Failed to create user:', response.status);
    }
})
.catch(error => console.error('Error:', error));
Note
The password is automatically encrypted by RESTHeart.

Update a user

cURL

curl -i -X PATCH [RESTHEART-URL]/users/foo \
  -H "Authorization: Basic [BASIC-AUTH]" \
  -H "Content-Type: application/json" \
  -d '{
    "password": "betterSecret"
}'

HTTPie

http PATCH [RESTHEART-URL]/users/foo \
  Content-Type:application/json \
  password="betterSecret"

JavaScript

const username = 'your-username';
const password = 'your-password';
const credentials = btoa(`${username}:${password}`);

fetch('[RESTHEART-URL]/users/foo', {
    method: 'PATCH',
    headers: {
        'Authorization': `Basic ${credentials}`,
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({
        "password": "betterSecret"
    })
})
.then(response => {
    if (response.ok) {
        console.log('User updated successfully');
    } else {
        console.error('Failed to update user:', response.status);
    }
})
.catch(error => console.error('Error:', error));

Delete a user

cURL

curl -i -X DELETE [RESTHEART-URL]/users/foo -H "Authorization: Basic [BASIC-AUTH]" \

HTTPie

http DELETE [RESTHEART-URL]/users/foo Authorization:"Basic [BASIC-AUTH]"

JavaScript

const username = 'your-username';
const password = 'your-password';
const credentials = btoa(`${username}:${password}`);

fetch('[RESTHEART-URL]/users/foo', {
    method: 'DELETE',
    headers: {
        'Authorization': `Basic ${credentials}`
    }
})
.then(response => {
    if (response.ok) {
        console.log('User deleted successfully');
    } else {
        console.error('Failed to delete user:', response.status);
    }
})
.catch(error => console.error('Error:', error));

Create an ACL document

cURL

curl -i -X POST [RESTHEART-URL]/acl \
  -H "Authorization: Basic [BASIC-AUTH]" \
  -H "Content-Type: application/json" \
  -d '{
  "predicate": "path-prefix[/inventory] and method[GET]",
  "roles": [ "user" ],
  "priority": 1
}'

HTTPie

http POST [RESTHEART-URL]/acl \
  Authorization:"Basic [BASIC-AUTH]" \
  Content-Type:application/json \
  predicate="path-prefix[/inventory] and method[GET]" \
  roles:='["user"]' \
  priority:=1

JavaScript

const username = 'your-username';
const password = 'your-password';
const credentials = btoa(`${username}:${password}`);

fetch('[RESTHEART-URL]/acl', {
    method: 'POST',
    headers: {
        'Authorization': `Basic ${credentials}`,
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({
        "predicate": "path-prefix[/inventory] and method[GET]",
        "roles": [ "user" ],
        "priority": 1
    })
})
.then(response => {
    if (response.ok) {
        console.log('ACL permission created successfully');
    } else {
        console.error('Failed to create ACL permission:', response.status);
    }
})
.catch(error => console.error('Error:', error));
Tip
Check Format of permission for more information on ACL permissions.