Update CHANGELOG, pep8 fixes

This commit is contained in:
Griatch 2018-10-23 01:04:25 +02:00
parent 0b6d869902
commit b6b07ccdb5
10 changed files with 290 additions and 264 deletions

View file

@ -10,6 +10,30 @@
- Add the Portal uptime to the `@time` command.
- Make the `@link` command first make a local search before a global search.
### Typeclasses
- Add new methods on all typeclasses, useful specifically for viewing the object in the web/admin:
+ `web_get_admin_url()`: Returns a path that, if followed, will display the object in the Admin backend.
+ `web_get_create_url()`: Returns a path for a view allowing the creation of new instances of this object.
+ `web_get_absolute_url()`: Django construct; returns a path that should display the object in a DetailView.
+ `web_get_update_url()`: Returns a path that should display the object in an UpdateView.
+ `web_get_delete_url()`: Returns a path that should display the object in a DeleteView.
- All typeclasses has new helper class method `create`, which encompasses useful functionality
that used to be embedded for example in the respective `@create` or `@connect` commands.
- DefaultAccount now has new class methods implementing many things that used to be in unloggedin
commands (these can now be customized on the class instead):
+ `is_banned()`: Checks if a given username or IP is banned.
+ `get_username_validators`: Return list of validators for username validation (see
`settings.AUTH_USERNAME_VALIDATORS`)
+ `authenticate`: Method to check given username/password.
+ `normalize_username`: Normalizes names so you can't fake names with similar-looking Unicode
chars.
+ `validate_username`: Mechanism for validating a username.
+ `validate_password`: Mechanism for validating a password.
+ `set_password`: Apply password to account, using validation checks.
### Utils
- Added more unit tests.
@ -34,7 +58,7 @@
to terminal and can be stopped with Ctrl-C. Using `evennia reload`, or reloading in-game, will
return Server to normal daemon operation.
- For validating passwords, use safe Django password-validation backend instead of custom Evennia one.
- Alias `evennia restart` to mean the same as `evennia reload`.
- Alias `evennia restart` to mean the same as `evennia reload`.
### Prototype changes

View file

