Skip to content

Commit d40fdbd

Browse files
committed
Allow banning/alllowing usernames patterns during registration
1 parent 1b62792 commit d40fdbd

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

policies/register/register.rego

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ allow if {
1414
count(violation) == 0
1515
}
1616

17+
username_allowed if {
18+
not data.registration.allowed_usernames
19+
}
20+
21+
username_allowed if {
22+
common.matches_string_constraints(input.username, data.registration.allowed_usernames)
23+
}
24+
1725
# METADATA
1826
# entrypoint: true
1927
violation contains {"field": "username", "code": "username-too-short", "msg": "username too short"} if {
@@ -39,6 +47,20 @@ violation contains {
3947
not regex.match(`^[a-z0-9.=_/-]+$`, input.username)
4048
}
4149

50+
violation contains {
51+
"field": "username", "code": "username-banned",
52+
"msg": "username is banned",
53+
} if {
54+
common.matches_string_constraints(input.username, data.registration.banned_usernames)
55+
}
56+
57+
violation contains {
58+
"field": "username", "code": "username-not-allowed",
59+
"msg": "username is not allowed",
60+
} if {
61+
not username_allowed
62+
}
63+
4264
violation contains {"msg": "unspecified registration method"} if {
4365
not input.registration_method
4466
}

policies/register/register_test.rego

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,20 @@ test_numeric_username if {
7575
not register.allow with input as {"username": "1234", "registration_method": "upstream-oauth2"}
7676
}
7777

78+
test_allowed_username if {
79+
register.allow with input as {"username": "hello", "registration_method": "upstream-oauth2"}
80+
with data.registration.allowed_usernames.literals as ["hello"]
81+
not register.allow with input as {"username": "hello", "registration_method": "upstream-oauth2"}
82+
with data.registration.allowed_usernames.literals as ["world"]
83+
}
84+
85+
test_banned_username if {
86+
not register.allow with input as {"username": "hello", "registration_method": "upstream-oauth2"}
87+
with data.registration.banned_usernames.literals as ["hello"]
88+
register.allow with input as {"username": "hello", "registration_method": "upstream-oauth2"}
89+
with data.registration.banned_usernames.literals as ["world"]
90+
}
91+
7892
test_ip_ban if {
7993
not register.allow with input as {
8094
"username": "hello",

0 commit comments

Comments
 (0)