add usages count for every token

This commit is contained in:
norohind 2022-01-05 22:01:54 +03:00
parent bc2e13786f
commit d50df50ca1
No known key found for this signature in database
GPG Key ID: 93F01ED4F5A20178
3 changed files with 11 additions and 2 deletions

View File

@ -103,6 +103,7 @@ class CAPIAuthorizer:
return None
row['expires_over'] = int(row['expires_on']) - int(time.time())
return row
def refresh_by_state(self, state: str, force_refresh=False, failure_tolerance=True) -> dict:

View File

@ -111,7 +111,12 @@ class Model:
self.db.execute(sqlite_requests.refresh_times_increment, {'state': state})
def get_token_for_user(self, state: str) -> dict:
return self.db.execute(sqlite_requests.get_token_for_user, {'state': state}).fetchone()
sql_params = {'state': state}
token = self.db.execute(sqlite_requests.get_token_for_user, sql_params).fetchone()
with self.db:
self.db.execute(sqlite_requests.increment_usages, sql_params)
return token
def list_all_records(self) -> list:
return self.db.execute(sqlite_requests.select_nickname_state_all).fetchall()

View File

@ -8,7 +8,8 @@ schema = """create table if not exists authorizations (
expires_in text,
timestamp_got_expires_in text,
nickname text unique,
refresh_tries int default 0
refresh_tries int default 0,
usages int default 0
);"""
insert_auth_init = """insert into authorizations
@ -48,3 +49,5 @@ from authorizations where state = :state;"""
select_nickname_state_all = """select nickname, state from authorizations where nickname is not null;"""
del_orphans = """delete from authorizations where nickname is null;"""
increment_usages = "update authorizations set usages = usages + 1 where state = :state;"