# OAuth2 update

* integrates authlib into project
* adds authlib package to requirements.txt
* refactors internal konova/utils/generators/generate_token method to use authlib's generate_token method and act as wrapper for backwards compatibility
* refactors OAuth views to be based on authlib's oauth client implementation
* refactors oauth model to work with authlib's oauth2 tokens
This commit is contained in:
2026-10-06 11:27:50 +02:00
parent 8de467ec24
commit e3d5ee82ee
4 changed files with 77 additions and 90 deletions
+10 -10
View File
@@ -1,7 +1,9 @@
import datetime
import json
from datetime import timedelta
import requests
from authlib.oauth2.rfc6749 import OAuth2Token
from django.core.exceptions import ObjectDoesNotExist
from django.db import models
from django.utils import timezone
@@ -78,27 +80,25 @@ class OAuthToken(UuidModel):
return str(self.access_token)
@staticmethod
def from_access_token_response(access_token_data: str, received_on):
def create_from_oauth2_token(oauth2_token: OAuth2Token):
"""
Creates an OAuthToken based on retrieved access token data (OAuth2.0 specification)
Args:
access_token_data (str): OAuth2.0 response data
received_on (): Timestamp when the response has been received
oauth2_token (str): OAuth2.0 response data
Returns:
"""
oauth_token = OAuthToken()
data = json.loads(access_token_data)
oauth_token.access_token = data.get("access_token")
oauth_token.refresh_token = data.get("refresh_token")
oauth_token.access_token = oauth2_token.get("access_token")
oauth_token.refresh_token = oauth2_token.get("refresh_token")
oauth_token.expires_on = datetime.datetime.fromtimestamp(oauth2_token.get("expires_at"))
expires_on = received_on + timedelta(
seconds=(data.get("expires_in") + OAuthToken.ASSUMED_LATENCY)
)
oauth_token.expires_on = expires_on
assert (oauth_token.access_token is not None and
oauth_token.refresh_token is not None and
oauth_token.expires_on is not None)
return oauth_token