canaille-globuzma/web/models.py

215 lines
6 KiB
Python
Raw Normal View History

import ldap
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-20 12:30:42 +00:00
util,
2020-08-14 11:18:08 +00:00
)
2020-08-19 14:20:57 +00:00
from flask import current_app, session
2020-08-17 13:49:48 +00:00
from .ldaputils import LDAPObjectHelper
2020-08-14 11:18:08 +00:00
class User(LDAPObjectHelper):
2020-08-19 14:56:04 +00:00
objectClass = ["person", "simpleSecurityObject", "uidObject"]
2020-08-18 15:39:34 +00:00
base = "ou=users"
2020-08-14 11:18:08 +00:00
id = "cn"
2020-08-19 14:20:57 +00:00
admin = False
@classmethod
2020-08-20 08:45:33 +00:00
def get(cls, dn=None, filter=None, conn=None):
2020-08-19 14:20:57 +00:00
conn = conn or cls.ldap()
user = super().get(dn, filter, conn)
admin_filter = current_app.config["LDAP"].get("ADMIN_FILTER")
if (
admin_filter
and user
and conn.search_s(user.dn, ldap.SCOPE_SUBTREE, admin_filter)
):
user.admin = True
return user
2020-08-20 08:45:33 +00:00
@classmethod
2020-08-21 08:23:39 +00:00
def authenticate(cls, login, password, signin=False):
2020-08-20 08:45:33 +00:00
filter = current_app.config["LDAP"].get("USER_FILTER").format(login=login)
user = User.get(filter=filter)
if not user or not user.check_password(password):
return None
2020-08-19 14:20:57 +00:00
2020-08-21 08:23:39 +00:00
if signin:
user.login()
2020-08-20 08:45:33 +00:00
return user
2020-08-14 11:18:08 +00:00
2020-08-21 08:23:39 +00:00
def login(self):
session["user_dn"] = self.dn
def logout(self):
try:
del session["user_dn"]
except KeyError:
pass
2020-08-14 11:18:08 +00:00
def check_password(self, password):
conn = ldap.initialize(current_app.config["LDAP"]["URI"])
try:
conn.simple_bind_s(self.dn, password)
return True
except ldap.INVALID_CREDENTIALS:
return False
finally:
conn.unbind_s()
2020-08-14 11:18:08 +00:00
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-18 15:39:34 +00:00
base = "ou=clients"
2020-08-14 11:18:08 +00:00
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-20 12:30:42 +00:00
return util.list_to_scope(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):
2020-08-21 08:06:53 +00:00
return all(r in self.oauthResponseType for r in response_type.split(" "))
2020-08-14 11:18:08 +00:00
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-18 15:39:34 +00:00
base = "ou=authorizations"
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
2020-08-17 16:49:05 +00:00
def is_expired(self):
return (
datetime.datetime.strptime(self.oauthAuthorizationDate, "%Y%m%d%H%M%SZ")
+ datetime.timedelta(seconds=int(self.oauthAuthorizationLifetime))
< datetime.datetime.now()
)
2020-08-14 13:26:14 +00:00
2020-08-17 16:02:38 +00:00
def get_auth_time(self):
2020-08-17 16:49:05 +00:00
auth_time = datetime.datetime.strptime(
self.oauthAuthorizationDate, "%Y%m%d%H%M%SZ"
)
2020-08-17 16:02:38 +00:00
return (auth_time - datetime.datetime(1970, 1, 1)).total_seconds()
2020-08-24 12:44:32 +00:00
@property
def code_challenge(self):
return self.oauthCodeChallenge
2020-08-14 11:18:08 +00:00
class Token(LDAPObjectHelper, TokenMixin):
2020-08-16 17:39:14 +00:00
objectClass = ["oauthToken"]
2020-08-18 15:39:34 +00:00
base = "ou=tokens"
2020-08-16 17:39:14 +00:00
id = "oauthAccessToken"
2020-08-14 11:18:08 +00:00
2020-08-24 13:38:11 +00:00
@property
def revoked(self):
return self.oauthRevoked in ("yes", "YES", 1, "on", "ON", "TRUE", "true")
@revoked.setter
def revoked(self, value):
if value:
2020-08-24 13:56:30 +00:00
self.oauthRevoked = "TRUE"
2020-08-24 13:38:11 +00:00
else:
2020-08-24 13:56:30 +00:00
self.oauthRevoked = "FALSE"
2020-08-24 13:38:11 +00:00
2020-08-14 11:18:08 +00:00
def get_client_id(self):
2020-08-24 08:52:21 +00:00
return self.oauthClientID
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
2020-08-24 12:44:32 +00:00
def get_issued_at(self):
issue_date = datetime.datetime.strptime(self.oauthIssueDate, "%Y%m%d%H%M%SZ")
return (issue_date - datetime.datetime(1970, 1, 1)).total_seconds()
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):
2020-08-24 13:38:11 +00:00
if self.revoked:
return False
2020-08-24 08:52:21 +00:00
return (
datetime.datetime.strptime(self.oauthIssueDate, "%Y%m%d%H%M%SZ")
+ datetime.timedelta(seconds=int(self.oauthTokenLifetime))
>= datetime.datetime.now()
)