Skip to content
This repository was archived by the owner on Feb 18, 2025. It is now read-only.

Commit 911c569

Browse files
author
Rad Azzouz
committed
Merge pull request #75 from AgileBits/improvement/improvements-for-change-password
Improvements for the change password method.
2 parents a3b4682 + a2066bd commit 911c569

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-10
lines changed

Demos/App Demo for iOS/App Demo for iOS/ChangePasswordViewController.m

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,36 @@ - (UIStatusBarStyle)preferredStatusBarStyle{
3333
}
3434

3535
- (IBAction)changePasswordIn1Password:(id)sender {
36+
NSString *changedPassword = self.freshPasswordTextField.text ? : @"";
37+
38+
// Validate that the new and confirmation passwords match.
39+
if (changedPassword.length > 0 && ![changedPassword isEqualToString:self.confirmPasswordTextField.text]) {
40+
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Confirmation password doesn't match the new password" message:@"The new and the confirmation passwords must match" preferredStyle:UIAlertControllerStyleAlert];
41+
[self presentViewController:alert animated:YES completion:nil];
42+
return;
43+
}
44+
45+
// 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.
46+
// NOTE: If you support username changes, please validate make the necessary validations first, then add `AppExtensionUsernameKey: @"New username"` in the loginDetails dictionary.
47+
NSString *currentUsername = [LoginInformation sharedLoginInformation].username ? : @"";
48+
49+
NSDictionary *loginDetails = @{
50+
AppExtensionTitleKey: @"ACME",
51+
AppExtensionOldUsernameKey: currentUsername, // 1Password will prompt the user to create a new item if no matching logins are found with this username.
52+
AppExtensionPasswordKey: changedPassword,
53+
AppExtensionOldPasswordKey: self.oldPasswordTextField.text ? : @"",
54+
AppExtensionNotesKey: @"Saved with the ACME app",
55+
};
56+
3657
// Password generation options are optional, but are very handy in case you have strict rules about password lengths
3758
NSDictionary *passwordGenerationOptions = @{
3859
AppExtensionGeneratedPasswordMinLengthKey: @(6),
3960
AppExtensionGeneratedPasswordMaxLengthKey: @(50)
4061
};
4162

4263
__weak typeof (self) miniMe = self;
43-
NSString *username = [LoginInformation sharedLoginInformation].username ? : @"";
44-
[[OnePasswordExtension sharedExtension] changePasswordForLoginWithUsername:username andURLString:@"https://www.acme.com" passwordGenerationOptions:passwordGenerationOptions forViewController:self sender:sender completion:^(NSDictionary *loginDict, NSError *error) {
64+
65+
[[OnePasswordExtension sharedExtension] changePasswordForLoginForURLString:@"https://www.acme.com" loginDetails:loginDetails passwordGenerationOptions:passwordGenerationOptions forViewController:self sender:sender completion:^(NSDictionary *loginDict, NSError *error) {
4566
if (!loginDict) {
4667
if (error.code != AppExtensionErrorCodeCancelledByUser) {
4768
NSLog(@"Error invoking 1Password App Extension for find login: %@", error);

OnePasswordExtension.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ FOUNDATION_EXPORT NSString *const AppExtensionNotesKey;
2121
FOUNDATION_EXPORT NSString *const AppExtensionSectionTitleKey;
2222
FOUNDATION_EXPORT NSString *const AppExtensionFieldsKey;
2323
FOUNDATION_EXPORT NSString *const AppExtensionReturnedFieldsKey;
24+
FOUNDATION_EXPORT NSString *const AppExtensionOldUsernameKey;
2425
FOUNDATION_EXPORT NSString *const AppExtensionOldPasswordKey;
2526
FOUNDATION_EXPORT NSString *const AppExtensionPasswordGereratorOptionsKey;
2627

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

8990
/*!
9091
Called from your web view controller, this method will show all the saved logins for the active page in the provided web

OnePasswordExtension.m

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
NSString *const AppExtensionSectionTitleKey = @"section_title";
2828
NSString *const AppExtensionFieldsKey = @"fields";
2929
NSString *const AppExtensionReturnedFieldsKey = @"returned_fields";
30+
NSString *const AppExtensionOldUsernameKey = @"old_username";
3031
NSString *const AppExtensionOldPasswordKey = @"old_password";
3132
NSString *const AppExtensionPasswordGereratorOptionsKey = @"password_generator_options";
3233

@@ -188,9 +189,8 @@ - (void)storeLoginForURLString:(NSString *)URLString loginDetails:(NSDictionary
188189
#endif
189190
}
190191

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

@@ -206,9 +206,8 @@ - (void)changePasswordForLoginWithUsername:(NSString *)username andURLString:(NS
206206
#ifdef __IPHONE_8_0
207207
NSMutableDictionary *item = [NSMutableDictionary new];
208208
item[AppExtensionVersionNumberKey] = VERSION_NUMBER;
209-
item[AppExtensionUsernameKey] = username;
210209
item[AppExtensionURLStringKey] = URLString;
211-
210+
[item addEntriesFromDictionary:loginDetailsDict];
212211
if (passwordGenerationOptions.count > 0) {
213212
item[AppExtensionPasswordGereratorOptionsKey] = passwordGenerationOptions;
214213
}

README.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,21 +177,35 @@ An important thing to notice is the `AppExtensionURLStringKey` is set to the exa
177177

178178
### Use Case #3: Change Password
179179

180-
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.
180+
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.
181181

182182
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:
183183

184184
```objective-c
185185
- (IBAction)changePasswordIn1Password:(id)sender {
186+
NSString *changedPassword = self.freshPasswordTextField.text ? : @"";
187+
188+
// 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.
189+
// NOTE: If you support username changes, please validate make the necessary validations first, then add `AppExtensionUsernameKey: @"New username"` in the loginDetails dictionary.
190+
NSString *currentUsername = [LoginInformation sharedLoginInformation].username ? : @"";
191+
192+
NSDictionary *loginDetails = @{
193+
AppExtensionTitleKey: @"ACME",
194+
AppExtensionOldUsernameKey: currentUsername, // 1Password will prompt the user to create a new item if no matching logins are found with this username.
195+
AppExtensionPasswordKey: changedPassword,
196+
AppExtensionOldPasswordKey: self.oldPasswordTextField.text ? : @"",
197+
AppExtensionNotesKey: @"Saved with the ACME app",
198+
};
199+
186200
// Password generation options are optional, but are very handy in case you have strict rules about password lengths
187201
NSDictionary *passwordGenerationOptions = @{
188202
AppExtensionGeneratedPasswordMinLengthKey: @(6),
189203
AppExtensionGeneratedPasswordMaxLengthKey: @(50)
190204
};
191205

192206
__weak typeof (self) miniMe = self;
193-
NSString *username = [LoginInformation sharedLoginInformation].username ? : @"";
194-
[[OnePasswordExtension sharedExtension] changePasswordForLoginWithUsername:username andURLString:@"https://www.acme.com" passwordGenerationOptions:passwordGenerationOptions forViewController:self sender:sender completion:^(NSDictionary *loginDict, NSError *error) {
207+
208+
[[OnePasswordExtension sharedExtension] changePasswordForLoginForURLString:@"https://www.acme.com" loginDetails:loginDetails passwordGenerationOptions:passwordGenerationOptions forViewController:self sender:sender completion:^(NSDictionary *loginDict, NSError *error) {
195209
if (!loginDict) {
196210
if (error.code != AppExtensionErrorCodeCancelledByUser) {
197211
NSLog(@"Error invoking 1Password App Extension for find login: %@", error);

0 commit comments

Comments
 (0)