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
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
def revoke(self): self.is_active = False db.session.commit() TokenLog.create_record(self.value, token_log.ACTION_DEACTIVATE)
def revoke(self): self.is_active = False db.session.commit() TokenLog.create_record(self.value, token_log.ACTION_DEACTIVATE)