[PATCH 2/2] Move the API token length in the config file and adjust db accordingly

Pierre-Yves Chibon pingou at pingoured.fr
Tue Jan 8 19:12:51 UTC 2013


Up to now the API token length was hard-coded in the code to 30 chars
long. With this commit this limit is now set in the configuration
file of the application.
In order for this to make sense, we need to bump the size of the field
in the database to 255 (should be more than enough).

NOTE: the alembic upgrade won't work on sqlite which consider pretty
much only two types of field: numeric or text. So it has no problem
storing a 100 chars long string in a varchar(40).
---
 .../versions/2e30169e58ce_change_api_token_len.py  |   28 ++++++++++++++++++++
 coprs_frontend/coprs/config.py                     |    3 ++
 coprs_frontend/coprs/views/misc.py                 |    4 +-
 3 files changed, 33 insertions(+), 2 deletions(-)
 create mode 100644 coprs_frontend/alembic/versions/2e30169e58ce_change_api_token_len.py

diff --git a/coprs_frontend/alembic/versions/2e30169e58ce_change_api_token_len.py b/coprs_frontend/alembic/versions/2e30169e58ce_change_api_token_len.py
new file mode 100644
index 0000000..14b2708
--- /dev/null
+++ b/coprs_frontend/alembic/versions/2e30169e58ce_change_api_token_len.py
@@ -0,0 +1,28 @@
+"""Change api_token length from varchar(40) to varchar(255)
+
+Revision ID: 2e30169e58ce
+Revises: 32ba137a3d56
+Create Date: 2013-01-08 19:42:16.562926
+
+"""
+
+# revision identifiers, used by Alembic.
+revision = '2e30169e58ce'
+down_revision = '32ba137a3d56'
+
+from alembic import op
+import sqlalchemy as sa
+
+
+def upgrade():
+    """ Change the api_token field from the user table from varchar(40) to
+    varchar(255).
+    """
+    op.alter_column("user", "api_token", type_=sa.String(255))
+
+
+def downgrade():
+    """ Change the api_token field from the user table from varchar(255) to
+    varchar(40).
+    """
+    op.alter_column("user", "api_token", type_=sa.String(40))
diff --git a/coprs_frontend/coprs/config.py b/coprs_frontend/coprs/config.py
index bbc6c0a..ef1578f 100644
--- a/coprs_frontend/coprs/config.py
+++ b/coprs_frontend/coprs/config.py
@@ -14,6 +14,9 @@ class Config(object):
     # SQLAlchemy
     SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.abspath(DATABASE)
 
+    # Token length, defaults to 30, DB set to varchar 255
+    API_TOKEN_LENGTH = 30
+
 class ProductionConfig(Config):
     DEBUG = False
 
diff --git a/coprs_frontend/coprs/views/misc.py b/coprs_frontend/coprs/views/misc.py
index 54e7ff3..78c9721 100644
--- a/coprs_frontend/coprs/views/misc.py
+++ b/coprs_frontend/coprs/views/misc.py
@@ -55,7 +55,7 @@ def create_or_login(resp):
         expiration_date_token = datetime.date.today() \
             + datetime.timedelta(days=30)
         user = models.User(openid_name = resp.identity_url, mail = resp.email,
-            api_token = generate_api_token(),
+            api_token = generate_api_token(app.config['API_TOKEN_LENGTH']),
             api_token_expiration = expiration_date_token)
         db.session.add(user)
         db.session.commit()
@@ -114,7 +114,7 @@ def api():
 @login_required
 def api_new_token():
     user = flask.g.user
-    user.api_token = generate_api_token()
+    user.api_token = generate_api_token(app.config['API_TOKEN_LENGTH'])
     user.api_token_expiration = datetime.date.today() \
         + datetime.timedelta(days=30)
     db.session.add(user)
-- 
1.7.1



More information about the copr-devel mailing list