2016-09-20 22:58:30 +02:00
|
|
|
import logging
|
2016-09-21 09:49:52 +02:00
|
|
|
from zope.interface import implementer
|
2016-09-20 22:58:30 +02:00
|
|
|
from twisted.cred import portal, checkers, credentials, error as cred_error
|
|
|
|
from twisted.internet import defer
|
|
|
|
from twisted.web import resource
|
2018-09-21 18:38:57 +02:00
|
|
|
from .keyring import Keyring
|
2016-09-20 22:58:30 +02:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
@implementer(portal.IRealm)
|
2018-07-22 00:34:59 +02:00
|
|
|
class HttpPasswordRealm:
|
2016-09-20 22:58:30 +02:00
|
|
|
def __init__(self, resource):
|
|
|
|
self.resource = resource
|
|
|
|
|
|
|
|
def requestAvatar(self, avatarId, mind, *interfaces):
|
2016-09-22 03:36:06 +02:00
|
|
|
log.debug("Processing request for %s", avatarId)
|
2016-09-20 22:58:30 +02:00
|
|
|
if resource.IResource in interfaces:
|
|
|
|
return (resource.IResource, self.resource, lambda: None)
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
|
2016-09-21 09:49:52 +02:00
|
|
|
@implementer(checkers.ICredentialsChecker)
|
2018-07-22 00:34:59 +02:00
|
|
|
class PasswordChecker:
|
2016-09-20 22:58:30 +02:00
|
|
|
credentialInterfaces = (credentials.IUsernamePassword,)
|
|
|
|
|
2018-09-21 18:38:57 +02:00
|
|
|
def __init__(self, keyring: Keyring):
|
|
|
|
self.api_key = keyring.api_key
|
2016-09-20 22:58:30 +02:00
|
|
|
|
|
|
|
def requestAvatarId(self, creds):
|
2018-09-21 18:38:57 +02:00
|
|
|
if creds.checkPassword(self.api_key.secret.encode()) and creds.username == self.api_key.name.encode():
|
|
|
|
return defer.succeed(creds.username)
|
2016-09-20 22:58:30 +02:00
|
|
|
log.warning('Incorrect username or password')
|
|
|
|
return defer.fail(cred_error.UnauthorizedLogin('Incorrect username or password'))
|