@ -370,40 +370,40 @@ class DefaultAccount(with_metaclass(TypeclassBase, AccountDB)):
def is_banned(cls, **kwargs):
"""
Checks if a given username or IP is banned.
Kwargs:
ip (str, optional): IP address.
username (str, optional): Username.
Returns:
is_banned (bool): Whether either is banned or not.
"""
ip = kwargs.get('ip', '').strip()
username = kwargs.get('username', '').lower().strip()
# Check IP and/or name bans
bans = ServerConfig.objects.conf("server_bans")
if bans and (any(tup[0] == username for tup in bans if username) or
any(tup[2].match(ip) for tup in bans if ip and tup[2])):
return True
return False
@classmethod
def get_username_validators(cls, validator_config=getattr(settings, 'AUTH_USERNAME_VALIDATORS', [])):
"""
Retrieves and instantiates validators for usernames.
Args:
validator_config (list): List of dicts comprising the battery of
validators to apply to a username.
Returns:
validators (list): List of instantiated Validator objects.
"""
objs = []
for validator in validator_config:
try:
@ -413,49 +413,49 @@ class DefaultAccount(with_metaclass(TypeclassBase, AccountDB)):
raise ImproperlyConfigured(msg % validator['NAME'])
objs.append(klass(**validator.get('OPTIONS', {})))
return objs
@classmethod
def authenticate(cls, username, password, ip='', **kwargs):
"""
Checks the given username/password against the database to see if the
Checks the given username/password against the database to see if the
credentials are valid.
Note that this simply checks credentials and returns a valid reference
to the user-- it does not log them in!
To finish the job:
After calling this from a Command, associate the account with a Session:
- session.sessionhandler.login(session, account)
...or after calling this from a View, associate it with an HttpRequest:
- django.contrib.auth.login(account, request)
Args:
username (str): Username of account
password (str): Password of account
ip (str, optional): IP address of client
Kwargs:
session (Session, optional): Session requesting authentication
Returns:
account (DefaultAccount, None): Account whose credentials were
provided if not banned.
errors (list): Error messages of any failures.
"""
errors = []
if ip: ip = str(ip)
# See if authentication is currently being throttled
if ip and LOGIN_THROTTLE.check(ip):
errors.append('Too many login failures; please try again in a few minutes.')
# With throttle active, do not log continued hits-- it is a
# waste of storage and can be abused to make your logs harder to
# read and/or fill up your disk.
return None, errors
# Check IP and/or name bans
banned = cls.is_banned(username=username, ip=ip)
if banned:
@ -465,19 +465,19 @@ class DefaultAccount(with_metaclass(TypeclassBase, AccountDB)):
logger.log_sec('Authentication Denied (Banned): %s (IP: %s).' % (username, ip))
LOGIN_THROTTLE.update(ip, 'Too many sightings of banned artifact.')
return None, errors
# Authenticate and get Account object
account = authenticate(username=username, password=password)
if not account:
# User-facing message
errors.append('Username and/or password is incorrect.')
# Log auth failures while throttle is inactive
logger.log_sec('Authentication Failure: %s (IP: %s).' % (username, ip))
# Update throttle
if ip: LOGIN_THROTTLE.update(ip, 'Too many authentication failures.')
# Try to call post-failure hook
session = kwargs.get('session', None)
if session:
@ -486,49 +486,49 @@ class DefaultAccount(with_metaclass(TypeclassBase, AccountDB)):
account.at_failed_login(session)
return None, errors
# Account successfully authenticated
logger.log_sec('Authentication Success: %s (IP: %s).' % (account, ip))
return account, errors
@classmethod
def normalize_username(cls, username):
"""
Django: Applies NFKC Unicode normalization to usernames so that visually
identical characters with different Unicode code points are considered
Django: Applies NFKC Unicode normalization to usernames so that visually
identical characters with different Unicode code points are considered
identical.
(This deals with the Turkish "i" problem and similar
annoyances. Only relevant if you go out of your way to allow Unicode
annoyances. Only relevant if you go out of your way to allow Unicode
usernames though-- Evennia accepts ASCII by default.)
In this case we're simply piggybacking on this feature to apply
additional normalization per Evennia's standards.
"""
username = super(DefaultAccount, cls).normalize_username(username)
# strip excessive spaces in accountname
username = re.sub(r"\s+", " ", username).strip()
return username
@classmethod
def validate_username(cls, username):
"""
Checks the given username against the username validator associated with
Account objects, and also checks the database to make sure it is unique.
Args:
username (str): Username to validate
Returns:
valid (bool): Whether or not the password passed validation
errors (list): Error messages of any failures
"""
valid = []
errors = []
# Make sure we're at least using the default validator
validators = cls.get_username_validators()
if not validators:
@ -541,14 +541,14 @@ class DefaultAccount(with_metaclass(TypeclassBase, AccountDB)):
except ValidationError as e:
valid.append(False)
errors.extend(e.messages)
# Disqualify if any check failed
if False in valid:
valid = False
else: valid = True
return valid, errors
@classmethod
def validate_password(cls, password, account=None):
"""
@ -608,48 +608,48 @@ class DefaultAccount(with_metaclass(TypeclassBase, AccountDB)):
super(DefaultAccount, self).set_password(password)
logger.log_sec("Password successfully changed for %s." % self)
self.at_password_change()
@classmethod
def create(cls, *args, **kwargs):
"""
Creates an Account (or Account/Character pair for MULTISESSION_MODE<2)
with default (or overridden) permissions and having joined them to the
Creates an Account (or Account/Character pair for MULTISESSION_MODE<2)
with default (or overridden) permissions and having joined them to the
appropriate default channels.
Kwargs:
username (str): Username of Account owner
password (str): Password of Account owner
email (str, optional): Email address of Account owner
ip (str, optional): IP address of requesting connection
guest (bool, optional): Whether or not this is to be a Guest account
permissions (str, optional): Default permissions for the Account
typeclass (str, optional): Typeclass to use for new Account
character_typeclass (str, optional): Typeclass to use for new char
when applicable.
Returns:
account (Account): Account if successfully created; None if not
errors (list): List of error messages in string form
"""
account = None
errors = []
username = kwargs.get('username')
password = kwargs.get('password')
email = kwargs.get('email', '').strip()
guest = kwargs.get('guest', False)
permissions = kwargs.get('permissions', settings.PERMISSION_ACCOUNT_DEFAULT)
typeclass = kwargs.get('typeclass', settings.BASE_ACCOUNT_TYPECLASS)
ip = kwargs.get('ip', '')
if ip and CREATION_THROTTLE.check(ip):
errors.append("You are creating too many accounts. Please log into an existing account.")
return None, errors
# Normalize username
username = cls.normalize_username(username)
@ -678,50 +678,50 @@ class DefaultAccount(with_metaclass(TypeclassBase, AccountDB)):
"\nIf you feel this ban is in error, please email an admin.|x"
errors.append(string)
return None, errors
# everything's ok. Create the new account account.
try:
try:
account = create.create_account(username, email, password, permissions=permissions, typeclass=typeclass)
logger.log_sec('Account Created: %s (IP: %s).' % (account, ip))
except Exception as e:
errors.append("There was an error creating the Account. If this problem persists, contact an admin.")
logger.log_trace()
return None, errors
# This needs to be set so the engine knows this account is
# logging in for the first time. (so it knows to call the right
# hooks during login later)
account.db.FIRST_LOGIN = True
# Record IP address of creation, if available
if ip: account.db.creator_ip = ip
# join the new account to the public channel
pchannel = ChannelDB.objects.get_channel(settings.DEFAULT_CHANNELS[0]["key"])
if not pchannel or not pchannel.connect(account):
string = "New account '%s' could not connect to public channel!" % account.key
errors.append(string)
logger.log_err(string)
if account and settings.MULTISESSION_MODE < 2:
# Load the appropriate Character class
character_typeclass = kwargs.get('character_typeclass', settings.BASE_CHARACTER_TYPECLASS)
character_home = kwargs.get('home')
Character = class_from_module(character_typeclass)
# Create the character
character, errs = Character.create(
account.key, account, ip=ip, typeclass=character_typeclass,
account.key, account, ip=ip, typeclass=character_typeclass,
permissions=permissions, home=character_home
)
errors.extend(errs)
if character:
# Update playable character list
account.db._playable_characters.append(character)
# We need to set this to have @ic auto-connect to this character
account.db._last_puppet = character
@ -731,7 +731,7 @@ class DefaultAccount(with_metaclass(TypeclassBase, AccountDB)):
# we won't see any errors at all.
errors.append("An error occurred. Please e-mail an admin if the problem persists.")
logger.log_trace()
# Update the throttle to indicate a new account was created from this IP
if ip and not guest: CREATION_THROTTLE.update(ip, 'Too many accounts being created.')
return account, errors
@ -1384,7 +1384,7 @@ class DefaultGuest(DefaultAccount):
This class is used for guest logins. Unlike Accounts, Guests and
their characters are deleted after disconnection.
"""
@classmethod
def create(cls, **kwargs):
"""
@ -1392,31 +1392,31 @@ class DefaultGuest(DefaultAccount):
if one is available for use.
"""
return cls.authenticate(**kwargs)
@classmethod
def authenticate(cls, **kwargs):
"""
Gets or creates a Guest account object.
Kwargs:
ip (str, optional): IP address of requestor; used for ban checking,
throttling and logging
Returns:
account (Object): Guest account object, if available
errors (list): List of error messages accrued during this request.
"""
errors = []
account = None
username = None
ip = kwargs.get('ip', '').strip()
# check if guests are enabled.
if not settings.GUEST_ENABLED:
errors.append('Guest accounts are not enabled on this server.')
return None, errors
try:
# Find an available guest name.
for name in settings.GUEST_LIST:
@ -1433,20 +1433,20 @@ class DefaultGuest(DefaultAccount):
home = settings.GUEST_HOME
permissions = settings.PERMISSION_GUEST_DEFAULT
typeclass = settings.BASE_GUEST_TYPECLASS
# Call parent class creator
account, errs = super(DefaultGuest, cls).create(
guest=True,
username=username,
password=password,
permissions=permissions,
typeclass=typeclass,
username=username,
password=password,
permissions=permissions,
typeclass=typeclass,
home=home,
ip=ip,
)
errors.extend(errs)
return account, errors
except Exception as e:
# We are in the middle between logged in and -not, so we have
# to handle tracebacks ourselves at this point. If we don't,
@ -1454,7 +1454,7 @@ class DefaultGuest(DefaultAccount):
errors.append("An error occurred. Please e-mail an admin if the problem persists.")
logger.log_trace()
return None, errors
return account, errors
def at_post_login(self, session=None, **kwargs):

