oncall-engine/engine/apps/mattermost/auth.py
Matias Bordese dcae98b02a
feat: add support for mattermost chatops (#5321)
Related to https://github.com/grafana/oncall/issues/96

---------

Co-authored-by: Ravishankar <ravishankar.gnanaprakasam@gmail.com>
2025-04-21 14:23:37 -03:00

29 lines
1.1 KiB
Python

import logging
import typing
from rest_framework import exceptions
from rest_framework.authentication import BaseAuthentication
from apps.mattermost.models import MattermostUser
from apps.mattermost.utils import MattermostEventAuthenticator, MattermostEventTokenInvalid
from apps.user_management.models import User
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
class MattermostEventAuthentication(BaseAuthentication):
def authenticate(self, request) -> typing.Tuple[User, None]:
if "context" not in request.data or "token" not in request.data["context"]:
raise exceptions.AuthenticationFailed("Auth token is missing")
auth = request.data["context"]["token"]
try:
MattermostEventAuthenticator.verify(auth)
mattermost_user = MattermostUser.objects.get(mattermost_user_id=request.data["user_id"])
except MattermostEventTokenInvalid:
raise exceptions.AuthenticationFailed("Invalid auth token")
except MattermostUser.DoesNotExist:
raise exceptions.AuthenticationFailed("Mattermost user not integrated")
return mattermost_user.user, None