From 7c27a542eb112657e11efa9a6e39c1d98ed342c1 Mon Sep 17 00:00:00 2001
From: Patrick Uiterwijk <puiterwijk@redhat.com>
Date: May 09 2016 20:19:29 +0000
Subject: [PATCH 1/4] login: authtest: add unicode character for unicode tests


Ticket: #213
Signed-off-by: Patrick Uiterwijk <puiterwijk@redhat.com>
Reviewed-by: Pierre-Yves Chibon <pingou@pingoured.fr>

---

diff --git a/ipsilon/login/authtest.py b/ipsilon/login/authtest.py
index aa2a73a..6b2db5b 100644
--- a/ipsilon/login/authtest.py
+++ b/ipsilon/login/authtest.py
@@ -1,4 +1,5 @@
 # Copyright (C) 2014 Ipsilon project Contributors, for license see COPYING
+# -*- coding: utf-8 -*-
 
 from ipsilon.login.common import LoginFormBase, LoginManagerBase, \
     LoginManagerInstaller
@@ -19,7 +20,7 @@ class TestAuth(LoginFormBase):
             if password == 'ipsilon':
                 cherrypy.log("User %s successfully authenticated." % username)
                 testdata = {
-                    'givenname': 'Test User',
+                    'givenname': 'Test User 一',
                     'surname': username,
                     'fullname': 'Test User %s' % username,
                     'email': '%s@example.com' % username,
diff --git a/tests/testmapping.py b/tests/testmapping.py
index 1fb38e0..59a173f 100755
--- a/tests/testmapping.py
+++ b/tests/testmapping.py
@@ -1,4 +1,5 @@
 #!/usr/bin/python
+# -*- coding: utf-8 -*-
 #
 # Copyright (C) 2015 Ipsilon project Contributors, for license see COPYING
 
@@ -195,7 +196,7 @@ if __name__ == '__main__':
             'NAME_ID': user,
             'fullname': 'Test User %s' % user,
             'surname': user,
-            'givenname': 'Test User',
+            'givenname': u'Test User 一',
             'email': '%s@example.com' % user,
             'groups': user,
         }
@@ -222,7 +223,7 @@ if __name__ == '__main__':
             'fullname': 'Test User %s' % user,
             'namefull': 'Test User %s' % user,
             'surname': user,
-            'givenname': 'Test User',
+            'givenname': u'Test User 一',
             'email': '%s@example.com' % user,
             'groups': user
         }
@@ -248,7 +249,7 @@ if __name__ == '__main__':
         expect = {
             'namefull': 'Test User %s' % user,
             'surname': user,
-            'givenname': 'Test User',
+            'givenname': u'Test User 一',
         }
         check_info_plugin(sess, idpname, spurl, expect)
     except Exception, e:  # pylint: disable=broad-except
@@ -273,7 +274,7 @@ if __name__ == '__main__':
         expect = {
             'fullname': 'Test User %s' % user,
             'surname': user,
-            'givenname': 'Test User',
+            'givenname': u'Test User 一',
             'email': '%s@example.com' % user,
         }
         check_info_plugin(sess, idpname, spurl, expect)
@@ -304,7 +305,7 @@ if __name__ == '__main__':
             'wholename': 'Test User %s' % user,
             'fullname': 'Test User %s' % user,
             'surname': user,
-            'givenname': 'Test User',
+            'givenname': u'Test User 一',
             'email': '%s@example.com' % user,
         }
         check_info_plugin(sess, idpname, spurl, expect)
@@ -336,7 +337,7 @@ if __name__ == '__main__':
             'wholename': 'Test User %s' % user,
             'fullname': 'Test User %s' % user,
             'surname': user,
-            'givenname': 'Test User',
+            'givenname': u'Test User 一',
             'email': '%s@example.com' % user,
             'NAME_ID': '%s@example.com' % user,
         }
@@ -363,7 +364,7 @@ if __name__ == '__main__':
         expect = {
             'fullname': 'Test User %s' % user,
             'surname': user,
-            'givenname': 'Test User',
+            'givenname': u'Test User 一',
             'email': '%s@example.com' % user,
         }
         check_info_plugin(sess, idpname, spurl, expect)
@@ -387,7 +388,7 @@ if __name__ == '__main__':
         expect = {
             'namefull': 'Test User %s' % user,
             'surname': user,
-            'givenname': 'Test User',
+            'givenname': u'Test User 一',
         }
         check_info_plugin(sess, idpname, spurl, expect)
     except Exception, e:  # pylint: disable=broad-except

