@@ -107,7 +107,7 @@ def get_token():
107
107
# Python3 Compatibility 2: Decode bytes to string in order to use split()
108
108
if isinstance (decoded , bytes ):
109
109
decoded = decoded .decode ()
110
- return decoded .split (':' )[0 ]
110
+ return decoded .split (':' )[0 ]. strip ()
111
111
except (binascii .Error , UnicodeDecodeError ):
112
112
logging .getLogger (__name__ ).exception ('badly formated token %s' , auth )
113
113
flask_restful .abort (401 , message = "Unauthorized, invalid token" , status = 401 )
@@ -141,6 +141,7 @@ def has_access(region, api, abort, user):
141
141
# if jormungandr is on public mode or database is not accessible, we skip the authentication process
142
142
logging .getLogger (__name__ ).debug ('User "has_access" to region/api not cached' )
143
143
144
+ # Connection to database verified only once when cache expires.
144
145
if current_app .config .get ('PUBLIC' , False ) or (not can_connect_to_database ()):
145
146
return True
146
147
@@ -193,11 +194,13 @@ def cache_get_user(token):
193
194
194
195
def uncached_get_user (token ):
195
196
logging .getLogger (__name__ ).debug ('Get User from token (uncached)' )
196
- if not can_connect_to_database ():
197
- logging .getLogger (__name__ ).debug ('Cannot connect to database, we set User to None' )
198
- return None
199
197
try :
200
198
user = User .get_from_token (token , datetime .datetime .now ())
199
+
200
+ # if user doesn't exist for a token, get default token with user_type = no_access
201
+ if not user :
202
+ user = User .get_without_access ()
203
+ logging .getLogger (__name__ ).warning ('Invalid token : {}' .format (token [0 :10 ]))
201
204
except Exception as e :
202
205
logging .getLogger (__name__ ).error ('No access to table User (error: {})' .format (e ))
203
206
g .can_connect_to_database = False
@@ -211,6 +214,7 @@ def uncached_get_user(token):
211
214
)
212
215
@cache .memoize (current_app .config [str ('CACHE_CONFIGURATION' )].get (str ('TIMEOUT_AUTHENTICATION' ), 300 ))
213
216
def cache_get_key (token ):
217
+ # This verification is done only once when cache expires.
214
218
if not can_connect_to_database ():
215
219
return None
216
220
try :
0 commit comments