Skip to content

Commit 3498617

Browse files
committed
WIP
1 parent 563d8c5 commit 3498617

File tree

6 files changed

+377
-10
lines changed

6 files changed

+377
-10
lines changed

pages/database-management/authentication-and-authorization.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ Learn how to manage users in Memgraph.
1717

1818
Learn how to manage roles, set up their privileges and fine-grained access control.
1919

20+
## [Multiple roles per user](/database-management/authentication-and-authorization/multiple-roles) (Enterprise)
21+
22+
Learn how to assign multiple roles to users simultaneously and understand how permissions are combined from all roles.
23+
2024
## [Auth system integrations](/database-management/authentication-and-authorization/auth-system-integrations) (Enterprise)
2125

2226
Learn how to integrate with third-party auth systems and manage user
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export default {
22
"users": "Users",
33
"role-based-access-control": "Role-based access control",
4+
"multiple-roles": "Multiple roles per user",
45
"auth-system-integrations": "Auth system integrations",
56
"impersonate-user": "Impersonate user"
67
}

pages/database-management/authentication-and-authorization/auth-system-integrations.mdx

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ privileges will still apply but you won't be able to manage them.
2727
### Roles
2828

2929
User roles must be defined in Memgraph before using auth modules because these
30-
modules return the role associated with the user.
30+
modules return the role(s) associated with the user. Memgraph now supports multiple roles per user, allowing auth modules to return either a single role or multiple roles.
3131

3232
### Flags
3333

@@ -85,8 +85,9 @@ The protocol used between Memgraph and the module is as follows:
8585
- Auth responses must be objects that contain the following fields:
8686
- `authenticated` - a `bool` indicating whether the user is allowed to log
8787
in to the database
88-
- `role` - a `string` indicating which role the user should have (must be
89-
supplied)
88+
- `role` - a `string` indicating which role the user should have (backward compatible)
89+
- `roles` - an array of strings indicating which roles the user should have (new format)
90+
- `username` - the user's username (optional, can be derived from auth token)
9091
- `errors` (optional) - if `authenticated` is false, Memgraph will put up a
9192
warning with the error message returned by the module
9293

@@ -95,6 +96,53 @@ Memgraph won't allow the user to log in to the database and will automatically
9596
restart the auth module for the next auth request. All crash logs will be seen
9697
in Memgraph's output (typically in `systemd` logs using `journalctl`).
9798

99+
### Multiple roles support
100+
101+
Memgraph now supports multiple roles per user in auth module responses. Auth modules can return either a single role (backward compatible) or multiple roles (new format).
102+
103+
#### Single role response (backward compatible)
104+
105+
```python
106+
def authenticate(username, password):
107+
return {
108+
"authenticated": True,
109+
"role": "moderator" # Single role as string
110+
}
111+
```
112+
113+
#### Multiple roles response (new format)
114+
115+
```python
116+
def authenticate(username, password):
117+
return {
118+
"authenticated": True,
119+
"roles": ["admin", "user"] # Multiple roles as array
120+
}
121+
```
122+
123+
#### Single role in array format
124+
125+
```python
126+
def authenticate(username, password):
127+
return {
128+
"authenticated": True,
129+
"roles": ["admin"] # Single role in array
130+
}
131+
```
132+
133+
The system will:
134+
1. First check for a `roles` field in the response
135+
2. If `roles` is an array, use all roles in the array
136+
3. If `roles` is a string, use it as a single role
137+
4. If no `roles` field is found, fall back to the `role` field for backward compatibility
138+
5. If no valid roles are found, authentication fails
139+
140+
When a user has multiple roles, their permissions are combined using the following rules:
141+
- **Grants**: If any role grants a permission, the user has that permission
142+
- **Denies**: If any role denies a permission, the user is denied that permission
143+
- **Database Access**: If any role grants access to a database, the user has access
144+
- **Fine-grained Permissions**: Combined using the same grant/deny logic
145+
98146
### Module example
99147

100148
This very simple example auth module is written in Python, but any programming
@@ -107,7 +155,15 @@ import io
107155

108156

109157
def authenticate(username, password):
110-
return {"authenticated": True, "role": "moderator"}
158+
# Example with multiple roles
159+
if username == "admin_user" and password == "password":
160+
return {"authenticated": True, "roles": ["admin", "user"]}
161+
162+
# Example with single role (backward compatible)
163+
if username == "moderator_user" and password == "password":
164+
return {"authenticated": True, "role": "moderator"}
165+
166+
return {"authenticated": False, "errors": "Invalid credentials"}
111167

112168

113169
if __name__ == "__main__":
@@ -132,8 +188,8 @@ files. For example:
132188
#!/usr/bin/python3
133189
import module
134190

135-
assert module.authenticate("sponge", "bob") == {"authenticated": True, "role": "analyst"}
136-
assert module.authenticate("CHUCK", "NORRIS") == {"authenticated": True, "role": "admin"}
191+
assert module.authenticate("admin_user", "password") == {"authenticated": True, "roles": ["admin", "user"]}
192+
assert module.authenticate("moderator_user", "password") == {"authenticated": True, "role": "moderator"}
137193
```
138194

139195
## Single sign-on
@@ -163,6 +219,10 @@ created in the Memgraph DB beforehand. Additionally, you have to grant [label-ba
163219

164220
</Callout>
165221

222+
<Callout type="info">
223+
SSO identity providers often return multiple roles for users. Memgraph now supports this natively - if your identity provider returns multiple roles, they will all be mapped to Memgraph roles and the user will have permissions from all assigned roles combined.
224+
</Callout>
225+
166226
### SAML
167227

168228
Memgraph has built-in support for single sign-on (SSO) over the SAML protocol

0 commit comments

Comments
 (0)