<commit_before># -*- coding: utf-8 -*-
#
#  2015-01-06 <NAME> <<EMAIL>>
#             The Password reset functions
#
# This code is free software; you can redistribute it and/or
# modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
# License as published by the Free Software Foundation; either
# version 3 of the License, or any later version.
#
# This code 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 AFFERO GENERAL PUBLIC LICENSE for more details.
#
# You should have received a copy of the GNU Affero General Public
# License along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#
from privacyidea.models import PasswordReset
from privacyidea.lib.crypto import (hash_with_pepper, verify_with_pepper,
                                    generate_password)
import logging
from privacyidea.lib.log import log_with
from privacyidea.lib.error import UserError, privacyIDEAError, ConfigAdminError
from privacyidea.lib.smtpserver import send_email_identifier
from privacyidea.lib.config import get_from_config
from privacyidea.lib.resolver import get_resolver_list
from privacyidea.lib.policy import ACTION, SCOPE, Match
from sqlalchemy import and_
from datetime import datetime


__doc__ = """
This is the library for creating a recovery code for password reset.
The recovery code is sent to the user.

The salted/peppered hash of the recovery code is stored in the passwordreset
database table.

This module is tested in tests/test_lib_passwordreset.py
"""

log = logging.getLogger(__name__)

BODY = u"""Someone requested to reset the password within privacyIDEA.

To reset your user password please visit the link

{0!s}/reset/{1!s}@{2!s}/{3!s}
"""


@log_with(log)
def create_recoverycode(user, email=None, expiration_seconds=3600,
                        recoverycode=None, base_url=""):
    """
    Create and send a password recovery code

    :param user: User for whom the password reset code should be sent
    :type user: User Object
    :param email: The optional email of the user
    :param recoverycode: Only used for testing purpose
    :return: bool
    """
    base_url = base_url.strip("recover")
    base_url += "#"
    recoverycode = recoverycode or generate_password(size=24)
    hash_code = hash_with_pepper(recoverycode)
    # send this recoverycode
    #
    pwreset = PasswordReset(hash_code, username=user.login,
                            realm=user.realm,
                            expiration_seconds=expiration_seconds)
    pwreset.save()

    res = False
    if not user:
        raise UserError("User required for recovery token.")<commit_msg>Fix the WebUI route for password recovery<commit_after># -*- coding: utf-8 -*-
#
#  2022-06-06 <NAME> <<EMAIL>>
#             Fix recovery link
#  2015-01-06 <NAME> <<EMAIL>>
#             The Password reset functions
#
# This code is free software; you can redistribute it and/or
# modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
# License as published by the Free Software Foundation; either
# version 3 of the License, or any later version.
#
# This code 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 AFFERO GENERAL PUBLIC LICENSE for more details.
#
# You should have received a copy of the GNU Affero General Public
# License along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#
from privacyidea.models import PasswordReset
from privacyidea.lib.crypto import (hash_with_pepper, verify_with_pepper,
                                    generate_password)
import logging
from privacyidea.lib.log import log_with
from privacyidea.lib.error import UserError, privacyIDEAError, ConfigAdminError
from privacyidea.lib.smtpserver import send_email_identifier
from privacyidea.lib.config import get_from_config
from privacyidea.lib.resolver import get_resolver_list
from privacyidea.lib.policy import ACTION, SCOPE, Match
from sqlalchemy import and_
from datetime import datetime


__doc__ = """
This is the library for creating a recovery code for password reset.
The recovery code is sent to the user.

The salted/peppered hash of the recovery code is stored in the passwordreset
database table.

This module is tested in tests/test_lib_passwordreset.py
"""

log = logging.getLogger(__name__)

BODY = u"""Someone requested to reset the password within privacyIDEA.

To reset your user password please visit the link

{0!s}/reset/{1!s}@{2!s}/{3!s}
"""


@log_with(log)
def create_recoverycode(user, email=None, expiration_seconds=3600,
                        recoverycode=None, base_url=""):
    """
    Create and send a password recovery code

    :param user: User for whom the password reset code should be sent
    :type user: User Object
    :param email: The optional email of the user
    :param recoverycode: Only used for testing purpose
    :return: bool
    """
    base_url = base_url.strip("recover")
    base_url += "#!"
    recoverycode = recoverycode or generate_password(size=24)
    hash_code = hash_with_pepper(recoverycode)
    # send this recoverycode
    #
    pwreset = PasswordReset(hash_code, username=user.login,
                            realm=user.realm,
                            expiration_seconds=expiration_seconds)
    pwreset.save()

    res = False
    if not user:
        raise UserError("User required for recovery token.")
{'fraction_non_alphanumeric': 0.07204147300679299, 'fraction_numerical': 0.008580622095101895, 'mean_word_length': 3.5120967741935485, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4916727', 'n_tokens_mistral': 1582, 'n_tokens_neox': 1511, 'n_words': 603}