iOS web browser with a focus on security and privacy
at master 210 lines 15 kB view raw
1// 2// 1Password Extension 3// 4// Lovingly handcrafted by Dave Teare, Michael Fey, Rad Azzouz, and Roustem Karimov. 5// Copyright (c) 2014 AgileBits. All rights reserved. 6// 7 8#import <Foundation/Foundation.h> 9#import <UIKit/UIKit.h> 10#import <MobileCoreServices/MobileCoreServices.h> 11 12#ifdef __IPHONE_8_0 13#import <WebKit/WebKit.h> 14#endif 15 16#if __has_feature(nullability) 17NS_ASSUME_NONNULL_BEGIN 18#else 19#define nullable 20#define __nullable 21#define nonnull 22#define __nonnull 23#endif 24 25// Login Dictionary keys - Used to get or set the properties of a 1Password Login 26#define AppExtensionURLStringKey @"url_string" 27#define AppExtensionUsernameKey @"username" 28#define AppExtensionPasswordKey @"password" 29#define AppExtensionTOTPKey @"totp" 30#define AppExtensionTitleKey @"login_title" 31#define AppExtensionNotesKey @"notes" 32#define AppExtensionSectionTitleKey @"section_title" 33#define AppExtensionFieldsKey @"fields" 34#define AppExtensionReturnedFieldsKey @"returned_fields" 35#define AppExtensionOldPasswordKey @"old_password" 36#define AppExtensionPasswordGeneratorOptionsKey @"password_generator_options" 37 38// Password Generator options - Used to set the 1Password Password Generator options when saving a new Login or when changing the password for for an existing Login 39#define AppExtensionGeneratedPasswordMinLengthKey @"password_min_length" 40#define AppExtensionGeneratedPasswordMaxLengthKey @"password_max_length" 41#define AppExtensionGeneratedPasswordRequireDigitsKey @"password_require_digits" 42#define AppExtensionGeneratedPasswordRequireSymbolsKey @"password_require_symbols" 43#define AppExtensionGeneratedPasswordForbiddenCharactersKey @"password_forbidden_characters" 44 45// Errors codes 46#define AppExtensionErrorDomain @"OnePasswordExtension" 47 48#define AppExtensionErrorCodeCancelledByUser 0 49#define AppExtensionErrorCodeAPINotAvailable 1 50#define AppExtensionErrorCodeFailedToContactExtension 2 51#define AppExtensionErrorCodeFailedToLoadItemProviderData 3 52#define AppExtensionErrorCodeCollectFieldsScriptFailed 4 53#define AppExtensionErrorCodeFillFieldsScriptFailed 5 54#define AppExtensionErrorCodeUnexpectedData 6 55#define AppExtensionErrorCodeFailedToObtainURLStringFromWebView 7 56 57// Note to creators of libraries or frameworks: 58// If you include this code within your library, then to prevent potential duplicate symbol 59// conflicts for adopters of your library, you should rename the OnePasswordExtension class 60// and associated typedefs. You might to so by adding your own project prefix, e.g., 61// MyLibraryOnePasswordExtension. 62 63typedef void (^OnePasswordLoginDictionaryCompletionBlock)(NSDictionary * __nullable loginDictionary, NSError * __nullable error); 64typedef void (^OnePasswordSuccessCompletionBlock)(BOOL success, NSError * __nullable error); 65typedef void (^OnePasswordExtensionItemCompletionBlock)(NSExtensionItem * __nullable extensionItem, NSError * __nullable error); 66 67@interface OnePasswordExtension : NSObject 68 69+ (OnePasswordExtension *)sharedExtension; 70 71/*! 72 @discussion Determines if the 1Password Extension is available. Allows you to only show the 1Password login button to those 73 that can use it. Of course, you could leave the button enabled and educate users about the virtues of strong, unique 74 passwords instead :) 75 76 @return isAppExtensionAvailable Returns YES if any app that supports the generic `org-appextension-feature-password-management` feature is installed on the device. 77 */ 78#ifdef __IPHONE_8_0 79- (BOOL)isAppExtensionAvailable NS_EXTENSION_UNAVAILABLE_IOS("Not available in an extension. Check if org-appextension-feature-password-management:// URL can be opened by the app."); 80#else 81- (BOOL)isAppExtensionAvailable; 82#endif 83 84/*! 85 Called from your login page, this method will find all available logins for the given URLString. 86 87 @discussion 1Password will show all matching Login for the naked domain of the given URLString. For example if the user has an item in your 1Password vault with "subdomain1.domain.com” as the website and another one with "subdomain2.domain.com”, and the URLString is "https://domain.com", 1Password will show both items. 88 89 However, if no matching login is found for "https://domain.com", the 1Password Extension will display the "Show all Logins" button so that the user can search among all the Logins in the vault. This is especially useful when the user has a login for "https://olddomain.com". 90 91 After the user selects a login, it is stored into an NSDictionary and given to your completion handler. Use the `Login Dictionary keys` above to 92 extract the needed information and update your UI. The completion block is guaranteed to be called on the main thread. 93 94 @param URLString For the matching Logins in the 1Password vault. 95 96 @param viewController The view controller from which the 1Password Extension is invoked. Usually `self` 97 98 @param sender The sender which triggers the share sheet to show. UIButton, UIBarButtonItem or UIView. Can also be nil on iPhone, but not on iPad. 99 100 @param completion A completion block called with two parameters loginDictionary and error once completed. The loginDictionary reply parameter that contains the username, password and the One-Time Password if available. The error Reply parameter that is nil if the 1Password Extension has been successfully completed, or it contains error information about the completion failure. 101 */ 102- (void)findLoginForURLString:(nonnull NSString *)URLString forViewController:(nonnull UIViewController *)viewController sender:(nullable id)sender completion:(nonnull OnePasswordLoginDictionaryCompletionBlock)completion; 103 104/*! 105 Create a new login within 1Password and allow the user to generate a new password before saving. 106 107 @discussion The provided URLString should be unique to your app or service and be identical to what you pass into the find login method. 108 The completion block is guaranteed to be called on the main 109 thread. 110 111 @param URLString For the new Login to be saved in 1Password. 112 113 @param loginDetailsDictionary about the Login to be saved, including custom fields, are stored in an dictionary and given to the 1Password Extension. 114 115 @param passwordGenerationOptions The Password generator options epresented in a dictionary form. 116 117 @param viewController The view controller from which the 1Password Extension is invoked. Usually `self` 118 119 @param sender The sender which triggers the share sheet to show. UIButton, UIBarButtonItem or UIView. Can also be nil on iPhone, but not on iPad. 120 121 @param completion A completion block which is called with type parameters loginDictionary and error. The loginDictionary reply parameter which contain all the information about the newly saved Login. Use the `Login Dictionary keys` above to extract the needed information and update your UI. For example, updating the UI with the newly generated password lets the user know their action was successful. The error reply parameter that is nil if the 1Password Extension has been successfully completed, or it contains error information about the completion failure. 122 */ 123- (void)storeLoginForURLString:(nonnull NSString *)URLString loginDetails:(nullable NSDictionary *)loginDetailsDictionary passwordGenerationOptions:(nullable NSDictionary *)passwordGenerationOptions forViewController:(nonnull UIViewController *)viewController sender:(nullable id)sender completion:(nonnull OnePasswordLoginDictionaryCompletionBlock)completion; 124 125/*! 126 Change the password for an existing login within 1Password. 127 128 @discussion The provided URLString should be unique to your app or service and be identical to what you pass into the find login method. The completion block is guaranteed to be called on the main thread. 129 130 1Password 6 and later: 131 The 1Password Extension will display all available the matching Logins for the given URL string. The user can choose which Login item to update. The "New Login" button will also be available at all times, in case the user wishes to to create a new Login instead, 132 133 1Password 5: 134 These are the three scenarios that are supported: 135 1. A single matching Login is found: 1Password will enter edit mode for that Login and will update its password using the value for AppExtensionPasswordKey. 136 2. More than a one matching Logins are found: 1Password will display a list of all matching Logins. The user must choose which one to update. Once in edit mode, the Login will be updated with the new password. 137 3. No matching login is found: 1Password will create a new Login using the optional fields if available to populate its properties. 138 139 @param URLString for the Login to be updated with a new password in 1Password. 140 141 @param loginDetailsDictionary about the Login to be saved, including old password and the username, are stored in an dictionary and given to the 1Password Extension. 142 143 @param passwordGenerationOptions The Password generator options epresented in a dictionary form. 144 145 @param viewController The view controller from which the 1Password Extension is invoked. Usually `self` 146 147 @param sender The sender which triggers the share sheet to show. UIButton, UIBarButtonItem or UIView. Can also be nil on iPhone, but not on iPad. 148 149 @param completion A completion block which is called with type parameters loginDictionary and error. The loginDictionary reply parameter which contain all the information about the newly updated Login, including the newly generated and the old password. Use the `Login Dictionary keys` above to extract the needed information and update your UI. For example, updating the UI with the newly generated password lets the user know their action was successful. The error reply parameter that is nil if the 1Password Extension has been successfully completed, or it contains error information about the completion failure. 150 */ 151- (void)changePasswordForLoginForURLString:(nonnull NSString *)URLString loginDetails:(nullable NSDictionary *)loginDetailsDictionary passwordGenerationOptions:(nullable NSDictionary *)passwordGenerationOptions forViewController:(UIViewController *)viewController sender:(nullable id)sender completion:(nonnull OnePasswordLoginDictionaryCompletionBlock)completion; 152 153/*! 154 Called from your web view controller, this method will show all the saved logins for the active page in the provided web 155 view, and automatically fill the HTML form fields. Supports both WKWebView and UIWebView. 156 157 @discussion 1Password will show all matching Login for the naked domain of the current website. For example if the user has an item in your 1Password vault with "subdomain1.domain.com” as the website and another one with "subdomain2.domain.com”, and the current website is "https://domain.com", 1Password will show both items. 158 159 However, if no matching login is found for "https://domain.com", the 1Password Extension will display the "New Login" button so that the user can create a new Login for the current website. 160 161 @param webView The web view which displays the form to be filled. The active UIWebView Or WKWebView. Must not be nil. 162 163 @param viewController The view controller from which the 1Password Extension is invoked. Usually `self` 164 165 @param sender The sender which triggers the share sheet to show. UIButton, UIBarButtonItem or UIView. Can also be nil on iPhone, but not on iPad. 166 167 @param yesOrNo Boolean flag. If YES is passed only matching Login items will be shown, otherwise the 1Password Extension will also display Credit Cards and Identities. 168 169 @param completion Completion block called on completion with parameters success, and error. The success reply parameter that is YES if the 1Password Extension has been successfully completed or NO otherwise. The error reply parameter that is nil if the 1Password Extension has been successfully completed, or it contains error information about the completion failure. 170 */ 171- (void)fillItemIntoWebView:(nonnull id)webView forViewController:(nonnull UIViewController *)viewController sender:(nullable id)sender showOnlyLogins:(BOOL)yesOrNo completion:(nonnull OnePasswordSuccessCompletionBlock)completion; 172 173/*! 174 Called in the UIActivityViewController completion block to find out whether or not the user selected the 1Password Extension activity. 175 176 @param activityType or the bundle identidier of the selected activity in the share sheet. 177 178 @return isOnePasswordExtensionActivityType Returns YES if the selected activity is the 1Password extension, NO otherwise. 179 */ 180- (BOOL)isOnePasswordExtensionActivityType:(nullable NSString *)activityType; 181 182/*! 183 The returned NSExtensionItem can be used to create your own UIActivityViewController. Use `isOnePasswordExtensionActivityType:` and `fillReturnedItems:intoWebView:completion:` in the activity view controller completion block to process the result. The completion block is guaranteed to be called on the main thread. 184 185 @param webView The web view which displays the form to be filled. The active UIWebView Or WKWebView. Must not be nil. 186 187 @param completion Completion block called on completion with extensionItem and error. The extensionItem reply parameter that is contains all the info required by the 1Password extension if has been successfully completed or nil otherwise. The error reply parameter that is nil if the 1Password extension item has been successfully created, or it contains error information about the completion failure. 188 */ 189- (void)createExtensionItemForWebView:(nonnull id)webView completion:(nonnull OnePasswordExtensionItemCompletionBlock)completion; 190 191/*! 192 Method used in the UIActivityViewController completion block to fill information into a web view. 193 194 @param returnedItems Array which contains the selected activity in the share sheet. Empty array if the share sheet is cancelled by the user. 195 @param webView The web view which displays the form to be filled. The active UIWebView Or WKWebView. Must not be nil. 196 197 @param completion Completion block called on completion with parameters success, and error. The success reply parameter that is YES if the 1Password Extension has been successfully completed or NO otherwise. The error reply parameter that is nil if the 1Password Extension has been successfully completed, or it contains error information about the completion failure. 198 */ 199- (void)fillReturnedItems:(nullable NSArray *)returnedItems intoWebView:(nonnull id)webView completion:(nonnull OnePasswordSuccessCompletionBlock)completion; 200 201/*! 202 Deprecated in version 1.5 203 @see Use fillItemIntoWebView:forViewController:sender:showOnlyLogins:completion: instead 204 */ 205- (void)fillLoginIntoWebView:(nonnull id)webView forViewController:(nonnull UIViewController *)viewController sender:(nullable id)sender completion:(nonnull OnePasswordSuccessCompletionBlock)completion __attribute__((deprecated("Use fillItemIntoWebView:forViewController:sender:showOnlyLogins:completion: instead. Deprecated in version 1.5"))); 206@end 207 208#if __has_feature(nullability) 209NS_ASSUME_NONNULL_END 210#endif