# What this PR does - Provide API URL when returning status to inform plugin or mobile app if it should be talking to a different backend in case of migration. - Add MobileAppAuthTokenAuthentication to status endpoint so that the app can use it. - Split PluginAuthentication (Checks user) and BasePluginAuthentication (Does not check user) and use BasePluginAuthentication in grafana-plugin app when getting status. - Removed PluginTokenVerified since it can be handled by BasePluginAuthentication. - Removed deprecated endpoints from grafana-plugin app. ## Which issue(s) this PR fixes ## Checklist - [x] Unit, integration, and e2e (if applicable) tests updated - [x] Documentation added (or `pr:no public docs` PR label added if not required) - [x] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not required)
45 lines
1.7 KiB
Python
45 lines
1.7 KiB
Python
from typing import Optional, Tuple
|
|
|
|
from rest_framework import exceptions
|
|
from rest_framework.authentication import BaseAuthentication, get_authorization_header
|
|
|
|
from apps.auth_token.exceptions import InvalidToken
|
|
from apps.user_management.models import User
|
|
|
|
from .models import MobileAppAuthToken, MobileAppVerificationToken
|
|
|
|
|
|
class MobileAppVerificationTokenAuthentication(BaseAuthentication):
|
|
model = MobileAppVerificationToken
|
|
|
|
def authenticate(self, request) -> Tuple[User, MobileAppVerificationToken]:
|
|
auth = get_authorization_header(request).decode("utf-8")
|
|
user, auth_token = self.authenticate_credentials(auth)
|
|
return user, auth_token
|
|
|
|
def authenticate_credentials(self, token_string: str) -> Tuple[User, MobileAppVerificationToken]:
|
|
try:
|
|
auth_token = self.model.validate_token_string(token_string)
|
|
except InvalidToken:
|
|
raise exceptions.AuthenticationFailed("Invalid token")
|
|
|
|
return auth_token.user, auth_token
|
|
|
|
|
|
class MobileAppAuthTokenAuthentication(BaseAuthentication):
|
|
model = MobileAppAuthToken
|
|
|
|
def authenticate(self, request) -> Optional[Tuple[User, MobileAppAuthToken]]:
|
|
auth = get_authorization_header(request).decode("utf-8")
|
|
user, auth_token = self.authenticate_credentials(auth)
|
|
if user is None:
|
|
return None
|
|
return user, auth_token
|
|
|
|
def authenticate_credentials(self, token_string: str) -> Tuple[Optional[User], Optional[MobileAppAuthToken]]:
|
|
try:
|
|
auth_token = self.model.validate_token_string(token_string)
|
|
except InvalidToken:
|
|
return None, None
|
|
|
|
return auth_token.user, auth_token
|