Skip to content

Commit ccd5d7e

Browse files
committed
Fixes an inconsistency in the way blog passwords were stored that might cause problems with .com blogs with custom domains.
Fixes an issue where an xmlrpc error due to bad credentials would prompt the user to update their password, but the password would not propigate to the .com api until the app was restarted. Removes the old UIAlertView used for updating credentials, replacing it with the EditSiteViewController for a standardized look and feel. Exposes a promptForPassword method to WPTableViewController subclasses. Would be nice to keep this hidden.
1 parent 83fb564 commit ccd5d7e

File tree

8 files changed

+69
-132
lines changed

8 files changed

+69
-132
lines changed

WordPress/Classes/Blog.m

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,19 @@ + (Blog *)createFromDictionary:(NSDictionary *)blogInfo withContext:(NSManagedOb
114114
blog.isAdmin = [NSNumber numberWithInt:[[blogInfo objectForKey:@"isAdmin"] intValue]];
115115

116116
NSError *error = nil;
117-
[SFHFKeychainUtils storeUsername:[blogInfo objectForKey:@"username"]
118-
andPassword:[blogInfo objectForKey:@"password"]
119-
forServiceName:blog.hostURL
120-
updateExisting:TRUE
121-
error:&error ];
117+
if(blog.isWPcom) {
118+
[SFHFKeychainUtils storeUsername:[blogInfo objectForKey:@"username"]
119+
andPassword:[blogInfo objectForKey:@"password"]
120+
forServiceName:@"WordPress.com"
121+
updateExisting:TRUE
122+
error:&error ];
123+
} else {
124+
[SFHFKeychainUtils storeUsername:[blogInfo objectForKey:@"username"]
125+
andPassword:[blogInfo objectForKey:@"password"]
126+
forServiceName:blog.hostURL
127+
updateExisting:TRUE
128+
error:&error ];
129+
}
122130
// TODO: save blog settings
123131
}
124132
return blog;

WordPress/Classes/EditSiteViewController.m

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,9 @@ - (void)viewDidLoad {
6565
}
6666
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"welcome_bg_pattern.png"]];
6767

68-
NSError *error = nil;
6968
self.url = blog.url;
7069
self.username = blog.username;
71-
if ([blog isWPcom]) {
72-
self.password = [SFHFKeychainUtils getPasswordForUsername:blog.username andServiceName:@"WordPress.com" error:&error];
73-
} else {
74-
self.password = [SFHFKeychainUtils getPasswordForUsername:blog.username andServiceName:blog.hostURL error:&error];
75-
}
70+
self.password = [blog fetchPassword];
7671

7772
self.startingUser = self.username;
7873
self.startingPwd = self.password;

