Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Credential handling for password-less learner certificate generation #8239

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 10 additions & 3 deletions kolibri/core/auth/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,33 @@
The appropriate classes should be listed in the AUTHENTICATION_BACKENDS. Note that authentication
backends are checked in the order they're listed.
"""
from kolibri.core.auth.models import Facility
from kolibri.core.auth.models import FacilityUser


FACILITY_CREDENTIAL_NAME = "facility"


class FacilityUserBackend(object):
"""
A class that implements authentication for FacilityUsers.
"""

def authenticate(self, username=None, password=None, facility=None):
def authenticate(self, request, username=None, password=None, **kwargs):
"""
Authenticates the user if the credentials correspond to a FacilityUser for the specified Facility.

:param username: a string
:param password: a string
:param facility: a Facility
:param kwargs: a dict of additional credentials (see `keyword`s)
:keyword facility: a Facility object or facility ID
:return: A FacilityUser instance if successful, or None if authentication failed.
"""
facility = kwargs.get(FACILITY_CREDENTIAL_NAME, None)
users = FacilityUser.objects.filter(username__iexact=username)
if facility:
users = users.filter(facility=facility)
facility_id = facility.pk if isinstance(facility, Facility) else facility
users = users.filter(facility_id=facility_id)
for user in users:
if user.check_password(password):
return user
Expand Down
8 changes: 7 additions & 1 deletion kolibri/core/auth/management/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from morango.models import ScopeDefinition
from six.moves.urllib.parse import urljoin

from kolibri.core.auth.backends import FACILITY_CREDENTIAL_NAME
from kolibri.core.auth.constants.morango_sync import ScopeDefinitions
from kolibri.core.auth.models import Facility
from kolibri.core.auth.models import FacilityUser
Expand Down Expand Up @@ -227,11 +228,16 @@ def get_client_and_server_certs(
username = input("Please enter username: ")
password = getpass.getpass("Please enter password: ")

# add facility so `FacilityUserBackend` can validate
userargs = {
FacilityUser.USERNAME_FIELD: username,
FACILITY_CREDENTIAL_NAME: dataset_id,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is being passed as the facility id, but is the dataset_id - is this not facility.dataset_id or is the variable name here just confusing?

}
client_cert = nc.certificate_signing_request(
server_cert,
client_scope,
csr_scope_params,
userargs=username,
userargs=userargs,
password=password,
)
else:
Expand Down