Groups

A group associates a name to a list of principals. It is useful in order to handle permissions. Groups are defined in buckets.

A group is a mapping with the following attributes:

  • data: attributes of the group object
    • id: the group object id
    • last_modified: the timestamp of the last modification
    • members: a list of principals
  • permissions: the ACLs for the group object (e.g who is allowed to read or update the group object itself.)

When used in permissions definitions, the full group URI has to be used:

{
    "write": ["/buckets/blog/groups/authors", "github:lili"],
    "read": ["system.Everyone"]
}

Creating a group

POST /buckets/(bucket_id)/groups
Synopsis:Creates a new bucket group with a generated ID.

Requires authentication

Example Request

$ echo '{"data": {"members": ["account:alice"]}}' | http POST http://localhost:8888/v1/buckets/blog/groups --auth="bob:p4ssw0rd" --verbose
POST /v1/buckets/blog/groups HTTP/1.1
Accept: application/json
Accept-Encoding: gzip, deflate
Authorization: Basic Ym9iOg==
Connection: keep-alive
Content-Length: 102
Content-Type: application/json
Host: localhost:8888
User-Agent: HTTPie/0.9.2

{
    "data": {
        "members": [
            "account:alice"
        ]
    }
}

Example Response

HTTP/1.1 201 Created
Access-Control-Expose-Headers: Backoff, Retry-After, Alert
Content-Length: 248
Content-Type: application/json; charset=UTF-8
Date: Thu, 18 Jun 2015 16:17:02 GMT
Server: waitress

{
    "data": {
        "id": "wZjuQfpS",
        "last_modified": 1434644222033,
        "members": [
            "account:alice"
        ]
    },
    "permissions": {
        "write": [
            "account:alice"
        ]
    }
}

HTTP Status Codes

  • 200 OK: This object already exists, the one stored on the database is returned
  • 201 Created: The object was created
  • 400 Bad Request: The request body is invalid
  • 401 Unauthorized: The request is missing authentication headers
  • 403 Forbidden: The user is not allowed to perform the operation, or the resource is not accessible
  • 406 Not Acceptable: The client doesn’t accept supported responses Content-Type
  • 412 Precondition Failed: List has changed since value in If-Match header
  • 415 Unsupported Media Type: The client request was not sent with a correct Content-Type

Replacing a group

PUT /buckets/(bucket_id)/groups/(group_id)
Synopsis:Creates or replaces a group with a chosen ID.

Requires authentication

Example Request

$ echo '{"data": {"members": ["account:alice"]}}' | http put http://localhost:8888/v1/buckets/blog/groups/readers --auth="bob:p4ssw0rd" --verbose
PUT /v1/buckets/blog/groups/readers HTTP/1.1
Accept: application/json
Accept-Encoding: gzip, deflate
Authorization: Basic Ym9iOg==
Connection: keep-alive
Content-Length: 102
Content-Type: application/json
Host: localhost:8888
User-Agent: HTTPie/0.9.2

{
    "data": {
        "members": [
            "account:alice"
        ]
    }
}

Example Response

HTTP/1.1 201 Created
Access-Control-Expose-Headers: Backoff, Retry-After, Alert
Content-Length: 247
Content-Type: application/json; charset=UTF-8
Date: Thu, 18 Jun 2015 16:41:01 GMT
Server: waitress

{
    "data": {
        "id": "readers",
        "last_modified": 1434645661227,
        "members": [
            "account:alice"
        ]
    },
    "permissions": {
        "write": [
            "account:bob"
        ]
    }
}

Validation and conflicts behaviour is similar to creating objects (POST).

If the If-Match: "<timestamp>" request header is provided as described in the section about timestamps, and if the object has changed meanwhile, a 412 Precondition Failed error is returned.

If the If-None-Match: * request header is provided and if there is already an existing object with this id, a 412 Precondition Failed error is returned.

Permissions

In the JSON request payloads, at least one of data and permissions must be provided. Permissions can thus be replaced independently from data.

In the case of creation, if only permissions is provided, an empty object is created.

The current user id is always added among the write principals.

See Permissions request payload.

HTTP Status Code

  • 201 Created: The object was created
  • 200 OK: The object was replaced
  • 400 Bad Request: The request body is invalid
  • 401 Unauthorized: The request is missing authentication headers
  • 403 Forbidden: The user is not allowed to perform the operation, or the resource is not accessible
  • 406 Not Acceptable: The client doesn’t accept supported responses Content-Type.
  • 412 Precondition Failed: Record was changed or deleted since value in If-Match header.
  • 415 Unsupported Media Type: The client request was not sent with a correct Content-Type.

Modify a group

