Skip to content

Commit

Permalink
Merge pull request #75 from AgileBits/improvement/improvements-for-ch…
Browse files Browse the repository at this point in the history
…ange-password

Improvements for the change password method.
  • Loading branch information
Rad Azzouz committed Aug 20, 2014
2 parents a3b4682 + a2066bd commit 911c569
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,36 @@ - (UIStatusBarStyle)preferredStatusBarStyle{
}

- (IBAction)changePasswordIn1Password:(id)sender {
NSString *changedPassword = self.freshPasswordTextField.text ? : @"";

// Validate that the new and confirmation passwords match.
if (changedPassword.length > 0 && ![changedPassword isEqualToString:self.confirmPasswordTextField.text]) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Confirmation password doesn't match the new password" message:@"The new and the confirmation passwords must match" preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
return;
}

// To change the password for a login in 1Password, you need to provide the username so that the extension will find the right item to update.
// NOTE: If you support username changes, please validate make the necessary validations first, then add `AppExtensionUsernameKey: @"New username"` in the loginDetails dictionary.
NSString *currentUsername = [LoginInformation sharedLoginInformation].username ? : @"";

NSDictionary *loginDetails = @{
AppExtensionTitleKey: @"ACME",
AppExtensionOldUsernameKey: currentUsername, // 1Password will prompt the user to create a new item if no matching logins are found with this username.
AppExtensionPasswordKey: changedPassword,
AppExtensionOldPasswordKey: self.oldPasswordTextField.text ? : @"",
AppExtensionNotesKey: @"Saved with the ACME app",
};

// Password generation options are optional, but are very handy in case you have strict rules about password lengths
NSDictionary *passwordGenerationOptions = @{
AppExtensionGeneratedPasswordMinLengthKey: @(6),
AppExtensionGeneratedPasswordMaxLengthKey: @(50)
};

