Merge lp:~nataliabidart/ubuntu-sso-client/reformat-tokens into lp:ubuntu-sso-client

Proposed by Natalia Bidart
Status: Merged
Approved by: Roman Yepishev
Approved revision: 611
Merged at revision: 609
Proposed branch: lp:~nataliabidart/ubuntu-sso-client/reformat-tokens
Merge into: lp:ubuntu-sso-client
Diff against target: 159 lines (+66/-14)
2 files modified
ubuntu_sso/keyring.py (+32/-4)
ubuntu_sso/tests/test_keyring.py (+34/-10)
To merge this branch: bzr merge lp:~nataliabidart/ubuntu-sso-client/reformat-tokens
Reviewer Review Type Date Requested Status
Roman Yepishev (community) fieldtest Approve
Alejandro J. Cura (community) Approve
Review via email: mp+34337@code.launchpad.net

Commit message

Token name is now built using '@'. Old quoted tokens are migrated to the new names (LP: #628158).

Description of the change

Token name is now built using '@'. Old quoted tokens are migrated to the new names (though the token name is not modified in the SSO side).

To post a comment you must log in.
Revision history for this message
Alejandro J. Cura (alecu) wrote :

Looks great.

review: Approve
611. By Natalia Bidart

Fixing typos.

Revision history for this message
Roman Yepishev (rye) wrote :

"Ubuntu One @ buzz" - looks great! Thanks!

review: Approve (fieldtest)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'ubuntu_sso/keyring.py'
--- ubuntu_sso/keyring.py 2010-08-26 20:56:29 +0000
+++ ubuntu_sso/keyring.py 2010-09-01 20:41:39 +0000
@@ -28,6 +28,10 @@
28from ubuntu_sso.logger import setupLogging28from ubuntu_sso.logger import setupLogging
29logger = setupLogging("ubuntu_sso.main")29logger = setupLogging("ubuntu_sso.main")
3030
31
32TOKEN_SEPARATOR = ' @ '
33SEPARATOR_REPLACEMENT = ' AT '
34
31U1_APP_NAME = "Ubuntu One"35U1_APP_NAME = "Ubuntu One"
32U1_KEY_NAME = "UbuntuOne token for https://ubuntuone.com"36U1_KEY_NAME = "UbuntuOne token for https://ubuntuone.com"
33U1_KEY_ATTR = {37U1_KEY_ATTR = {
@@ -36,14 +40,22 @@
36}40}
3741
3842
39def get_token_name(app_name):43def get_old_token_name(app_name):
40 """Build the token name."""44 """Build the token name (old style)."""
41 quoted_app_name = urllib.quote(app_name)45 quoted_app_name = urllib.quote(app_name)
42 computer_name = socket.gethostname()46 computer_name = socket.gethostname()
43 quoted_computer_name = urllib.quote(computer_name)47 quoted_computer_name = urllib.quote(computer_name)
44 return "%s - %s" % (quoted_app_name, quoted_computer_name)48 return "%s - %s" % (quoted_app_name, quoted_computer_name)
4549
4650
51def get_token_name(app_name):
52 """Build the token name."""
53 computer_name = socket.gethostname()
54 computer_name = computer_name.replace(TOKEN_SEPARATOR,
55 SEPARATOR_REPLACEMENT)
56 return TOKEN_SEPARATOR.join((app_name, computer_name)).encode('utf-8')
57
58
47class Keyring(object):59class Keyring(object):
48 """A Keyring for a given application name."""60 """A Keyring for a given application name."""
49 KEYRING_NAME = "login"61 KEYRING_NAME = "login"
@@ -61,12 +73,14 @@
61 if not name in keyring_names:73 if not name in keyring_names:
62 gnomekeyring.create_sync(name)74 gnomekeyring.create_sync(name)
6375
64 def _find_keyring_item(self):76 def _find_keyring_item(self, attr=None):
65 """Return the keyring item or None if not found."""77 """Return the keyring item or None if not found."""
78 if attr is None:
79 attr = self._get_keyring_attr()
66 try:80 try:
67 items = gnomekeyring.find_items_sync(81 items = gnomekeyring.find_items_sync(
68 gnomekeyring.ITEM_GENERIC_SECRET,82 gnomekeyring.ITEM_GENERIC_SECRET,
69 self._get_keyring_attr())83 attr)
70 except gnomekeyring.NoMatchError:84 except gnomekeyring.NoMatchError:
71 # if no items found, return None85 # if no items found, return None
72 return None86 return None
@@ -98,10 +112,24 @@
98 gnomekeyring.ITEM_GENERIC_SECRET, self.app_name,112 gnomekeyring.ITEM_GENERIC_SECRET, self.app_name,
99 self._get_keyring_attr(), secret, True)113 self._get_keyring_attr(), secret, True)
100114
115 def _migrate_old_token_name(self):
116 """Migrate credentials with old name, store them with new name."""
117 attr = self._get_keyring_attr()
118 attr['token-name'] = get_old_token_name(self.app_name)
119 item = self._find_keyring_item(attr=attr)
120 if item is not None:
121 self.set_ubuntusso_attr(dict(urlparse.parse_qsl(item.secret)))
122 gnomekeyring.item_delete_sync(item.keyring, item.item_id)
123
124 return self._find_keyring_item()
125
101 def get_ubuntusso_attr(self):126 def get_ubuntusso_attr(self):
102 """Return the secret of the SSO item in a dictionary."""127 """Return the secret of the SSO item in a dictionary."""
103 # If we have no attributes, return None128 # If we have no attributes, return None
104 item = self._find_keyring_item()129 item = self._find_keyring_item()
130 if item is None:
131 item = self._migrate_old_token_name()
132
105 if item is not None:133 if item is not None:
106 return dict(urlparse.parse_qsl(item.secret))134 return dict(urlparse.parse_qsl(item.secret))
107 else:135 else:
108136
=== modified file 'ubuntu_sso/tests/test_keyring.py'
--- ubuntu_sso/tests/test_keyring.py 2010-08-26 20:56:29 +0000
+++ ubuntu_sso/tests/test_keyring.py 2010-09-01 20:41:39 +0000
@@ -1,6 +1,7 @@
1# test_keyring - tests for ubuntu_sso.keyring1# test_keyring - tests for ubuntu_sso.keyring
2#2#
3# Author: Alejandro J. Cura <alecu@canonical.com>3# Author: Alejandro J. Cura <alecu@canonical.com>
4# Author: Natalia B. Bidart <natalia.bidart@canonical.com>
4#5#
5# Copyright 2010 Canonical Ltd.6# Copyright 2010 Canonical Ltd.
6#7#
@@ -26,6 +27,8 @@
2627
27from ubuntu_sso import keyring28from ubuntu_sso import keyring
2829
30APP_NAME = 'Yadda Yadda Doo'
31
2932
30def build_fake_gethostname(fake_hostname):33def build_fake_gethostname(fake_hostname):
31 """Return a fake hostname getter."""34 """Return a fake hostname getter."""
@@ -106,18 +109,29 @@
106 """A simple token name is built right."""109 """A simple token name is built right."""
107 sample_app_name = "UbuntuTwo"110 sample_app_name = "UbuntuTwo"
108 sample_hostname = "Darkstar"111 sample_hostname = "Darkstar"
109 expected_result = "UbuntuTwo - Darkstar"112 expected_result = "UbuntuTwo @ Darkstar"
110113
111 fake_gethostname = build_fake_gethostname(sample_hostname)114 fake_gethostname = build_fake_gethostname(sample_hostname)
112 self.patch(socket, "gethostname", fake_gethostname)115 self.patch(socket, "gethostname", fake_gethostname)
113 result = keyring.get_token_name(sample_app_name)116 result = keyring.get_token_name(sample_app_name)
114 self.assertEqual(result, expected_result)117 self.assertEqual(result, expected_result)
115118
116 def test_get_complex_token_name(self):119 def test_get_complex_token_name_for_app_name(self):
120 """A complex token name is built right too."""
121 sample_app_name = "Ubuntu @ Eleven"
122 sample_hostname = "Mate+Cocido"
123 expected_result = "Ubuntu @ Eleven @ Mate+Cocido"
124
125 fake_gethostname = build_fake_gethostname(sample_hostname)
126 self.patch(socket, "gethostname", fake_gethostname)
127 result = keyring.get_token_name(sample_app_name)
128 self.assertEqual(result, expected_result)
129
130 def test_get_complex_token_name_for_hostname(self):
117 """A complex token name is built right too."""131 """A complex token name is built right too."""
118 sample_app_name = "Ubuntu Eleven"132 sample_app_name = "Ubuntu Eleven"
119 sample_hostname = "Mate+Cocido"133 sample_hostname = "Mate @ Cocido"
120 expected_result = "Ubuntu%20Eleven - Mate%2BCocido"134 expected_result = "Ubuntu Eleven @ Mate AT Cocido"
121135
122 fake_gethostname = build_fake_gethostname(sample_hostname)136 fake_gethostname = build_fake_gethostname(sample_hostname)
123 self.patch(socket, "gethostname", fake_gethostname)137 self.patch(socket, "gethostname", fake_gethostname)
@@ -167,6 +181,16 @@
167 result = keyring.Keyring("appname").get_ubuntusso_attr()181 result = keyring.Keyring("appname").get_ubuntusso_attr()
168 self.assertEqual(result, sample_creds)182 self.assertEqual(result, sample_creds)
169183
184 def test_get_credentials_migrating_token(self):
185 """Test that credentials are properly retrieved and migrated."""
186 sample_creds = {"name": "sample creds name"}
187 obj = keyring.Keyring(APP_NAME)
188 obj.token_name = keyring.get_old_token_name(APP_NAME)
189 obj.set_ubuntusso_attr(sample_creds)
190
191 result = keyring.Keyring(APP_NAME).get_ubuntusso_attr()
192 self.assertEqual(result, sample_creds)
193
170 def test_get_old_cred_found(self):194 def test_get_old_cred_found(self):
171 """The method returns a new set of creds if old creds are found."""195 """The method returns a new set of creds if old creds are found."""
172 sample_oauth_token = "sample oauth token"196 sample_oauth_token = "sample oauth token"

Subscribers

People subscribed via source and target branches