View file

@ -7,10 +7,8 @@ from unittest import TestCase
from django.test import override_settings
from evennia.accounts.accounts import AccountSessionHandler
from evennia.accounts.accounts import DefaultAccount, DefaultGuest
from evennia.server.session import Session
from evennia.utils.test_resources import EvenniaTest
from evennia.utils import create
from evennia.utils.test_resources import EvenniaTest
from django.conf import settings
@ -61,78 +59,78 @@ class TestAccountSessionHandler(TestCase):
def test_count(self):
"Check count method"
self.assertEqual(self.handler.count(), len(self.handler.get()))
class TestDefaultGuest(EvenniaTest):
"Check DefaultGuest class"
ip = '212.216.134.22'
def test_authenticate(self):
# Guest account should not be permitted
account, errors = DefaultGuest.authenticate(ip=self.ip)
self.assertFalse(account, 'Guest account was created despite being disabled.')
settings.GUEST_ENABLED = True
settings.GUEST_LIST = ['bruce_wayne']
# Create a guest account
account, errors = DefaultGuest.authenticate(ip=self.ip)
self.assertTrue(account, 'Guest account should have been created.')
# Create a second guest account
account, errors = DefaultGuest.authenticate(ip=self.ip)
self.assertFalse(account, 'Two guest accounts were created with a single entry on the guest list!')
settings.GUEST_ENABLED = False
class TestDefaultAccountAuth(EvenniaTest):
def setUp(self):
super(TestDefaultAccountAuth, self).setUp()
self.password = "testpassword"
self.account.delete()
self.account = create.create_account("TestAccount%s" % randint(100000, 999999), email="test@test.com", password=self.password, typeclass=DefaultAccount)
def test_authentication(self):
"Confirm Account authentication method is authenticating/denying users."
# Valid credentials
obj, errors = DefaultAccount.authenticate(self.account.name, self.password)
self.assertTrue(obj, 'Account did not authenticate given valid credentials.')
# Invalid credentials
obj, errors = DefaultAccount.authenticate(self.account.name, 'xyzzy')
self.assertFalse(obj, 'Account authenticated using invalid credentials.')
def test_create(self):
"Confirm Account creation is working as expected."
# Create a normal account
account, errors = DefaultAccount.create(username='ziggy', password='stardust11')
self.assertTrue(account, 'New account should have been created.')
# Try creating a duplicate account
account2, errors = DefaultAccount.create(username='Ziggy', password='starman11')
self.assertFalse(account2, 'Duplicate account name should not have been allowed.')
account.delete()
def test_throttle(self):
"Confirm throttle activates on too many failures."
for x in xrange(20):
obj, errors = DefaultAccount.authenticate(self.account.name, 'xyzzy', ip='12.24.36.48')
self.assertFalse(obj, 'Authentication was provided a bogus password; this should NOT have returned an account!')
self.assertTrue('too many login failures' in errors[-1].lower(), 'Failed logins should have been throttled.')
def test_username_validation(self):
"Check username validators deny relevant usernames"
# Should not accept Unicode by default, lest users pick names like this
result, error = DefaultAccount.validate_username('¯\_(ツ)_/¯')
self.assertFalse(result, "Validator allowed kanji in username.")
# Should not allow duplicate username
result, error = DefaultAccount.validate_username(self.account.name)
self.assertFalse(result, "Duplicate username should not have passed validation.")
# Should not allow username too short
result, error = DefaultAccount.validate_username('xx')
self.assertFalse(result, "2-character username passed validation.")
@ -277,17 +275,17 @@ class TestDefaultAccount(TestCase):
class TestAccountPuppetDeletion(EvenniaTest):
@override_settings(MULTISESSION_MODE=2)
def test_puppet_deletion(self):
# Check for existing chars
self.assertFalse(self.account.db._playable_characters, 'Account should not have any chars by default.')
# Add char1 to account's playable characters
self.account.db._playable_characters.append(self.char1)
self.assertTrue(self.account.db._playable_characters, 'Char was not added to account.')
# See what happens when we delete char1.
self.char1.delete()
# Playable char list should be empty.
self.assertFalse(self.account.db._playable_characters, 'Playable character list is not empty! %s' % self.account.db._playable_characters)
self.assertFalse(self.account.db._playable_characters, 'Playable character list is not empty! %s' % self.account.db._playable_characters)

