Exemple #1
0
    def load(self, yaml_timestamp):
        """Load the data from disk.

        Load from a pickle file if available, or load the original YAML
        and write a pickle file otherwise.
        """
        try_pickle = self.try_pickle if self.try_pickle is not None else is_heroku()

        if not try_pickle:
            return self.load_uncached()

        stat = os.stat(self.pickle_filename) if os.path.exists(self.pickle_filename) else None
        if stat and stat.st_mtime >= yaml_timestamp:
            # Pickle file is newer than the YAML, just read that
            return self.load_pickle()

        data = self.load_uncached()

        # Write a pickle file, first write to a tempfile then rename
        # into place because multiple processes might try to do this in parallel,
        # plus we only want `path.exists(pickle_file)` to return True once the
        # file is actually complete and readable.
        with atomic_write_file(self.pickle_filename) as f:
            pickle.dump(data, f)

        return data
Exemple #2
0
    def login():
        body = request.json
        # Validations
        if not isinstance(body, dict):
            return 'body must be an object', 400
        if not isinstance(body.get('username'), str):
            return 'username must be a string', 400
        if not isinstance(body.get('password'), str):
            return 'password must be a string', 400

        # If username has an @-sign, then it's an email
        if '@' in body['username']:
            user = DATABASE.user_by_email(body['username'])
        else:
            user = DATABASE.user_by_username(body['username'])

        if not user:
            return 'invalid username/password', 403
        if not check_password(body['password'], user['password']):
            return 'invalid username/password', 403

        # If the number of bcrypt rounds has changed, create a new hash.
        new_hash = None
        if config['bcrypt_rounds'] != extract_bcrypt_rounds(user['password']):
            new_hash = hash(body['password'], make_salt())

        cookie = make_salt()
        DATABASE.store_token({
            'id': cookie,
            'username': user['username'],
            'ttl': times() + session_length
        })
        if new_hash:
            DATABASE.record_login(user['username'], new_hash)
        else:
            DATABASE.record_login(user['username'])
        resp = make_response({})

        # We set the cookie to expire in a year, just so that the browser won't invalidate it if the same cookie gets renewed by constant use.
        # The server will decide whether the cookie expires.
        resp.set_cookie(TOKEN_COOKIE_NAME,
                        value=cookie,
                        httponly=True,
                        secure=is_heroku(),
                        samesite='Lax',
                        path='/',
                        max_age=365 * 24 * 60 * 60)

        # Remember the current user on the session. This is "new style" logins, which should ultimately
        # replace "old style" logins (with the cookie above), as it requires fewer database calls.
        remember_current_user(user)

        return resp
Exemple #3
0
            return redirect(url, code=302)


# Unique random key for sessions.
# For settings with multiple workers, an environment variable is required, otherwise cookies will be constantly removed and re-set by different workers.
if utils.is_production():
    if not os.getenv('SECRET_KEY'):
        raise RuntimeError(
            'The SECRET KEY must be provided for non-dev environments.')

    app.config['SECRET_KEY'] = os.getenv('SECRET_KEY')

else:
    app.config['SECRET_KEY'] = os.getenv('SECRET_KEY', uuid.uuid4().hex)

if utils.is_heroku():
    app.config.update(
        SESSION_COOKIE_SECURE=True,
        SESSION_COOKIE_HTTPONLY=True,
        SESSION_COOKIE_SAMESITE='Lax',
    )

# Set security attributes for cookies in a central place - but not when running locally, so that session cookies work well without HTTPS

Compress(app)
Commonmark(app)
parse_logger = jsonbin.MultiParseLogger(jsonbin.JsonBinLogger.from_env_vars(),
                                        jsonbin.S3ParseLogger.from_env_vars())
querylog.LOG_QUEUE.set_transmitter(
    aws_helpers.s3_querylog_transmitter_from_env())
