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
1=== modified file 'ubuntu_sso/keyring.py'
2--- ubuntu_sso/keyring.py 2010-08-26 20:56:29 +0000
3+++ ubuntu_sso/keyring.py 2010-09-01 20:41:39 +0000
4@@ -28,6 +28,10 @@
5 from ubuntu_sso.logger import setupLogging
6 logger = setupLogging("ubuntu_sso.main")
7
8+
9+TOKEN_SEPARATOR = ' @ '
10+SEPARATOR_REPLACEMENT = ' AT '
11+
12 U1_APP_NAME = "Ubuntu One"
13 U1_KEY_NAME = "UbuntuOne token for https://ubuntuone.com"
14 U1_KEY_ATTR = {
15@@ -36,14 +40,22 @@
16 }
17
18
19-def get_token_name(app_name):
20- """Build the token name."""
21+def get_old_token_name(app_name):
22+ """Build the token name (old style)."""
23 quoted_app_name = urllib.quote(app_name)
24 computer_name = socket.gethostname()
25 quoted_computer_name = urllib.quote(computer_name)
26 return "%s - %s" % (quoted_app_name, quoted_computer_name)
27
28
29+def get_token_name(app_name):
30+ """Build the token name."""
31+ computer_name = socket.gethostname()
32+ computer_name = computer_name.replace(TOKEN_SEPARATOR,
33+ SEPARATOR_REPLACEMENT)
34+ return TOKEN_SEPARATOR.join((app_name, computer_name)).encode('utf-8')
35+
36+
37 class Keyring(object):
38 """A Keyring for a given application name."""
39 KEYRING_NAME = "login"
40@@ -61,12 +73,14 @@
41 if not name in keyring_names:
42 gnomekeyring.create_sync(name)
43
44- def _find_keyring_item(self):
45+ def _find_keyring_item(self, attr=None):
46 """Return the keyring item or None if not found."""
47+ if attr is None:
48+ attr = self._get_keyring_attr()
49 try:
50 items = gnomekeyring.find_items_sync(
51 gnomekeyring.ITEM_GENERIC_SECRET,
52- self._get_keyring_attr())
53+ attr)
54 except gnomekeyring.NoMatchError:
55 # if no items found, return None
56 return None
57@@ -98,10 +112,24 @@
58 gnomekeyring.ITEM_GENERIC_SECRET, self.app_name,
59 self._get_keyring_attr(), secret, True)
60
61+ def _migrate_old_token_name(self):
62+ """Migrate credentials with old name, store them with new name."""
63+ attr = self._get_keyring_attr()
64+ attr['token-name'] = get_old_token_name(self.app_name)
65+ item = self._find_keyring_item(attr=attr)
66+ if item is not None:
67+ self.set_ubuntusso_attr(dict(urlparse.parse_qsl(item.secret)))
68+ gnomekeyring.item_delete_sync(item.keyring, item.item_id)
69+
70+ return self._find_keyring_item()
71+
72 def get_ubuntusso_attr(self):
73 """Return the secret of the SSO item in a dictionary."""
74 # If we have no attributes, return None
75 item = self._find_keyring_item()
76+ if item is None:
77+ item = self._migrate_old_token_name()
78+
79 if item is not None:
80 return dict(urlparse.parse_qsl(item.secret))
81 else:
82
83=== modified file 'ubuntu_sso/tests/test_keyring.py'
84--- ubuntu_sso/tests/test_keyring.py 2010-08-26 20:56:29 +0000
85+++ ubuntu_sso/tests/test_keyring.py 2010-09-01 20:41:39 +0000
86@@ -1,6 +1,7 @@
87 # test_keyring - tests for ubuntu_sso.keyring
88 #
89 # Author: Alejandro J. Cura <alecu@canonical.com>
90+# Author: Natalia B. Bidart <natalia.bidart@canonical.com>
91 #
92 # Copyright 2010 Canonical Ltd.
93 #
94@@ -26,6 +27,8 @@
95
96 from ubuntu_sso import keyring
97
98+APP_NAME = 'Yadda Yadda Doo'
99+
100
101 def build_fake_gethostname(fake_hostname):
102 """Return a fake hostname getter."""
103@@ -106,18 +109,29 @@
104 """A simple token name is built right."""
105 sample_app_name = "UbuntuTwo"
106 sample_hostname = "Darkstar"
107- expected_result = "UbuntuTwo - Darkstar"
108-
109- fake_gethostname = build_fake_gethostname(sample_hostname)
110- self.patch(socket, "gethostname", fake_gethostname)
111- result = keyring.get_token_name(sample_app_name)
112- self.assertEqual(result, expected_result)
113-
114- def test_get_complex_token_name(self):
115+ expected_result = "UbuntuTwo @ Darkstar"
116+
117+ fake_gethostname = build_fake_gethostname(sample_hostname)
118+ self.patch(socket, "gethostname", fake_gethostname)
119+ result = keyring.get_token_name(sample_app_name)
120+ self.assertEqual(result, expected_result)
121+
122+ def test_get_complex_token_name_for_app_name(self):
123+ """A complex token name is built right too."""
124+ sample_app_name = "Ubuntu @ Eleven"
125+ sample_hostname = "Mate+Cocido"
126+ expected_result = "Ubuntu @ Eleven @ Mate+Cocido"
127+
128+ fake_gethostname = build_fake_gethostname(sample_hostname)
129+ self.patch(socket, "gethostname", fake_gethostname)
130+ result = keyring.get_token_name(sample_app_name)
131+ self.assertEqual(result, expected_result)
132+
133+ def test_get_complex_token_name_for_hostname(self):
134 """A complex token name is built right too."""
135 sample_app_name = "Ubuntu Eleven"
136- sample_hostname = "Mate+Cocido"
137- expected_result = "Ubuntu%20Eleven - Mate%2BCocido"
138+ sample_hostname = "Mate @ Cocido"
139+ expected_result = "Ubuntu Eleven @ Mate AT Cocido"
140
141 fake_gethostname = build_fake_gethostname(sample_hostname)
142 self.patch(socket, "gethostname", fake_gethostname)
143@@ -167,6 +181,16 @@
144 result = keyring.Keyring("appname").get_ubuntusso_attr()
145 self.assertEqual(result, sample_creds)
146
147+ def test_get_credentials_migrating_token(self):
148+ """Test that credentials are properly retrieved and migrated."""
149+ sample_creds = {"name": "sample creds name"}
150+ obj = keyring.Keyring(APP_NAME)
151+ obj.token_name = keyring.get_old_token_name(APP_NAME)
152+ obj.set_ubuntusso_attr(sample_creds)
153+
154+ result = keyring.Keyring(APP_NAME).get_ubuntusso_attr()
155+ self.assertEqual(result, sample_creds)
156+
157 def test_get_old_cred_found(self):
158 """The method returns a new set of creds if old creds are found."""
159 sample_oauth_token = "sample oauth token"

Subscribers

People subscribed via source and target branches