View file

@ -2,17 +2,11 @@
Commands that are available from the connect screen.
"""
import re
import time
import datetime
from django.conf import settings
from django.contrib.auth import authenticate
from evennia.accounts.models import AccountDB
from evennia.objects.models import ObjectDB
from evennia.comms.models import ChannelDB
from evennia.server.models import ServerConfig
from evennia.server.sessionhandler import SESSIONS
from evennia.server.throttle import Throttle
from evennia.utils import class_from_module, create, logger, utils, gametime
from evennia.commands.cmdhandler import CMD_LOGINSTART
@ -26,6 +20,7 @@ __all__ = ("CmdUnconnectedConnect", "CmdUnconnectedCreate",
MULTISESSION_MODE = settings.MULTISESSION_MODE
CONNECTION_SCREEN_MODULE = settings.CONNECTION_SCREEN_MODULE
def create_guest_account(session):
"""
Creates a guest account/character for this session, if one is available.
@ -40,10 +35,10 @@ def create_guest_account(session):
"""
enabled = settings.GUEST_ENABLED
address = session.address
# Get account class
Guest = class_from_module(settings.BASE_GUEST_TYPECLASS)
# Get an available guest account
# authenticate() handles its own throttling
account, errors = Guest.authenticate(ip=address)
@ -53,6 +48,7 @@ def create_guest_account(session):
session.msg("|R%s|n" % '\n'.join(errors))
return enabled, None
def create_normal_account(session, name, password):
"""
Creates an account with the given name and password.
@ -67,9 +63,9 @@ def create_normal_account(session, name, password):
"""
# Get account class
Account = class_from_module(settings.BASE_ACCOUNT_TYPECLASS)
address = session.address
# Match account name and check password
# authenticate() handles all its own throttling
account, errors = Account.authenticate(username=name, password=password, ip=address, session=session)
@ -108,19 +104,19 @@ class CmdUnconnectedConnect(COMMAND_DEFAULT_CLASS):
"""
session = self.caller
address = session.address
args = self.args
# extract double quote parts
parts = [part.strip() for part in re.split(r"\"", args) if part.strip()]
if len(parts) == 1:
# this was (hopefully) due to no double quotes being found, or a guest login
parts = parts[0].split(None, 1)
# Guest login
if len(parts) == 1 and parts[0].lower() == "guest":
# Get Guest typeclass
Guest = class_from_module(settings.BASE_GUEST_TYPECLASS)
account, errors = Guest.authenticate(ip=address)
if account:
session.sessionhandler.login(session, account)
@ -128,14 +124,14 @@ class CmdUnconnectedConnect(COMMAND_DEFAULT_CLASS):
else:
session.msg("|R%s|n" % '\n'.join(errors))
return
if len(parts) != 2:
session.msg("\n\r Usage (without <>): connect <name> <password>")
return
# Get account class
Account = class_from_module(settings.BASE_ACCOUNT_TYPECLASS)
name, password = parts
account, errors = Account.authenticate(username=name, password=password, ip=address, session=session)
if account:
@ -168,7 +164,7 @@ class CmdUnconnectedCreate(COMMAND_DEFAULT_CLASS):
args = self.args.strip()
address = session.address
# Get account class
Account = class_from_module(settings.BASE_ACCOUNT_TYPECLASS)
@ -182,7 +178,7 @@ class CmdUnconnectedCreate(COMMAND_DEFAULT_CLASS):
"\nIf <name> or <password> contains spaces, enclose it in double quotes."
session.msg(string)
return
username, password = parts
# everything's ok. Create the new account account.

