Changeset - e3aab61a9411
kallithea/controllers/admin/my_account.py
Show inline comments
 
@@ -248,25 +248,25 @@ class MyAccountController(BaseController
 

	
 
    def my_account_api_keys_add(self):
 
        lifetime = safe_int(request.POST.get('lifetime'), -1)
 
        description = request.POST.get('description')
 
        ApiKeyModel().create(self.authuser.user_id, description, lifetime)
 
        Session().commit()
 
        h.flash(_("Api key successfully created"), category='success')
 
        h.flash(_("API key successfully created"), category='success')
 
        return redirect(url('my_account_api_keys'))
 

	
 
    def my_account_api_keys_delete(self):
 
        api_key = request.POST.get('del_api_key')
 
        user_id = self.authuser.user_id
 
        if request.POST.get('del_api_key_builtin'):
 
            user = User.get(user_id)
 
            if user:
 
                user.api_key = generate_api_key(user.username)
 
                Session().add(user)
 
                Session().commit()
 
                h.flash(_("Api key successfully reset"), category='success')
 
                h.flash(_("API key successfully reset"), category='success')
 
        elif api_key:
 
            ApiKeyModel().delete(api_key, self.authuser.user_id)
 
            Session().commit()
 
            h.flash(_("Api key successfully deleted"), category='success')
 
            h.flash(_("API key successfully deleted"), category='success')
 

	
 
        return redirect(url('my_account_api_keys'))
kallithea/controllers/admin/users.py
Show inline comments
 
@@ -308,13 +308,13 @@ class UsersController(BaseController):
 
            return redirect(url('users'))
 

	
 
        lifetime = safe_int(request.POST.get('lifetime'), -1)
 
        description = request.POST.get('description')
 
        ApiKeyModel().create(c.user.user_id, description, lifetime)
 
        Session().commit()
 
        h.flash(_("Api key successfully created"), category='success')
 
        h.flash(_("API key successfully created"), category='success')
 
        return redirect(url('edit_user_api_keys', id=c.user.user_id))
 

	
 
    def delete_api_key(self, id):
 
        c.user = User.get_or_404(id)
 
        if c.user.username == User.DEFAULT_USER:
 
            h.flash(_("You can't edit this user"), category='warning')
 
@@ -324,17 +324,17 @@ class UsersController(BaseController):
 
        if request.POST.get('del_api_key_builtin'):
 
            user = User.get(c.user.user_id)
 
            if user:
 
                user.api_key = generate_api_key(user.username)
 
                Session().add(user)
 
                Session().commit()
 
                h.flash(_("Api key successfully reset"), category='success')
 
                h.flash(_("API key successfully reset"), category='success')
 
        elif api_key:
 
            ApiKeyModel().delete(api_key, c.user.user_id)
 
            Session().commit()
 
            h.flash(_("Api key successfully deleted"), category='success')
 
            h.flash(_("API key successfully deleted"), category='success')
 

	
 
        return redirect(url('edit_user_api_keys', id=c.user.user_id))
 

	
 
    def update_account(self, id):
 
        pass
 

	
kallithea/controllers/api/__init__.py
Show inline comments
 
@@ -131,13 +131,13 @@ class JSONRPCController(WSGIController):
 
        except ValueError, e:
 
            # catch JSON errors Here
 
            return jsonrpc_error(retid=self._req_id,
 
                                 message="JSON parse error ERR:%s RAW:%r"
 
                                 % (e, raw_body))
 

	
 
        # check AUTH based on API KEY
 
        # check AUTH based on API key
 
        try:
 
            self._req_api_key = json_body['api_key']
 
            self._req_id = json_body['id']
 
            self._req_method = json_body['method']
 
            self._request_params = json_body['args']
 
            if not isinstance(self._request_params, dict):
 
@@ -153,25 +153,25 @@ class JSONRPCController(WSGIController):
 

	
 
        # check if we can find this session using api_key
 
        try:
 
            u = User.get_by_api_key(self._req_api_key)
 
            if u is None:
 
                return jsonrpc_error(retid=self._req_id,
 
                                     message='Invalid API KEY')
 
                                     message='Invalid API key')
 

	
 
            #check if we are allowed to use this IP
 
            auth_u = AuthUser(u.user_id, self._req_api_key, ip_addr=ip_addr)
 
            if not auth_u.ip_allowed:
 
                return jsonrpc_error(retid=self._req_id,
 
                        message='request from IP:%s not allowed' % (ip_addr,))
 
            else:
 
                log.info('Access for IP:%s allowed' % (ip_addr,))
 

	
 
        except Exception, e:
 
            return jsonrpc_error(retid=self._req_id,
 
                                 message='Invalid API KEY')
 
                                 message='Invalid API key')
 

	
 
        self._error = None
 
        try:
 
            self._func = self._find_method()
 
        except AttributeError, e:
 
            return jsonrpc_error(retid=self._req_id,
 
@@ -205,13 +205,13 @@ class JSONRPCController(WSGIController):
 
                                    self._func.__name__, USER_SESSION_ATTR)
 
            )
 

	
 
        # get our arglist and check if we provided them as args
 
        for arg, default in func_kwargs.iteritems():
 
            if arg == USER_SESSION_ATTR:
 
                # USER_SESSION_ATTR is something translated from api key and
 
                # USER_SESSION_ATTR is something translated from API key and
 
                # this is checked before so we don't need validate it
 
                continue
 

	
 
            # skip the required param check if it's default value is
 
            # NotImplementedType (default_empty)
 
            if default == default_empty and arg not in self._request_params:
