Merge lp:~rockstar/ubuntuoneauthkit/oauth into lp:ubuntuoneauthkit

Proposed by Paul Hummer
Status: Merged
Approved by: Paul Hummer
Approved revision: 20
Merged at revision: 8
Proposed branch: lp:~rockstar/ubuntuoneauthkit/oauth
Merge into: lp:ubuntuoneauthkit
Prerequisite: lp:~rockstar/ubuntuoneauthkit/more-cleanup
Diff against target: 247 lines (+114/-42)
4 files modified
UbuntuOneAuthKit.xcodeproj/project.pbxproj (+20/-12)
UbuntuOneAuthKit/UOAuthManager.h (+8/-0)
UbuntuOneAuthKit/UOAuthManager.m (+59/-29)
UbuntuOneAuthKitTests/UbuntuOneAuthKitTests.m (+27/-1)
To merge this branch: bzr merge lp:~rockstar/ubuntuoneauthkit/oauth
Reviewer Review Type Date Requested Status
Zachery Bir Approve
Review via email: mp+126374@code.launchpad.net

Commit message

Store the OAuth data using the UOKeyManager. Add interfaces for checking those tokens. Add the accompanying tests.

Description of the change

Store the OAuth data using the UOKeyManager. Add interfaces for checking those tokens. Add the accompanying tests.

