From d2f349433c08d2317bc2de6b21dd16cdb58f400c Mon Sep 17 00:00:00 2001
From: Munyoki Kilyungi
Date: Tue, 12 Mar 2024 02:41:57 +0300
Subject: 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>
---
 .../oauth2/models/authorization_code.py               | 19 ++++++++++---------
 1 file 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)
-- 
cgit v1.2.3