View file

@ -195,7 +195,7 @@ class DefaultObject(with_metaclass(TypeclassBase, ObjectDB)):
# lockstring of newly created objects, for easy overloading.
# Will be formatted with the appropriate attributes.
lockstring = "control:id({account_id}) or perm(Admin);delete:id({account_id}) or perm(Admin)"
objects = ObjectManager()
# on-object properties
@ -863,66 +863,66 @@ class DefaultObject(with_metaclass(TypeclassBase, ObjectDB)):
string = "This place should not exist ... contact an admin."
obj.msg(_(string))
obj.move_to(home)
@classmethod
def create(cls, key, account=None, **kwargs):
"""
Creates a basic object with default parameters, unless otherwise
specified or extended.
Provides a friendlier interface to the utils.create_object() function.
Args:
key (str): Name of the new object.
account (Account): Account to attribute this object to.
Kwargs:
description (str): Brief description for this object.
ip (str): IP address of creator (for object auditing).
Returns:
object (Object): A newly created object of the given typeclass.
errors (list): A list of errors in string form, if any.
"""
errors = []
obj = None
# Get IP address of creator, if available
ip = kwargs.pop('ip', '')
# If no typeclass supplied, use this class
kwargs['typeclass'] = kwargs.pop('typeclass', cls)
# Set the supplied key as the name of the intended object
kwargs['key'] = key
# Get a supplied description, if any
description = kwargs.pop('description', '')
# Create a sane lockstring if one wasn't supplied
lockstring = kwargs.get('locks')
if account and not lockstring:
lockstring = cls.lockstring.format(account_id=account.id)
kwargs['locks'] = lockstring
# Create object
try:
obj = create.create_object(**kwargs)
# Record creator id and creation IP
if ip: obj.db.creator_ip = ip
if account: obj.db.creator_id = account.id
# Set description if there is none, or update it if provided
if description or not obj.db.desc:
desc = description if description else "You see nothing special."
obj.db.desc = desc
except Exception as e:
errors.append("An error occurred while creating this '%s' object." % key)
logger.log_err(e)
return obj, errors
def copy(self, new_key=None):
@ -1895,81 +1895,81 @@ class DefaultCharacter(DefaultObject):
# lockstring of newly created rooms, for easy overloading.
# Will be formatted with the appropriate attributes.
lockstring = "puppet:id({character_id}) or pid({account_id}) or perm(Developer) or pperm(Developer)"
@classmethod
def create(cls, key, account, **kwargs):
"""
Creates a basic Character with default parameters, unless otherwise
specified or extended.
Provides a friendlier interface to the utils.create_character() function.
Args:
key (str): Name of the new Character.
account (obj): Account to associate this Character with. Required as
an argument, but one can fake it out by supplying None-- it will
change the default lockset and skip creator attribution.
Kwargs:
description (str): Brief description for this object.
ip (str): IP address of creator (for object auditing).
Returns:
character (Object): A newly created Character of the given typeclass.
errors (list): A list of errors in string form, if any.
"""
errors = []
obj = None
# Get IP address of creator, if available
ip = kwargs.pop('ip', '')
# If no typeclass supplied, use this class
kwargs['typeclass'] = kwargs.pop('typeclass', cls)
# Set the supplied key as the name of the intended object
kwargs['key'] = key
# Get home for character
kwargs['home'] = ObjectDB.objects.get_id(kwargs.get('home', settings.DEFAULT_HOME))
# Get permissions
kwargs['permissions'] = kwargs.get('permissions', settings.PERMISSION_ACCOUNT_DEFAULT)
# Get description if provided
description = kwargs.pop('description', '')
# Get locks if provided
locks = kwargs.pop('locks', '')
try:
# Create the Character
obj = create.create_object(**kwargs)
# Record creator id and creation IP
if ip: obj.db.creator_ip = ip
if account: obj.db.creator_id = account.id
# Add locks
if not locks and account:
# Allow only the character itself and the creator account to puppet this character (and Developers).
locks = cls.lockstring.format(**{'character_id': obj.id, 'account_id': account.id})
elif not locks and not account:
locks = cls.lockstring.format(**{'character_id': obj.id, 'account_id': -1})
obj.locks.add(locks)
# If no description is set, set a default description
if description or not obj.db.desc:
obj.db.desc = description if description else "This is a character."
except Exception as e:
errors.append("An error occurred while creating this '%s' object." % key)
logger.log_err(e)
return obj, errors
def basetype_setup(self):
"""
Setup character-specific security.
@ -2097,60 +2097,60 @@ class DefaultRoom(DefaultObject):
"""
Creates a basic Room with default parameters, unless otherwise
specified or extended.
Provides a friendlier interface to the utils.create_object() function.
Args:
key (str): Name of the new Room.
account (obj): Account to associate this Room with.
Kwargs:
description (str): Brief description for this object.
ip (str): IP address of creator (for object auditing).
Returns:
room (Object): A newly created Room of the given typeclass.
errors (list): A list of errors in string form, if any.
"""
errors = []
obj = None
# Get IP address of creator, if available
ip = kwargs.pop('ip', '')
# If no typeclass supplied, use this class
kwargs['typeclass'] = kwargs.pop('typeclass', cls)
# Set the supplied key as the name of the intended object
kwargs['key'] = key
# Get who to send errors to
kwargs['report_to'] = kwargs.pop('report_to', account)
# Get description, if provided
description = kwargs.pop('description', '')
try:
# Create the Room
obj = create.create_object(**kwargs)
# Set appropriate locks
lockstring = kwargs.get('locks', cls.lockstring.format(id=account.id))
obj.locks.add(lockstring)
# Record creator id and creation IP
if ip: obj.db.creator_ip = ip
if account: obj.db.creator_id = account.id
# If no description is set, set a default description
if description or not obj.db.desc:
obj.db.desc = description if description else "This is a room."
except Exception as e:
errors.append("An error occurred while creating this '%s' object." % key)
logger.log_err(e)
return obj, errors
def basetype_setup(self):
@ -2230,13 +2230,13 @@ class DefaultExit(DefaultObject):
exit_command = ExitCommand
priority = 101
# lockstring of newly created exits, for easy overloading.
# Will be formatted with the {id} of the creating object.
lockstring = "control:id({id}) or perm(Admin); " \
"delete:id({id}) or perm(Admin); " \
"edit:id({id}) or perm(Admin)"
# Helper classes and methods to implement the Exit. These need not
# be overloaded unless one want to change the foundation for how
# Exits work. See the end of the class for hook methods to overload.
@ -2274,72 +2274,72 @@ class DefaultExit(DefaultObject):
return exit_cmdset
# Command hooks
@classmethod
def create(cls, key, account, source, dest, **kwargs):
"""
Creates a basic Exit with default parameters, unless otherwise
specified or extended.
Provides a friendlier interface to the utils.create_object() function.
Args:
key (str): Name of the new Exit, as it should appear from the
source room.
account (obj): Account to associate this Exit with.
source (Room): The room to create this exit in.
dest (Room): The room to which this exit should go.
Kwargs:
description (str): Brief description for this object.
ip (str): IP address of creator (for object auditing).
Returns:
exit (Object): A newly created Room of the given typeclass.
errors (list): A list of errors in string form, if any.
"""
errors = []
obj = None
# Get IP address of creator, if available
ip = kwargs.pop('ip', '')
# If no typeclass supplied, use this class
kwargs['typeclass'] = kwargs.pop('typeclass', cls)
# Set the supplied key as the name of the intended object
kwargs['key'] = key
# Get who to send errors to
kwargs['report_to'] = kwargs.pop('report_to', account)
# Set to/from rooms
kwargs['location'] = source
kwargs['destination'] = dest
description = kwargs.pop('description', '')
try:
# Create the Exit
obj = create.create_object(**kwargs)
# Set appropriate locks
lockstring = kwargs.get('locks', cls.lockstring.format(id=account.id))
obj.locks.add(lockstring)
# Record creator id and creation IP
if ip: obj.db.creator_ip = ip
if account: obj.db.creator_id = account.id
# If no description is set, set a default description
if description or not obj.db.desc:
obj.db.desc = description if description else "This is an exit."
except Exception as e:
errors.append("An error occurred while creating this '%s' object." % key)
logger.log_err(e)
return obj, errors
def basetype_setup(self):

View file

@ -1,10 +1,11 @@
from evennia.utils.test_resources import EvenniaTest
from evennia import DefaultObject, DefaultCharacter, DefaultRoom, DefaultExit
class DefaultObjectTest(EvenniaTest):
ip = '212.216.139.14'
def test_object_create(self):
description = 'A home for a grouch.'
obj, errors = DefaultObject.create('trashcan', self.account, description=description, ip=self.ip)
@ -12,7 +13,7 @@ class DefaultObjectTest(EvenniaTest):
self.assertFalse(errors, errors)
self.assertEqual(description, obj.db.desc)
self.assertEqual(obj.db.creator_ip, self.ip)
def test_character_create(self):
description = 'A furry green monster, reeking of garbage.'
obj, errors = DefaultCharacter.create('oscar', self.account, description=description, ip=self.ip)
@ -20,7 +21,7 @@ class DefaultObjectTest(EvenniaTest):
self.assertFalse(errors, errors)
self.assertEqual(description, obj.db.desc)
self.assertEqual(obj.db.creator_ip, self.ip)
def test_room_create(self):
description = 'A dimly-lit alley behind the local Chinese restaurant.'
obj, errors = DefaultRoom.create('alley', self.account, description=description, ip=self.ip)
@ -28,7 +29,7 @@ class DefaultObjectTest(EvenniaTest):
self.assertFalse(errors, errors)
self.assertEqual(description, obj.db.desc)
self.assertEqual(obj.db.creator_ip, self.ip)
def test_exit_create(self):
description = 'The steaming depths of the dumpster, ripe with refuse in various states of decomposition.'
obj, errors = DefaultExit.create('in', self.account, self.room1, self.room2, description=description, ip=self.ip)
@ -43,4 +44,4 @@ class DefaultObjectTest(EvenniaTest):
self.assertTrue('admin' in self.char1.web_get_admin_url())
self.assertTrue(self.room1.get_absolute_url())
self.assertTrue('admin' in self.room1.web_get_admin_url())
self.assertTrue('admin' in self.room1.web_get_admin_url())

View file

@ -323,31 +323,31 @@ class DefaultScript(ScriptBase):
or describe a state that changes under certain conditions.
"""
@classmethod
def create(cls, key, **kwargs):
"""
Provides a passthrough interface to the utils.create_script() function.
Args:
key (str): Name of the new object.
Returns:
object (Object): A newly created object of the given typeclass.
errors (list): A list of errors in string form, if any.
"""
errors = []
obj = None
kwargs['key'] = key
try:
obj = create.create_script(**kwargs)
except Exception as e:
errors.append("The script '%s' encountered errors and could not be created." % key)
logger.log_err(e)
return obj, errors
def at_script_creation(self):

