Skip to content

Commit 82cfc74

Browse files
committed
FEAT: Allow to set domain, secure and same_site on cookies
1 parent fed94c0 commit 82cfc74

File tree

4 files changed

+42
-18
lines changed

4 files changed

+42
-18
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="streamlit-authenticator",
8-
version="0.4.2-alpha",
8+
version="0.4.2-alpha1",
99
author="Mohammad Khorasani",
1010
author_email="[email protected]",
1111
description="A secure authentication module to manage user access in a Streamlit application.",

streamlit_authenticator/controllers/cookie_controller.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
- typing: Provides standard type hints for Python functions.
77
"""
88

9-
from typing import Any, Dict, Optional
9+
from typing import Any, Dict, Literal, Optional, Union
1010

1111
from ..models import CookieModel
1212

@@ -21,7 +21,10 @@ def __init__(
2121
cookie_name: Optional[str] = None,
2222
cookie_key: Optional[str] = None,
2323
cookie_expiry_days: Optional[float] = None,
24-
path: Optional[str] = None
24+
path: Optional[str] = None,
25+
domain: Optional[str] = None,
26+
secure: Optional[bool] = None,
27+
same_site: Union[bool, None, Literal["lax", "strict"]] = "strict",
2528
) -> None:
2629
"""
2730
Initializes the CookieController instance.
@@ -40,7 +43,10 @@ def __init__(
4043
self.cookie_model = CookieModel(cookie_name,
4144
cookie_key,
4245
cookie_expiry_days,
43-
path)
46+
path,
47+
domain,
48+
secure,
49+
same_site)
4450
def delete_cookie(self) -> None:
4551
"""
4652
Deletes the re-authentication cookie from the user's browser.

streamlit_authenticator/models/cookie_model.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
- extra_streamlit_components: Provides cookie management for Streamlit.
1111
"""
1212

13-
from typing import Any, Dict, Optional
1413
from datetime import datetime, timedelta
14+
from typing import Any, Dict, Literal, Optional, Union
15+
16+
import extra_streamlit_components as stx
1517
import jwt
16-
from jwt import DecodeError, InvalidSignatureError
1718
import streamlit as st
18-
import extra_streamlit_components as stx
19+
from jwt import DecodeError, InvalidSignatureError
1920

2021
from ..utilities import Helpers
2122

@@ -30,7 +31,10 @@ def __init__(
3031
cookie_name: Optional[str] = None,
3132
cookie_key: Optional[str] = None,
3233
cookie_expiry_days: Optional[float] = None,
33-
path: Optional[str] = None
34+
path: Optional[str] = None,
35+
domain: Optional[str] = None,
36+
secure: Optional[bool] = None,
37+
same_site: Union[bool, None, Literal["lax", "strict"]] = "strict",
3438
) -> None:
3539
"""
3640
Initializes the CookieModel instance.
@@ -58,6 +62,9 @@ def __init__(
5862
self.cookie_manager = stx.CookieManager()
5963
self.token = None
6064
self.exp_date = None
65+
self.domain = domain
66+
self.secure = secure
67+
self.same_site = same_site
6168
def delete_cookie(self) -> None:
6269
"""
6370
Deletes the re-authentication cookie from the user's browser.
@@ -96,7 +103,10 @@ def set_cookie(self) -> None:
96103
token = self._token_encode()
97104
self.cookie_manager.set(self.cookie_name, token,
98105
expires_at=datetime.now() + \
99-
timedelta(days=self.cookie_expiry_days))
106+
timedelta(days=self.cookie_expiry_days),
107+
domain=self.domain,
108+
secure=self.secure,
109+
same_site=self.same_site)
100110
def _set_exp_date(self) -> float:
101111
"""
102112
Computes the expiration timestamp for the authentication cookie.

streamlit_authenticator/views/authentication_view.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@
1616

1717
import streamlit as st
1818

19-
from ..controllers import AuthenticationController, CookieController
2019
from .. import params
21-
from ..utilities import (DeprecationError,
22-
Encryptor,
23-
Helpers,
24-
LogoutError,
25-
ResetError,
26-
UpdateError,
27-
Validator)
20+
from ..controllers import AuthenticationController, CookieController
21+
from ..utilities import (
22+
DeprecationError,
23+
Encryptor,
24+
Helpers,
25+
LogoutError,
26+
ResetError,
27+
UpdateError,
28+
Validator,
29+
)
2830

2931

3032
class Authenticate:
@@ -41,6 +43,9 @@ def __init__(
4143
validator: Optional[Validator] = None,
4244
auto_hash: bool = True,
4345
api_key: Optional[str] = None,
46+
domain: Optional[str] = None,
47+
secure: Optional[bool] = None,
48+
same_site: Union[bool, None, Literal["lax", "strict"]] = "strict",
4449
**kwargs: Optional[Dict[str, Any]]
4550
) -> None:
4651
"""
@@ -77,7 +82,10 @@ def __init__(
7782
self.cookie_controller = CookieController(cookie_name,
7883
cookie_key,
7984
cookie_expiry_days,
80-
self.path)
85+
self.path,
86+
domain,
87+
secure,
88+
same_site)
8189
self.authentication_controller = AuthenticationController(credentials,
8290
validator,
8391
auto_hash,

0 commit comments

Comments
 (0)