aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMunyoki Kilyungi2024-03-12 02:41:57 +0300
committerMunyoki Kilyungi2024-03-13 15:34:57 +0300
commitd2f349433c08d2317bc2de6b21dd16cdb58f400c (patch)
tree01acf73c2e6382a3b2f6097c6f520b62fce351ff
parent1c3d0fc73dfe4682ff41a2c8bd84a29f2d2b130a (diff)
downloadgn-auth-d2f349433c08d2317bc2de6b21dd16cdb58f400c.tar.gz
Define AuthorisationCode using frozen dataclass.
* gn_auth/auth/authentication/oauth2/models/authorization_code.py: Import dataclass, asdict, cached_property and AuthorizationCodeMixin. Remove NamedTuple import. (AuthorisationCode): Use frozen dataclass and explicitly inherit from AuthorizationCodeMixin. Delete unnecessary comment. (AuthorisationCode.response_type): Make this a cached_property. (AuthorisationCode.get_nonce): Delete. This is not defined in the RFC6749 spec. (save_authorisation_code): Replace _asdict() with asdict(...). Signed-off-by: Munyoki Kilyungi <me@bonfacemunyoki.com>
-rw-r--r--gn_auth/auth/authentication/oauth2/models/authorization_code.py19
1 files changed, 10 insertions, 9 deletions
diff --git a/gn_auth/auth/authentication/oauth2/models/authorization_code.py b/gn_auth/auth/authentication/oauth2/models/authorization_code.py
index 7bce0ca..be5fdad 100644
--- a/gn_auth/auth/authentication/oauth2/models/authorization_code.py
+++ b/gn_auth/auth/authentication/oauth2/models/authorization_code.py
@@ -1,7 +1,10 @@
"""Model and functions for handling the Authorisation Code"""
-from uuid import UUID
from datetime import datetime
-from typing import NamedTuple
+from dataclasses import dataclass, asdict
+from functools import cached_property
+from uuid import UUID
+from authlib.oauth2.rfc6749 import AuthorizationCodeMixin
+
from pymonad.tools import monad_from_none_or_value
from pymonad.maybe import Just, Maybe, Nothing
@@ -16,11 +19,12 @@ from ...users import User, user_by_id
EXPIRY_IN_SECONDS = 300 # in seconds
-class AuthorisationCode(NamedTuple):
+# pylint: disable=[too-many-instance-attributes]
+@dataclass(frozen=True)
+class AuthorisationCode(AuthorizationCodeMixin):
"""
The AuthorisationCode model for the auth(entic|oris)ation system.
"""
- # Instance variables
code_id: UUID
code: str
client: OAuth2Client
@@ -32,7 +36,7 @@ class AuthorisationCode(NamedTuple):
code_challenge_method: str
user: User
- @property
+ @cached_property
def response_type(self) -> str:
"""
For authorisation code flow, the response_type type MUST always be
@@ -52,9 +56,6 @@ class AuthorisationCode(NamedTuple):
"""Return the assigned scope for this AuthorisationCode."""
return self.scope
- def get_nonce(self):
- """Get the one-time use token."""
- return self.nonce
def authorisation_code(conn: db.DbConnection ,
code: str,
@@ -94,7 +95,7 @@ def save_authorisation_code(conn: db.DbConnection,
":auth_time, :code_challenge, :code_challenge_method, :user_id"
")",
{
- **auth_code._asdict(),
+ **asdict(auth_code),
"code_id": str(auth_code.code_id),
"client_id": str(auth_code.client.client_id),
"user_id": str(auth_code.user.user_id)