kallithea/controllers/api/api.py
Show inline comments
 
@@ -550,13 +550,13 @@ class ApiController(JSONRPCController):
 

	
 
            id : <id_given_in_input>
 
            result: None if user does not exist or
 
                    {
 
                        "user_id" :     "<user_id>",
 
                        "api_key" :     "<api_key>",
 
                        "api_keys":     "[<list of all api keys including additional ones>]"
 
                        "api_keys":     "[<list of all API keys including additional ones>]"
 
                        "username" :    "<username>",
 
                        "firstname":    "<firstname>",
 
                        "lastname" :    "<lastname>",
 
                        "email" :       "<email>",
 
                        "emails":       "[<list of all emails including additional ones>]",
 
                        "ip_addresses": "[<ip_address_for_user>,...]",
kallithea/lib/auth.py
Show inline comments
 
@@ -143,13 +143,13 @@ def get_crypt_password(password):
 
def check_password(password, hashed):
 
    return KallitheaCrypto.hash_check(password, hashed)
 

	
 

	
 
def generate_api_key(str_, salt=None):
 
    """
 
    Generates API KEY from given string
 
    Generates API key from given string
 

	
 
    :param str_:
 
    :param salt:
 
    """
 

	
 
    if salt is None:
 
@@ -516,15 +516,15 @@ class AuthUser(object):
 

	
 
        # lookup by userid
 
        if self.user_id is not None and self.user_id != self.anonymous_user.user_id:
 
            log.debug('Auth User lookup by USER ID %s' % self.user_id)
 
            is_user_loaded = user_model.fill_data(self, user_id=self.user_id)
 

	
 
        # try go get user by api key
 
        # try go get user by API key
 
        elif self._api_key and self._api_key != self.anonymous_user.api_key:
 
            log.debug('Auth User lookup by API KEY %s' % self._api_key)
 
            log.debug('Auth User lookup by API key %s' % self._api_key)
 
            is_user_loaded = user_model.fill_data(self, api_key=self._api_key)
 

	
 
        # lookup by username
 
        elif self.username:
 
            log.debug('Auth User lookup by USER NAME %s' % self.username)
 
            is_user_loaded = user_model.fill_data(self, username=self.username)
kallithea/lib/dbmigrate/schema/db_2_2_0.py
Show inline comments
 
@@ -434,13 +434,13 @@ class User(Base, BaseModel):
 
    # notifications assigned to this user
 
    user_created_notifications = relationship('Notification', cascade='all')
 
    # comments created by this user
 
    user_comments = relationship('ChangesetComment', cascade='all')
 
    #extra emails for this user
 
    user_emails = relationship('UserEmailMap', cascade='all')
 
    #extra api keys
 
    #extra API keys
 
    user_api_keys = relationship('UserApiKeys', cascade='all')
 

	
 

	
 
    @hybrid_property
 
    def email(self):
 
        return self._email
kallithea/lib/dbmigrate/schema/db_2_2_3.py
Show inline comments
 
@@ -434,13 +434,13 @@ class User(Base, BaseModel):
 
    # notifications assigned to this user
 
    user_created_notifications = relationship('Notification', cascade='all')
 
    # comments created by this user
 
    user_comments = relationship('ChangesetComment', cascade='all')
 
    #extra emails for this user
 
    user_emails = relationship('UserEmailMap', cascade='all')
 
    #extra api keys
 
    #extra API keys
 
    user_api_keys = relationship('UserApiKeys', cascade='all')
 

	
 

	
 
    @hybrid_property
 
    def email(self):
 
        return self._email
kallithea/model/api_key.py
Show inline comments
 
@@ -12,13 +12,13 @@
 
# You should have received a copy of the GNU General Public License
 
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
"""
 
kallithea.model.api_key
 
~~~~~~~~~~~~~~~~~~~~~~~
 

	
 
api key model for Kallithea
 
API key model for Kallithea
 

	
 
This file was forked by the Kallithea project in July 2014.
 
Original author and date, and relevant copyright and licensing information is below:
 
:created_on: Sep 8, 2013
 
:author: marcink
 
:copyright: (c) 2013 RhodeCode GmbH, and others.
kallithea/model/db.py
Show inline comments
 
@@ -435,13 +435,13 @@ class User(Base, BaseModel):
 
    # notifications assigned to this user
 
    user_created_notifications = relationship('Notification', cascade='all')
 
    # comments created by this user
 
    user_comments = relationship('ChangesetComment', cascade='all')
 
    #extra emails for this user
 
    user_emails = relationship('UserEmailMap', cascade='all')
 
    #extra api keys
 
    #extra API keys
 
    user_api_keys = relationship('UserApiKeys', cascade='all')
 

	
 

	
 
    @hybrid_property
 
    def email(self):
 
        return self._email
kallithea/model/user.py
Show inline comments
 
@@ -346,13 +346,13 @@ class UserModel(BaseModel):
 
        Fills auth_user attributes with those taken from database.
 
        Additionally sets is_authenticated if lookup fails
 
        present in database
 

	
 
        :param auth_user: instance of user to set attributes
 
        :param user_id: user id to fetch by
 
        :param api_key: api key to fetch by
 
        :param api_key: API key to fetch by
 
        :param username: username to fetch by
 
        """
 
        if user_id is None and api_key is None and username is None:
 
            raise Exception('You need to pass user_id, api_key or username')
 

	
 
        dbuser = None
kallithea/templates/admin/my_account/my_account_api_keys.html
Show inline comments
 
@@ -8,13 +8,13 @@
 
        <td>${_('expires')}: ${_('never')}</td>
 
        <td>
 
            ${h.form(url('my_account_api_keys'),method='delete')}
 
                ${h.hidden('del_api_key',c.user.api_key)}
 
                ${h.hidden('del_api_key_builtin',1)}
 
                <button class="btn btn-mini btn-danger" type="submit"
 
                        onclick="return confirm('${_('Confirm to reset this api key: %s') % c.user.api_key}');">
 
                        onclick="return confirm('${_('Confirm to reset this API key: %s') % c.user.api_key}');">
 
                    ${_('reset')}
 
                </button>
 
            ${h.end_form()}
 
        </td>
 
    </tr>
 
    %if c.user_api_keys:
 
@@ -34,34 +34,34 @@
 
                 %endif
 
            </td>
 
            <td>
 
                ${h.form(url('my_account_api_keys'),method='delete')}
 
                    ${h.hidden('del_api_key',api_key.api_key)}
 
                    <button class="btn btn-mini btn-danger" type="submit"
 
                            onclick="return confirm('${_('Confirm to remove this api key: %s') % api_key.api_key}');">
 
                            onclick="return confirm('${_('Confirm to remove this API key: %s') % api_key.api_key}');">
 
                        <i class="icon-minus-circled"></i>
 
                        ${_('remove')}
 
                    </button>
 
                ${h.end_form()}
 
            </td>
 
          </tr>
 
        %endfor
 
    %else:
 
    <tr><td><div class="ip">${_('No additional api keys specified')}</div></td></tr>
 
    <tr><td><div class="ip">${_('No additional API keys specified')}</div></td></tr>
 
    %endif
 
  </table>
 
</div>
 

	
 
<div>
 
    ${h.form(url('my_account_api_keys'), method='post')}
 
    <div class="form">
 
        <!-- fields -->
 
        <div class="fields">
 
             <div class="field">
 
                <div class="label">
 
                    <label for="description">${_('New api key')}:</label>
 
                    <label for="description">${_('New API key')}:</label>
 
                </div>
 
                <div class="input">
 
                    ${h.text('description', class_='medium', placeholder=_('Description'))}
 
                    ${h.select('lifetime', '', c.lifetime_options)}
 
                </div>
 
             </div>
kallithea/templates/admin/users/user_edit_api_keys.html
Show inline comments
 
@@ -8,13 +8,13 @@
 
        <td>${_('expires')}: ${_('never')}</td>
 
        <td>
 
            ${h.form(url('edit_user_api_keys', id=c.user.user_id),method='delete')}
 
                ${h.hidden('del_api_key',c.user.api_key)}
 
                ${h.hidden('del_api_key_builtin',1)}
 
                <button class="btn btn-mini btn-danger" type="submit"
 
                        onclick="return confirm('${_('Confirm to reset this api key: %s') % c.user.api_key}');">
 
                        onclick="return confirm('${_('Confirm to reset this API key: %s') % c.user.api_key}');">
 
                    ${_('reset')}
 
                </button>
 
            ${h.end_form()}
 
        </td>
 
    </tr>
 
    %if c.user_api_keys:
 
@@ -34,34 +34,34 @@
 
                 %endif
 
            </td>
 
            <td>
 
                ${h.form(url('edit_user_api_keys', id=c.user.user_id),method='delete')}
 
                    ${h.hidden('del_api_key',api_key.api_key)}
 
                    <button class="btn btn-mini btn-danger" type="submit"
 
                            onclick="return confirm('${_('Confirm to remove this api key: %s') % api_key.api_key}');">
 
                            onclick="return confirm('${_('Confirm to remove this API key: %s') % api_key.api_key}');">
 
                        <i class="icon-minus-circled"></i>
 
                        ${_('remove')}
 
                    </button>
 
                ${h.end_form()}
 
            </td>
 
          </tr>
 
        %endfor
 
    %else:
 
    <tr><td><div class="ip">${_('No additional api keys specified')}</div></td></tr>
 
    <tr><td><div class="ip">${_('No additional API keys specified')}</div></td></tr>
 
    %endif
 
  </table>
 
</div>
 

	
 
<div>
 
    ${h.form(url('edit_user_api_keys', id=c.user.user_id), method='put')}
 
    <div class="form">
 
        <!-- fields -->
 
        <div class="fields">
 
             <div class="field">
 
                <div class="label">
 
                    <label for="description">${_('New api key')}:</label>
 
                    <label for="description">${_('New API key')}:</label>
 
                </div>
 
                <div class="input">
 
                    ${h.text('description', class_='medium', placeholder=_('Description'))}
 
                    ${h.select('lifetime', '', c.lifetime_options)}
 
                </div>
 
             </div>
kallithea/tests/api/api_base.py
Show inline comments
 
@@ -163,13 +163,13 @@ class _BaseTestApi(object):
 
        self.assertEqual(oattr1(), oattr1)
 

	
 
    def test_api_wrong_key(self):
 
        id_, params = _build_data('trololo', 'get_user')
 
        response = api_call(self, params)
 

	
 
        expected = 'Invalid API KEY'
 
        expected = 'Invalid API key'
 
        self._compare_error(id_, expected, given=response.body)
 

	
 
    def test_api_missing_non_optional_param(self):
 
        id_, params = _build_data(self.apikey, 'get_repo')
 
        response = api_call(self, params)
 

	
kallithea/tests/functional/test_admin_users.py
Show inline comments
 
@@ -449,13 +449,13 @@ class TestAdminUsersController(TestContr
 
        self.log_user()
 
        user = User.get_by_username(TEST_USER_REGULAR_LOGIN)
 
        user_id = user.user_id
 

	
 
        response = self.app.post(url('edit_user_api_keys', id=user_id),
 
                 {'_method': 'put', 'description': desc, 'lifetime': lifetime, '_authentication_token': self.authentication_token()})
 
        self.checkSessionFlash(response, 'Api key successfully created')
 
        self.checkSessionFlash(response, 'API key successfully created')
 
        try:
 
            response = response.follow()
 
            user = User.get(user_id)
 
            for api_key in user.api_keys:
 
                response.mustcontain(api_key)
 
        finally:
 
@@ -467,22 +467,22 @@ class TestAdminUsersController(TestContr
 
        self.log_user()
 
        user = User.get_by_username(TEST_USER_REGULAR_LOGIN)
 
        user_id = user.user_id
 

	
 
        response = self.app.post(url('edit_user_api_keys', id=user_id),
 
                {'_method': 'put', 'description': 'desc', 'lifetime': -1, '_authentication_token': self.authentication_token()})
 
        self.checkSessionFlash(response, 'Api key successfully created')
 
        self.checkSessionFlash(response, 'API key successfully created')
 
        response = response.follow()
 

	
 
        #now delete our key
 
        keys = UserApiKeys.query().filter(UserApiKeys.user_id == user_id).all()
 
        self.assertEqual(1, len(keys))
 

	
 
        response = self.app.post(url('edit_user_api_keys', id=user_id),
 
                 {'_method': 'delete', 'del_api_key': keys[0].api_key, '_authentication_token': self.authentication_token()})
 
        self.checkSessionFlash(response, 'Api key successfully deleted')
 
        self.checkSessionFlash(response, 'API key successfully deleted')
 
        keys = UserApiKeys.query().filter(UserApiKeys.user_id == user_id).all()
 
        self.assertEqual(0, len(keys))
 

	
 
    def test_reset_main_api_key(self):
 
        self.log_user()
 
        user = User.get_by_username(TEST_USER_REGULAR_LOGIN)
 
@@ -491,9 +491,9 @@ class TestAdminUsersController(TestContr
 
        response = self.app.get(url('edit_user_api_keys', id=user_id))
 
        response.mustcontain(api_key)
 
        response.mustcontain('expires: never')
 

	
 
        response = self.app.post(url('edit_user_api_keys', id=user_id),
 
                 {'_method': 'delete', 'del_api_key_builtin': api_key, '_authentication_token': self.authentication_token()})
 
        self.checkSessionFlash(response, 'Api key successfully reset')
 
        self.checkSessionFlash(response, 'API key successfully reset')
 
        response = response.follow()
 
        response.mustcontain(no=[api_key])
kallithea/tests/functional/test_login.py
Show inline comments
 
@@ -360,13 +360,13 @@ class TestLoginController(TestController
 
        with mock.patch('kallithea.CONFIG', whitelist):
 
            self.assertEqual(['ChangesetController:changeset_raw'],
 
                             whitelist['api_access_controllers_whitelist'])
 

	
 
            new_api_key = ApiKeyModel().create(TEST_USER_ADMIN_LOGIN, u'test')
 
            Session().commit()
 
            #patch the api key and make it expired
 
            #patch the API key and make it expired
 
            new_api_key.expires = 0
 
            Session().add(new_api_key)
 
            Session().commit()
 
            with fixture.anon_access(False):
 
                self.app.get(url(controller='changeset',
 
                                 action='changeset_raw',
kallithea/tests/functional/test_my_account.py
Show inline comments
 
@@ -198,13 +198,13 @@ class TestMyAccountController(TestContro
 
    ])
 
    def test_my_account_add_api_keys(self, desc, lifetime):
 
        usr = self.log_user('test_regular2', 'test12')
 
        user = User.get(usr['user_id'])
 
        response = self.app.post(url('my_account_api_keys'),
 
                                 {'description': desc, 'lifetime': lifetime, '_authentication_token': self.authentication_token()})
 
        self.checkSessionFlash(response, 'Api key successfully created')
 
        self.checkSessionFlash(response, 'API key successfully created')
 
        try:
 
            response = response.follow()
 
            user = User.get(usr['user_id'])
 
            for api_key in user.api_keys:
 
                response.mustcontain(api_key)
 
        finally:
 
@@ -214,22 +214,22 @@ class TestMyAccountController(TestContro
 

	
 
    def test_my_account_remove_api_key(self):
 
        usr = self.log_user('test_regular2', 'test12')
 
        user = User.get(usr['user_id'])
 
        response = self.app.post(url('my_account_api_keys'),
 
                                 {'description': 'desc', 'lifetime': -1, '_authentication_token': self.authentication_token()})
 
        self.checkSessionFlash(response, 'Api key successfully created')
 
        self.checkSessionFlash(response, 'API key successfully created')
 
        response = response.follow()
 

	
 
        #now delete our key
 
        keys = UserApiKeys.query().all()
 
        self.assertEqual(1, len(keys))
 

	
 
        response = self.app.post(url('my_account_api_keys'),
 
                 {'_method': 'delete', 'del_api_key': keys[0].api_key, '_authentication_token': self.authentication_token()})
 
        self.checkSessionFlash(response, 'Api key successfully deleted')
 
        self.checkSessionFlash(response, 'API key successfully deleted')
 
        keys = UserApiKeys.query().all()
 
        self.assertEqual(0, len(keys))
 

	
 

	
 
    def test_my_account_reset_main_api_key(self):
 
        usr = self.log_user('test_regular2', 'test12')
 
@@ -238,9 +238,9 @@ class TestMyAccountController(TestContro
 
        response = self.app.get(url('my_account_api_keys'))
 
        response.mustcontain(api_key)
 
        response.mustcontain('expires: never')
 

	
 
        response = self.app.post(url('my_account_api_keys'),
 
                 {'_method': 'delete', 'del_api_key_builtin': api_key, '_authentication_token': self.authentication_token()})
 
        self.checkSessionFlash(response, 'Api key successfully reset')
 
        self.checkSessionFlash(response, 'API key successfully reset')
 
        response = response.follow()
 
        response.mustcontain(no=[api_key])
0 comments (0 inline, 0 general)