Skip to content

Commit

Permalink
update ip token association
Browse files Browse the repository at this point in the history
  • Loading branch information
zeratax committed Jul 10, 2021
1 parent 5814998 commit 4454b3c
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 15 deletions.
4 changes: 3 additions & 1 deletion alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ def run_migrations_online():

with connectable.connect() as connection:
context.configure(
connection=connection, target_metadata=target_metadata
connection=connection,
target_metadata=target_metadata,
render_as_batch=config.get_main_option('sqlalchemy.url').startswith('sqlite:///')
)

with context.begin_transaction():
Expand Down
69 changes: 69 additions & 0 deletions alembic/versions/130b5c2275d8_update_ip_token_association.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'''update ip token association
Revision ID: 130b5c2275d8
Revises: 140a25d5f185
Create Date: 2021-07-10 20:40:46.937634
'''
from alembic import op
import sqlalchemy as sa
from sqlalchemy import Table, Column, Integer, String, ForeignKey
from sqlalchemy.engine.reflection import Inspector
from flask_sqlalchemy import SQLAlchemy

# revision identifiers, used by Alembic.
revision = '130b5c2275d8'
down_revision = '140a25d5f185'
branch_labels = None
depends_on = None

db = SQLAlchemy()
conn = op.get_bind()

def upgrade():
ips = conn.execute('select id, address from ips').fetchall()
associations = conn.execute('select ips, tokens from association').fetchall()

final_associations = []
for association in associations:
association_ip, association_token = association
for ip in ips:
id, ip_address = ip
if ip_address == association_ip:
final_associations.append({'ips': id, 'tokens': association_token})

op.drop_table('association')

association = op.create_table(
'association', db.Model.metadata,
Column('ips', Integer, ForeignKey('ips.id'), primary_key=True),
Column('tokens', String(255), ForeignKey('tokens.name'), primary_key=True)
)


op.bulk_insert(association, final_associations)



def downgrade():
ips = conn.execute('select id, address from ips').fetchall()
associations = conn.execute('select ips, tokens from association').fetchall()

final_associations = []
for association in associations:
association_ip, association_token = association
for ip in ips:
id, ip_address = ip
if id == association_ip:
final_associations.append({'ips': ip_address, 'tokens': association_token})

op.drop_table('association')

association = op.create_table(
'association', db.Model.metadata,
Column('ips', String(255), ForeignKey('ips.address'), primary_key=True),
Column('tokens', String(255), ForeignKey('tokens.name'), primary_key=True)
)


op.bulk_insert(association, final_associations)
15 changes: 9 additions & 6 deletions alembic/versions/140a25d5f185_create_tokens_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,16 @@ def upgrade():
sa.Column('ips', Integer, ForeignKey('association.id'))
)
else:
with op.batch_alter_table('tokens') as batch_op:
batch_op.alter_column('ex_date', new_column_name='expiration_date', nullable=True)
batch_op.alter_column('one_time', new_column_name='max_usage')
try:
with op.batch_alter_table('tokens') as batch_op:
batch_op.alter_column('ex_date', new_column_name='expiration_date', nullable=True)
batch_op.alter_column('one_time', new_column_name='max_usage')

batch_op.add_column(
Column('disabled', Boolean, default=False)
)
batch_op.add_column(
Column('disabled', Boolean, default=False)
)
except KeyError:
pass


if 'association' not in tables:
Expand Down
2 changes: 1 addition & 1 deletion matrix_registration/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
from . import tokens
from . import config

__version__ = '1.0.0.dev4'
__version__ = '1.0.0.dev5'
name = 'matrix_registration'
5 changes: 4 additions & 1 deletion matrix_registration/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@ def read_config(self, dictionary):
def get_secrets(self):
with open(f"{self.secrets_dir}/secrets") as file:
for line in file:
k, v = line.split('=')
try:
k, v = line.lower().split('=')
except NameError:
logger.error(f'secret "{line}" in wrong format, please use "key=value"')
setattr(self, k.strip(), v.strip())


Expand Down
9 changes: 3 additions & 6 deletions matrix_registration/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def random_readable_string(length=3, wordlist=WORD_LIST_PATH):


association_table = Table('association', db.Model.metadata,
Column('ips', String, ForeignKey('ips.address'), primary_key=True),
Column('tokens', Integer, ForeignKey('tokens.name'), primary_key=True))
Column('ips', Integer, ForeignKey('ips.id'), primary_key=True),
Column('tokens', String(255), ForeignKey('tokens.name'), primary_key=True))


class IP(db.Model):
Expand Down Expand Up @@ -146,10 +146,7 @@ def use(self, token_name, ip_address=False):
token = self.get_token(token_name)
if token:
if token.use(ip_address):
try:
session.commit()
except exc.IntegrityError:
logger.warning("User already used this token before!")
session.commit()
return True
return False

Expand Down

0 comments on commit 4454b3c

Please sign in to comment.