canaille-globuzma/web/models.py

146 lines
4.2 KiB
Python
Raw Normal View History

2020-08-14 13:26:14 +00:00
import time
2020-08-14 11:18:08 +00:00
import datetime
from authlib.common.encoding import json_loads, json_dumps
from authlib.oauth2.rfc6749 import (
ClientMixin,
TokenMixin,
AuthorizationCodeMixin,
)
2020-08-17 13:49:48 +00:00
from .ldaputils import LDAPObjectHelper
2020-08-14 11:18:08 +00:00
class User(LDAPObjectHelper):
objectClass = ["person"]
base = "ou=users,dc=mydomain,dc=tld"
id = "cn"
def check_password(self, password):
return password == "valid"
2020-08-17 15:49:49 +00:00
@property
def name(self):
return self.cn[0]
2020-08-14 11:18:08 +00:00
class Client(LDAPObjectHelper, ClientMixin):
2020-08-14 13:26:14 +00:00
objectClass = ["oauthClient"]
2020-08-14 11:18:08 +00:00
base = "ou=clients,dc=mydomain,dc=tld"
id = "oauthClientID"
2020-08-17 13:49:48 +00:00
@property
def issue_date(self):
return datetime.datetime.strptime(self.oauthIssueDate, "%Y%m%d%H%M%SZ")
2020-08-14 11:18:08 +00:00
def get_client_id(self):
2020-08-16 18:16:57 +00:00
return self.oauthClientID
2020-08-14 11:18:08 +00:00
def get_default_redirect_uri(self):
2020-08-17 15:49:49 +00:00
return self.oauthRedirectURIs[0]
2020-08-14 11:18:08 +00:00
def get_allowed_scope(self, scope):
2020-08-16 18:16:57 +00:00
return self.oauthScope
2020-08-14 11:18:08 +00:00
def check_redirect_uri(self, redirect_uri):
2020-08-17 15:49:49 +00:00
return redirect_uri in self.oauthRedirectURIs
2020-08-14 11:18:08 +00:00
def has_client_secret(self):
2020-08-17 07:45:35 +00:00
return bool(self.oauthClientSecret)
2020-08-14 11:18:08 +00:00
def check_client_secret(self, client_secret):
2020-08-16 18:16:57 +00:00
return client_secret == self.oauthClientSecret
2020-08-14 11:18:08 +00:00
def check_token_endpoint_auth_method(self, method):
2020-08-16 18:16:57 +00:00
return method == self.oauthTokenEndpointAuthMethod
2020-08-14 11:18:08 +00:00
def check_response_type(self, response_type):
return response_type in self.oauthResponseType
def check_grant_type(self, grant_type):
return grant_type in self.oauthGrantType
2020-08-14 13:26:14 +00:00
@property
def client_info(self):
return dict(
client_id=self.client_id,
client_secret=self.client_secret,
client_id_issued_at=self.client_id_issued_at,
client_secret_expires_at=self.client_secret_expires_at,
)
@property
def client_metadata(self):
if "client_metadata" in self.__dict__:
return self.__dict__["client_metadata"]
if self._client_metadata:
data = json_loads(self._client_metadata)
self.__dict__["client_metadata"] = data
return data
return {}
def set_client_metadata(self, value):
self._client_metadata = json_dumps(value)
@property
def redirect_uris(self):
return self.client_metadata.get("redirect_uris", [])
@property
def token_endpoint_auth_method(self):
return self.client_metadata.get(
"token_endpoint_auth_method", "client_secret_basic"
)
2020-08-14 11:18:08 +00:00
class AuthorizationCode(LDAPObjectHelper, AuthorizationCodeMixin):
2020-08-16 17:39:14 +00:00
objectClass = ["oauthAuthorizationCode"]
2020-08-14 11:18:08 +00:00
base = "ou=authorizations,dc=mydomain,dc=tld"
2020-08-14 13:26:14 +00:00
id = "oauthCode"
2020-08-14 11:18:08 +00:00
def get_redirect_uri(self):
2020-08-17 15:49:49 +00:00
return self.oauthRedirectURI
2020-08-14 11:18:08 +00:00
def get_scope(self):
2020-08-17 16:02:38 +00:00
return self.oauthScope
2020-08-14 11:18:08 +00:00
2020-08-17 16:02:38 +00:00
def get_nonce(self):
return self.oauthNonce
2020-08-14 13:26:14 +00:00
def get_client_id(self):
2020-08-17 15:49:49 +00:00
return self.oauthClientID
2020-08-14 13:26:14 +00:00
def get_expires_in(self):
2020-08-17 15:49:49 +00:00
return self.oauthAuthorizationLifetime
2020-08-14 13:26:14 +00:00
def get_expires_at(self):
2020-08-17 15:49:49 +00:00
return datetime.datetime.strptime(self.oauthAuthorizationDate, "%Y%m%d%H%M%SZ") + datetime.timedelta(seconds=int(self.oauthAuthorizationLifetime))
2020-08-14 13:26:14 +00:00
2020-08-17 16:02:38 +00:00
def get_auth_time(self):
auth_time = datetime.datetime.strptime(self.oauthAuthorizationDate, "%Y%m%d%H%M%SZ")
return (auth_time - datetime.datetime(1970, 1, 1)).total_seconds()
2020-08-14 11:18:08 +00:00
class Token(LDAPObjectHelper, TokenMixin):
2020-08-16 17:39:14 +00:00
objectClass = ["oauthToken"]
2020-08-14 11:18:08 +00:00
base = "ou=tokens,dc=mydomain,dc=tld"
2020-08-16 17:39:14 +00:00
id = "oauthAccessToken"
2020-08-14 11:18:08 +00:00
def get_client_id(self):
2020-08-16 18:16:57 +00:00
return self.authzClientID
2020-08-14 11:18:08 +00:00
def get_scope(self):
2020-08-16 17:39:14 +00:00
return " ".join(self.oauthScope)
2020-08-14 11:18:08 +00:00
def get_expires_in(self):
2020-08-16 18:16:57 +00:00
return int(self.oauthTokenLifetime)
2020-08-14 11:18:08 +00:00
def get_expires_at(self):
2020-08-16 18:16:57 +00:00
issue_date = datetime.datetime.strptime(self.oauthIssueDate, "%Y%m%d%H%M%SZ")
2020-08-14 11:18:08 +00:00
issue_timestamp = (issue_date - datetime.datetime(1970, 1, 1)).total_seconds()
2020-08-16 18:16:57 +00:00
return issue_timestamp + int(self.oauthTokenLifetime)
2020-08-14 13:26:14 +00:00
def is_refresh_token_active(self):
if self.revoked:
return False
expires_at = self.issued_at + self.expires_in * 2
return expires_at >= time.time()