From 6c75d27e31b39a7d036852e576a71ecaf97b975f Mon Sep 17 00:00:00 2001
From: Patrick Uiterwijk <puiterwijk@redhat.com>
Date: May 09 2016 20:19:32 +0000
Subject: [PATCH 2/4] helpers: common: setup postgres server as unicode-capable


This is required to be able to insert any unicode into the
database, which we really do want to test.

Ticket: #213
Signed-off-by: Patrick Uiterwijk <puiterwijk@redhat.com>
Reviewed-by: Pierre-Yves Chibon <pingou@pingoured.fr>

---

diff --git a/tests/helpers/common.py b/tests/helpers/common.py
index 5e17952..70dffd2 100755
--- a/tests/helpers/common.py
+++ b/tests/helpers/common.py
@@ -231,7 +231,7 @@ basicConstraints = CA:false""" % {'certdir': os.path.join(self.testdir,
         return http_conf_file
 
     def setup_pgdb(self, datadir, env):
-        cmd = ['/usr/bin/pg_ctl', 'initdb', '-D', datadir]
+        cmd = ['/usr/bin/pg_ctl', 'initdb', '-D', datadir, '-o', '-E UNICODE']
         subprocess.check_call(cmd, env=env)
         auth = 'host all all 127.0.0.1/24 trust\n'
         filename = os.path.join(datadir, 'pg_hba.conf')

From dd10301327be1e57ff59bcc94cd7b6e6981be52c Mon Sep 17 00:00:00 2001
From: Patrick Uiterwijk <puiterwijk@redhat.com>
Date: May 09 2016 20:19:35 +0000
Subject: [PATCH 3/4] providers: saml: remove str and unicode conversion


The conversion to str() breaks on unicode strings, since those
would fall outside of the accepted range of characters.
Removing this makes Python automatically do the right thing by
encoding it as str if possible, and unicode otherwise.

Ticket: #213
Signed-off-by: Patrick Uiterwijk <puiterwijk@redhat.com>
Reviewed-by: Pierre-Yves Chibon <pingou@pingoured.fr>

---

diff --git a/ipsilon/providers/saml2/auth.py b/ipsilon/providers/saml2/auth.py
index 08423a6..d3eb3f5 100644
--- a/ipsilon/providers/saml2/auth.py
+++ b/ipsilon/providers/saml2/auth.py
@@ -323,7 +323,6 @@ class AuthenticateRequest(ProviderPageBase):
                 attr = lasso.Saml2Attribute()
                 attr.name = key
                 attr.nameFormat = lasso.SAML2_ATTRIBUTE_NAME_FORMAT_BASIC
-                value = str(value).encode('utf-8')
                 self.debug('value %s' % value)
                 node = lasso.MiscTextNode.newWithString(value)
                 node.textChild = True

From 8188a334a22e903392cd874ba433099cedb4d927 Mon Sep 17 00:00:00 2001
From: Patrick Uiterwijk <puiterwijk@redhat.com>
Date: May 09 2016 20:19:38 +0000
Subject: [PATCH 4/4] Encode session information as unicode


This will allow us to store any character for user information in
any database.
Postgres driver will automatically coerce for us, but some, like
the sqlite backend, does not so this makes it work for both cases.

Ticket: #213
Signed-off-by: Patrick Uiterwijk <puiterwijk@redhat.com>
Reviewed-by: Pierre-Yves Chibon <pingou@pingoured.fr>

---

diff --git a/ipsilon/util/data.py b/ipsilon/util/data.py
index f42b02d..ed22540 100644
--- a/ipsilon/util/data.py
+++ b/ipsilon/util/data.py
@@ -828,6 +828,9 @@ class SAML2SessionStore(Store):
             datum['supported_logout_mechs'] = ','.join(
                 datum['supported_logout_mechs']
             )
+        for attr in datum:
+            if isinstance(datum[attr], str):
+                datum[attr] = unicode(datum[attr], 'utf-8')
         return self.new_unique_data(self.table, datum)
 
     def get_session(self, session_id=None, request_id=None):
@@ -859,6 +862,9 @@ class SAML2SessionStore(Store):
         return logged_in
 
     def update_session(self, datum):
+        for attr in datum:
+            if isinstance(datum[attr], str):
+                datum[attr] = unicode(datum[attr], 'utf-8')
         self.save_unique_data(self.table, datum)
 
     def remove_session(self, uuidval):