PATCH /buckets/(bucket_id)/groups/(group_id)
Synopsis:Modifies an existing group.

Requires authentication

Example Request

$ echo '{"data": {"members": ["account:alice"]}}' | http patch http://localhost:8888/v1/buckets/blog/groups/readers --auth="bob:p4ssw0rd" --verbose
PATCH /v1/buckets/blog/groups/readers HTTP/1.1
Accept: application/json
Accept-Encoding: gzip, deflate
Authorization: Basic Ym9iOg==
Connection: keep-alive
Content-Length: 102
Content-Type: application/json
Host: localhost:8888
User-Agent: HTTPie/0.9.2

{
    "data": {
        "members": [
            "account:alice"
        ]
    }
}

Example Response

HTTP/1.1 200 OK
Access-Control-Expose-Headers: Backoff, Retry-After, Alert
Content-Length: 247
Content-Type: application/json; charset=UTF-8
Date: Thu, 18 Jun 2015 16:41:01 GMT
Server: waitress

{
    "data": {
        "id": "readers",
        "last_modified": 1434645661227,
        "members": [
            "account:alice"
        ]
    },
    "permissions": {
        "write": [
            "account:bob"
        ]
    }
}

If the object is missing (or already deleted), a 404 Not Found is returned only if the user has write access to the object parent, otherwise a 403 Forbidden is returned to avoid leaking information about non-accessible objects. The consumer might decide to ignore it.

If the If-Match: "<timestamp>" request header is provided as described in the section about timestamps, and if the object has changed meanwhile, a 412 Precondition Failed error is returned.

Note

last_modified is updated to the current server timestamp, only if a field value was changed.

Attributes merge

The provided values are merged with the existing object. For example:

  • {"a":"b"} + {"a":"c"}{"a":"c"}
  • {"a":"b"} + {"b":"c"}{"a":"b", "b":"c"}
  • {"a":"b"} + {"a":null}{"a":null} : attributes can’t be removed with patch
  • {"a": {"b":"c"}} + {"a":{"d":"e"}}{"a":{"d":"e"}} : sub-objects are replaced, not merged

JSON merge is currently supported using Content-Type: application/merge-patch+json. This provides support to merging sub-objects and removing attibutes. For example:

  • {"a":"b"} + {"a":null}{}
  • {"a": {"b":"c"}} + {"a":{"d":"e"}}{"a":{"b":"c", "d":"e"}}
  • {} + {"a":{"b":{"c":null}}}{"a":{"b":{}}}

Light response body

If a Response-Behavior request header is set to light, only the fields whose value was changed are returned. If set to diff, only the fields whose value became different than the one provided are returned.

JSON Patch Operations

JSON-Patch is a way to define a sequence of operations to be applied on a JSON object.

It’s possible to use JSON-Patch by sending the request header Content-Type: application/json-patch+json.

When using this request header, the body should contain a list of operations, for example:

[
    { "op": "test", "path": "data/a", "value": "foo" },
    { "op": "remove", "path": "/data/a" },
    { "op": "add", "path": "/data/b", "value": [ "foo", "bar" ] },
    { "op": "replace", "path": "/data/b", "value": 42 },
    { "op": "move", "from": "/data/a", "path": "/data/c" },
    { "op": "copy", "from": "/data/b", "path": "/data/d" }
]

For more information about each operation, please refer to JSON-Patch Specification.

This is very useful when altering permissions since there is no need to get the current value before adding some principal to the list.

Permissions

When permissions are modified, the same behaviour used for the body is expected. But notice thought that there are some permissions specific behaviours, such as:

  1. The current user id is always added among the write principals.
  2. Value is not used on permission JSON Patch operations and can be omitted.
[
    { "op": "test", "path": "/permissions/read/fxa:alice" },
    { "op": "add", "path": "/permissions/read/system.Everyone" },
    { "op": "remove", "path": "/permissions/read/fxa:bob" }
]
  1. On JSON Merge operations, null values can be used to remove all principals from a specific permission level. As a consequence, using null for permissions on other types of patch operations is considered a no-op.

For more information on permissions, see Permissions request payload.

HTTP Status Codes

  • 200 OK: The object was modified
  • 400 Bad Request: The request body is invalid, or a read-only field was modified
  • 401 Unauthorized: The request is missing authentication headers
  • 403 Forbidden: The user is not allowed to perform the operation, or the resource is not accessible
  • 404 Not Found: The object does not exist or was deleted
  • 406 Not Acceptable: The client doesn’t accept supported responses Content-Type.
  • 412 Precondition Failed: Record changed since value in If-Match header
  • 415 Unsupported Media Type: The client request was not sent with a correct Content-Type.

