Commit 00e72ea4 authored by John Z Wu's avatar John Z Wu Committed by Commit Bot

Add CWVPassword for representing a saved password used in autofill

Change-Id: I6b45699c6813d62987f96dbeafbca112410bcc15
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1817317
Commit-Queue: John Wu <jzw@chromium.org>
Reviewed-by: default avatarHiroshi Ichikawa <ichikawa@chromium.org>
Cr-Commit-Position: refs/heads/master@{#699415}
parent 90121a18
......@@ -89,6 +89,7 @@ if (ios_web_view_enable_autofill) {
"public/cwv_credit_card.h",
"public/cwv_credit_card_saver.h",
"public/cwv_credit_card_verifier.h",
"public/cwv_password.h",
"public/cwv_preferences_autofill.h",
"public/cwv_web_view_autofill.h",
"public/cwv_web_view_configuration_autofill.h",
......@@ -250,6 +251,8 @@ if (ios_web_view_enable_autofill) {
"internal/autofill/cwv_credit_card_saver_internal.h",
"internal/autofill/cwv_credit_card_verifier.mm",
"internal/autofill/cwv_credit_card_verifier_internal.h",
"internal/passwords/cwv_password.mm",
"internal/passwords/cwv_password_internal.h",
"internal/passwords/cwv_password_controller.h",
"internal/passwords/cwv_password_controller.mm",
]
......@@ -418,6 +421,7 @@ test("ios_web_view_unittests") {
"internal/cwv_scroll_view_unittest.mm",
"internal/cwv_ssl_status_unittest.mm",
"internal/cwv_web_view_configuration_unittest.mm",
"internal/passwords/cwv_password_unittest.mm",
"internal/signin/cwv_identity_unittest.mm",
"internal/sync/cwv_sync_controller_unittest.mm",
"internal/translate/cwv_translation_controller_unittest.mm",
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/web_view/internal/passwords/cwv_password_internal.h"
#import <objc/runtime.h>
#include "base/strings/sys_string_conversions.h"
#include "components/autofill/core/common/password_form.h"
#include "components/password_manager/core/browser/password_ui_utils.h"
#import "ios/web_view/internal/utils/nsobject_description_utils.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
@implementation CWVPassword {
autofill::PasswordForm _passwordForm;
}
- (instancetype)initWithPasswordForm:
(const autofill::PasswordForm&)passwordForm {
self = [super init];
if (self) {
_passwordForm = passwordForm;
auto name_and_link =
password_manager::GetShownOriginAndLinkUrl(_passwordForm);
_title = base::SysUTF8ToNSString(name_and_link.first);
_site = base::SysUTF8ToNSString(name_and_link.second.spec());
}
return self;
}
#pragma mark - Public
- (NSString*)username {
if (self.blacklisted) {
return nil;
}
return base::SysUTF16ToNSString(_passwordForm.username_value);
}
- (NSString*)password {
if (self.blacklisted) {
return nil;
}
return base::SysUTF16ToNSString(_passwordForm.password_value);
}
- (BOOL)isBlacklisted {
return _passwordForm.blacklisted_by_user;
}
#pragma mark - NSObject
- (NSString*)debugDescription {
NSString* debugDescription = [super debugDescription];
return [debugDescription
stringByAppendingFormat:@"\n%@", CWVPropertiesDescription(self)];
}
#pragma mark - Internal
- (autofill::PasswordForm*)internalPasswordForm {
return &_passwordForm;
}
@end
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_WEB_VIEW_INTERNAL_PASSWORDS_CWV_PASSWORD_INTERNAL_H_
#define IOS_WEB_VIEW_INTERNAL_PASSWORDS_CWV_PASSWORD_INTERNAL_H_
#include "components/autofill/core/common/password_form.h"
#import "ios/web_view/public/cwv_password.h"
@interface CWVPassword ()
- (instancetype)initWithPasswordForm:(const autofill::PasswordForm&)passwordForm
NS_DESIGNATED_INITIALIZER;
// The internal autofill credit card that is wrapped by this object.
// Intentionally not declared as a property to avoid issues when read by
// -[NSObject valueForKey:].
- (autofill::PasswordForm*)internalPasswordForm;
@end
#endif // IOS_WEB_VIEW_INTERNAL_PASSWORDS_CWV_PASSWORD_INTERNAL_H_
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/web_view/internal/passwords/cwv_password_internal.h"
#include "base/strings/sys_string_conversions.h"
#include "components/autofill/core/common/password_form.h"
#include "components/password_manager/core/browser/password_ui_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
#import "testing/gtest_mac.h"
#include "testing/platform_test.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace ios_web_view {
using CWVPasswordTest = PlatformTest;
// Tests CWVPassword initialization for a blacklisted site.
TEST_F(CWVPasswordTest, Blacklisted) {
autofill::PasswordForm password_form;
password_form.origin = GURL("http://www.example.com/accounts/LoginAuth");
password_form.action = GURL("http://www.example.com/accounts/Login");
password_form.username_element = base::SysNSStringToUTF16(@"Email");
password_form.username_value = base::SysNSStringToUTF16(@"test@egmail.com");
password_form.password_element = base::SysNSStringToUTF16(@"Passwd");
password_form.password_value = base::SysNSStringToUTF16(@"test");
password_form.submit_element = base::SysNSStringToUTF16(@"signIn");
password_form.signon_realm = "http://www.example.com/";
password_form.preferred = false;
password_form.scheme = autofill::PasswordForm::Scheme::kHtml;
password_form.blacklisted_by_user = true;
auto name_and_link =
password_manager::GetShownOriginAndLinkUrl(password_form);
CWVPassword* password =
[[CWVPassword alloc] initWithPasswordForm:password_form];
EXPECT_EQ(password_form, *[password internalPasswordForm]);
EXPECT_NSEQ(base::SysUTF8ToNSString(name_and_link.first), password.title);
EXPECT_NSEQ(base::SysUTF8ToNSString(name_and_link.second.spec()),
password.site);
EXPECT_TRUE(password.blacklisted);
EXPECT_FALSE(password.username);
EXPECT_FALSE(password.password);
}
// Tests CWVPassword initialization for a non-blacklisted site.
TEST_F(CWVPasswordTest, NonBlacklisted) {
autofill::PasswordForm password_form;
password_form.origin = GURL("http://www.example.com/accounts/LoginAuth");
password_form.action = GURL("http://www.example.com/accounts/Login");
password_form.username_element = base::SysNSStringToUTF16(@"Email");
password_form.username_value = base::SysNSStringToUTF16(@"test@egmail.com");
password_form.password_element = base::SysNSStringToUTF16(@"Passwd");
password_form.password_value = base::SysNSStringToUTF16(@"test");
password_form.submit_element = base::SysNSStringToUTF16(@"signIn");
password_form.signon_realm = "http://www.example.com/";
password_form.preferred = false;
password_form.scheme = autofill::PasswordForm::Scheme::kHtml;
password_form.blacklisted_by_user = false;
auto name_and_link =
password_manager::GetShownOriginAndLinkUrl(password_form);
CWVPassword* password =
[[CWVPassword alloc] initWithPasswordForm:password_form];
EXPECT_EQ(password_form, *[password internalPasswordForm]);
EXPECT_NSEQ(base::SysUTF8ToNSString(name_and_link.first), password.title);
EXPECT_NSEQ(base::SysUTF8ToNSString(name_and_link.second.spec()),
password.site);
EXPECT_FALSE(password.blacklisted);
EXPECT_NSEQ(@"test@egmail.com", password.username);
EXPECT_NSEQ(@"test", password.password);
}
} // namespace ios_web_view
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_WEB_VIEW_PUBLIC_CWV_PASSWORD_H_
#define IOS_WEB_VIEW_PUBLIC_CWV_PASSWORD_H_
#import <Foundation/Foundation.h>
#import "cwv_export.h"
NS_ASSUME_NONNULL_BEGIN
// Represents a password for autofilling login forms.
CWV_EXPORT
@interface CWVPassword : NSObject
// Display friendly title for this object.
@property(nonatomic, copy, readonly) NSString* title;
// The url for which this password can be used in its login form.
@property(nonatomic, copy, readonly) NSString* site;
// Whether or not |site| has been blacklisted by the user. This means password
// autofill will never occur.
@property(nonatomic, readonly, getter=isBlacklisted) BOOL blacklisted;
// The login username. This is nil if |blacklisted| or if only the |password|
// was saved.
@property(nonatomic, nullable, copy, readonly) NSString* username;
// The login password. This is nil iff |blacklisted|.
// Note that you should only display this after the user authenticates via iOS'
// native authentication mechanism. e.g. Passcode and/or Touch/Face ID.
@property(nonatomic, nullable, copy, readonly) NSString* password;
- (instancetype)init NS_UNAVAILABLE;
@end
NS_ASSUME_NONNULL_END
#endif // IOS_WEB_VIEW_PUBLIC_CWV_PASSWORD_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment