chromium/components/affiliations/core/browser/affiliation_utils.h

// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// This file contains utilities related to working with "facets".
//
// A "facet" is defined as the manifestation of a logical application on a given
// platform. For example, "My Bank" may have released an Android application
// and a Web application accessible from a browser. These are all facets of the
// "My Bank" logical application.
//
// Facets that belong to the same logical application are said to be affiliated
// with each other. Conceptually, "affiliations" can be seen as an equivalence
// relation defined over the set of all facets. Each equivalence class contains
// facets that belong to the same logical application, and therefore should be
// treated as synonymous for certain purposes, e.g., sharing credentials.
//
// A valid facet identifier will be a URI of the form:
//
//   * https://<host>[:<port>]
//
//      For web sites. Only HTTPS sites are supported. The syntax corresponds to
//      that of 'serialized-origin' in RFC 6454. That is, in canonical form, the
//      URI must not contain components other than the scheme (required, must be
//      "https"), host (required), and port (optional); with canonicalization
//      performed the same way as it normally would be for standard URLs.
//
//   * android://<certificate_hash>@<package_name>
//
//      For Android applications. In canonical form, the URI must not contain
//      components other than the scheme (must be "android"), username, and host
//      (all required). The host part must be a valid Android package name, with
//      no escaping, so it must be composed of characters [a-zA-Z0-9_.].
//
//      The username part must be the hash of the certificate used to sign the
//      APK, base64-encoded using padding and the "URL and filename safe" base64
//      alphabet, with no further escaping. This is normally calculated as:
//
//        echo -n -e "$PEM_KEY" |
//          openssl x509 -outform DER |
//          openssl sha -sha512 -binary | base64 | tr '+/' '-_'
//

#ifndef COMPONENTS_AFFILIATIONS_CORE_BROWSER_AFFILIATION_UTILS_H_
#define COMPONENTS_AFFILIATIONS_CORE_BROWSER_AFFILIATION_UTILS_H_

#include <compare>
#include <cstddef>
#include <iosfwd>
#include <string>
#include <vector>

#include "base/check.h"
#include "base/containers/flat_set.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "url/gurl.h"
#include "url/third_party/mozilla/url_parse.h"

namespace affiliations {

// Encapsulates a facet URI in canonical form.
//
// This is a very light-weight wrapper around an std::string containing the text
// of the URI, and can be passed around as a value. The main rationale for the
// existence of this class is to make it clearer in the code when a certain URI
// is known to be a valid facet URI in canonical form, and to allow verifying
// and converting URIs to such canonical form.
//
// Note that it would be impractical to use GURL to represent facet URIs, as
// GURL has built-in logic to parse the rest of the URI according to its scheme,
// and obviously, it does not recognize the "android" scheme. Therefore, after
// parsing, everything ends up in the path component, which is not too helpful.
class FacetURI {};

// The branding information for a given facet. Corresponds to the |BrandingInfo|
// message in affiliation_api.proto.
struct FacetBrandingInfo {};

// Facet struct, corresponds to the |Facet| message in affiliation_api.proto.
struct Facet {};

// A collection of facets affiliated with each other, i.e. an equivalence class.
AffiliatedFacets;

// A collection of grouped facets. Used to group passwords in the UI.
struct GroupedFacets {};

// This functions merges groups together if one of the following applies:
// * the same facet is present in both groups.
// * eTLD+1 of a facet in one group matches eTLD+1 of a facet in another group.
std::vector<GroupedFacets> MergeRelatedGroups(
    const base::flat_set<std::string>& psl_extensions,
    const std::vector<GroupedFacets>& groups);

// A collection of facets affiliated with each other, i.e. an equivalence class,
// plus a timestamp that indicates the last time the data was updated from an
// authoritative source.
struct AffiliatedFacetsWithUpdateTime {};

// Returns whether or not equivalence classes |a| and |b| are equal, that is,
// whether or not they consist of the same set of facet URIs. Note that branding
// information is ignored for this check.
//
// Note that this will do some sorting, so it can be expensive for large inputs.
bool AreEquivalenceClassesEqual(const AffiliatedFacets& a,
                                const AffiliatedFacets& b);

// A shorter way to spell FacetURI::IsValidAndroidFacetURI().
bool IsValidAndroidFacetURI(const std::string& uri);

// Retrieves the extended top level domain for a given |url|
// ("https://www.facebook.com/" => "facebook.com"). If the calculated top
// private domain matches an entry from the |psl_extensions| (e.g. "app.link"),
// the domain is extended by one level ("https://facebook.app.link/" =>
// "facebook.app.link"). If the |url| is not a valid URI or has an unsupported
// schema (e.g. "android://"), empty string is returned.
std::string GetExtendedTopLevelDomain(
    const GURL& url,
    const base::flat_set<std::string>& psl_extensions);

// Two URLs are considered an Extended Public Suffix Domain match if they have
// the same extended top level domain (See `GetExtendedTopLevelDomain`). The
// `psl_extensions` list is used to calculate the appropriate top domain. If one
// or both arguments do not describe valid URLs, returns false.
bool IsExtendedPublicSuffixDomainMatch(
    const GURL& url1,
    const GURL& url2,
    const base::flat_set<std::string>& psl_extensions);

// For logging use only.
std::ostream& operator<<(std::ostream& os, const FacetURI& facet_uri);

// Needed for testing.
bool operator==(const FacetBrandingInfo& lhs, const FacetBrandingInfo& rhs);
bool operator!=(const FacetBrandingInfo& lhs, const FacetBrandingInfo& rhs);
bool operator==(const Facet& lhs, const Facet& rhs);
bool operator!=(const Facet& lhs, const Facet& rhs);
bool operator==(const GroupedFacets& lhs, const GroupedFacets& rhs);
bool operator!=(const GroupedFacets& lhs, const GroupedFacets& rhs);

struct FacetURIHash {};

}  // namespace affiliations

#endif  // COMPONENTS_AFFILIATIONS_CORE_BROWSER_AFFILIATION_UTILS_H_