Skip to content

Commit 22ee87a

Browse files
committed
add identity rego
1 parent 9d6509e commit 22ee87a

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

policies/register/identity.rego

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package identity
2+
3+
headers = {
4+
"Content-Type": "application/json",
5+
"Accept": "application/json"
6+
}
7+
8+
url = sprintf("%s?medium=%s&address=%s", [data.external_service.url, "email", input.email])
9+
10+
11+
get_identity_info = http.send(
12+
{
13+
"method": "get",
14+
"url": url,
15+
"headers": headers
16+
}
17+
)

policies/register/register.rego

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
package register
55

66
import rego.v1
7+
import data.identity
78

89
import data.common
910
import data.email as email_policy
11+
import future.keywords.contains
12+
import future.keywords.if
13+
import future.keywords.in
1014

1115
default allow := false
1216

@@ -92,3 +96,69 @@ violation contains object.union({"field": "email"}, v) if {
9296
# Get the violation object from the email policy
9397
some v in email_policy.violation
9498
}
99+
100+
101+
# Violation for email on wrong homeserver
102+
violation contains {
103+
"field": "email",
104+
"code": "email-wrong-homeserver",
105+
"msg": "email is registered on a different homeserver"
106+
} if {
107+
# Check if email is present
108+
input.email
109+
110+
# Check if external service configuration exists
111+
data.external_service
112+
113+
# Get API response
114+
identity_info_json := identity.get_identity_info
115+
116+
# Check if "hs" is present in the response
117+
"hs" in identity_info_json
118+
119+
# Check if the hs does NOT match the server_name
120+
identity_info_json.hs != data.server_name
121+
}
122+
123+
# Violation for email requiring invitation
124+
violation contains {
125+
"field": "email",
126+
"code": "email-invitation-required",
127+
"msg": "invitation required for this email"
128+
} if {
129+
# Check if email is present
130+
input.email
131+
132+
# Check if external service configuration exists
133+
data.external_service
134+
135+
# Get API response
136+
identity_info_json := identity.get_identity_info
137+
138+
# Check if "hs" is present and matches server_name
139+
"hs" in identity_info_json
140+
identity_info_json.hs == data.server_name
141+
142+
# Check if requires_invite is true and invited is false
143+
identity_info_json.requires_invite == true
144+
identity_info_json.invited == false
145+
}
146+
147+
# Violation for email with missing hs field
148+
violation contains {
149+
"field": "email",
150+
"code": "email-invalid-response",
151+
"msg": "invalid response from identity server"
152+
} if {
153+
# Check if email is present
154+
input.email
155+
156+
# Check if external service configuration exists
157+
data.external_service
158+
159+
# Get API response
160+
identity_info_json := identity.get_identity_info
161+
162+
# Check if "hs" is NOT present in the response
163+
not "hs" in identity_info_json
164+
}

policies/register/register_test.rego

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package register_test
22

33
import data.register
44
import rego.v1
5+
import future.keywords.if
6+
import future.keywords.in
57

68
mock_registration := {
79
"registration_method": "password",
@@ -111,3 +113,51 @@ test_ip_ban if {
111113
}
112114
with data.requester.banned_user_agents.substrings as ["Evil"]
113115
}
116+
117+
# Test external service allowing registration
118+
test_external_service_allowed if {
119+
# Create a test input with only the necessary fields
120+
test_input := {
121+
"username": "hello",
122+
"email": "[email protected]",
123+
"registration_method": "password",
124+
}
125+
126+
register.allow with input as test_input
127+
with as {
128+
"url": "https://matrix.agent.agriculture.tchap.gouv.fr/_matrix/identity/api/v1/info"
129+
}
130+
with data.server_name as "matrix.org"
131+
}
132+
133+
mock_http_missing_hs_response(request) = response if {
134+
response := {
135+
"status_code": 200,
136+
"body": {
137+
"invited": true,
138+
"requires_invite": false
139+
}
140+
}
141+
}
142+
143+
mock_http_wrong_hs_response(request) = response if {
144+
response := {
145+
"status_code": 200,
146+
"body": {
147+
"hs": "wrong.org",
148+
"invited": true,
149+
"requires_invite": false
150+
}
151+
}
152+
}
153+
154+
mock_http_requires_invite_response(request) = response if {
155+
response := {
156+
"status_code": 200,
157+
"body": {
158+
"hs": "matrix.org",
159+
"invited": false,
160+
"requires_invite": true
161+
}
162+
}
163+
}

0 commit comments

Comments
 (0)