Skip to content

Commit 8b9454d

Browse files
committed
Optimize basic auth.
1 parent 64f8e6d commit 8b9454d

File tree

1 file changed

+4
-9
lines changed
  • usr/lib/python3/dist-packages/linuxmusterApi/security

1 file changed

+4
-9
lines changed

usr/lib/python3/dist-packages/linuxmusterApi/security/basic_auth.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
BASIC_AUTH = HTTPBasic()
1313

14-
def generate_jwt(user):
14+
def generate_jwt(user, role, school):
1515
"""
1616
Generate a valid jwt for a specific user.
1717
@@ -22,20 +22,15 @@ def generate_jwt(user):
2222
"""
2323

2424

25-
user_details = lr.get(f'/users/{user}')
26-
if not user_details:
27-
# User not found in ldap tree, discarding request
28-
return ''
29-
3025
with open('/etc/linuxmuster/api/config.yml', 'r') as config_file:
3126
config = yaml.load(config_file, Loader=yaml.SafeLoader)
3227

3328
secret = base64.b64decode(config['secret'])
3429

3530
payload = {
3631
'user': user,
37-
'role': user_details['sophomorixRole'],
38-
'school': user_details['sophomorixSchoolname']
32+
'role': role,
33+
'school': school
3934
}
4035

4136
token = jwt.encode(payload, secret, algorithm="HS512")
@@ -61,7 +56,7 @@ def __call__(self, credentials: Annotated[HTTPBasicCredentials, Depends(BASIC_AU
6156
)
6257

6358
if user.test_password(password=credentials.password):
64-
return generate_jwt(user.cn)
59+
return generate_jwt(user.cn, user.sophomorixRole, user.sophomorixSchoolname)
6560

6661
raise HTTPException(
6762
status_code=status.HTTP_401_UNAUTHORIZED,

0 commit comments

Comments
 (0)