lbry-sdk/lbrynet/lbrynet_daemon/auth/auth.py

49 lines
1.5 KiB
Python
Raw Normal View History

import logging
2016-09-21 09:49:52 +02:00
from zope.interface import implementer
from twisted.cred import portal, checkers, credentials, error as cred_error
from twisted.internet import defer
from twisted.web import resource
2016-10-07 23:29:20 +02:00
from lbrynet.lbrynet_daemon.auth.util import load_api_keys
log = logging.getLogger(__name__)
@implementer(portal.IRealm)
2016-09-21 09:49:52 +02:00
class HttpPasswordRealm(object):
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)
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)
class PasswordChecker(object):
credentialInterfaces = (credentials.IUsernamePassword,)
2016-09-21 09:49:52 +02:00
def __init__(self, passwords):
self.passwords = passwords
@classmethod
def load_file(cls, key_path):
keys = load_api_keys(key_path)
return cls.load(keys)
@classmethod
def load(cls, password_dict):
passwords = {key: password_dict[key].secret for key in password_dict}
return cls(passwords)
def requestAvatarId(self, creds):
if creds.username in self.passwords:
pw = self.passwords.get(creds.username)
pw_match = creds.checkPassword(pw)
2016-09-21 09:49:52 +02:00
if pw_match:
return defer.succeed(creds.username)
log.warning('Incorrect username or password')
return defer.fail(cred_error.UnauthorizedLogin('Incorrect username or password'))