14.6.8. crate_anon.nlp_webserver.security

crate_anon/nlp_webserver/security.py


Copyright (C) 2015, University of Cambridge, Department of Psychiatry. Created by Rudolf Cardinal (rnc1001@cam.ac.uk).

This file is part of CRATE.

CRATE is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

CRATE is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with CRATE. If not, see <https://www.gnu.org/licenses/>.


Security functions for CRATE implementation of an NLPRP server.

class crate_anon.nlp_webserver.security.Credentials(username: str, password: str)[source]

Represents username/password credentials sent by the user to us.

__init__(username: str, password: str) None[source]
crate_anon.nlp_webserver.security.check_password(pw: str, hashed_pw: str) bool[source]

Checks a password against its hash.

Parameters:
  • pw – the clear-text password

  • hashed_pw – the stored hashed version

Returns:

do they match?

crate_anon.nlp_webserver.security.decrypt_password(encrypted_pw: bytes, cipher_suite: Fernet) str[source]

Decrypts a password using the specified cipher suite.

Parameters:
  • encrypted_pw – the encrypted password, as bytes

  • cipher_suite – a Python object with the method decrypt(encrypted_pw)

Returns:

the decrypted password as a string

crate_anon.nlp_webserver.security.encrypt_password(password: str) bytes[source]

Encrypts a password using the configured key.

crate_anon.nlp_webserver.security.generate_encryption_key() None[source]

Generates a key to be used for reversible encryption of passwords and prints it to screen. The key should then be put in the config file.

To be called via the command line.

crate_anon.nlp_webserver.security.get_auth_credentials(request: Request) Credentials | None[source]

Gets username and password as a Credentials obejct, from an HTTP request. Returns None if there is a problem.

crate_anon.nlp_webserver.security.hash_password(pw: str) str[source]

Encrypts a password using bcrypt.