Show More
@@ -1,98 +1,100 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2013-2017 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | """ |
|
22 | 22 | authentication tokens model for RhodeCode |
|
23 | 23 | """ |
|
24 | 24 | |
|
25 | 25 | import time |
|
26 | 26 | import logging |
|
27 | 27 | import traceback |
|
28 | 28 | from sqlalchemy import or_ |
|
29 | 29 | |
|
30 | 30 | from rhodecode.model import BaseModel |
|
31 | 31 | from rhodecode.model.db import UserApiKeys |
|
32 | 32 | from rhodecode.model.meta import Session |
|
33 | 33 | |
|
34 | 34 | log = logging.getLogger(__name__) |
|
35 | 35 | |
|
36 | 36 | |
|
37 | 37 | class AuthTokenModel(BaseModel): |
|
38 | 38 | cls = UserApiKeys |
|
39 | 39 | |
|
40 | 40 | def create(self, user, description, lifetime=-1, role=UserApiKeys.ROLE_ALL): |
|
41 | 41 | """ |
|
42 | 42 | :param user: user or user_id |
|
43 | 43 | :param description: description of ApiKey |
|
44 | 44 | :param lifetime: expiration time in minutes |
|
45 | 45 | :param role: role for the apikey |
|
46 | 46 | """ |
|
47 | 47 | from rhodecode.lib.auth import generate_auth_token |
|
48 | 48 | |
|
49 | 49 | user = self._get_user(user) |
|
50 | 50 | |
|
51 | 51 | new_auth_token = UserApiKeys() |
|
52 | 52 | new_auth_token.api_key = generate_auth_token(user.username) |
|
53 | 53 | new_auth_token.user_id = user.user_id |
|
54 | 54 | new_auth_token.description = description |
|
55 | 55 | new_auth_token.role = role |
|
56 | 56 | new_auth_token.expires = time.time() + (lifetime * 60) \ |
|
57 | 57 | if lifetime != -1 else -1 |
|
58 | 58 | Session().add(new_auth_token) |
|
59 | 59 | |
|
60 | 60 | return new_auth_token |
|
61 | 61 | |
|
62 | 62 | def delete(self, api_key, user=None): |
|
63 | 63 | """ |
|
64 | 64 | Deletes given api_key, if user is set it also filters the object for |
|
65 | 65 | deletion by given user. |
|
66 | 66 | """ |
|
67 | 67 | api_key = UserApiKeys.query().filter(UserApiKeys.api_key == api_key) |
|
68 | 68 | |
|
69 | 69 | if user: |
|
70 | 70 | user = self._get_user(user) |
|
71 | 71 | api_key = api_key.filter(UserApiKeys.user_id == user.user_id) |
|
72 | 72 | |
|
73 | 73 | api_key = api_key.scalar() |
|
74 | 74 | try: |
|
75 | 75 | Session().delete(api_key) |
|
76 | 76 | except Exception: |
|
77 | 77 | log.error(traceback.format_exc()) |
|
78 | 78 | raise |
|
79 | 79 | |
|
80 | 80 | def get_auth_tokens(self, user, show_expired=True): |
|
81 | 81 | user = self._get_user(user) |
|
82 | 82 | user_auth_tokens = UserApiKeys.query()\ |
|
83 | 83 | .filter(UserApiKeys.user_id == user.user_id) |
|
84 | 84 | if not show_expired: |
|
85 | 85 | user_auth_tokens = user_auth_tokens\ |
|
86 | 86 | .filter(or_(UserApiKeys.expires == -1, |
|
87 | 87 | UserApiKeys.expires >= time.time())) |
|
88 | user_auth_tokens = user_auth_tokens.order_by( | |
|
89 | UserApiKeys.user_api_key_id) | |
|
88 | 90 | return user_auth_tokens |
|
89 | 91 | |
|
90 | 92 | def get_auth_token(self, auth_token): |
|
91 | 93 | auth_token = UserApiKeys.query().filter( |
|
92 | 94 | UserApiKeys.api_key == auth_token) |
|
93 | 95 | auth_token = auth_token \ |
|
94 | 96 | .filter(or_(UserApiKeys.expires == -1, |
|
95 | 97 | UserApiKeys.expires >= time.time()))\ |
|
96 | 98 | .first() |
|
97 | 99 | |
|
98 | 100 | return auth_token |
General Comments 0
You need to be logged in to leave comments.
Login now