1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This file contains utilities related to working with "facets".
7 // A "facet" is defined as the manifestation of a logical application on a given
8 // platform. For example, "My Bank" may have released an Android application
9 // and a Web application accessible from a browser. These are all facets of the
10 // "My Bank" logical application.
12 // Facets that belong to the same logical application are said to be affiliated
13 // with each other. Conceptually, "affiliations" can be seen as an equivalence
14 // relation defined over the set of all facets. Each equivalence class contains
15 // facets that belong to the same logical application, and therefore should be
16 // treated as synonymous for certain purposes, e.g., sharing credentials.
18 // A valid facet identifier will be a URI of the form:
20 // * https://<host>[:<port>]
22 // For web sites. Only HTTPS sites are supported. The syntax corresponds to
23 // that of 'serialized-origin' in RFC 6454. That is, in canonical form, the
24 // URI must not contain components other than the scheme (required, must be
25 // "https"), host (required), and port (optional); with canonicalization
26 // performed the same way as it normally would be for standard URLs.
28 // * android://<certificate_hash>@<package_name>
30 // For Android applications. In canonical form, the URI must not contain
31 // components other than the scheme (must be "android"), username, and host
32 // (all required). The host part must be a valid Android package name, with
33 // no escaping, so it must be composed of characters [a-zA-Z0-9_.].
35 // The username part must be the hash of the certificate used to sign the
36 // APK, base64-encoded using padding and the "URL and filename safe" base64
37 // alphabet, with no further escaping. This is normally calculated as:
39 // echo -n -e "$PEM_KEY" |
40 // openssl x509 -outform DER |
41 // openssl sha -sha512 -binary | base64 | tr '+/' '-_'
44 #ifndef COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_UTILS_H_
45 #define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_UTILS_H_
51 #include "base/containers/hash_tables.h"
52 #include "base/logging.h"
53 #include "base/strings/utf_string_conversions.h"
54 #include "base/time/time.h"
55 #include "net/base/net_util.h"
56 #include "url/third_party/mozilla/url_parse.h"
60 } // namespace autofill
66 namespace password_manager
{
68 // Encapsulates a facet URI in canonical form.
70 // This is a very light-weight wrapper around an std::string containing the text
71 // of the URI, and can be passed around as a value. The main rationale for the
72 // existance of this class is to make it clearer in the code when a certain URI
73 // is known to be a valid facet URI in canonical form, and to allow verifying
74 // and converting URIs to such canonical form.
76 // Note that it would be impractical to use GURL to represent facet URIs, as
77 // GURL has built-in logic to parse the rest of the URI according to its scheme,
78 // and obviously, it does not recognize the "android" scheme. Therefore, after
79 // parsing, everything ends up in the path component, which is not too helpful.
84 // As a light-weight std::string wrapper, allow copy and assign.
85 FacetURI(const FacetURI
&) = default;
86 FacetURI
& operator=(const FacetURI
&) = default;
88 // Constructs an instance to encapsulate the canonical form of |spec|.
89 // If |spec| is not a valid facet URI, then an invalid instance is returned,
90 // which then should be discarded.
91 static FacetURI
FromPotentiallyInvalidSpec(const std::string
& spec
);
93 // Constructs a valid FacetURI instance from a valid |canonical_spec|.
94 // Note: The passed-in URI is not verified at all. Use only when you are sure
95 // the URI is valid and in canonical form.
96 static FacetURI
FromCanonicalSpec(const std::string
& canonical_spec
);
98 // Comparison operators so that FacetURI can be used in std::equal.
99 bool operator==(const FacetURI
& other
) const;
100 bool operator!=(const FacetURI
& other
) const;
102 // Relational operators so that FacetURI can be used in sorted containers.
103 bool operator<(const FacetURI
& other
) const;
104 bool operator>(const FacetURI
& other
) const;
106 // Returns whether or not this instance represents a valid facet identifier
107 // referring to a Web application.
108 bool IsValidWebFacetURI() const;
110 // Returns whether or not this instance represents a valid facet identifier
111 // referring to an Android application.
112 bool IsValidAndroidFacetURI() const;
114 // Returns whether or not this instance represents a valid facet identifier
115 // referring to either a Web or an Android application. The empty identfier is
116 // not considered valid.
117 bool is_valid() const { return is_valid_
; }
119 // Returns whether or not this instance represents the empty facet identifier.
120 bool is_empty() const { return canonical_spec_
.empty(); }
122 // Returns the canonical scheme of the encapsulated facet URI, provided it is
123 // valid, or the empty string otherwise.
124 std::string
scheme() const;
126 // Returns the canonical package name that the encapsulated facet URI
127 // references, provided it is a valid Android facet URI, or the empty string
129 std::string
android_package_name() const;
131 // Returns the text of the encapsulated canonical URI, which must be valid.
132 const std::string
& canonical_spec() const {
134 return canonical_spec_
;
137 // Returns the text of the encapsulated canonical URI, even if it is invalid.
138 const std::string
& potentially_invalid_spec() const {
139 return canonical_spec_
;
143 // Internal constructor to be used by the static factory methods.
144 FacetURI(const std::string
& canonical_spec
, bool is_valid
);
146 // Whether |canonical_spec_| contains a valid facet URI in canonical form.
149 // The text of the encapsulated canonical URI, valid if and only if
150 // |is_valid_| is true.
151 std::string canonical_spec_
;
153 // Identified components of the canonical spec.
157 // A collection of facets affiliated with each other, i.e. an equivalence class.
158 typedef std::vector
<FacetURI
> AffiliatedFacets
;
160 // A collection of facets affiliated with each other, i.e. an equivalence class,
161 // plus a timestamp that indicates the last time the data was updated from an
162 // authoritative source.
163 struct AffiliatedFacetsWithUpdateTime
{
164 AffiliatedFacetsWithUpdateTime();
165 ~AffiliatedFacetsWithUpdateTime();
167 AffiliatedFacets facets
;
168 base::Time last_update_time
;
171 // Returns whether or not equivalence classes |a| and |b| are equal, that is,
172 // whether or not they consist of the same set of facets.
174 // Note that this will do some sorting, so it can be expensive for large inputs.
175 bool AreEquivalenceClassesEqual(const AffiliatedFacets
& a
,
176 const AffiliatedFacets
& b
);
178 // A shorter way to spell FacetURI::IsValidAndroidFacetURI().
179 bool IsValidAndroidFacetURI(const std::string
& uri
);
181 // Returns whether or not affiliation based matching is enabled, either via
182 // command line flags or field trials. The command line flag, if present, always
184 bool IsAffiliationBasedMatchingEnabled(const base::CommandLine
& command_line
);
186 // Returns whether or not propagating password changes to affiliated saved web
187 // credentials is enabled via variation parameters. This allows disabling only
188 // the sub-feature while leaving the rest of the affiliation-based matching
189 // enabled. If the main feature is forced enabled/disabled via the command line,
190 // the sub-feature will be force enabled/disabled correspondingly.
191 bool IsPropagatingPasswordChangesToWebCredentialsEnabled(
192 const base::CommandLine
& command_line
);
194 // Returns whether or not affiliation requests for dummy facets should be
195 // triggered as part of an experiment to exercise AffiliationService code before
196 // users would get a chance to have any real Android-based credentials. If the
197 // main feature is forced enabled/disabled via the command line, the experiment
198 // is force enabled/disabled correspondingly.
199 bool IsAffiliationRequestsForDummyFacetsEnabled(
200 const base::CommandLine
& command_line
);
202 // Returns the origin URI in a format which can be presented to a user based of
203 // |password_from| field values. For web URIs |languages| is using in order to
204 // determine whether a URI is 'comprehensible' to a user who understands
206 std::string
GetHumanReadableOrigin(const autofill::PasswordForm
& password_form
,
207 const std::string
& languages
);
209 // For logging use only.
210 std::ostream
& operator<<(std::ostream
& os
, const FacetURI
& facet_uri
);
212 } // namespace password_manager
214 // Provide a hash function so that hash_sets and maps can contain FacetURIs.
215 namespace BASE_HASH_NAMESPACE
{
218 struct hash
<password_manager::FacetURI
> {
219 size_t operator()(const password_manager::FacetURI
& facet_uri
) const {
220 return hash
<std::string
>()(facet_uri
.potentially_invalid_spec());
224 } // namespace BASE_HASH_NAMESPACE
226 #endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_UTILS_H_