def generate_token(cls, owner_id):
        """Generates new token for a specified user and revokes all other
        tokens owned by this user.

        Returns:
            Value of the new token.
        """
        if owner_id is not None:
            last_hour_q = cls.query.filter(
                cls.owner_id == owner_id,
                cls.created > datetime.utcnow() - timedelta(hours=1),
            )
            if last_hour_q.count() > 0:
                raise TokenGenerationLimitException("Can't generate more than one token per hour.")
            cls.revoke_tokens(owner_id)

        new_token = cls(
            value=generate_string(TOKEN_LENGTH),
            owner_id=owner_id,
        )
        db.session.add(new_token)
        db.session.commit()

        TokenLog.create_record(new_token.value, token_log.ACTION_CREATE)

        return new_token.value
Beispiel #2
0
    def generate_token(cls, owner_id):
        """Generates new token for a specified user and revokes all other
        tokens owned by this user.

        Returns:
            Value of the new token.
        """
        if owner_id is not None:
            last_hour_q = cls.query.filter(
                cls.owner_id == owner_id,
                cls.created > datetime.utcnow() - timedelta(hours=1),
            )
            if last_hour_q.count() > 0:
                raise TokenGenerationLimitException(
                    "Can't generate more than one token per hour.")
            cls.revoke_tokens(owner_id)

        new_token = cls(
            value=generate_string(TOKEN_LENGTH),
            owner_id=owner_id,
        )
        db.session.add(new_token)
        db.session.commit()

        TokenLog.create_record(new_token.value, token_log.ACTION_CREATE)

        return new_token.value
Beispiel #3
0
 def overview(self):
     return self.render(
         'admin/stats/overview.html',
         active_user_count=AccessLog.active_user_count(),
         top_downloaders=AccessLog.top_downloaders(10),
         token_actions=TokenLog.list(10)[0],
     )
Beispiel #4
0
 def overview(self):
     return self.render(
         'admin/stats/overview.html',
         active_user_count=AccessLog.active_user_count(),
         top_downloaders=AccessLog.top_downloaders(10),
         token_actions=TokenLog.list(10)[0],
     )
Beispiel #5
0
 def token_log(self):
     page = int(request.args.get('page', default=1))
     if page < 1:
         return redirect(url_for('.token_log'))
     limit = 20
     offset = (page - 1) * limit
     token_actions, count = TokenLog.list(limit=limit, offset=offset)
     return self.render(
         'admin/stats/token-log.html',
         token_actions=token_actions,
         page=page,
         limit=limit,
         count=count,
     )
Beispiel #6
0
 def token_log(self):
     page = int(request.args.get('page', default=1))
     if page < 1:
         return redirect(url_for('.token_log'))
     limit = 20
     offset = (page - 1) * limit
     token_actions, count = TokenLog.list(limit=limit, offset=offset)
     return self.render(
         'admin/stats/token-log.html',
         token_actions=token_actions,
         page=page,
         limit=limit,
         count=count,
     )
 def revoke(self):
     self.is_active = False
     db.session.commit()
     TokenLog.create_record(self.value, token_log.ACTION_DEACTIVATE)
Beispiel #8
0
 def revoke(self):
     self.is_active = False
     db.session.commit()
     TokenLog.create_record(self.value, token_log.ACTION_DEACTIVATE)