WordPress/Classes/PostSettingsViewController.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,9 @@ - (void)loadFeaturedImage:(NSURL *)imageURL {
228228
if (!triedAuthOnce) {
229229
triedAuthOnce = YES;
230230

231-
NSError *error = nil;
232231
Blog *blog = self.apost.blog;
233232
NSString *username = blog.username;
234-
NSString *password = [SFHFKeychainUtils getPasswordForUsername:blog.username andServiceName:blog.hostURL error:&error];
233+
NSString *password = [blog fetchPassword];
235234

236235
NSMutableURLRequest *mRequest = [[NSMutableURLRequest alloc] init];
237236
NSString *requestBody = [NSString stringWithFormat:@"log=%@&pwd=%@&redirect_to=",

WordPress/Classes/PostsViewController.m

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ - (void)editPost:(AbstractPost *)apost;
1616
- (void)showSelectedPost;
1717
- (void)checkLastSyncDate;
1818
- (void)syncFinished;
19+
- (void)handleInvalidCredentialsNotification:(NSNotification *)notification;
1920

2021
@end
2122

@@ -230,7 +231,11 @@ - (void)deletePostAtIndexPath:(NSIndexPath *)indexPath{
230231
Post *post = [self.resultsController objectAtIndexPath:indexPath];
231232
[post deletePostWithSuccess:nil failure:^(NSError *error) {
232233
NSDictionary *errInfo = [NSDictionary dictionaryWithObjectsAndKeys:self.blog, @"currentBlog", nil];
233-
[[NSNotificationCenter defaultCenter] postNotificationName:kXML_RPC_ERROR_OCCURS object:error userInfo:errInfo];
234+
if([error code] == 403) {
235+
[self promptForPassword];
236+
} else {
237+
[[NSNotificationCenter defaultCenter] postNotificationName:kXML_RPC_ERROR_OCCURS object:error userInfo:errInfo];
238+
}
234239
[self syncItemsWithUserInteraction:NO];
235240
if(IS_IPAD && self.postReaderViewController) {
236241
if(self.postReaderViewController.apost == post) {

WordPress/Classes/WPAddCategoryViewController.m

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#import "WPAddCategoryViewController.h"
2-
#import "PostSettingsViewController.h"
2+
#import "EditSiteViewController.h"
33
#import "WordPressAppDelegate.h"
4-
#import "Reachability.h"
54
#import "UIBarButtonItem+Styled.h"
65

76
@implementation WPAddCategoryViewController
@@ -139,10 +138,27 @@ - (IBAction)saveAddCategory:(id)sender {
139138
[self removeProgressIndicator];
140139
[self dismiss];
141140
} failure:^(NSError *error) {
142-
NSDictionary *errInfo = [NSDictionary dictionaryWithObjectsAndKeys:self.blog, @"currentBlog", nil];
143-
[[NSNotificationCenter defaultCenter] postNotificationName:kXML_RPC_ERROR_OCCURS object:error userInfo:errInfo];
144141
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
145142
[self removeProgressIndicator];
143+
144+
if ([error code] == 403) {
145+
146+
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Couldn't Connect", @"")
147+
message:NSLocalizedString(@"The username or password stored in the app may be out of date. Please re-enter your password in the settings and try again.", @"")
148+
delegate:nil
149+
cancelButtonTitle:nil
150+
otherButtonTitles:NSLocalizedString(@"OK", @""), nil];
151+
[alertView show];
152+
153+
// bad login/pass combination
154+
EditSiteViewController *editSiteViewController = [[EditSiteViewController alloc] initWithNibName:nil bundle:nil];
155+
editSiteViewController.blog = self.blog;
156+
[self.navigationController pushViewController:editSiteViewController animated:YES];
157+
158+
} else {
159+
NSDictionary *errInfo = [NSDictionary dictionaryWithObjectsAndKeys:self.blog, @"currentBlog", nil];
160+
[[NSNotificationCenter defaultCenter] postNotificationName:kXML_RPC_ERROR_OCCURS object:error userInfo:errInfo];
161+
}
146162
}];
147163
}
148164

WordPress/Classes/WPTableViewController.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
#import "Blog.h"
1010
#import "SettingsViewControllerDelegate.h"
1111

12-
@interface WPTableViewController : UITableViewController <NSFetchedResultsControllerDelegate, UIAlertViewDelegate,SettingsViewControllerDelegate>
12+
@interface WPTableViewController : UITableViewController <NSFetchedResultsControllerDelegate, UIAlertViewDelegate, SettingsViewControllerDelegate>
1313

1414
@property (nonatomic, strong) Blog *blog;
1515

16+
- (void)promptForPassword;
17+
1618
@end

WordPress/Classes/WPTableViewController.m

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -631,27 +631,7 @@ - (void)syncItemsWithUserInteraction:(BOOL)userInteraction {
631631
[alertView show];
632632

633633
} else if (error.code == 403 && editSiteViewController == nil) {
634-
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Couldn't Connect", @"")
635-
message:NSLocalizedString(@"The username or password stored in the app may be out of date. Please re-enter your password in the settings and try again.", @"")
636-
delegate:nil
637-
cancelButtonTitle:nil
638-
otherButtonTitles:NSLocalizedString(@"OK", @""), nil];
639-
[alertView show];
640-
641-
// bad login/pass combination
642-
editSiteViewController = [[EditSiteViewController alloc] initWithNibName:nil bundle:nil];
643-
editSiteViewController.blog = self.blog;
644-
editSiteViewController.isCancellable = YES;
645-
editSiteViewController.delegate = self;
646-
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:editSiteViewController];
647-
648-
if(IS_IPAD == YES) {
649-
navController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
650-
navController.modalPresentationStyle = UIModalPresentationFormSheet;
651-
}
652-
653-
[self.panelNavigationController presentModalViewController:navController animated:YES];
654-
634+
[self promptForPassword];
655635
} else if (userInteraction) {
656636
[WPError showAlertWithError:error title:NSLocalizedString(@"Couldn't sync", @"")];
657637
}
@@ -662,6 +642,29 @@ - (void)syncItemsWithUserInteraction:(BOOL)userInteraction {
662642
}];
663643
}
664644

645+
- (void)promptForPassword {
646+
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Couldn't Connect", @"")
647+
message:NSLocalizedString(@"The username or password stored in the app may be out of date. Please re-enter your password in the settings and try again.", @"")
648+
delegate:nil
649+
cancelButtonTitle:nil
650+
otherButtonTitles:NSLocalizedString(@"OK", @""), nil];
651+
[alertView show];
652+
653+
// bad login/pass combination
654+
editSiteViewController = [[EditSiteViewController alloc] initWithNibName:nil bundle:nil];
655+
editSiteViewController.blog = self.blog;
656+
editSiteViewController.isCancellable = YES;
657+
editSiteViewController.delegate = self;
658+
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:editSiteViewController];
659+
660+
if(IS_IPAD == YES) {
661+
navController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
662+
navController.modalPresentationStyle = UIModalPresentationFormSheet;
663+
}
664+
665+
[self.panelNavigationController presentModalViewController:navController animated:YES];
666+
}
667+
665668
#pragma mark - Swipe gestures
666669

