Show More
@@ -1,140 +1,140 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 |
|
2 | |||
3 | # Copyright (C) 2010-2016 RhodeCode GmbH |
|
3 | # Copyright (C) 2010-2016 RhodeCode GmbH | |
4 | # |
|
4 | # | |
5 | # This program is free software: you can redistribute it and/or modify |
|
5 | # This program is free software: you can redistribute it and/or modify | |
6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
6 | # it under the terms of the GNU Affero General Public License, version 3 | |
7 | # (only), as published by the Free Software Foundation. |
|
7 | # (only), as published by the Free Software Foundation. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU Affero General Public License |
|
14 | # You should have received a copy of the GNU Affero General Public License | |
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | # |
|
16 | # | |
17 | # This program is dual-licensed. If you wish to learn more about the |
|
17 | # This program is dual-licensed. If you wish to learn more about the | |
18 | # App Enlight Enterprise Edition, including its added features, Support |
|
18 | # App Enlight Enterprise Edition, including its added features, Support | |
19 | # services, and proprietary license terms, please see |
|
19 | # services, and proprietary license terms, please see | |
20 | # https://rhodecode.com/licenses/ |
|
20 | # https://rhodecode.com/licenses/ | |
21 |
|
21 | |||
22 | import json |
|
22 | import json | |
23 |
|
23 | |||
24 | from pyramid.security import unauthenticated_userid |
|
24 | from pyramid.security import unauthenticated_userid | |
25 |
|
25 | |||
26 | import appenlight.lib.helpers as helpers |
|
26 | import appenlight.lib.helpers as helpers | |
27 |
|
27 | |||
28 | from authomatic.providers import oauth2, oauth1 |
|
28 | from authomatic.providers import oauth2, oauth1 | |
29 | from authomatic import Authomatic |
|
29 | from authomatic import Authomatic | |
30 | from appenlight.models.user import User |
|
30 | from appenlight.models.user import User | |
31 |
|
31 | |||
32 |
|
32 | |||
33 | class CSRFException(Exception): |
|
33 | class CSRFException(Exception): | |
34 | pass |
|
34 | pass | |
35 |
|
35 | |||
36 |
|
36 | |||
37 | class JSONException(Exception): |
|
37 | class JSONException(Exception): | |
38 | pass |
|
38 | pass | |
39 |
|
39 | |||
40 |
|
40 | |||
41 | def get_csrf_token(request): |
|
41 | def get_csrf_token(request): | |
42 | return request.session.get_csrf_token() |
|
42 | return request.session.get_csrf_token() | |
43 |
|
43 | |||
44 |
|
44 | |||
45 | def safe_json_body(request): |
|
45 | def safe_json_body(request): | |
46 | """ |
|
46 | """ | |
47 | Returns None if json body is missing or erroneous |
|
47 | Returns None if json body is missing or erroneous | |
48 | """ |
|
48 | """ | |
49 | try: |
|
49 | try: | |
50 | return request.json_body |
|
50 | return request.json_body | |
51 | except ValueError: |
|
51 | except ValueError: | |
52 | return None |
|
52 | return None | |
53 |
|
53 | |||
54 |
|
54 | |||
55 | def unsafe_json_body(request): |
|
55 | def unsafe_json_body(request): | |
56 | """ |
|
56 | """ | |
57 | Throws JSONException if json can't deserialize |
|
57 | Throws JSONException if json can't deserialize | |
58 | """ |
|
58 | """ | |
59 | try: |
|
59 | try: | |
60 | return request.json_body |
|
60 | return request.json_body | |
61 | except ValueError: |
|
61 | except ValueError: | |
62 | raise JSONException('Incorrect JSON') |
|
62 | raise JSONException('Incorrect JSON') | |
63 |
|
63 | |||
64 |
|
64 | |||
65 | def get_user(request): |
|
65 | def get_user(request): | |
66 | if not request.path_info.startswith('/static'): |
|
66 | if not request.path_info.startswith('/static'): | |
67 | user_id = unauthenticated_userid(request) |
|
67 | user_id = unauthenticated_userid(request) | |
68 | try: |
|
68 | try: | |
69 | user_id = int(user_id) |
|
69 | user_id = int(user_id) | |
70 | except Exception: |
|
70 | except Exception: | |
71 | return None |
|
71 | return None | |
72 |
|
72 | |||
73 | if user_id: |
|
73 | if user_id: | |
74 | user = User.by_id(user_id) |
|
74 | user = User.by_id(user_id) | |
75 | if user: |
|
75 | if user: | |
76 | request.environ['appenlight.username'] = '%d:%s' % ( |
|
76 | request.environ['appenlight.username'] = '%d:%s' % ( | |
77 | user_id, user.user_name) |
|
77 | user_id, user.user_name) | |
78 | return user |
|
78 | return user | |
79 | else: |
|
79 | else: | |
80 | return None |
|
80 | return None | |
81 |
|
81 | |||
82 |
|
82 | |||
83 | def es_conn(request): |
|
83 | def es_conn(request): | |
84 | return request.registry.es_conn |
|
84 | return request.registry.es_conn | |
85 |
|
85 | |||
86 |
|
86 | |||
87 | def add_flash_to_headers(request, clear=True): |
|
87 | def add_flash_to_headers(request, clear=True): | |
88 | """ |
|
88 | """ | |
89 | Adds pending flash messages to response, if clear is true clears out the |
|
89 | Adds pending flash messages to response, if clear is true clears out the | |
90 | flash queue |
|
90 | flash queue | |
91 | """ |
|
91 | """ | |
92 | flash_msgs = helpers.get_type_formatted_flash(request) |
|
92 | flash_msgs = helpers.get_type_formatted_flash(request) | |
93 | request.response.headers['x-flash-messages'] = json.dumps(flash_msgs) |
|
93 | request.response.headers['x-flash-messages'] = json.dumps(flash_msgs) | |
94 | helpers.clear_flash(request) |
|
94 | helpers.clear_flash(request) | |
95 |
|
95 | |||
96 |
|
96 | |||
97 | def get_authomatic(request): |
|
97 | def get_authomatic(request): | |
98 | settings = request.registry.settings |
|
98 | settings = request.registry.settings | |
99 | # authomatic social auth |
|
99 | # authomatic social auth | |
100 | authomatic_conf = { |
|
100 | authomatic_conf = { | |
101 | # callback http://yourapp.com/social_auth/twitter |
|
101 | # callback http://yourapp.com/social_auth/twitter | |
102 | 'twitter': { |
|
102 | 'twitter': { | |
103 | 'class_': oauth1.Twitter, |
|
103 | 'class_': oauth1.Twitter, | |
104 | 'consumer_key': settings.get('authomatic.pr.twitter.key', ''), |
|
104 | 'consumer_key': settings.get('authomatic.pr.twitter.key', ''), | |
105 | 'consumer_secret': settings.get('authomatic.pr.twitter.secret', |
|
105 | 'consumer_secret': settings.get('authomatic.pr.twitter.secret', | |
106 | ''), |
|
106 | ''), | |
107 | }, |
|
107 | }, | |
108 | # callback http://yourapp.com/social_auth/facebook |
|
108 | # callback http://yourapp.com/social_auth/facebook | |
109 | 'facebook': { |
|
109 | 'facebook': { | |
110 | 'class_': oauth2.Facebook, |
|
110 | 'class_': oauth2.Facebook, | |
111 |
'consumer_key': settings.get('authomatic.pr.facebook.app_id', ' |
|
111 | 'consumer_key': settings.get('authomatic.pr.facebook.app_id', ''), | |
112 | 'consumer_secret': settings.get('authomatic.pr.facebook.secret', |
|
112 | 'consumer_secret': settings.get('authomatic.pr.facebook.secret', | |
113 | ''), |
|
113 | ''), | |
114 | 'scope': ['email'], |
|
114 | 'scope': ['email'], | |
115 | }, |
|
115 | }, | |
116 | # callback http://yourapp.com/social_auth/google |
|
116 | # callback http://yourapp.com/social_auth/google | |
117 | 'google': { |
|
117 | 'google': { | |
118 | 'class_': oauth2.Google, |
|
118 | 'class_': oauth2.Google, | |
119 | 'consumer_key': settings.get('authomatic.pr.google.key', ''), |
|
119 | 'consumer_key': settings.get('authomatic.pr.google.key', ''), | |
120 | 'consumer_secret': settings.get( |
|
120 | 'consumer_secret': settings.get( | |
121 | 'authomatic.pr.google.secret', ''), |
|
121 | 'authomatic.pr.google.secret', ''), | |
122 | 'scope': ['profile', 'email'], |
|
122 | 'scope': ['profile', 'email'], | |
123 | }, |
|
123 | }, | |
124 | 'github': { |
|
124 | 'github': { | |
125 | 'class_': oauth2.GitHub, |
|
125 | 'class_': oauth2.GitHub, | |
126 | 'consumer_key': settings.get('authomatic.pr.github.key', ''), |
|
126 | 'consumer_key': settings.get('authomatic.pr.github.key', ''), | |
127 | 'consumer_secret': settings.get( |
|
127 | 'consumer_secret': settings.get( | |
128 | 'authomatic.pr.github.secret', ''), |
|
128 | 'authomatic.pr.github.secret', ''), | |
129 | 'scope': ['repo', 'public_repo', 'user:email'], |
|
129 | 'scope': ['repo', 'public_repo', 'user:email'], | |
130 | 'access_headers': {'User-Agent': 'AppEnlight'}, |
|
130 | 'access_headers': {'User-Agent': 'AppEnlight'}, | |
131 | }, |
|
131 | }, | |
132 | 'bitbucket': { |
|
132 | 'bitbucket': { | |
133 | 'class_': oauth1.Bitbucket, |
|
133 | 'class_': oauth1.Bitbucket, | |
134 | 'consumer_key': settings.get('authomatic.pr.bitbucket.key', ''), |
|
134 | 'consumer_key': settings.get('authomatic.pr.bitbucket.key', ''), | |
135 | 'consumer_secret': settings.get( |
|
135 | 'consumer_secret': settings.get( | |
136 | 'authomatic.pr.bitbucket.secret', '') |
|
136 | 'authomatic.pr.bitbucket.secret', '') | |
137 | } |
|
137 | } | |
138 | } |
|
138 | } | |
139 | return Authomatic( |
|
139 | return Authomatic( | |
140 | config=authomatic_conf, secret=settings['authomatic.secret']) |
|
140 | config=authomatic_conf, secret=settings['authomatic.secret']) |
General Comments 0
You need to be logged in to leave comments.
Login now