Show More
@@ -1,185 +1,185 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 |
|
2 | |||
3 | # Copyright (C) 2010-2020 RhodeCode GmbH |
|
3 | # Copyright (C) 2010-2020 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 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
18 | # RhodeCode Enterprise Edition, including its added features, Support services, | |
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ | |
20 |
|
20 | |||
21 | import logging |
|
21 | import logging | |
22 | import uuid |
|
22 | import uuid | |
23 |
|
23 | |||
24 |
|
24 | |||
25 | from pyramid.httpexceptions import HTTPBadRequest, HTTPForbidden, HTTPBadGateway |
|
25 | from pyramid.httpexceptions import HTTPBadRequest, HTTPForbidden, HTTPBadGateway | |
26 |
|
26 | |||
27 | from rhodecode.apps._base import BaseAppView |
|
27 | from rhodecode.apps._base import BaseAppView | |
28 | from rhodecode.lib.channelstream import ( |
|
28 | from rhodecode.lib.channelstream import ( | |
29 | channelstream_request, get_channelstream_server_url, |
|
29 | channelstream_request, get_channelstream_server_url, | |
30 | ChannelstreamConnectionException, |
|
30 | ChannelstreamConnectionException, | |
31 | ChannelstreamPermissionException, |
|
31 | ChannelstreamPermissionException, | |
32 | check_channel_permissions, |
|
32 | check_channel_permissions, | |
33 | get_connection_validators, |
|
33 | get_connection_validators, | |
34 | get_user_data, |
|
34 | get_user_data, | |
35 | parse_channels_info, |
|
35 | parse_channels_info, | |
36 | update_history_from_logs, |
|
36 | update_history_from_logs, | |
37 | USER_STATE_PUBLIC_KEYS) |
|
37 | USER_STATE_PUBLIC_KEYS) | |
38 |
|
38 | |||
39 | from rhodecode.lib.auth import NotAnonymous |
|
39 | from rhodecode.lib.auth import NotAnonymous | |
40 |
|
40 | |||
41 | log = logging.getLogger(__name__) |
|
41 | log = logging.getLogger(__name__) | |
42 |
|
42 | |||
43 |
|
43 | |||
44 | class ChannelstreamView(BaseAppView): |
|
44 | class ChannelstreamView(BaseAppView): | |
45 |
|
45 | |||
46 | def load_default_context(self): |
|
46 | def load_default_context(self): | |
47 | c = self._get_local_tmpl_context() |
|
47 | c = self._get_local_tmpl_context() | |
48 | self.channelstream_config = \ |
|
48 | self.channelstream_config = \ | |
49 | self.request.registry.rhodecode_plugins['channelstream'] |
|
49 | self.request.registry.rhodecode_plugins['channelstream'] | |
50 | if not self.channelstream_config.get('enabled'): |
|
50 | if not self.channelstream_config.get('enabled'): | |
51 |
log. |
|
51 | log.warning('Channelstream plugin is disabled') | |
52 | raise HTTPBadRequest() |
|
52 | raise HTTPBadRequest() | |
53 |
|
53 | |||
54 | return c |
|
54 | return c | |
55 |
|
55 | |||
56 | @NotAnonymous() |
|
56 | @NotAnonymous() | |
57 | def channelstream_connect(self): |
|
57 | def channelstream_connect(self): | |
58 | """ handle authorization of users trying to connect """ |
|
58 | """ handle authorization of users trying to connect """ | |
59 |
|
59 | |||
60 | self.load_default_context() |
|
60 | self.load_default_context() | |
61 | try: |
|
61 | try: | |
62 | json_body = self.request.json_body |
|
62 | json_body = self.request.json_body | |
63 | except Exception: |
|
63 | except Exception: | |
64 | log.exception('Failed to decode json from request') |
|
64 | log.exception('Failed to decode json from request') | |
65 | raise HTTPBadRequest() |
|
65 | raise HTTPBadRequest() | |
66 |
|
66 | |||
67 | try: |
|
67 | try: | |
68 | channels = check_channel_permissions( |
|
68 | channels = check_channel_permissions( | |
69 | json_body.get('channels'), |
|
69 | json_body.get('channels'), | |
70 | get_connection_validators(self.request.registry)) |
|
70 | get_connection_validators(self.request.registry)) | |
71 | except ChannelstreamPermissionException: |
|
71 | except ChannelstreamPermissionException: | |
72 | log.error('Incorrect permissions for requested channels') |
|
72 | log.error('Incorrect permissions for requested channels') | |
73 | raise HTTPForbidden() |
|
73 | raise HTTPForbidden() | |
74 |
|
74 | |||
75 | user = self._rhodecode_user |
|
75 | user = self._rhodecode_user | |
76 | if user.user_id: |
|
76 | if user.user_id: | |
77 | user_data = get_user_data(user.user_id) |
|
77 | user_data = get_user_data(user.user_id) | |
78 | else: |
|
78 | else: | |
79 | user_data = { |
|
79 | user_data = { | |
80 | 'id': None, |
|
80 | 'id': None, | |
81 | 'username': None, |
|
81 | 'username': None, | |
82 | 'first_name': None, |
|
82 | 'first_name': None, | |
83 | 'last_name': None, |
|
83 | 'last_name': None, | |
84 | 'icon_link': None, |
|
84 | 'icon_link': None, | |
85 | 'display_name': None, |
|
85 | 'display_name': None, | |
86 | 'display_link': None, |
|
86 | 'display_link': None, | |
87 | } |
|
87 | } | |
88 |
|
88 | |||
89 | #user_data['permissions'] = self._rhodecode_user.permissions_safe |
|
89 | #user_data['permissions'] = self._rhodecode_user.permissions_safe | |
90 |
|
90 | |||
91 | payload = { |
|
91 | payload = { | |
92 | 'username': user.username, |
|
92 | 'username': user.username, | |
93 | 'user_state': user_data, |
|
93 | 'user_state': user_data, | |
94 | 'conn_id': str(uuid.uuid4()), |
|
94 | 'conn_id': str(uuid.uuid4()), | |
95 | 'channels': channels, |
|
95 | 'channels': channels, | |
96 | 'channel_configs': {}, |
|
96 | 'channel_configs': {}, | |
97 | 'state_public_keys': USER_STATE_PUBLIC_KEYS, |
|
97 | 'state_public_keys': USER_STATE_PUBLIC_KEYS, | |
98 | 'info': { |
|
98 | 'info': { | |
99 | 'exclude_channels': ['broadcast'] |
|
99 | 'exclude_channels': ['broadcast'] | |
100 | } |
|
100 | } | |
101 | } |
|
101 | } | |
102 | filtered_channels = [channel for channel in channels |
|
102 | filtered_channels = [channel for channel in channels | |
103 | if channel != 'broadcast'] |
|
103 | if channel != 'broadcast'] | |
104 | for channel in filtered_channels: |
|
104 | for channel in filtered_channels: | |
105 | payload['channel_configs'][channel] = { |
|
105 | payload['channel_configs'][channel] = { | |
106 | 'notify_presence': True, |
|
106 | 'notify_presence': True, | |
107 | 'history_size': 100, |
|
107 | 'history_size': 100, | |
108 | 'store_history': True, |
|
108 | 'store_history': True, | |
109 | 'broadcast_presence_with_user_lists': True |
|
109 | 'broadcast_presence_with_user_lists': True | |
110 | } |
|
110 | } | |
111 | # connect user to server |
|
111 | # connect user to server | |
112 | channelstream_url = get_channelstream_server_url( |
|
112 | channelstream_url = get_channelstream_server_url( | |
113 | self.channelstream_config, '/connect') |
|
113 | self.channelstream_config, '/connect') | |
114 | try: |
|
114 | try: | |
115 | connect_result = channelstream_request( |
|
115 | connect_result = channelstream_request( | |
116 | self.channelstream_config, payload, '/connect') |
|
116 | self.channelstream_config, payload, '/connect') | |
117 | except ChannelstreamConnectionException: |
|
117 | except ChannelstreamConnectionException: | |
118 | log.exception( |
|
118 | log.exception( | |
119 | 'Channelstream service at {} is down'.format(channelstream_url)) |
|
119 | 'Channelstream service at {} is down'.format(channelstream_url)) | |
120 | return HTTPBadGateway() |
|
120 | return HTTPBadGateway() | |
121 |
|
121 | |||
122 | channel_info = connect_result.get('channels_info') |
|
122 | channel_info = connect_result.get('channels_info') | |
123 | if not channel_info: |
|
123 | if not channel_info: | |
124 | raise HTTPBadRequest() |
|
124 | raise HTTPBadRequest() | |
125 |
|
125 | |||
126 | connect_result['channels'] = channels |
|
126 | connect_result['channels'] = channels | |
127 | connect_result['channels_info'] = parse_channels_info( |
|
127 | connect_result['channels_info'] = parse_channels_info( | |
128 | channel_info, include_channel_info=filtered_channels) |
|
128 | channel_info, include_channel_info=filtered_channels) | |
129 | update_history_from_logs(self.channelstream_config, |
|
129 | update_history_from_logs(self.channelstream_config, | |
130 | filtered_channels, connect_result) |
|
130 | filtered_channels, connect_result) | |
131 | return connect_result |
|
131 | return connect_result | |
132 |
|
132 | |||
133 | @NotAnonymous() |
|
133 | @NotAnonymous() | |
134 | def channelstream_subscribe(self): |
|
134 | def channelstream_subscribe(self): | |
135 | """ can be used to subscribe specific connection to other channels """ |
|
135 | """ can be used to subscribe specific connection to other channels """ | |
136 | self.load_default_context() |
|
136 | self.load_default_context() | |
137 | try: |
|
137 | try: | |
138 | json_body = self.request.json_body |
|
138 | json_body = self.request.json_body | |
139 | except Exception: |
|
139 | except Exception: | |
140 | log.exception('Failed to decode json from request') |
|
140 | log.exception('Failed to decode json from request') | |
141 | raise HTTPBadRequest() |
|
141 | raise HTTPBadRequest() | |
142 | try: |
|
142 | try: | |
143 | channels = check_channel_permissions( |
|
143 | channels = check_channel_permissions( | |
144 | json_body.get('channels'), |
|
144 | json_body.get('channels'), | |
145 | get_connection_validators(self.request.registry)) |
|
145 | get_connection_validators(self.request.registry)) | |
146 | except ChannelstreamPermissionException: |
|
146 | except ChannelstreamPermissionException: | |
147 | log.error('Incorrect permissions for requested channels') |
|
147 | log.error('Incorrect permissions for requested channels') | |
148 | raise HTTPForbidden() |
|
148 | raise HTTPForbidden() | |
149 | payload = {'conn_id': json_body.get('conn_id', ''), |
|
149 | payload = {'conn_id': json_body.get('conn_id', ''), | |
150 | 'channels': channels, |
|
150 | 'channels': channels, | |
151 | 'channel_configs': {}, |
|
151 | 'channel_configs': {}, | |
152 | 'info': { |
|
152 | 'info': { | |
153 | 'exclude_channels': ['broadcast']} |
|
153 | 'exclude_channels': ['broadcast']} | |
154 | } |
|
154 | } | |
155 | filtered_channels = [chan for chan in channels if chan != 'broadcast'] |
|
155 | filtered_channels = [chan for chan in channels if chan != 'broadcast'] | |
156 | for channel in filtered_channels: |
|
156 | for channel in filtered_channels: | |
157 | payload['channel_configs'][channel] = { |
|
157 | payload['channel_configs'][channel] = { | |
158 | 'notify_presence': True, |
|
158 | 'notify_presence': True, | |
159 | 'history_size': 100, |
|
159 | 'history_size': 100, | |
160 | 'store_history': True, |
|
160 | 'store_history': True, | |
161 | 'broadcast_presence_with_user_lists': True |
|
161 | 'broadcast_presence_with_user_lists': True | |
162 | } |
|
162 | } | |
163 |
|
163 | |||
164 | channelstream_url = get_channelstream_server_url( |
|
164 | channelstream_url = get_channelstream_server_url( | |
165 | self.channelstream_config, '/subscribe') |
|
165 | self.channelstream_config, '/subscribe') | |
166 | try: |
|
166 | try: | |
167 | connect_result = channelstream_request( |
|
167 | connect_result = channelstream_request( | |
168 | self.channelstream_config, payload, '/subscribe') |
|
168 | self.channelstream_config, payload, '/subscribe') | |
169 | except ChannelstreamConnectionException: |
|
169 | except ChannelstreamConnectionException: | |
170 | log.exception( |
|
170 | log.exception( | |
171 | 'Channelstream service at {} is down'.format(channelstream_url)) |
|
171 | 'Channelstream service at {} is down'.format(channelstream_url)) | |
172 | return HTTPBadGateway() |
|
172 | return HTTPBadGateway() | |
173 |
|
173 | |||
174 | channel_info = connect_result.get('channels_info') |
|
174 | channel_info = connect_result.get('channels_info') | |
175 | if not channel_info: |
|
175 | if not channel_info: | |
176 | raise HTTPBadRequest() |
|
176 | raise HTTPBadRequest() | |
177 |
|
177 | |||
178 | # include_channel_info will limit history only to new channel |
|
178 | # include_channel_info will limit history only to new channel | |
179 | # to not overwrite histories on other channels in client |
|
179 | # to not overwrite histories on other channels in client | |
180 | connect_result['channels_info'] = parse_channels_info( |
|
180 | connect_result['channels_info'] = parse_channels_info( | |
181 | channel_info, |
|
181 | channel_info, | |
182 | include_channel_info=filtered_channels) |
|
182 | include_channel_info=filtered_channels) | |
183 | update_history_from_logs( |
|
183 | update_history_from_logs( | |
184 | self.channelstream_config, filtered_channels, connect_result) |
|
184 | self.channelstream_config, filtered_channels, connect_result) | |
185 | return connect_result |
|
185 | return connect_result |
General Comments 0
You need to be logged in to leave comments.
Login now