|
1 |
# hobo - portal to configure and deploy applications
|
|
2 |
# Copyright (C) 2015-2022 Entr'ouvert
|
|
3 |
#
|
|
4 |
# This program is free software: you can redistribute it and/or modify it
|
|
5 |
# under the terms of the GNU Affero General Public License as published
|
|
6 |
# by the Free Software Foundation, either version 3 of the License, or
|
|
7 |
# (at your option) any later version.
|
|
8 |
#
|
|
9 |
# This program is distributed in the hope that it will be useful,
|
|
10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 |
# GNU Affero General Public License for more details.
|
|
13 |
#
|
|
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/>.
|
|
16 |
|
|
17 |
import urllib
|
|
18 |
|
|
19 |
import requests
|
|
20 |
from django.conf import settings
|
|
21 |
from django.utils.http import urlencode
|
|
22 |
from requests import Response
|
|
23 |
from requests import Session as RequestsSession
|
|
24 |
from requests.auth import AuthBase
|
|
25 |
|
|
26 |
from hobo.signature import sign_url
|
|
27 |
|
|
28 |
|
|
29 |
class PublikSignature(AuthBase):
|
|
30 |
def __init__(self, secret):
|
|
31 |
self.secret = secret
|
|
32 |
|
|
33 |
def __call__(self, request):
|
|
34 |
request.url = sign_url(request.url, self.secret)
|
|
35 |
return request
|
|
36 |
|
|
37 |
|
|
38 |
def get_known_service_for_url(url):
|
|
39 |
netloc = urllib.parse.urlparse(url).netloc
|
|
40 |
for services in settings.KNOWN_SERVICES.values():
|
|
41 |
for service in services.values():
|
|
42 |
remote_url = service.get('url')
|
|
43 |
if urllib.parse.urlparse(remote_url).netloc == netloc:
|
|
44 |
return service
|
|
45 |
return None
|
|
46 |
|
|
47 |
|
|
48 |
class Requests(RequestsSession):
|
|
49 |
def request(self, method, url, **kwargs):
|
|
50 |
remote_service = get_known_service_for_url(url)
|
|
51 |
kwargs['auth'] = PublikSignature(remote_service.get('secret'))
|
|
52 |
|
|
53 |
# only keeps the path (URI) in url parameter, scheme and netloc are
|
|
54 |
# in remote_service
|
|
55 |
scheme, netloc, path, params, query, fragment = urllib.parse.urlparse(url)
|
|
56 |
url = urllib.parse.urlunparse(('', '', path, params, query, fragment))
|
|
57 |
|
|
58 |
query_params = {'orig': remote_service.get('orig')}
|
|
59 |
|
|
60 |
remote_service_base_url = remote_service.get('url')
|
|
61 |
scheme, netloc, dummy, params, old_query, fragment = urllib.parse.urlparse(remote_service_base_url)
|
|
62 |
|
|
63 |
query = urlencode(query_params)
|
|
64 |
url = urllib.parse.urlunparse((scheme, netloc, path, params, query, fragment))
|
|
65 |
|
|
66 |
return super().request(method, url, **kwargs)
|
0 |
|
-
|