Skip to content

Commit

Permalink
Prevent unecessary import resulting in catastrophies + lazy-load the …
Browse files Browse the repository at this point in the history
…session secrets
  • Loading branch information
alexAubin committed Dec 21, 2023
1 parent 38b3cfd commit 6022be5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@
"doc",
]

PORTAL_SETTINGS_DIR = "/etc/yunohost/portal"


def app_list(full=False, upgradable=False):
"""
Expand Down Expand Up @@ -1619,7 +1621,6 @@ def app_ssowatconf():
_get_domain_portal_dict,
)
from yunohost.permission import user_permission_list
from yunohost.portal import PORTAL_SETTINGS_DIR

domains = domain_list()["domains"]
portal_domains = domain_list(exclude_subdomains=True)["domains"]
Expand Down
17 changes: 14 additions & 3 deletions src/authenticators/ldap_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,18 @@

logger = logging.getLogger("yunohost.authenticators.ldap_admin")

SESSION_SECRET = open("/etc/yunohost/.admin_cookie_secret").read().strip()

def SESSION_SECRET():
# Only load this once actually requested to avoid boring issues like
# "secret doesnt exists yet" (before postinstall) and therefore service
# miserably fail to start
if not SESSION_SECRET.value:
SESSION_SECRET.value = open("/etc/yunohost/.admin_cookie_secret").read().strip()
assert SESSION_SECRET.value
return SESSION_SECRET.value


SESSION_SECRET.value = None
SESSION_FOLDER = "/var/cache/yunohost/sessions"
SESSION_VALIDITY = 3 * 24 * 3600 # 3 days

Expand Down Expand Up @@ -148,7 +159,7 @@ def set_session_cookie(self, infos):

response.set_cookie(
"yunohost.admin",
jwt.encode(infos, SESSION_SECRET, algorithm="HS256"),
jwt.encode(infos, SESSION_SECRET(), algorithm="HS256"),
secure=True,
httponly=True,
path="/",
Expand All @@ -166,7 +177,7 @@ def get_session_cookie(self, raise_if_no_session_exists=True):
token = request.get_cookie("yunohost.admin", default="").encode()
infos = jwt.decode(
token,
SESSION_SECRET,
SESSION_SECRET(),
algorithms="HS256",
options={"require": ["id", "user"]},
)
Expand Down
21 changes: 16 additions & 5 deletions src/authenticators/ldap_ynhuser.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,18 @@

logger = logging.getLogger("yunohostportal.authenticators.ldap_ynhuser")

SESSION_SECRET = open("/etc/yunohost/.ssowat_cookie_secret").read().strip()

def SESSION_SECRET():
# Only load this once actually requested to avoid boring issues like
# "secret doesnt exists yet" (before postinstall) and therefore service
# miserably fail to start
if not SESSION_SECRET.value:
SESSION_SECRET.value = open("/etc/yunohost/.ssowat_cookie_secret").read().strip()
assert SESSION_SECRET.value
return SESSION_SECRET.value


SESSION_SECRET.value = None
SESSION_FOLDER = "/var/cache/yunohost-portal/sessions"
SESSION_VALIDITY = 3 * 24 * 3600 # 3 days

Expand Down Expand Up @@ -87,7 +98,7 @@ def user_is_allowed_on_domain(user: str, domain: str) -> bool:
# The result is a string formatted as <password_enc_b64>|<iv_b64>
# For example: ctl8kk5GevYdaA5VZ2S88Q==|yTAzCx0Gd1+MCit4EQl9lA==
def encrypt(data):
alg = algorithms.AES(SESSION_SECRET.encode())
alg = algorithms.AES(SESSION_SECRET().encode())
iv = os.urandom(int(alg.block_size / 8))

E = Cipher(alg, modes.CBC(iv), default_backend()).encryptor()
Expand All @@ -104,7 +115,7 @@ def decrypt(data_enc_and_iv_b64):
data_enc = base64.b64decode(data_enc_b64)
iv = base64.b64decode(iv_b64)

alg = algorithms.AES(SESSION_SECRET.encode())
alg = algorithms.AES(SESSION_SECRET().encode())
D = Cipher(alg, modes.CBC(iv), default_backend()).decryptor()
p = padding.PKCS7(alg.block_size).unpadder()
data_padded = D.update(data_enc)
Expand Down Expand Up @@ -181,7 +192,7 @@ def set_session_cookie(self, infos):

response.set_cookie(
"yunohost.portal",
jwt.encode(infos, SESSION_SECRET, algorithm="HS256"),
jwt.encode(infos, SESSION_SECRET(), algorithm="HS256"),
secure=True,
httponly=True,
path="/",
Expand All @@ -200,7 +211,7 @@ def get_session_cookie(self, decrypt_pwd=False):
token = request.get_cookie("yunohost.portal", default="").encode()
infos = jwt.decode(
token,
SESSION_SECRET,
SESSION_SECRET(),
algorithms="HS256",
options={"require": ["id", "host", "user", "pwd"]},
)
Expand Down

0 comments on commit 6022be5

Please sign in to comment.