#4239 fix calculation of days until API key expires
Merged 3 months ago by pingou. Opened 3 months ago by karsten.
karsten/pagure issue-4230  into  master

file modified
+19 -6

@@ -27,8 +27,13 @@ 

  

      current_time = datetime.utcnow()

      day_diff_for_mail = [10, 5, 1]

-     email_dates = [email_day.date() for email_day in \

-             [current_time + timedelta(days=i) for i in day_diff_for_mail]]

+     email_dates = [

+         email_day.date()

+         for email_day in [

+             current_time + timedelta(days=i)

+                    for i in day_diff_for_mail

+         ]

+     ]

  

      session = pagure.lib.model_base.create_session(_config['DB_URL'])

      tokens = session.query(model.Token).all()

@@ -41,7 +46,7 @@ 

              username = user.fullname or user.username

              user_email = user.default_email

              api_key = token.id

-             days_left = token.expiration.day - datetime.utcnow().day

+             days_left = (token.expiration - datetime.utcnow()).days

              subject = 'Pagure API key expiration date is near!'

              if token.project:

                  text = '''Hi %s,

@@ -50,19 +55,27 @@ 

  Please get a new key for non-interrupted service.

  

  Thanks,

- Your Pagure Admin. ''' % (username, api_key, token.project.fullname, days_left)

+ Your Pagure Admin. ''' % (

+                     username,

+                     api_key[:5],

+                     token.project.fullname,

+                     days_left

+                 )

              else:

                  text = '''Hi %s,

  Your Pagure API key %s will expire in %s day(s).

  Please get a new key for non-interrupted service.

  

  Thanks,

- Your Pagure Admin. ''' % (username, api_key, days_left)

+ Your Pagure Admin. ''' % (

+                     username,

+                     api_key[:5],

+                     days_left)

              if not check:

                  msg = pagure.lib.notify.send_email(text, subject, user_email)

              else:

                  print('Sending email to %s (%s) about key: %s' % (

-                     username, user_emailk, token.id))

+                     username, user_email, token.id))

              if debug:

                  print('Sent mail to %s' % username)

  

file modified
+13 -11

@@ -2021,24 +2021,26 @@ 

  

      authorized_users = [project.user.username]

      authorized_users.extend(

-         [user.user for user in project.access_users['admin']])

+         [user.user for user in project.access_users["admin"]]

+     )

      if flask.g.fas_user.user not in authorized_users:

          raise pagure.exceptions.APIError(

-             401, error_code=APIERROR.ENOTHIGHENOUGH)

+             401, error_code=APIERROR.ENOTHIGHENOUGH

+         )

  

      user_obj = pagure.lib.query.search_user(

-         flask.g.session, username=flask.g.fas_user.user)

+         flask.g.session, username=flask.g.fas_user.user

+     )

      user_project_tokens = [

-         token for token in user_obj.tokens if token.project_id == project.id]

+         token for token in user_obj.tokens if token.project_id == project.id

+     ]

  

      connector = {

-         'hook_token': project.hook_token,

-         'api_tokens': [

-             {'description': t.description,

-              'id': t.id,

-              'expired': t.expired

-              } for t in user_project_tokens

-         ]

+         "hook_token": project.hook_token,

+         "api_tokens": [

+             {"description": t.description, "id": t.id, "expired": t.expired}

+             for t in user_project_tokens

+         ],

      }

  

      return flask.jsonify({"connector": connector, "status": "ok"})

fix issue 4230
fix issue 4231

API key reminder should not send the API key in clear text. Shorten displayed
key to 5 chars.

Calculation of remaining days until a API key expires was wrong as it took into account only the days part of the date and ignored month or year.

Signed-off-by: Karsten Hopp karsten@redhat.com

Nice and easy, thanks for fixing these :)

rebased onto f3d98dd6694faafa122f2f0efefa2ee992fe578c

3 months ago

I'm rewriting the commit message before merging this though :)

rebased onto 9905fb1

3 months ago

Pull-Request has been merged by pingou

3 months ago