Skip to content

Commit

Permalink
validate token for websockets to get sub -> userinfo to log user email
Browse files Browse the repository at this point in the history
  • Loading branch information
indy-independence committed Feb 16, 2024
1 parent f64ce1a commit 3ceac57
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
3 changes: 1 addition & 2 deletions src/cnaas_nms/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
from cnaas_nms.api.system import api as system_api
from cnaas_nms.app_settings import api_settings, auth_settings
from cnaas_nms.tools.log import get_logger
from cnaas_nms.tools.rbac.token import Token
from cnaas_nms.tools.security import get_oauth_userinfo, oauth_required
from cnaas_nms.version import __api_version__

Expand Down Expand Up @@ -193,7 +192,7 @@ def socketio_on_connect():
# if oidc, get userinfo
if auth_settings.OIDC_ENABLED:
try:
token = Token(token_string, None)
token = oauth_required.get_token_validator("bearer").authenticate_token(token_string)
user = get_oauth_userinfo(token)["email"]
except InvalidTokenError as e:
logger.debug("InvalidTokenError: " + format(e))
Expand Down
8 changes: 4 additions & 4 deletions src/cnaas_nms/tools/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ def get_oauth_userinfo(token: Token) -> Any:
return json.loads(cached_userinfo)
except RedisError as e:
logger.debug("Redis cache error: {}".format(str(e)))
except KeyError as e:
logger.debug("KeyError: {}".format(str(e)))
except (TypeError, KeyError) as e:
logger.debug("Error while getting userinfo cache: {}".format(str(e)))

# Request the userinfo
try:
Expand Down Expand Up @@ -113,8 +113,8 @@ def get_oauth_userinfo(token: Token) -> Any:
raise InvalidTokenError("Invalid JSON in userinfo response: {}".format(str(e)))
except RedisError as e:
logger.debug("Redis cache error: {}".format(str(e)))
except KeyError as e:
logger.debug("KeyError: {}".format(str(e)))
except (TypeError, KeyError) as e:
logger.debug("Error while getting userinfo cache: {}".format(str(e)))
return resp.json()


Expand Down

0 comments on commit 3ceac57

Please sign in to comment.