OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 // This file contains utilities related to working with "facets". |
| 6 // |
| 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. |
| 11 // |
| 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. |
| 17 // |
| 18 // A valid facet identifier will be a URI of the form: |
| 19 // |
| 20 // * https://<host>[:<port>] |
| 21 // |
| 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. |
| 27 // |
| 28 // * android://<certificate_hash>@<package_name> |
| 29 // |
| 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_.]. |
| 34 // |
| 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: |
| 38 // |
| 39 // echo -n -e "$PEM_KEY" | |
| 40 // openssl x509 -outform DER | |
| 41 // openssl sha -sha512 -binary | base64 | tr '+/' '-_' |
| 42 // |
| 43 |
| 44 #ifndef COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_UTILS_H_ |
| 45 #define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_UTILS_H_ |
| 46 |
| 47 #include <string> |
| 48 #include <vector> |
| 49 |
| 50 #include "base/logging.h" |
| 51 |
| 52 namespace password_manager { |
| 53 |
| 54 // Encapsulates a facet URI in canonical form. |
| 55 // |
| 56 // This is a very light-weight wrapper around an std::string containing the text |
| 57 // of the URI, and can be passed around as a value. The main rationale for the |
| 58 // existance of this class is to make it clearer in the code when a certain URI |
| 59 // is known to be a valid facet URI in canonical form, and to allow verifying |
| 60 // and converting URIs to such canonical form. |
| 61 // |
| 62 // Note that it would be impractical to use GURL to represent facet URIs, as |
| 63 // GURL has built-in logic to parse the rest of the URI according to its scheme, |
| 64 // and obviously, it does not recognize the "android" scheme. Therefore, after |
| 65 // parsing, everything ends up in the path component, which is not too helpful. |
| 66 class FacetURI { |
| 67 public: |
| 68 FacetURI(); |
| 69 |
| 70 // As a light-weight std::string wrapper, allow copy and assign. |
| 71 FacetURI(const FacetURI&) = default; |
| 72 FacetURI& operator=(const FacetURI&) = default; |
| 73 |
| 74 // Constructs an instance to encapsulate the canonical form of |spec|. |
| 75 // If |spec| is not a valid facet URI, then an invalid instance is returned, |
| 76 // which then should be discarded. |
| 77 static FacetURI FromPotentiallyInvalidSpec(const std::string& spec); |
| 78 |
| 79 // Constructs a valid FacetURI instance from a valid |canonical_spec|. |
| 80 // Note: The passed-in URI is not verified at all. Use only when you are sure |
| 81 // the URI is valid and in canonical form. |
| 82 static FacetURI FromCanonicalSpec(const std::string& canonical_spec); |
| 83 |
| 84 // Comparison operators so that FacetURI can be used in std::equal. |
| 85 bool operator==(const FacetURI& other) const; |
| 86 bool operator!=(const FacetURI& other) const; |
| 87 |
| 88 // Relational operators so that FacetURI can be used in sorted containers. |
| 89 bool operator<(const FacetURI& other) const; |
| 90 bool operator>(const FacetURI& other) const; |
| 91 |
| 92 // Returns whether or not this instance represents a valid facet identifier |
| 93 // referring to a Web application. |
| 94 bool IsValidWebFacetURI() const; |
| 95 |
| 96 // Returns whether or not this instance represents a valid facet identifier |
| 97 // referring to an Android application. |
| 98 bool IsValidAndroidFacetURI() const; |
| 99 |
| 100 // Returns whether or not this instance represents a valid facet identifier |
| 101 // referring to either a Web or an Android application. |
| 102 bool is_valid() const { |
| 103 return is_valid_; |
| 104 } |
| 105 |
| 106 // Returns the text of the encapsulated canonical URI, which must be valid. |
| 107 const std::string& canonical_spec() const { |
| 108 DCHECK(is_valid_); |
| 109 return canonical_spec_; |
| 110 } |
| 111 |
| 112 private: |
| 113 // Internal constructor to be used by the static factory methods. |
| 114 FacetURI(const std::string& canonical_spec, bool is_valid); |
| 115 |
| 116 // Whether |canonical_spec_| contains a valid facet URI in canonical form. |
| 117 bool is_valid_; |
| 118 |
| 119 // The text of the encapsulated canonical URI, valid if and only if |
| 120 // |is_valid_| is true. |
| 121 std::string canonical_spec_; |
| 122 }; |
| 123 |
| 124 // A collection of facets affiliated with each other, i.e. an equivalence class. |
| 125 typedef std::vector<FacetURI> AffiliatedFacets; |
| 126 |
| 127 // Returns whether or not equivalence classes |a| and |b| are equal, that is, |
| 128 // whether or not they consist of the same set of facets. |
| 129 // |
| 130 // Note that this will do some sorting, so it can be expensive for large inputs. |
| 131 bool AreEquivalenceClassesEqual(const AffiliatedFacets& a, |
| 132 const AffiliatedFacets& b); |
| 133 |
| 134 } // namespace password_manager |
| 135 |
| 136 #endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_UTILS_H_ |
OLD | NEW |