__weak typeof (self) miniMe = self;
NSString *username = [LoginInformation sharedLoginInformation].username ? : @"";
[[OnePasswordExtension sharedExtension] changePasswordForLoginWithUsername:username andURLString:@"https://www.acme.com" passwordGenerationOptions:passwordGenerationOptions forViewController:self sender:sender completion:^(NSDictionary *loginDict, NSError *error) {

[[OnePasswordExtension sharedExtension] changePasswordForLoginForURLString:@"https://www.acme.com" loginDetails:loginDetails passwordGenerationOptions:passwordGenerationOptions forViewController:self sender:sender completion:^(NSDictionary *loginDict, NSError *error) {
if (!loginDict) {
if (error.code != AppExtensionErrorCodeCancelledByUser) {
NSLog(@"Error invoking 1Password App Extension for find login: %@", error);
Expand Down
3 changes: 2 additions & 1 deletion OnePasswordExtension.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ FOUNDATION_EXPORT NSString *const AppExtensionNotesKey;
FOUNDATION_EXPORT NSString *const AppExtensionSectionTitleKey;
FOUNDATION_EXPORT NSString *const AppExtensionFieldsKey;
FOUNDATION_EXPORT NSString *const AppExtensionReturnedFieldsKey;
FOUNDATION_EXPORT NSString *const AppExtensionOldUsernameKey;
FOUNDATION_EXPORT NSString *const AppExtensionOldPasswordKey;
FOUNDATION_EXPORT NSString *const AppExtensionPasswordGereratorOptionsKey;

Expand Down Expand Up @@ -84,7 +85,7 @@ FOUNDATION_EXPORT NSInteger const AppExtensionErrorCodeUnexpectedData;
newly generated password lets the user know their action was successful. The completion block is guaranteed to be called on the main
thread.
*/
- (void)changePasswordForLoginWithUsername:(NSString *)username andURLString:(NSString *)URLString passwordGenerationOptions:(NSDictionary *)passwordGenerationOptions forViewController:(UIViewController *)viewController sender:(id)sender completion:(void (^)(NSDictionary *loginDict, NSError *error))completion;
- (void)changePasswordForLoginForURLString:(NSString *)URLString loginDetails:(NSDictionary *)loginDetailsDict passwordGenerationOptions:(NSDictionary *)passwordGenerationOptions forViewController:(UIViewController *)viewController sender:(id)sender completion:(void (^)(NSDictionary *loginDict, NSError *error))completion;

/*!
Called from your web view controller, this method will show all the saved logins for the active page in the provided web
Expand Down
7 changes: 3 additions & 4 deletions OnePasswordExtension.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
NSString *const AppExtensionSectionTitleKey = @"section_title";
NSString *const AppExtensionFieldsKey = @"fields";
NSString *const AppExtensionReturnedFieldsKey = @"returned_fields";
NSString *const AppExtensionOldUsernameKey = @"old_username";
NSString *const AppExtensionOldPasswordKey = @"old_password";
NSString *const AppExtensionPasswordGereratorOptionsKey = @"password_generator_options";

Expand Down Expand Up @@ -188,9 +189,8 @@ - (void)storeLoginForURLString:(NSString *)URLString loginDetails:(NSDictionary
#endif
}

- (void)changePasswordForLoginWithUsername:(NSString *)username andURLString:(NSString *)URLString passwordGenerationOptions:(NSDictionary *)passwordGenerationOptions forViewController:(UIViewController *)viewController sender:(id)sender completion:(void (^)(NSDictionary *loginDict, NSError *error))completion
- (void)changePasswordForLoginForURLString:(NSString *)URLString loginDetails:(NSDictionary *)loginDetailsDict passwordGenerationOptions:(NSDictionary *)passwordGenerationOptions forViewController:(UIViewController *)viewController sender:(id)sender completion:(void (^)(NSDictionary *loginDict, NSError *error))completion
{
NSAssert(username != nil, @"username must not be nil");
NSAssert(URLString != nil, @"URLString must not be nil");
NSAssert(viewController != nil, @"viewController must not be nil");

Expand All @@ -206,9 +206,8 @@ - (void)changePasswordForLoginWithUsername:(NSString *)username andURLString:(NS
#ifdef __IPHONE_8_0
NSMutableDictionary *item = [NSMutableDictionary new];
item[AppExtensionVersionNumberKey] = VERSION_NUMBER;
item[AppExtensionUsernameKey] = username;
item[AppExtensionURLStringKey] = URLString;

[item addEntriesFromDictionary:loginDetailsDict];
if (passwordGenerationOptions.count > 0) {
item[AppExtensionPasswordGereratorOptionsKey] = passwordGenerationOptions;
}
Expand Down
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,21 +177,35 @@ An important thing to notice is the `AppExtensionURLStringKey` is set to the exa

### Use Case #3: Change Password

Allow your users to easily change passwords for saved logins in 1Password directly from your change password page. The old and the newly generated are returned to you so you can update your UI and complete the password change process.
Allow your users to easily change passwords for saved logins in 1Password directly from your change password page. The updated login along with the old and the newly generated are returned to you so you can update your UI and complete the password change process. If no matching login is found in 1Password, the user will be prompted to save a new login instead.

Adding 1Password to your change password screen is very similar to adding 1Password to your login and registration screens. In this case you'll wire the 1Password button to an action like this:

```objective-c
- (IBAction)changePasswordIn1Password:(id)sender {
NSString *changedPassword = self.freshPasswordTextField.text ? : @"";

// To change the password for a login in 1Password, you need to provide the username so that the extension will find the right item to update.
// NOTE: If you support username changes, please validate make the necessary validations first, then add `AppExtensionUsernameKey: @"New username"` in the loginDetails dictionary.
NSString *currentUsername = [LoginInformation sharedLoginInformation].username ? : @"";

NSDictionary *loginDetails = @{
AppExtensionTitleKey: @"ACME",
AppExtensionOldUsernameKey: currentUsername, // 1Password will prompt the user to create a new item if no matching logins are found with this username.
AppExtensionPasswordKey: changedPassword,
AppExtensionOldPasswordKey: self.oldPasswordTextField.text ? : @"",
AppExtensionNotesKey: @"Saved with the ACME app",
};

// Password generation options are optional, but are very handy in case you have strict rules about password lengths
NSDictionary *passwordGenerationOptions = @{
AppExtensionGeneratedPasswordMinLengthKey: @(6),
AppExtensionGeneratedPasswordMaxLengthKey: @(50)
};

__weak typeof (self) miniMe = self;
NSString *username = [LoginInformation sharedLoginInformation].username ? : @"";
[[OnePasswordExtension sharedExtension] changePasswordForLoginWithUsername:username andURLString:@"https://www.acme.com" passwordGenerationOptions:passwordGenerationOptions forViewController:self sender:sender completion:^(NSDictionary *loginDict, NSError *error) {

[[OnePasswordExtension sharedExtension] changePasswordForLoginForURLString:@"https://www.acme.com" loginDetails:loginDetails passwordGenerationOptions:passwordGenerationOptions forViewController:self sender:sender completion:^(NSDictionary *loginDict, NSError *error) {
if (!loginDict) {
if (error.code != AppExtensionErrorCodeCancelledByUser) {
NSLog(@"Error invoking 1Password App Extension for find login: %@", error);
Expand Down

0 comments on commit 911c569

Please sign in to comment.