1 /* 2 * Copyright (C) 2023 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 android.adservices.adselection; 18 19 import static com.android.adservices.data.adselection.EncryptionKeyConstants.EncryptionKeyType.ENCRYPTION_KEY_TYPE_AUCTION; 20 21 import android.net.Uri; 22 23 import com.android.adservices.data.adselection.DBEncryptionKey; 24 import com.android.adservices.data.adselection.DBProtectedServersEncryptionConfig; 25 import com.android.adservices.service.common.httpclient.AdServicesHttpClientResponse; 26 27 import com.google.auto.value.AutoValue; 28 import com.google.common.collect.ImmutableMap; 29 30 import org.json.JSONArray; 31 import org.json.JSONException; 32 import org.json.JSONObject; 33 34 import java.util.List; 35 36 public class AuctionEncryptionKeyFixture { 37 public static final Long DEFAULT_MAX_AGE_SECONDS = 604800L; 38 public static final String DEFAULT_MAX_AGE = "max-age=" + DEFAULT_MAX_AGE_SECONDS; 39 public static final String DEFAULT_CACHED_AGE = "800"; 40 private static final Long EXPIRY_TTL_1SEC = 1L; 41 private static final String CACHE_CONTROL_HEADER_LABEL = "cache-control"; 42 private static final String CACHED_AGE_HEADER_LABEL = "age"; 43 44 private static final String KEY_ID_LABEL = "id"; 45 private static final String PUBLIC_KEY_LABEL = "key"; 46 public static final String AUCTION_KEY_FETCH_DEFAULT_URI = "https://foo.bar/auctionkey"; 47 public static final String COORDINATOR_URL_AUCTION = "https://example-auction.com/full/url"; 48 public static final String COORDINATOR_URL_AUCTION_2 = "https://example-auction-2.com/full/url"; 49 public static final String ALLOWLIST = 50 COORDINATOR_URL_AUCTION + "," + COORDINATOR_URL_AUCTION_2; 51 public static final String COORDINATOR_URL_AUCTION_ORIGIN = "https://example-auction.com"; 52 public static final Uri COORDINATOR_URL_AUCTION_URI = Uri.parse(COORDINATOR_URL_AUCTION); 53 public static final Uri COORDINATOR_URL_AUCTION_2_URI = Uri.parse(COORDINATOR_URL_AUCTION_2); 54 public static final Uri COORDINATOR_URL_AUCTION_ORIGIN_URI = 55 Uri.parse("https://example-auction.com"); 56 public static final DBEncryptionKey ENCRYPTION_KEY_AUCTION = 57 DBEncryptionKey.builder() 58 .setKeyIdentifier("152233fc-f255-4c3d-b3ef-7e2b7fbb9ca7") 59 .setPublicKey("3QKut1VYAJGrE3TTu4NGZq3sPSgAeRaaTIdi7eZqtwk=") 60 .setEncryptionKeyType(ENCRYPTION_KEY_TYPE_AUCTION) 61 .setExpiryTtlSeconds(1000L) 62 .build(); 63 public static final DBEncryptionKey ENCRYPTION_KEY_AUCTION_TTL_1SECS = 64 DBEncryptionKey.builder() 65 .setKeyIdentifier("1b333681-4fe3-4ba9-b603-5169d421734c") 66 .setPublicKey("nD4MogDIKg+NIXiJ3lEPHcf8mYOl1wjioNFe6h9pUAI=") 67 .setEncryptionKeyType(ENCRYPTION_KEY_TYPE_AUCTION) 68 .setExpiryTtlSeconds(EXPIRY_TTL_1SEC) 69 .build(); 70 public static final ImmutableMap<String, List<String>> DEFAULT_RESPONSE_HEADERS = 71 ImmutableMap.of( 72 CACHE_CONTROL_HEADER_LABEL, 73 List.of(DEFAULT_MAX_AGE), 74 CACHED_AGE_HEADER_LABEL, 75 List.of(DEFAULT_CACHED_AGE)); 76 public static final AuctionKey AUCTION_KEY_1 = 77 AuctionKey.builder() 78 .setKeyId("152233fc-f255-4c3d-b3ef-7e2b7fbb9ca7") 79 .setPublicKey("3QKut1VYAJGrE3TTu4NGZq3sPSgAeRaaTIdi7eZqtwk=") 80 .build(); 81 public static final AuctionKey AUCTION_KEY_2 = 82 AuctionKey.builder() 83 .setKeyId("1b333681-4fe3-4ba9-b603-5169d421734c") 84 .setPublicKey("nD4MogDIKg+NIXiJ3lEPHcf8mYOl1wjioNFe6h9pUAI=") 85 .build(); 86 private static final AuctionKey AUCTION_KEY_3 = 87 AuctionKey.builder() 88 .setKeyId("420ec4eb-8753-4c45-88b9-7917baecbd21") 89 .setPublicKey("KROE/B6FBOV2+6coHHtpGi1Gxoi+bOh9srrBP64JPBk") 90 .build(); 91 private static final AuctionKey AUCTION_KEY_4 = 92 AuctionKey.builder() 93 .setKeyId("49b4de4f-4c9e-4285-afa0-78c3baea13b3") 94 .setPublicKey("7tabvCt19oMF5Quu4cAQetS6xlLFkjIbcY6330+cjlo=") 95 .build(); 96 private static final AuctionKey AUCTION_KEY_5 = 97 AuctionKey.builder() 98 .setKeyId("7b6724dc-839c-4108-bfa7-2e73eb19e5fe") 99 .setPublicKey("t/dzKzHJKe7k//n2u7wDdvxRtgXy9SncfXz6g8JB/m4=") 100 .build(); 101 102 public static final DBProtectedServersEncryptionConfig ENCRYPTION_KEY_AUCTION_WITH_COORDINATOR = 103 DBProtectedServersEncryptionConfig.builder() 104 .setKeyIdentifier(AUCTION_KEY_2.keyId()) 105 .setPublicKey(AUCTION_KEY_2.publicKey()) 106 .setEncryptionKeyType(ENCRYPTION_KEY_TYPE_AUCTION) 107 .setExpiryTtlSeconds(1000L) 108 .setCoordinatorUrl(COORDINATOR_URL_AUCTION) 109 .build(); 110 getAuctionResponseBodySingleKey()111 public static String getAuctionResponseBodySingleKey() throws JSONException { 112 JSONObject json = new JSONObject(); 113 json.put("keys", new JSONArray().put(getAuctionKeyJson(AUCTION_KEY_1))); 114 return json.toString(); 115 } 116 mockAuctionKeyFetchResponse()117 public static AdServicesHttpClientResponse mockAuctionKeyFetchResponse() throws JSONException { 118 return AdServicesHttpClientResponse.builder() 119 .setResponseBody(getDefaultAuctionResponseBody()) 120 .setResponseHeaders(DEFAULT_RESPONSE_HEADERS) 121 .build(); 122 } 123 mockAuctionKeyFetchResponseWithOneKey()124 public static AdServicesHttpClientResponse mockAuctionKeyFetchResponseWithOneKey() 125 throws JSONException { 126 return AdServicesHttpClientResponse.builder() 127 .setResponseBody(getDefaultAuctionResponseBodyWithOneKey()) 128 .setResponseHeaders(DEFAULT_RESPONSE_HEADERS) 129 .build(); 130 } 131 132 /** Gets the AdServicesHttpClientResponse with the provided AuctionKey */ mockAuctionKeyFetchResponseWithGivenKey( AuctionKey auctionKey)133 public static AdServicesHttpClientResponse mockAuctionKeyFetchResponseWithGivenKey( 134 AuctionKey auctionKey) throws JSONException { 135 return AdServicesHttpClientResponse.builder() 136 .setResponseBody(getDeterministicAuctionResponseBody(auctionKey)) 137 .setResponseHeaders(DEFAULT_RESPONSE_HEADERS) 138 .build(); 139 } 140 getAuctionKeyJson(AuctionKey key)141 private static JSONObject getAuctionKeyJson(AuctionKey key) throws JSONException { 142 return new JSONObject() 143 .put(KEY_ID_LABEL, key.keyId()) 144 .put(PUBLIC_KEY_LABEL, key.publicKey()); 145 } 146 getDefaultAuctionResponseBody()147 private static String getDefaultAuctionResponseBody() throws JSONException { 148 JSONObject json = new JSONObject(); 149 json.put( 150 "keys", 151 new JSONArray() 152 .put(getAuctionKeyJson(AUCTION_KEY_1)) 153 .put(getAuctionKeyJson(AUCTION_KEY_2)) 154 .put(getAuctionKeyJson(AUCTION_KEY_3)) 155 .put(getAuctionKeyJson(AUCTION_KEY_4)) 156 .put(getAuctionKeyJson(AUCTION_KEY_5))); 157 return json.toString(); 158 } 159 getDeterministicAuctionResponseBody(AuctionKey auctionKey)160 public static String getDeterministicAuctionResponseBody(AuctionKey auctionKey) 161 throws JSONException { 162 JSONObject json = new JSONObject(); 163 json.put("keys", new JSONArray().put(getAuctionKeyJson(auctionKey))); 164 return json.toString(); 165 } 166 getDefaultAuctionResponseBodyWithOneKey()167 private static String getDefaultAuctionResponseBodyWithOneKey() throws JSONException { 168 JSONObject json = new JSONObject(); 169 json.put("keys", new JSONArray().put(getAuctionKeyJson(AUCTION_KEY_1))); 170 return json.toString(); 171 } 172 173 @AutoValue 174 public abstract static class AuctionKey { keyId()175 public abstract String keyId(); 176 publicKey()177 public abstract String publicKey(); 178 builder()179 public static AuctionKey.Builder builder() { 180 return new AutoValue_AuctionEncryptionKeyFixture_AuctionKey.Builder(); 181 } 182 183 @AutoValue.Builder 184 public abstract static class Builder { setKeyId(String keyId)185 public abstract Builder setKeyId(String keyId); 186 setPublicKey(String publicKey)187 public abstract Builder setPublicKey(String publicKey); 188 build()189 public abstract AuctionKey build(); 190 } 191 } 192 } 193