Exemple #4
0
    def signup():
        body = request.json
        # Validations, mandatory fields
        if not isinstance(body, dict):
            return 'body must be an object', 400
        if not isinstance(body.get('username'), str):
            return 'username must be a string', 400
        if '@' in body['username']:
            return 'username cannot contain an @-sign', 400
        if ':' in body['username']:
            return 'username cannot contain a colon', 400
        if len(body['username'].strip()) < 3:
            return 'username must be at least three characters long', 400
        if not isinstance(body.get('password'), str):
            return 'password must be a string', 400
        if len(body['password']) < 6:
            return 'password must be at least six characters long', 400
        if not isinstance(body.get('email'), str):
            return 'email must be a string', 400
        if not valid_email(body['email']):
            return 'email must be a valid email', 400
        # Validations, optional fields
        if 'country' in body:
            if not body['country'] in countries:
                return 'country must be a valid country', 400
        if 'birth_year' in body:
            if not isinstance(body.get('birth_year'),
                              int) or body['birth_year'] <= 1900 or body[
                                  'birth_year'] > datetime.datetime.now().year:
                return 'birth_year must be a year between 1900 and ' + datetime.datetime.now(
                ).year, 400
        if 'gender' in body:
            if body['gender'] != 'm' and body['gender'] != 'f' and body[
                    'gender'] != 'o':
                return 'gender must be m/f/o', 400
        if 'prog_experience' in body and body['prog_experience'] not in [
                'yes', 'no'
        ]:
            return 'If present, prog_experience must be "yes" or "no"', 400
        if 'experience_languages' in body:
            if not isinstance(body['experience_languages'], list):
                return 'If present, experience_languages must be an array', 400
            for language in body['experience_languages']:
                if language not in [
                        'scratch', 'other_block', 'python', 'other_text'
                ]:
                    return 'Invalid language: ' + str(language), 400

        user = DATABASE.user_by_username(body['username'].strip().lower())
        if user:
            return 'username exists', 403
        email = DATABASE.user_by_email(body['email'].strip().lower())
        if email:
            return 'email exists', 403

        hashed = hash(body['password'], make_salt())

        token = make_salt()
        hashed_token = hash(token, make_salt())
        username = body['username'].strip().lower()
        email = body['email'].strip().lower()

        if not is_testing_request(
                request) and 'subscribe' in body and body['subscribe'] == True:
            # If we have a Mailchimp API key, we use it to add the subscriber through the API
            if os.getenv('MAILCHIMP_API_KEY') and os.getenv(
                    'MAILCHIMP_AUDIENCE_ID'):
                # The first domain in the path is the server name, which is contained in the Mailchimp API key
                request_path = 'https://' + os.getenv(
                    'MAILCHIMP_API_KEY').split(
                        '-')[1] + '.api.mailchimp.com/3.0/lists/' + os.getenv(
                            'MAILCHIMP_AUDIENCE_ID') + '/members'
                request_headers = {
                    'Content-Type': 'application/json',
                    'Authorization': 'apikey ' + os.getenv('MAILCHIMP_API_KEY')
                }
                request_body = {'email_address': email, 'status': 'subscribed'}
                r = requests.post(request_path,
                                  headers=request_headers,
                                  data=json.dumps(request_body))

                subscription_error = None
                if r.status_code != 200 and r.status_code != 400:
                    subscription_error = True
                # We can get a 400 if the email is already subscribed to the list. We should ignore this error.
                if r.status_code == 400 and not re.match(
                        '.*already a list member', r.text):
                    subscription_error = True
                # If there's an error in subscription through the API, we report it to the main email address
                if subscription_error:
                    send_email(
                        config['email']['sender'],
                        'ERROR - Subscription to Hedy newsletter on signup',
                        email, '<p>' + email + '</p><pre>Status:' +
                        str(r.status_code) + '    Body:' + r.text + '</pre>')
            # Otherwise, we send an email to notify about this to the main email address
            else:
                send_email(config['email']['sender'],
                           'Subscription to Hedy newsletter on signup', email,
                           '<p>' + email + '</p>')

        user = {
            'username': username,
            'password': hashed,
            'email': email,
            'created': timems(),
            'verification_pending': hashed_token,
            'last_login': timems()
        }

        for field in [
                'country', 'birth_year', 'gender', 'prog_experience',
                'experience_languages'
        ]:
            if field in body:
                if field == 'experience_languages' and len(body[field]) == 0:
                    continue
                user[field] = body[field]

        DATABASE.store_user(user)

        print(user)

        # We automatically login the user
        cookie = make_salt()
        DATABASE.store_token({
            'id': cookie,
            'username': user['username'],
            'ttl': times() + session_length
        })

        # If this is an e2e test, we return the email verification token directly instead of emailing it.
        if is_testing_request(request):
            resp = make_response({'username': username, 'token': hashed_token})
        # Otherwise, we send an email with a verification link and we return an empty body
        else:
            send_email_template(
                'welcome_verify', email, requested_lang(),
                os.getenv('BASE_URL', 'http://localhost') +
                '/auth/verify?username='******'&token=' + urllib.parse.quote_plus(hashed_token))
            resp = make_response({})

        # We set the cookie to expire in a year, just so that the browser won't invalidate it if the same cookie gets renewed by constant use.
        # The server will decide whether the cookie expires.
        resp.set_cookie(cookie_name,
                        value=cookie,
                        httponly=True,
                        secure=is_heroku(),
                        samesite='Lax',
                        path='/',
                        max_age=365 * 24 * 60 * 60)
        return resp