View file

@ -2,25 +2,26 @@ from collections import defaultdict, deque
from evennia.utils import logger
import time
class Throttle(object):
"""
Keeps a running count of failed actions per IP address.
Keeps a running count of failed actions per IP address.
Available methods indicate whether or not the number of failures exceeds a
particular threshold.
This version of the throttle is usable by both the terminal server as well
as the web server, imposes limits on memory consumption by using deques
with length limits instead of open-ended lists, and removes sparse keys when
no recent failures have been recorded.
"""
error_msg = 'Too many failed attempts; you must wait a few minutes before trying again.'
def __init__(self, **kwargs):
"""
Allows setting of throttle parameters.
Kwargs:
limit (int): Max number of failures before imposing limiter
timeout (int): number of timeout seconds after
@ -32,67 +33,67 @@ class Throttle(object):
self.storage = defaultdict(deque)
self.cache_size = self.limit = kwargs.get('limit', 5)
self.timeout = kwargs.get('timeout', 5 * 60)
def get(self, ip=None):
"""
Convenience function that returns the storage table, or part of.
Args:
ip (str, optional): IP address of requestor
Returns:
storage (dict): When no IP is provided, returns a dict of all
current IPs being tracked and the timestamps of their recent
storage (dict): When no IP is provided, returns a dict of all
current IPs being tracked and the timestamps of their recent
failures.
timestamps (deque): When an IP is provided, returns a deque of
timestamps (deque): When an IP is provided, returns a deque of
timestamps of recent failures only for that IP.
"""
if ip: return self.storage.get(ip, deque(maxlen=self.cache_size))
else: return self.storage
def update(self, ip, failmsg='Exceeded threshold.'):
"""
Store the time of the latest failure.
Args:
ip (str): IP address of requestor
failmsg (str, optional): Message to display in logs upon activation
of throttle.
Returns:
None
"""
# Get current status
previously_throttled = self.check(ip)
# Enforce length limits
if not self.storage[ip].maxlen:
self.storage[ip] = deque(maxlen=self.cache_size)
self.storage[ip].append(time.time())
# See if this update caused a change in status
currently_throttled = self.check(ip)
# If this makes it engage, log a single activation event
if (not previously_throttled and currently_throttled):
logger.log_sec('Throttle Activated: %s (IP: %s, %i hits in %i seconds.)' % (failmsg, ip, self.limit, self.timeout))
def check(self, ip):
"""
This will check the session's address against the
storage dictionary to check they haven't spammed too many
storage dictionary to check they haven't spammed too many
fails recently.
Args:
ip (str): IP address of requestor
Returns:
throttled (bool): True if throttling is active,
False otherwise.
"""
now = time.time()
ip = str(ip)
@ -110,5 +111,3 @@ class Throttle(object):
return False
else:
return False

View file

@ -4,31 +4,32 @@ from django.utils.translation import gettext as _
from evennia.accounts.models import AccountDB
import re
class EvenniaUsernameAvailabilityValidator:
"""
Checks to make sure a given username is not taken or otherwise reserved.
"""
def __call__(self, username):
"""
Validates a username to make sure it is not in use or reserved.
Args:
username (str): Username to validate
Returns:
None (None): None if password successfully validated,
raises ValidationError otherwise.
"""
# Check guest list
if (settings.GUEST_LIST and username.lower() in (guest.lower() for guest in settings.GUEST_LIST)):
raise ValidationError(
_('Sorry, that username is reserved.'),
code='evennia_username_reserved',
)
# Check database
exists = AccountDB.objects.filter(username__iexact=username).exists()
if exists:
@ -37,33 +38,36 @@ class EvenniaUsernameAvailabilityValidator:
code='evennia_username_taken',
)
class EvenniaPasswordValidator:
def __init__(self, regex=r"^[\w. @+\-',]+$", policy="Password should contain a mix of letters, spaces, digits and @/./+/-/_/'/, only."):
def __init__(self, regex=r"^[\w. @+\-',]+$",
policy="Password should contain a mix of letters, "
"spaces, digits and @/./+/-/_/'/, only."):
"""
Constructs a standard Django password validator.
Args:
regex (str): Regex pattern of valid characters to allow.
policy (str): Brief explanation of what the defined regex permits.
"""
self.regex = regex
self.policy = policy
def validate(self, password, user=None):
"""
Validates a password string to make sure it meets predefined Evennia
acceptable character policy.
Args:
password (str): Password to validate
user (None): Unused argument but required by Django
Returns:
None (None): None if password successfully validated,
raises ValidationError otherwise.
"""
# Check complexity
if not re.findall(self.regex, password):
@ -76,11 +80,12 @@ class EvenniaPasswordValidator:
"""
Returns a user-facing explanation of the password policy defined
by this validator.
Returns:
text (str): Explanation of password policy.
"""
return _(
"%s From a terminal client, you can also use a phrase of multiple words if you enclose the password in double quotes." % self.policy
)
"%s From a terminal client, you can also use a phrase of multiple words if "
"you enclose the password in double quotes." % self.policy
)

View file

@ -222,7 +222,8 @@ COMMAND_RATE_WARNING = "You entered commands too fast. Wait a moment and try aga
# 0 or less.
MAX_CHAR_LIMIT = 6000
# The warning to echo back to users if they enter a very large string
MAX_CHAR_LIMIT_WARNING = "You entered a string that was too long. Please break it up into multiple parts."
MAX_CHAR_LIMIT_WARNING = ("You entered a string that was too long. "
"Please break it up into multiple parts.")
# If this is true, errors and tracebacks from the engine will be
# echoed as text in-game as well as to the log. This can speed up
# debugging. OBS: Showing full tracebacks to regular users could be a
@ -410,12 +411,14 @@ CMDSET_CHARACTER = "commands.default_cmdsets.CharacterCmdSet"
CMDSET_ACCOUNT = "commands.default_cmdsets.AccountCmdSet"
# Location to search for cmdsets if full path not given
CMDSET_PATHS = ["commands", "evennia", "contribs"]
# Fallbacks for cmdset paths that fail to load. Note that if you change the path for your default cmdsets,
# you will also need to copy CMDSET_FALLBACKS after your change in your settings file for it to detect the change.
CMDSET_FALLBACKS = {CMDSET_CHARACTER: 'evennia.commands.default.cmdset_character.CharacterCmdSet',
CMDSET_ACCOUNT: 'evennia.commands.default.cmdset_account.AccountCmdSet',
CMDSET_SESSION: 'evennia.commands.default.cmdset_session.SessionCmdSet',
CMDSET_UNLOGGEDIN: 'evennia.commands.default.cmdset_unloggedin.UnloggedinCmdSet'}
# Fallbacks for cmdset paths that fail to load. Note that if you change the path for your
# default cmdsets, you will also need to copy CMDSET_FALLBACKS after your change in your
# settings file for it to detect the change.
CMDSET_FALLBACKS = {
CMDSET_CHARACTER: 'evennia.commands.default.cmdset_character.CharacterCmdSet',
CMDSET_ACCOUNT: 'evennia.commands.default.cmdset_account.AccountCmdSet',
CMDSET_SESSION: 'evennia.commands.default.cmdset_session.SessionCmdSet',
CMDSET_UNLOGGEDIN: 'evennia.commands.default.cmdset_unloggedin.UnloggedinCmdSet'}
# Parent class for all default commands. Changing this class will
# modify all default commands, so do so carefully.
COMMAND_DEFAULT_CLASS = "evennia.commands.default.muxcommand.MuxCommand"
@ -810,7 +813,7 @@ AUTH_PASSWORD_VALIDATORS = [
{'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator'},
{'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator'},
{'NAME': 'evennia.server.validators.EvenniaPasswordValidator'}]
# Username validation plugins
AUTH_USERNAME_VALIDATORS = [
{'NAME': 'django.contrib.auth.validators.ASCIIUsernameValidator'},
@ -830,7 +833,7 @@ TEST_RUNNER = 'evennia.server.tests.EvenniaTestSuiteRunner'
# Django extesions are useful third-party tools that are not
# always included in the default django distro.
try:
import django_extensions
import django_extensions # noqa
INSTALLED_APPS = INSTALLED_APPS + ('django_extensions',)
except ImportError:
# Django extensions are not installed in all distros.