[copr] master: Move the API token length in the config file and adjust db accordingly 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). (f6dc2ef)

bkabrda at fedorahosted.org bkabrda at fedorahosted.org
Wed Jan 9 07:51:30 UTC 2013


Repository : http://git.fedorahosted.org/cgit/copr.git

On branch  : master

>---------------------------------------------------------------

commit f6dc2efe1d749a50732f3ee72caac42df9854158
Author: Pierre-Yves Chibon <pingou at pingoured.fr>
Date:   Wed Jan 9 07:31:37 2013 +0100

    Move the API token length in the config file and adjust db accordingly
    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(-)

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)



More information about the copr-devel mailing list