To post a comment you must log in.
Revision history for this message
Zachery Bir (urbanape) :
review: Approve
Revision history for this message
Ubuntu One Auto Pilot (otto-pilot) wrote :

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'UbuntuOneAuthKit.xcodeproj/project.pbxproj'
2--- UbuntuOneAuthKit.xcodeproj/project.pbxproj 2012-09-26 03:53:18 +0000
3+++ UbuntuOneAuthKit.xcodeproj/project.pbxproj 2012-09-26 03:53:18 +0000
4@@ -192,10 +192,29 @@
5 children = (
6 523CC104160B6B7000B36474 /* UOKeyManager.h */,
7 523CC105160B6B7000B36474 /* UOKeyManager.m */,
8+ 52527AFD16050BA7000F9A90 /* UONetworkStatusCoordinator.h */,
9+ 52527AFE16050BA7000F9A90 /* UONetworkStatusCoordinator.m */,
10+ 52B57DF116057F590031E95C /* UOURLQueryStringParser.h */,
11+ 52B57DF216057F590031E95C /* UOURLQueryStringParser.m */,
12 );
13 name = Utilities;
14 sourceTree = "<group>";
15 };
16+ 523CC13D161285F900B36474 /* Operations */ = {
17+ isa = PBXGroup;
18+ children = (
19+ 528515AE1605034A004A1F7C /* UOAbstractNetworkOperation.h */,
20+ 528515AF1605034A004A1F7C /* UOAbstractNetworkOperation.m */,
21+ 52B57DE916052C700031E95C /* UOHTTPFetchOperation.h */,
22+ 52B57DEA16052C700031E95C /* UOHTTPFetchOperation.m */,
23+ 528515AA160502AB004A1F7C /* UOJSONFetchOperation.h */,
24+ 528515AB160502AB004A1F7C /* UOJSONFetchOperation.m */,
25+ 52B57DED16057DD80031E95C /* UOSSOMusicCredsFetchOperation.h */,
26+ 52B57DEE16057DD80031E95C /* UOSSOMusicCredsFetchOperation.m */,
27+ );
28+ name = Operations;
29+ sourceTree = "<group>";
30+ };
31 52527B0216052179000F9A90 /* Dependencies */ = {
32 isa = PBXGroup;
33 children = (
34@@ -250,21 +269,10 @@
35 528515681604F16B004A1F7C /* UbuntuOneAuthKit */ = {
36 isa = PBXGroup;
37 children = (
38+ 523CC13D161285F900B36474 /* Operations */,
39 523CC0FE160B697E00B36474 /* Utilities */,
40- 528515AE1605034A004A1F7C /* UOAbstractNetworkOperation.h */,
41- 528515AF1605034A004A1F7C /* UOAbstractNetworkOperation.m */,
42 5285159A1604F473004A1F7C /* UOAuthManager.h */,
43 5285159B1604F473004A1F7C /* UOAuthManager.m */,
44- 52B57DE916052C700031E95C /* UOHTTPFetchOperation.h */,
45- 52B57DEA16052C700031E95C /* UOHTTPFetchOperation.m */,
46- 528515AA160502AB004A1F7C /* UOJSONFetchOperation.h */,
47- 528515AB160502AB004A1F7C /* UOJSONFetchOperation.m */,
48- 52527AFD16050BA7000F9A90 /* UONetworkStatusCoordinator.h */,
49- 52527AFE16050BA7000F9A90 /* UONetworkStatusCoordinator.m */,
50- 52B57DED16057DD80031E95C /* UOSSOMusicCredsFetchOperation.h */,
51- 52B57DEE16057DD80031E95C /* UOSSOMusicCredsFetchOperation.m */,
52- 52B57DF116057F590031E95C /* UOURLQueryStringParser.h */,
53- 52B57DF216057F590031E95C /* UOURLQueryStringParser.m */,
54 528515691604F16B004A1F7C /* Supporting Files */,
55 );
56 path = UbuntuOneAuthKit;
57
58=== modified file 'UbuntuOneAuthKit/UOAuthManager.h'
59--- UbuntuOneAuthKit/UOAuthManager.h 2012-09-26 03:53:18 +0000
60+++ UbuntuOneAuthKit/UOAuthManager.h 2012-09-26 03:53:18 +0000
61@@ -8,6 +8,10 @@
62
63 #import <Foundation/Foundation.h>
64
65+static NSString *UOOAuthConsumerKey = @"UOOAuthConsumerKey";
66+static NSString *UOOAuthConsumerSecret = @"UOOAuthConsumerSecret";
67+static NSString *UOOAuthToken = @"UOOAuthToken";
68+static NSString *UOOAuthTokenSecret = @"UOOAuthTokenSecret";
69 static NSString *UOSubsonicUsernameKey = @"UOSubsonicUsername";
70 static NSString *UOSubsonicPasswordKey = @"UOSubsonicPassword";
71
72@@ -16,7 +20,11 @@
73 @interface UOAuthManager : NSObject
74 + (UOAuthManager *)sharedAuthManager;
75 - (void)loginWithUsername:(NSString *)username andPassword:(NSString *)password tokenName:(NSString *)tokenName completionBlock:(void(^)())completionBlock errorBlock:(void(^)(NSError *))errorBlock;
76+- (void)logout;
77+- (BOOL)hasCredentials;
78+
79 - (void)fetchSubsonicCredentials:(void(^)(NSString *))completionBlock errorBlock:(void(^)(NSError *))errorBlock;
80+- (BOOL)hasSubsonicCredentials;
81 - (NSString *)subsonicUsername;
82 - (NSString *)subsonicPassword;
83 - (void)clearSubsonicCredentials;
84
85=== modified file 'UbuntuOneAuthKit/UOAuthManager.m'
86--- UbuntuOneAuthKit/UOAuthManager.m 2012-09-26 03:53:18 +0000
87+++ UbuntuOneAuthKit/UOAuthManager.m 2012-09-26 03:53:18 +0000
88@@ -32,14 +32,6 @@
89
90 @interface UOAuthManager () {
91 NSOperationQueue *operationQueue;
92-
93- /* rockstar (16 Sept 2012) - I don't like this interface. It does not persist,
94- * and is not reusable across different runs (so it's required to run again, which
95- * is craziness...
96- */
97- OAConsumer *consumer;
98- OAToken *token;
99- OAPlaintextSignatureProvider *signatureProvider;
100 }
101 @end
102
103@@ -82,36 +74,42 @@
104
105 /* New token needs to be imported into Ubuntu One from SSO */
106 NSDictionary *response = (NSDictionary *)jsonResult;
107- NSString *encodedUsername = [username urlParameterEncodedString];
108- NSURL *tokenURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://one.ubuntu.com/oauth/sso-finished-so-get-tokens/%@", encodedUsername]];
109-
110- consumer = [[OAConsumer alloc] initWithKey:[response objectForKey:@"consumer_key"] secret:[response objectForKey:@"consumer_secret"]];
111- token = [[OAToken alloc] initWithKey:[response objectForKey:@"token"] secret:[response objectForKey:@"token_secret"]];
112- signatureProvider = [[OAPlaintextSignatureProvider alloc] init];
113- OAMutableURLRequest *oauthRequest = [[OAMutableURLRequest alloc] initWithURL:tokenURL consumer:consumer token:token realm:nil signatureProvider:signatureProvider];
114- [oauthRequest prepare];
115-
116- UOHTTPFetchOperation *tokenSyncOperation = [UOHTTPFetchOperation httpOperationWithRequest:oauthRequest action:^(NSString *responseBody, NSError *error) {
117- if (error) {
118- if (errorBlock) {
119- errorBlock(error);
120- }
121- return;
122- }
123- if (completionBlock) {
124- completionBlock();
125- }
126- }];
127- [operationQueue addOperation:tokenSyncOperation];
128+
129+ UOKeyManager *keyManager = [UOKeyManager sharedManager];
130+ [keyManager createKey:UOOAuthConsumerKey withValue:[response objectForKey:@"consumer_key"]];
131+ [keyManager createKey:UOOAuthConsumerSecret withValue:[response objectForKey:@"consumer_secret"]];
132+ [keyManager createKey:UOOAuthToken withValue:[response objectForKey:@"token"]];
133+ [keyManager createKey:UOOAuthTokenSecret withValue:[response objectForKey:@"token_secret"]];
134+
135+ [self syncUbuntuSSOTokens:username completionBlock:completionBlock errorBlock:errorBlock];
136 }];
137 tokenCreationOperation.credential = credential;
138 [operationQueue addOperation:tokenCreationOperation];
139 }
140
141+- (void)logout {
142+ UOKeyManager *keyManager = [UOKeyManager sharedManager];
143+ [keyManager deleteKey:UOOAuthConsumerKey];
144+ [keyManager deleteKey:UOOAuthConsumerSecret];
145+ [keyManager deleteKey:UOOAuthToken];
146+ [keyManager deleteKey:UOOAuthTokenSecret];
147+
148+ [self clearSubsonicCredentials];
149+}
150+
151+- (BOOL)hasCredentials {
152+ UOKeyManager *keyManager = [UOKeyManager sharedManager];
153+ return (BOOL)[keyManager readKey:UOOAuthConsumerKey];
154+}
155+
156 #pragma mark
157 #pragma mark Subsonic specific methods
158
159 -(void)fetchSubsonicCredentials:(void (^)(NSString *))completionBlock errorBlock:(void (^)(NSError *))errorBlock {
160+ UOKeyManager *keyManager = [UOKeyManager sharedManager];
161+ OAConsumer *consumer = [[OAConsumer alloc] initWithKey:[keyManager readKey:UOOAuthConsumerKey] secret:[keyManager readKey:UOOAuthConsumerSecret]];
162+ OAToken *token = [[OAToken alloc] initWithKey:[keyManager readKey:UOOAuthToken] secret:[keyManager readKey:UOOAuthTokenSecret]];
163+ OAPlaintextSignatureProvider *signatureProvider = [[OAPlaintextSignatureProvider alloc] init];
164
165 NSURL *credsURL = [NSURL URLWithString:@"https://one.ubuntu.com/phones/creds/ios?scheme=x-ubuntuone-music"];
166 OAMutableURLRequest *credsRequest = [[OAMutableURLRequest alloc] initWithURL:credsURL consumer:consumer token:token realm:nil signatureProvider:signatureProvider];
167@@ -171,4 +169,36 @@
168 [manager deleteKey:UOSubsonicPasswordKey];
169 }
170
171+-(BOOL)hasSubsonicCredentials {
172+ return (BOOL)[[UOKeyManager sharedManager] readKey:UOSubsonicUsernameKey];
173+}
174+
175+#pragma mark
176+#pragma mark Private methods
177+
178+-(void)syncUbuntuSSOTokens:(NSString *)username completionBlock:(void (^)())completionBlock errorBlock:(void (^)(NSError *))errorBlock {
179+ UOKeyManager *keyManager = [UOKeyManager sharedManager];
180+ NSString *encodedUsername = [username urlParameterEncodedString];
181+ NSURL *tokenURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://one.ubuntu.com/oauth/sso-finished-so-get-tokens/%@", encodedUsername]];
182+
183+ OAConsumer *consumer = [[OAConsumer alloc] initWithKey:[keyManager readKey:UOOAuthConsumerKey] secret:[keyManager readKey:UOOAuthConsumerSecret]];
184+ OAToken *token = [[OAToken alloc] initWithKey:[keyManager readKey:UOOAuthToken] secret:[keyManager readKey:UOOAuthTokenSecret]];
185+ OAPlaintextSignatureProvider *signatureProvider = [[OAPlaintextSignatureProvider alloc] init];
186+ OAMutableURLRequest *oauthRequest = [[OAMutableURLRequest alloc] initWithURL:tokenURL consumer:consumer token:token realm:nil signatureProvider:signatureProvider];
187+ [oauthRequest prepare];
188+
189+ UOHTTPFetchOperation *tokenSyncOperation = [UOHTTPFetchOperation httpOperationWithRequest:oauthRequest action:^(NSString *responseBody, NSError *error) {
190+ if (error) {
191+ if (errorBlock) {
192+ errorBlock(error);
193+ }
194+ return;
195+ }
196+ if (completionBlock) {
197+ completionBlock();
198+ }
199+ }];
200+ [operationQueue addOperation:tokenSyncOperation];
201+}
202+
203 @end
204
205=== modified file 'UbuntuOneAuthKitTests/UbuntuOneAuthKitTests.m'
206--- UbuntuOneAuthKitTests/UbuntuOneAuthKitTests.m 2012-09-26 03:53:18 +0000
207+++ UbuntuOneAuthKitTests/UbuntuOneAuthKitTests.m 2012-09-26 03:53:18 +0000
208@@ -26,7 +26,7 @@
209 - (void)tearDown
210 {
211 UOKeyManager *manager = [UOKeyManager sharedManager];
212- NSArray *keys = [NSArray arrayWithObjects:TestKey, UOSubsonicUsernameKey, UOSubsonicPasswordKey, nil];
213+ NSArray *keys = [NSArray arrayWithObjects:TestKey, UOOAuthConsumerKey, UOOAuthConsumerSecret, UOOAuthToken, UOOAuthTokenSecret, UOSubsonicUsernameKey, UOSubsonicPasswordKey, nil];
214 for (NSString *key in keys) {
215 [manager deleteKey:key];
216 }
217@@ -81,4 +81,30 @@
218 STAssertNil([[UOKeyManager sharedManager] readKey:UOSubsonicPasswordKey], nil);
219 }
220
221+-(void)testUOAuthManagerLogout {
222+ [[UOKeyManager sharedManager] createKey:UOOAuthConsumerKey withValue:@"myConsumerKey"];
223+ [[UOKeyManager sharedManager] createKey:UOOAuthConsumerSecret withValue:@"myConsumerSecret"];
224+ [[UOKeyManager sharedManager] createKey:UOOAuthToken withValue:@"myToken"];
225+ [[UOKeyManager sharedManager] createKey:UOOAuthTokenSecret withValue:@"myTokenSecret"];
226+
227+ [[UOAuthManager sharedAuthManager] logout];
228+
229+ STAssertNil([[UOKeyManager sharedManager] readKey:UOOAuthConsumerKey], nil);
230+ STAssertNil([[UOKeyManager sharedManager] readKey:UOOAuthConsumerSecret], nil);
231+ STAssertNil([[UOKeyManager sharedManager] readKey:UOOAuthToken], nil);
232+ STAssertNil([[UOKeyManager sharedManager] readKey:UOOAuthTokenSecret], nil);
233+}
234+
235+-(void)testUOAuthManagerHasCredentials {
236+ STAssertFalse([[UOAuthManager sharedAuthManager] hasCredentials], nil);
237+ [[UOKeyManager sharedManager] createKey:UOOAuthConsumerKey withValue:@"myConsumerKey"];
238+ STAssertTrue([[UOAuthManager sharedAuthManager] hasCredentials], nil);
239+}
240+
241+-(void)testUOAuthManagerHasSubsonicCredentials {
242+ STAssertFalse([[UOAuthManager sharedAuthManager] hasSubsonicCredentials], nil);
243+ [[UOKeyManager sharedManager] createKey:UOSubsonicUsernameKey withValue:@"Test User"];
244+ STAssertTrue([[UOAuthManager sharedAuthManager] hasSubsonicCredentials], nil);
245+}
246+
247 @end

Subscribers

People subscribed via source and target branches