From 825458738f46522dc06c13d0eb9350fdb7d4929ae7a4e16e86aac540a85a5def Mon Sep 17 00:00:00 2001
From: Axel Braun <axel.braun@gmx.de>
Date: Tue, 27 Jul 2021 15:06:22 +0000
Subject: [PATCH] Accepting request 908626 from Application:ERP:GNUHealth:3.8

- fix_werkzeug_2.x.patch added for python-Werkzeug 2.x compatibility

OBS-URL: https://build.opensuse.org/request/show/908626
OBS-URL: https://build.opensuse.org/package/show/Application:ERP:GNUHealth:Factory/trytond?expand=0&rev=130
---
 fix_werkzeug_2.x.patch | 54 ++++++++++++++++++++++++++++++++++++++++++
 trytond.changes        |  5 ++++
 trytond.spec           |  7 ++++++
 3 files changed, 66 insertions(+)
 create mode 100644 fix_werkzeug_2.x.patch

diff --git a/fix_werkzeug_2.x.patch b/fix_werkzeug_2.x.patch
new file mode 100644
index 0000000..9acc5b7
--- /dev/null
+++ b/fix_werkzeug_2.x.patch
@@ -0,0 +1,54 @@
+diff -U 3 -dHrN -- a/trytond/protocols/wrappers.py b/trytond/protocols/wrappers.py
+--- a/trytond/protocols/wrappers.py	2019-08-11 19:34:43.000000000 +0200
++++ b/trytond/protocols/wrappers.py	2021-07-27 15:59:38.213817153 +0200
+@@ -8,7 +8,6 @@
+ 
+ from werkzeug.wrappers import Request as _Request, Response
+ from werkzeug.utils import cached_property
+-from werkzeug.http import wsgi_to_bytes, bytes_to_wsgi
+ from werkzeug.datastructures import Authorization
+ from werkzeug.exceptions import abort, HTTPException
+ 
+@@ -84,7 +83,8 @@
+ def parse_authorization_header(value):
+     if not value:
+         return
+-    value = wsgi_to_bytes(value)
++    if not isinstance(value, bytes):
++        value = value.encode('latin1')    
+     try:
+         auth_type, auth_info = value.split(None, 1)
+         auth_type = auth_type.lower()
+@@ -98,9 +98,9 @@
+         except Exception:
+             return
+         return Authorization('session', {
+-                'username': bytes_to_wsgi(username),
++                'username': username.decode("latin1"),                
+                 'userid': userid,
+-                'session': bytes_to_wsgi(session),
++                'session': session.decode("latin1"),                
+                 })
+ 
+ 
+@@ -171,16 +171,17 @@
+             pool = Pool()
+             UserApplication = pool.get('res.user.application')
+ 
+-            authorization = wsgi_to_bytes(request.headers['Authorization'])
++            authorization = request.headers['Authorization']            
+             try:
+                 auth_type, auth_info = authorization.split(None, 1)
+                 auth_type = auth_type.lower()
+             except ValueError:
+                 abort(401)
+-            if auth_type != b'bearer':
++            if auth_type != 'bearer':
+                 abort(403)
+ 
+-            application = UserApplication.check(bytes_to_wsgi(auth_info), name)
++            application = UserApplication.check(auth_info, name)
++            
+             if not application:
+                 abort(403)
+             transaction = Transaction()
diff --git a/trytond.changes b/trytond.changes
index 0681321..21dd5b5 100644
--- a/trytond.changes
+++ b/trytond.changes
@@ -1,3 +1,8 @@
+-------------------------------------------------------------------
+Tue Jul 27 14:04:30 UTC 2021 - Axel Braun <axel.braun@gmx.de>
+
+- fix_werkzeug_2.x.patch added for python-Werkzeug 2.x compatibility 
+
 -------------------------------------------------------------------
 Fri Jun 18 15:48:52 UTC 2021 - Axel Braun <axel.braun@gmx.de>
 
diff --git a/trytond.spec b/trytond.spec
index face032..99bef9f 100644
--- a/trytond.spec
+++ b/trytond.spec
@@ -36,6 +36,7 @@ Source6:        https://keybase.io/cedrickrier/pgp_keys.asc?fingerprint=7C5A4360
 Source20:       %{name}.service
 Patch0:         fix_werkzeug.patch
 Patch1:         revert_werkzeug_setup.patch
+Patch2:         fix_werkzeug_2.x.patch
 BuildRequires:  fdupes
 BuildRequires:  python3-Werkzeug
 BuildRequires:  python3-bcrypt
@@ -84,6 +85,12 @@ cp %{SOURCE2} .
 %patch0 -p1
 %patch1 -p1
 
+#Werkzeug2 is not compatible with Werkzeug 1.x, so we need a conditional patch
+echo 0%{?suse_version}
+%if 0%{?suse_version} >= 1550
+%patch2 -p1
+%endif
+
 %build
 %python3_build