• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.adservices.service.measurement.util;
18 
19 import android.content.Context;
20 import android.net.Uri;
21 
22 import com.android.adservices.LoggerFactory;
23 import com.android.adservices.data.enrollment.EnrollmentDao;
24 import com.android.adservices.service.Flags;
25 import com.android.adservices.service.common.AppManifestConfigHelper;
26 import com.android.adservices.service.common.WebAddresses;
27 import com.android.adservices.service.enrollment.EnrollmentData;
28 
29 import java.util.Optional;
30 
31 /** Enrollment utilities for measurement. */
32 public final class Enrollment {
33     public static final String LOCALHOST_ENROLLMENT_ID = "localhost_enrollment_id";
34     public static final String LOCALHOST_IP_ENROLLMENT_ID = "localhost_ip_enrollment_id";
35 
Enrollment()36     private Enrollment() { }
37 
38     /**
39      * Returns an {@code Optional<String>} of the ad-tech enrollment record ID.
40      *
41      * @param registrationUri the ad-tech URL used to register a source or trigger.
42      * @param packageName Package mame of the registrant
43      * @param enrollmentDao an instance of {@code EnrollmentDao}.
44      * @param context a valid {@code Context} object
45      * @param flags a valid {@code Flags} object
46      * @return enrollmentId if enrollment id exists and all validations pass otherwise empty
47      */
getValidEnrollmentId( Uri registrationUri, String packageName, EnrollmentDao enrollmentDao, Context context, Flags flags)48     public static Optional<String> getValidEnrollmentId(
49             Uri registrationUri,
50             String packageName,
51             EnrollmentDao enrollmentDao,
52             Context context,
53             Flags flags) {
54         if (WebAddresses.isLocalhost(registrationUri)) {
55             return Optional.of(
56                     WebAddresses.isLocalhostIp(registrationUri)
57                             ? LOCALHOST_IP_ENROLLMENT_ID
58                             : LOCALHOST_ENROLLMENT_ID);
59         }
60 
61         Uri uriWithoutParams = registrationUri.buildUpon().clearQuery().fragment(null).build();
62 
63         EnrollmentData enrollmentData =
64                 enrollmentDao.getEnrollmentDataFromMeasurementUrl(uriWithoutParams);
65         if (enrollmentData == null) {
66             LoggerFactory.getMeasurementLogger()
67                     .w(
68                             "Enrollment check failed, Reason: Enrollment Id Not Found, "
69                                     + "Registration URI: %s",
70                             registrationUri);
71             return Optional.empty();
72         }
73         if (flags.isEnrollmentBlocklisted(enrollmentData.getEnrollmentId())) {
74             LoggerFactory.getMeasurementLogger()
75                     .w(
76                             "Enrollment check failed, Reason: Enrollment Id in blocklist, "
77                                     + "Registration URI: %s, Enrollment Id: %s",
78                             registrationUri, enrollmentData.getEnrollmentId());
79             return Optional.empty();
80         }
81         if (!AppManifestConfigHelper.isAllowedAttributionAccess(
82                 packageName, enrollmentData.getEnrollmentId())) {
83             LoggerFactory.getMeasurementLogger()
84                     .w(
85                             "Enrollment check failed, Reason: Enrollment Id missing from "
86                                     + "App Manifest AdTech allowlist, "
87                                     + "Registration URI: %s, Enrollment Id: %s",
88                             registrationUri, enrollmentData.getEnrollmentId());
89             return Optional.empty();
90         }
91         return Optional.of(enrollmentData.getEnrollmentId());
92     }
93 }
94