Skip to content

Commit 7d406cf

Browse files
committed
[fix] Personal access token name fix
Until now a random string was generated as a personal access token name in the schema upgrade script that copies auth session tokens to the personal_access_tokens table. In rare cases the random generator could return the same random string twice. For this reason in PostgreSQL an incremental integer is used instead of the random name. Until now all auth session tokens became a personal access token. From now only the "can_expire = false" tokens become personal access tokens.
1 parent 99a4952 commit 7d406cf

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

web/server/codechecker_server/migrations/config/versions/7ed50f8b3fb8_new_table_for_personal_access_tokens.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,30 @@ def upgrade():
4646

4747
dialect = op.get_context().dialect.name
4848
if dialect == "sqlite":
49-
random_string = "hex(randomblob(4))"
50-
with op.batch_alter_table("auth_sessions", recreate="never") as ba:
51-
ba.alter_column("can_expire", new_column_name="can_expire_UNUSED")
49+
token_name = "hex(randomblob(4))"
5250
else:
53-
random_string = "substr(md5(random()::text), 1, 8)"
54-
op.drop_column('auth_sessions', 'can_expire')
51+
token_name = "concat('token', row_number() over ())"
5552

5653
one_year_later = datetime.now() + timedelta(days=365)
5754
op.execute(
5855
f"""
5956
INSERT INTO personal_access_tokens (user_name, token_name, token,
6057
description, last_access, expiration)
61-
SELECT user_name, {random_string}, token, description, last_access,
58+
SELECT user_name, {token_name}, token, description, last_access,
6259
'{one_year_later}'
6360
FROM auth_sessions
61+
WHERE can_expire = false
6462
""")
63+
op.execute("""
64+
DELETE FROM auth_sessions
65+
WHERE can_expire = false
66+
""")
67+
68+
if dialect == "sqlite":
69+
with op.batch_alter_table("auth_sessions", recreate="never") as ba:
70+
ba.alter_column("can_expire", new_column_name="can_expire_UNUSED")
71+
else:
72+
op.drop_column('auth_sessions', 'can_expire')
6573
# ### end Alembic commands ###
6674

6775

0 commit comments

Comments
 (0)