Retrieving a group

GET /buckets/(bucket_id)/groups/(group_id)
Synopsis:Returns the group object.

Requires authentication

Example Request

$ http get http://localhost:8888/v1/buckets/blog/groups/readers --auth="bob:p4ssw0rd" --verbose
GET /v1/buckets/blog/groups/readers HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Authorization: Basic Ym9iOg==
Connection: keep-alive
Host: localhost:8888
User-Agent: HTTPie/0.9.2

Example Response

HTTP/1.1 200 OK
Access-Control-Expose-Headers: Backoff, Retry-After, Alert, Last-Modified, ETag
Content-Length: 247
Content-Type: application/json; charset=UTF-8
Date: Thu, 18 Jun 2015 16:44:07 GMT
Etag: "1434645847532"
Last-Modified: Thu, 18 Jun 2015 16:44:07 GMT
Server: waitress

{
    "data": {
        "id": "readers",
        "last_modified": 1434645661227,
        "members": [
            "account:alice"
        ]
    },
    "permissions": {
        "write": [
            "account:bob"
        ]
    }
}

If the If-None-Match: "<timestamp>" request header is provided, and if the object has not changed meanwhile, a 304 Not Modified is returned.

HTTP Status Codes

  • 200 OK: The request was processed
  • 304 Not Modified: Object did not change since value in If-None-Match header
  • 400 Bad Request: The request header is invalid
  • 401 Unauthorized: The request is missing authentication headers
  • 403 Forbidden: The user is not allowed to perform the operation, or the resource is not accessible
  • 404 Not Found: The object does not exist or was deleted
  • 406 Not Acceptable: The client doesn’t accept supported responses Content-Type
  • 412 Precondition Failed: Object changed since value in If-Match header

Retrieving all groups

GET /buckets/(bucket_id)/groups
Synopsis:Returns the list of groups for the bucket.

Requires authentication

Example Request

$ http get http://localhost:8888/v1/buckets/blog/groups --auth="bob:p4ssw0rd" --verbose
GET /v1/buckets/blog/groups HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Authorization: Basic Ym9iOg==
Connection: keep-alive
Host: localhost:8888
User-Agent: HTTPie/0.9.2

Example Response

HTTP/1.1 200 OK
Access-Control-Expose-Headers: Backoff, Retry-After, Alert, Content-Length, Next-Page, Total-Records, Last-Modified, ETag
Content-Length: 147
Content-Type: application/json; charset=UTF-8
Date: Thu, 13 Aug 2015 12:16:05 GMT
Etag: "1439468156451"
Last-Modified: Thu, 13 Aug 2015 12:15:56 GMT
Server: waitress
Total-Records: 1

{
    "data": [
        {
            "id": "vAQSwSca",
            "last_modified": 1439468156451,
            "members": [
                "account:alice"
            ]
        }
    ]
}

A Total-Records response header indicates the total number of objects of the list (not the response, since it can be paginated).

A Last-Modified response header provides a human-readable (rounded to second) of the current collection timestamp.

For cache and concurrency control, an ETag response header gives the value that consumers can provide in subsequent requests using If-Match and If-None-Match headers (see section about timestamps).

List of available URL parameters

Filtering, sorting, partial responses and paginating can all be combined together.

  • ?_sort=-last_modified&_limit=100&_fields=title

HTTP Status Codes

  • 200 OK: The request was processed
  • 304 Not Modified: List has not changed since value in If-None-Match header
  • 400 Bad Request: The request querystring is invalid
  • 401 Unauthorized: The request is missing authentication headers
  • 403 Forbidden: The user is not allowed to perform the operation, or the resource is not accessible
  • 406 Not Acceptable: The client doesn’t accept supported responses Content-Type
  • 412 Precondition Failed: List has changed since value in If-Match header

Deleting all groups

DELETE /buckets/(bucket_id)/groups
Synopsis:Delete every writable group in the bucket.

Requires authentication

Example Request

$ http delete http://localhost:8888/v1/buckets/blog/groups --auth="bob:p4ssw0rd" --verbose
DELETE /v1/buckets/blog/groups HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Authorization: Basic dG9rZW46Ym9iLXRva2Vu
Connection: keep-alive
Content-Length: 0
Host: localhost:8888
User-Agent: HTTPie/0.9.3

Example Response

HTTP/1.1 200 OK
Access-Control-Expose-Headers: Retry-After, Content-Length, Alert, Backoff
Content-Length: 72
Content-Type: application/json; charset=UTF-8
Date: Sun, 20 Nov 2016 03:04:57 GMT
Server: waitress

{
    "data": [
        {
            "deleted": true,
            "id": "readers",
            "last_modified": 1479611097155
        }
    ]
}

