<commit_before>from config import MAIL_FROM, SALT
from datetime import datetime
from util.mail import Mail
from util.secret import Secret
from util.user import User
from util.validator import Validator

class API(object):
    def __init__(self, db):
        self.user = User(db)

    def changeemail(self, oldemail, newemail, password):
        Validator.email(oldemail)
        Validator.email(newemail)
        Validator.password(password)

        if self.user.get(oldemail)['password'] != Secret.hash(password, SALT):
            raise Exception('The password is incorrect for this user.')
        
        token = Secret.generate(16)

        self.user.update(oldemail, email=newemail, enabled=False, token=token)

        Mail.send(MAIL_FROM, newemail, 'RiverID Email Change', token)

        return dict(oldemail=oldemail, newemail=newemail)

    def changepassword(self, email, oldpassword, newpassword):
        Validator.email(email)
        Validator.password(<PASSWORD>)
        Validator.password(<PASSWORD>)

        if self.user.get(email)['password'] != Secret.hash(oldpassword, SALT):
            raise Exception('The old password is incorrect for this user.')

        self.user.update(email, password=Secret.hash(newpassword, SALT))

        return dict(email=email)

    def checkpassword(self, email, password):
        Validator.email(email)
        Validator.password(password)

        return dict(email=email, valid=self.user.get(email)['password'] == Secret.hash(password, SALT)}
    
    def confirmemail(self, email, token):
        Validator.email(email)
        Validator.token(token)

        user = self.user.get(email)

        if not user['token']:
            raise Exception('This email address has already been confirmed.')

        if user['token'] != token:
            raise Exception('The token is not valid for this email address.')

        self.user.update(email, enabled=True, token=False)

        return dict(email=email)

    def currentsessions(self, email, session):
        Validator.email(email)
        Validator.session(session)

        return dict(email=email, sessions=self.user.get(email)['sessions'])

    def registered(self, email):
        Validator.email(email)

        return dict(email=email, registered=self.user.exists(email))

    def requestpassword(self, email):
        Validator.email(email)

        token = Secret.generate(16)

        if self.user.exists(email):
            subject = 'RiverID Change Password'
            self.user.update(email, token=token)
        else:
            subject = 'RiverID Registration'
            self.user.insert(email, enabled=False, token=token)

        Mail.send(MAIL_FROM, email, subject, token)

        return dict(email=email)

    def setpassword(self, email, token, password):
        Validator.email(email)
        Validator.token(token)
        Validator.password(password)

        user = self.user.get(email)

        if not user['token']:
            raise Exception('No password change has been requested for this email address.')

        if user['token'] != token:
            raise Exception('The token is not valid for this email address.')

        self.user.update(email, enabled=True, token=False, password=Secret.hash(password, SALT))

        return dict(email=email)

    def signin(self, email, password):
        Validator.email(email)
        Validator.password(password)

        id = Secret.generate(64)
        start = datetime.isoformat(datetime.utcnow())

        self.user.add(email, 'session', id=id, start=start)

        return dict(email=email, id=id, start=start)

    def signout(self, email, session):
        Validator.email(email)<commit_msg>Some syntactical sugar<commit_after>from config import MAIL_FROM, SALT
from datetime import datetime
from util.mail import Mail
from util.secret import Secret
from util.user import User
from util.validator import Validator

class API(object):
    def __init__(self, db):
        self.user = User(db)

    def changeemail(self, oldemail, newemail, password):
        Validator.email(oldemail)
        Validator.email(newemail)
        Validator.password(password)

        if self.user.get(oldemail)['password'] != Secret.hash(password, SALT):
            raise Exception('The password is incorrect for this user.')
        
        token = Secret.generate(16)

        self.user.update(oldemail, email=newemail, enabled=False, token=token)

        Mail.send(MAIL_FROM, newemail, 'RiverID Email Change', token)

        return dict(oldemail=oldemail, newemail=newemail)

    def changepassword(self, email, oldpassword, newpassword):
        Validator.email(email)
        Validator.password(oldpassword)
        Validator.password(newpassword)

        if self.user.get(email)['password'] != Secret.hash(oldpassword, SALT):
            raise Exception('The old password is incorrect for this user.')

        self.user.update(email, password=Secret.hash(newpassword, SALT))

        return dict(email=email)

    def checkpassword(self, email, password):
        Validator.email(email)
        Validator.password(password)

        return dict(email=email, valid=self.user.get(email)['password'] == Secret.hash(password, SALT)}
    
    def confirmemail(self, email, token):
        Validator.email(email)
        Validator.token(token)

        user = self.user.get(email)

        if not user['token']:
            raise Exception('This email address has already been confirmed.')

        if user['token'] != token:
            raise Exception('The token is not valid for this email address.')

        self.user.update(email, enabled=True, token=False)

        return dict(email=email)

    def currentsessions(self, email, session):
        Validator.email(email)
        Validator.session(session)

        return dict(email=email, sessions=self.user.get(email)['sessions'])

    def registered(self, email):
        Validator.email(email)

        return dict(email=email, registered=self.user.exists(email))

    def requestpassword(self, email):
        Validator.email(email)

        token = Secret.generate(16)

        if self.user.exists(email):
            subject = 'RiverID Change Password'
            self.user.update(email, token=token)
        else:
            subject = 'RiverID Registration'
            self.user.insert(email, enabled=False, token=token)

        Mail.send(MAIL_FROM, email, subject, token)

        return dict(email=email)

    def setpassword(self, email, token, password):
        Validator.email(email)
        Validator.token(token)
        Validator.password(password)

        user = self.user.get(email)

        if not user['token']:
            raise Exception('No password change has been requested for this email address.')

        if user['token'] != token:
            raise Exception('The token is not valid for this email address.')

        self.user.update(email, enabled=True, token=False, password=Secret.hash(password, SALT))

        return dict(email=email)

    def signin(self, email, password):
        Validator.email(email)
        Validator.password(password)

        id = Secret.generate(64)
        start = datetime.utcnow().isoformat()

        self.user.add(email, 'session', id=id, start=start)

        return dict(email=email, id=id, start=start)

    def signout(self, email, session):
        Validator.email(email)
{'fraction_non_alphanumeric': 0.10896309314586995, 'fraction_numerical': 0.0016222793024199, 'mean_word_length': 2.847113884555382, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1278652', 'n_tokens_mistral': 2111, 'n_tokens_neox': 1977, 'n_words': 503}