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
 
@@ -21,26 +21,24 @@ This file was forked by the Kallithea pr
 
Original author and date, and relevant copyright and licensing information is below:
 
:created_on: Apr 4, 2010
 
:author: marcink
 
:copyright: (c) 2013 RhodeCode GmbH, and others.
 
:license: GPLv3, see LICENSE.md for more details.
 
"""
 
import time
 
import os
 
import logging
 
import traceback
 
import hashlib
 
import itertools
 
import collections
 

	
 
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
 

	
 
from kallithea import __platform__, is_windows, is_unix
 
from kallithea.config.routing import url
 
@@ -603,15 +601,13 @@ class AuthUser(object):
 
        return compute(user_id, user_is_admin,
 
                       user_inherit_default_permissions, explicit, algo)
 

	
 
    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
 

	
 
    @property
 
    def is_admin(self):
kallithea/model/api_key.py
Show inline comments
 
@@ -24,13 +24,12 @@ 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 logging
 
from sqlalchemy import or_
 

	
 
from kallithea.lib.utils2 import generate_api_key
 
from kallithea.model.base import BaseModel
 
from kallithea.model.db import User, UserApiKeys
 
from kallithea.model.meta import Session
 

	
 
@@ -72,10 +71,8 @@ class ApiKeyModel(BaseModel):
 

	
 
    def get_api_keys(self, user, show_expired=True):
 
        user = User.guess_instance(user)
 
        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
 
@@ -615,17 +615,13 @@ class User(Base, BaseDbModel):
 
            q = q.options(FromCache("sql_cache_short",
 
                                    "get_api_key_%s" % api_key))
 
        res = q.scalar()
 

	
 
        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
 

	
 
    @classmethod
 
    def get_by_email(cls, email, cache=False):
 
@@ -739,17 +735,15 @@ class UserApiKeys(Base, BaseDbModel):
 
    description = Column(UnicodeText(), nullable=False)
 
    expires = Column(Float(53), nullable=False)
 
    created_on = Column(DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
 

	
 
    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):
 
    __tablename__ = 'user_email_map'
 
    __table_args__ = (
 
        Index('uem_email_idx', 'email'),
kallithea/templates/admin/my_account/my_account_api_keys.html
Show inline comments
 
@@ -15,20 +15,20 @@
 
                </button>
 
            ${h.end_form()}
 
        </td>
 
    </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))}
 
                    %endif
 
                 %endif
 
            </td>
kallithea/templates/admin/users/user_edit_api_keys.html
Show inline comments
 
@@ -15,20 +15,20 @@
 
                </button>
 
            ${h.end_form()}
 
        </td>
 
    </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))}
 
                    %endif
 
                 %endif
 
            </td>
0 comments (0 inline, 0 general)