It supports the same filtering, sorting and pagination capabilities as GET.

If the number of records to be deleted exceeds to pagination limit, a response header Next-Page will be provided.

If the If-Match: "<timestamp>" request header is provided, and if the list has changed meanwhile, a 412 Precondition Failed error is returned.

HTTP Status Codes

  • 200 OK: The objects were deleted
  • 401 Unauthorized: The request is missing authentication headers
  • 403 Forbidden: The user is not allowed to perform the operation, or the resource is not accessible
  • 405 Method Not Allowed: This endpoint is not available
  • 406 Not Acceptable: The client doesn’t accept supported responses Content-Type
  • 412 Precondition Failed: The list has changed since value in If-Match header

Deleting a group

DELETE /buckets/(bucket_id)/groups/(group_id)
Synopsis:Deletes a specific group.

Requires authentication

Example Request

$ http delete http://localhost:8888/v1/buckets/blog/groups/readers --auth="bob:p4ssw0rd" --verbose
DELETE /v1/buckets/blog/groups/readers HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Authorization: Basic Ym9iOg==
Connection: keep-alive
Content-Length: 0
Host: localhost:8888
User-Agent: HTTPie/0.9.2

Example Response

HTTP/1.1 200 OK
Access-Control-Expose-Headers: Backoff, Retry-After, Alert
Content-Length: 70
Content-Type: application/json; charset=UTF-8
Date: Thu, 18 Jun 2015 16:47:29 GMT
Server: waitress

{
    "data": {
        "deleted": true,
        "id": "readers",
        "last_modified": 1434646049488
    }
}

If the object is missing (or already deleted), a 404 Not Found is returned only if the user has write access to the object parent, otherwise a 403 Forbidden is returned to avoid leaking information about non-accessible objects. The consumer might decide to ignore it.

If the If-Match request header is provided, and if the object has changed meanwhile, a 412 Precondition Failed error is returned.

Note

Once deleted, an object will appear in the list when polling for changes, with a deleted status (delete=true) and will have most of its fields empty.

HTTP Status Codes

  • 200 OK: The object was deleted
  • 401 Unauthorized: The request is missing authentication headers
  • 403 Forbidden: The user is not allowed to perform the operation, or the resource is not accessible
  • 404 Not Found: The object does not exist or was already deleted
  • 406 Not Acceptable: The client doesn’t accept supported responses Content-Type.
  • 412 Precondition Failed: Record changed since value in If-Match header

Metadata validation using JSON schema

Requires setting kinto.experimental_collection_schema_validation to True.

By default, only the members must respect a particular schema (list of strings), and groups will accept any kind of additional fields.

In order to validate the additional fields, it is possible to define a JSON schema on the parent bucket metadata.

Set or replace a schema

Just modify the group:schema attribute of the parent bucket object:

Example request

$ echo '{
  "data": {
    "group:schema": {
      "title": "Supporters group",
      "type": "object",
      "properties": {
          "email": {"type": "string"},
          "members": {"type": "array", "item": {"type": "string"}}
      },
      "required": ["email"]
    }
  }
}' | http PATCH "http://localhost:8888/v1/buckets/blog" --auth bob:p4ssw0rd --verbose
PATCH /v1/buckets/blog HTTP/1.1
Accept: application/json
Accept-Encoding: gzip, deflate
Authorization: Basic YWRtaW46
Connection: keep-alive
Content-Length: 236
Content-Type: application/json; charset=utf-8
Host: localhost:8888
User-Agent: HTTPie/0.8.0

{
    "data": {
        "group:schema": {
            "properties": {
                "email": {
                    "type": "string"
                },
                "members": {
                    "type": "array",
                    "item": {
                        "type": "string"
                    }
                }
            },
            "required": [
                "email"
            ],
            "title": "Supporters group",
            "type": "object"
        }
    }
}

Example response

HTTP/1.1 200 OK
Access-Control-Expose-Headers: Backoff, Retry-After, Alert, Content-Length
Content-Length: 300
Content-Type: application/json; charset=UTF-8
Date: Fri, 21 Aug 2015 12:31:40 GMT
Etag: "1440160300818"
Last-Modified: Fri, 21 Aug 2015 12:31:40 GMT
Server: waitress

{
    "data": {
        "group:schema": {
            "properties": {
                "email": {
                    "type": "string"
                },
                "members": {
                    "type": "array",
                    "item": {
                        "type": "string"
                    }
                }
            },
            "required": [
                "email"
            ],
            "title": "Supporters group",
            "type": "object"
        }
    },
    "permissions": {
        "write": [
            "account:bob"
        ]
    }
}