667670
- (void)enableSwipeGestureRecognizer {

WordPress/Classes/WordPressAppDelegate.m

Lines changed: 1 addition & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ @interface WordPressAppDelegate (Private)
2828
- (void)setAppBadge;
2929
- (void)checkIfStatsShouldRun;
3030
- (void)runStats;
31-
- (void)showPasswordAlert;
3231
- (void)cleanUnusedMediaFileFromTmpDir;
3332
- (void)customizeAppearance;
3433
- (void)toggleExtraDebuggingIfNeeded;
@@ -427,19 +426,6 @@ - (void)showNotificationErrorAlert:(NSNotification *)notification {
427426
if ([[err domain] isEqualToString:@"org.wordpress.iphone"]){
428427
if([err code] == 401)
429428
cleanedErrorMsg = NSLocalizedString(@"Sorry, you cannot access this feature. Please check your User Role on this blog.", @"");
430-
else if([err code] == 403) { //403 = bad username/password
431-
NSDictionary *errInfo = [notification userInfo];
432-
//check if the user has NOT changed the blog during the loading
433-
if( (errInfo != nil) && ([errInfo objectForKey:@"currentBlog"] != nil )
434-
&& currentBlog == [errInfo objectForKey:@"currentBlog"] ) {
435-
passwordAlertRunning = YES;
436-
[self performSelectorOnMainThread:@selector(showPasswordAlert) withObject:nil waitUntilDone:NO];
437-
} else {
438-
//do not show the alert
439-
[self setAlertRunning:NO];
440-
}
441-
return;
442-
}
443429
}
444430

445431
// ignore HTTP auth canceled errors
@@ -457,54 +443,6 @@ - (void)showNotificationErrorAlert:(NSNotification *)notification {
457443
[self showAlertWithTitle:NSLocalizedString(@"Error", @"Generic popup title for any type of error.") message:cleanedErrorMsg];
458444
}
459445

460-
461-
- (void)showPasswordAlert {
462-
463-
UILabel *labelPasswd;
464-
465-
NSString *lineBreaks;
466-
467-
if (IS_IPAD)
468-
lineBreaks = @"\n\n\n\n";
469-
else
470-
lineBreaks = @"\n\n\n";
471-
472-
UIAlertView *customSizeAlert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Incorrect Password", @"If the password was lost, a popup asks the author to update their password, this is the popup's title.")
473-
message:lineBreaks // IMPORTANT
474-
delegate:self
475-
cancelButtonTitle:NSLocalizedString(@"Cancel", @"Cancel button label.")
476-
otherButtonTitles:NSLocalizedString(@"Save", @"Save button label (saving content, ex: Post, Page, Comment)."), nil];
477-
478-
customSizeAlert.tag = 101;
479-
480-
labelPasswd = [[UILabel alloc] initWithFrame:CGRectMake(12.0, 48.0, 260.0, 29.0)];
481-
labelPasswd.backgroundColor = [UIColor clearColor];
482-
labelPasswd.textColor = [UIColor whiteColor];
483-
labelPasswd.text = NSLocalizedString(@"Please update your password:", @"If the password was lost, a popup asks the author to update their password, this is the popup's description.");
484-
[customSizeAlert addSubview:labelPasswd];
485-
486-
passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 82.0, 260.0, 29.0)];
487-
[passwordTextField setBackgroundColor:[UIColor whiteColor]];
488-
[passwordTextField setContentVerticalAlignment: UIControlContentVerticalAlignmentCenter];
489-
passwordTextField.keyboardType = UIKeyboardTypeDefault;
490-
passwordTextField.secureTextEntry = YES;
491-
492-
[passwordTextField setTag:123];
493-
494-
[customSizeAlert addSubview:passwordTextField];
495-
496-
//fix the dialog position for older devices on iOS 3
497-
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
498-
if (version <= 3.1)
499-
{
500-
customSizeAlert.transform = CGAffineTransformTranslate(customSizeAlert.transform, 0.0, 100.0);
501-
}
502-
503-
[customSizeAlert show];
504-
505-
[passwordTextField becomeFirstResponder]; //this line should always be called on MainThread
506-
}
507-
508446
- (void)showContentDetailViewController:(UIViewController *)viewController {
509447
if (viewController) {
510448
[panelNavigationController pushViewController:viewController animated:YES];
@@ -1233,36 +1171,7 @@ - (void) handleAuthenticationCancelForChallenge: (NSURLAuthenticationChallenge *
12331171
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
12341172
[self setAlertRunning:NO];
12351173

1236-
if (alertView.tag == 101) { //Password Alert
1237-
passwordAlertRunning = NO;
1238-
if(currentBlog != nil) {
1239-
NSError *error = nil;
1240-
1241-
if ([passwordTextField.text isEqualToString:@""])
1242-
return;
1243-
1244-
//check if the current blog is a WP.COM blog
1245-
if(currentBlog.isWPcom) {
1246-
[SFHFKeychainUtils storeUsername:currentBlog.username
1247-
andPassword:passwordTextField.text
1248-
forServiceName:@"WordPress.com"
1249-
updateExisting:YES
1250-
error:&error];
1251-
} else {
1252-
[SFHFKeychainUtils storeUsername:currentBlog.username
1253-
andPassword:passwordTextField.text
1254-
forServiceName:currentBlog.hostURL
1255-
updateExisting:YES
1256-
error:&error];
1257-
}
1258-
1259-
if (error) {
1260-
[FileLogger log:@"%@ %@ Error saving password for %@: %@", self, NSStringFromSelector(_cmd), currentBlog.url, error];
1261-
} else {
1262-
[FileLogger log:@"%@ %@ %@", self, NSStringFromSelector(_cmd), currentBlog.url];
1263-
}
1264-
}
1265-
} else if (alertView.tag == 102) { // Update alert
1174+
if (alertView.tag == 102) { // Update alert
12661175
if (buttonIndex == 1) {
12671176
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://itunes.apple.com/us/app/wordpress/id335703880?mt=8&ls=1"]];
12681177
}

0 commit comments

Comments
 (0)