Skip to content

Commit e858018

Browse files
committed
chore(passport): add username validators
1 parent 519f748 commit e858018

8 files changed

Lines changed: 116 additions & 27 deletions

File tree

charts/passport/templates/_helpers.tpl

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ volumeMounts:
9090
readOnly: true
9191
{{- end }}
9292

93-
9493
{{/* Generate passport deployment volumes */}}
9594
{{- define "passport.volumes" }}
9695
volumes:
@@ -101,3 +100,77 @@ volumes:
101100
configMap:
102101
name: passport-config
103102
{{- end }}
103+
104+
{{/* Generate passport default reserved usernames */}}
105+
{{- define "passport.defaultReservedUsernames" }}
106+
admin
107+
administrator
108+
anonymous
109+
asshole
110+
bastard
111+
billing
112+
callback
113+
cancer
114+
cocaine
115+
contact
116+
coronavirus
117+
cracker
118+
database
119+
developer
120+
doopai
121+
drycc
122+
email
123+
explore
124+
faggot
125+
feedback
126+
hacker
127+
helpdesk
128+
hentai
129+
heroin
130+
hitler
131+
homophobic
132+
horny
133+
idiot
134+
killer
135+
login
136+
logout
137+
moderator
138+
murder
139+
nigger
140+
nigga
141+
official
142+
payment
143+
pedophile
144+
pornhub
145+
profile
146+
racist
147+
rapist
148+
recovery
149+
register
150+
retard
151+
scammer
152+
security
153+
service
154+
settings
155+
sexist
156+
signup
157+
signin
158+
slave
159+
spammer
160+
staff
161+
suicide
162+
support
163+
system
164+
terrorism
165+
trending
166+
undefined
167+
update
168+
username
169+
verification
170+
verify
171+
webmaster
172+
webhook
173+
wetback
174+
whore
175+
xvideos
176+
{{- end }}

charts/passport/templates/passport-configmap.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ metadata:
55
labels:
66
heritage: drycc
77
data:
8-
data:
8+
reserved-usernames.txt: |-
9+
{{- if .Values.reservedUsernames }}
10+
{{- (tpl .Values.reservedUsernames $) | nindent 4 }}
11+
{{- else}}
12+
{{- include "passport.defaultReservedUsernames" . | nindent 4 }}
13+
{{- end }}
914
init-applications.json: |-
1015
{{ toPrettyJson .Values.initApplications | indent 4 }}

charts/passport/values.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ environment:
8686
adminUsername: "admin"
8787
adminPassword: "admin"
8888
adminEmail: "admin@email.com"
89-
89+
# Reserved usernames
90+
reservedUsernames: ""
9091
# The following configurations to initialize oauth2 application
9192
# Names are all lowercase letters
9293
# The key and secret are generated automatically if they are empty

rootfs/api/models.py

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,14 @@
11
from django.db import models
2-
from django.conf import settings
3-
from django.contrib.auth import validators
42
from django.contrib.auth.models import AbstractUser
53
from django.utils.translation import gettext_lazy as _
6-
74
from oauth2_provider.models import AbstractApplication
5+
from .validators import UsernameValidator
86

97

108
class User(AbstractUser):
11-
username_validator = validators.UnicodeUsernameValidator(
12-
regex=settings.USERNAME_REGEX,
13-
message=_("Enter a valid username. This value may match the regex {}.".format(
14-
settings.USERNAME_REGEX
15-
))
16-
)
9+
username_validator = UsernameValidator()
1710
email = models.EmailField(_('email address'), unique=True)
1811

19-
@property
20-
def roles(self) -> list[str]:
21-
results = []
22-
if self.is_superuser:
23-
results.append("admin")
24-
if self.is_staff:
25-
results.append("staff")
26-
if self.is_active:
27-
results.append("users")
28-
return results
29-
3012

3113
class Application(AbstractApplication):
3214

rootfs/api/oauth2_validators.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ class CustomOAuth2Validator(OAuth2Validator):
99
"name": "profile",
1010
"username": "profile",
1111
"email": "email",
12-
"roles": "profile",
1312
"first_name": "profile",
1413
"last_name": "profile",
1514
"is_staff": "profile",
@@ -24,7 +23,6 @@ def get_additional_claims(self, request):
2423
claims["name"] = request.user.username
2524
claims["username"] = request.user.username
2625
claims["email"] = request.user.email
27-
claims["roles"] = request.user.roles
2826
claims["first_name"] = request.user.first_name
2927
claims["last_name"] = request.user.last_name
3028
claims["is_staff"] = request.user.is_staff

rootfs/api/serializers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
class UserSerializer(serializers.ModelSerializer):
1818
class Meta:
1919
model = User
20-
fields = ('id', 'username', 'email', 'roles', 'first_name', 'last_name',
20+
fields = ('id', 'username', 'email', 'first_name', 'last_name',
2121
'is_staff', 'is_active', 'is_superuser')
22-
read_only_fields = ('id', 'username', 'roles', 'is_staff', 'is_active',
22+
read_only_fields = ('id', 'username', 'is_staff', 'is_active',
2323
'is_superuser')
2424

2525

rootfs/api/settings/production.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,16 @@
401401

402402
# username regex
403403
USERNAME_REGEX = os.environ.get('USERNAME_REGEX', '^[a-z][a-z0-9]{4,}$')
404+
405+
# reserved username
406+
RESERVED_USERNAMES_PATH = os.environ.get(
407+
'RESERVED_USERNAMES_PATH', '/etc/drycc/passport/reserved-usernames.txt')
408+
if os.path.exists(RESERVED_USERNAMES_PATH):
409+
with open(RESERVED_USERNAMES_PATH) as f:
410+
RESERVED_USERNAMES = [line.strip() for line in f if line]
411+
else:
412+
RESERVED_USERNAMES = ["drycc", "admin", "doopai"]
413+
404414
# hcaptcha config
405415
H_CAPTCHA_KEY = os.environ.get("H_CAPTCHA_KEY")
406416
H_CAPTCHA_SECRET = os.environ.get("H_CAPTCHA_SECRET")

rootfs/api/validators.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from django.contrib.auth import validators
2+
from django.utils.translation import gettext_lazy as _
3+
from django.conf import settings
4+
from django.core.exceptions import ValidationError
5+
6+
7+
class UsernameValidator(validators.UnicodeUsernameValidator):
8+
regex = settings.USERNAME_REGEX
9+
message = _(
10+
f"Enter a valid username. This value may match the regex {regex}."
11+
)
12+
13+
def __call__(self, value):
14+
if value in settings.RESERVED_USERNAMES:
15+
raise ValidationError(
16+
_("The current username is on the blocklist."),
17+
code=self.code,
18+
params={"value": value}
19+
)
20+
super().__call__(value)

0 commit comments

Comments
 (0)