Skip to content

Commit e41b6f7

Browse files
DefteZvimalloc
authored andcommitted
Add posibility to set up cookies max-age in runtime (#81)
1 parent 043ba23 commit e41b6f7

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

flask_jwt_extended/utils.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,14 @@ def get_csrf_token(encoded_token):
157157
return token['csrf']
158158

159159

160-
def set_access_cookies(response, encoded_access_token):
160+
def set_access_cookies(response, encoded_access_token, max_age=None):
161161
"""
162162
Takes a flask response object, and configures it to set the encoded access
163163
token in a cookie (as well as a csrf access cookie if enabled)
164+
165+
:param max_age: If this is None, it will use the 'JWT_SESSION_COOKIE'
166+
config value. Else it will use max_age as cookie "max-age".
167+
Values should be integer number of seconds
164168
"""
165169
if not config.jwt_in_cookies:
166170
raise RuntimeWarning("set_access_cookies() called without "
@@ -169,7 +173,7 @@ def set_access_cookies(response, encoded_access_token):
169173
# Set the access JWT in the cookie
170174
response.set_cookie(config.access_cookie_name,
171175
value=encoded_access_token,
172-
max_age=config.cookie_max_age,
176+
max_age=max_age or config.cookie_max_age,
173177
secure=config.cookie_secure,
174178
httponly=True,
175179
domain=config.cookie_domain,
@@ -179,17 +183,21 @@ def set_access_cookies(response, encoded_access_token):
179183
if config.csrf_protect and config.csrf_in_cookies:
180184
response.set_cookie(config.access_csrf_cookie_name,
181185
value=get_csrf_token(encoded_access_token),
182-
max_age=config.cookie_max_age,
186+
max_age=max_age or config.cookie_max_age,
183187
secure=config.cookie_secure,
184188
httponly=False,
185189
domain=config.cookie_domain,
186190
path=config.access_csrf_cookie_path)
187191

188192

189-
def set_refresh_cookies(response, encoded_refresh_token):
193+
def set_refresh_cookies(response, encoded_refresh_token, max_age=None):
190194
"""
191195
Takes a flask response object, and configures it to set the encoded refresh
192196
token in a cookie (as well as a csrf refresh cookie if enabled)
197+
198+
:param max_age: If this is None, it will use the 'JWT_SESSION_COOKIE'
199+
config value. Else it will use max_age as cookie "max-age".
200+
Values should be integer number of seconds
193201
"""
194202
if not config.jwt_in_cookies:
195203
raise RuntimeWarning("set_refresh_cookies() called without "
@@ -198,7 +206,7 @@ def set_refresh_cookies(response, encoded_refresh_token):
198206
# Set the refresh JWT in the cookie
199207
response.set_cookie(config.refresh_cookie_name,
200208
value=encoded_refresh_token,
201-
max_age=config.cookie_max_age,
209+
max_age=max_age or config.cookie_max_age,
202210
secure=config.cookie_secure,
203211
httponly=True,
204212
domain=config.cookie_domain,
@@ -208,7 +216,7 @@ def set_refresh_cookies(response, encoded_refresh_token):
208216
if config.csrf_protect and config.csrf_in_cookies:
209217
response.set_cookie(config.refresh_csrf_cookie_name,
210218
value=get_csrf_token(encoded_refresh_token),
211-
max_age=config.cookie_max_age,
219+
max_age=max_age or config.cookie_max_age,
212220
secure=config.cookie_secure,
213221
httponly=False,
214222
domain=config.cookie_domain,

0 commit comments

Comments
 (0)