Changeset - 25623c3c63aa
[Not reviewed]
default
0 5 0
Søren Løvborg - 9 years ago 2017-02-22 21:20:39
sorenl@unity3d.com
api: simplify API key expiration checks
5 files changed with 10 insertions and 23 deletions:
0 comments (0 inline, 0 general)
kallithea/lib/auth.py
Show inline comments
 
@@ -24,7 +24,6 @@ Original author and date, and relevant c
 
:copyright: (c) 2013 RhodeCode GmbH, and others.
 
:license: GPLv3, see LICENSE.md for more details.
 
"""
 
import time
 
import os
 
import logging
 
import traceback
 
@@ -37,7 +36,6 @@ from decorator import decorator
 
from pylons import request, session
 
from pylons.i18n.translation import _
 
from webhelpers.pylonslib import secure_form
 
from sqlalchemy import or_
 
from sqlalchemy.orm.exc import ObjectDeletedError
 
from sqlalchemy.orm import joinedload
 
from webob.exc import HTTPFound, HTTPBadRequest, HTTPForbidden, HTTPMethodNotAllowed
 
@@ -606,9 +604,7 @@ class AuthUser(object):
 
    def _get_api_keys(self):
 
        api_keys = [self.api_key]
 
        for api_key in UserApiKeys.query() \
 
                .filter(UserApiKeys.user_id == self.user_id) \
 
                .filter(or_(UserApiKeys.expires == -1,
 
                            UserApiKeys.expires >= time.time())).all():
 
                .filter_by(user_id=self.user_id, is_expired=False):
 
            api_keys.append(api_key.api_key)
 

	
 
        return api_keys
kallithea/model/api_key.py
Show inline comments
 
@@ -27,7 +27,6 @@ Original author and date, and relevant c
 

	
 
import time
 
import logging
 
from sqlalchemy import or_
 

	
 
from kallithea.lib.utils2 import generate_api_key
 
from kallithea.model.base import BaseModel
 
@@ -75,7 +74,5 @@ class ApiKeyModel(BaseModel):
 
        user_api_keys = UserApiKeys.query() \
 
            .filter(UserApiKeys.user_id == user.user_id)
 
        if not show_expired:
 
            user_api_keys = user_api_keys \
 
                .filter(or_(UserApiKeys.expires == -1,
 
                            UserApiKeys.expires >= time.time()))
 
            user_api_keys = user_api_keys.filter_by(is_expired=False)
 
        return user_api_keys
kallithea/model/db.py
Show inline comments
 
@@ -618,11 +618,7 @@ class User(Base, BaseDbModel):
 

	
 
        if fallback and not res:
 
            #fallback to additional keys
 
            _res = UserApiKeys.query() \
 
                .filter(UserApiKeys.api_key == api_key) \
 
                .filter(or_(UserApiKeys.expires == -1,
 
                            UserApiKeys.expires >= time.time())) \
 
                .first()
 
            _res = UserApiKeys.query().filter_by(api_key=api_key, is_expired=False).first()
 
            if _res:
 
                res = _res.user
 
        return res
 
@@ -742,11 +738,9 @@ class UserApiKeys(Base, BaseDbModel):
 

	
 
    user = relationship('User')
 

	
 
    @property
 
    def expired(self):
 
        if self.expires == -1:
 
            return False
 
        return time.time() > self.expires
 
    @hybrid_property
 
    def is_expired(self):
 
        return (self.expires != -1) & (time.time() > self.expires)
 

	
 

	
 
class UserEmailMap(Base, BaseDbModel):
kallithea/templates/admin/my_account/my_account_api_keys.html
Show inline comments
 
@@ -18,14 +18,14 @@
 
    </tr>
 
    %if c.user_api_keys:
 
        %for api_key in c.user_api_keys:
 
          <tr class="${'expired' if api_key.expired else ''}">
 
          <tr class="${'expired' if api_key.is_expired else ''}">
 
            <td style="width: 450px"><div class="truncate autoexpand" style="width:120px;font-size:16px;font-family: monospace">${api_key.api_key}</div></td>
 
            <td>${api_key.description}</td>
 
            <td style="min-width: 80px">
 
                 %if api_key.expires == -1:
 
                  ${_('Expires')}: ${_('Never')}
 
                 %else:
 
                    %if api_key.expired:
 
                    %if api_key.is_expired:
 
                        ${_('Expired')}: ${h.age(h.time_to_datetime(api_key.expires))}
 
                    %else:
 
                        ${_('Expires')}: ${h.age(h.time_to_datetime(api_key.expires))}
kallithea/templates/admin/users/user_edit_api_keys.html
Show inline comments
 
@@ -18,14 +18,14 @@
 
    </tr>
 
    %if c.user_api_keys:
 
        %for api_key in c.user_api_keys:
 
          <tr class="${'expired' if api_key.expired else ''}">
 
          <tr class="${'expired' if api_key.is_expired else ''}">
 
            <td style="width: 450px"><div class="truncate autoexpand" style="width:120px;font-size:16px;font-family: monospace">${api_key.api_key}</div></td>
 
            <td>${api_key.description}</td>
 
            <td style="min-width: 80px">
 
                 %if api_key.expires == -1:
 
                  ${_('Expires')}: ${_('Never')}
 
                 %else:
 
                    %if api_key.expired:
 
                    %if api_key.is_expired:
 
                        ${_('Expired')}: ${h.age(h.time_to_datetime(api_key.expires))}
 
                    %else:
 
                        ${_('Expires')}: ${h.age(h.time_to_datetime(api_key.expires))}
0 comments (0 inline, 0 general)