|
1 | 1 | package com.uid2.operator.service; |
2 | 2 |
|
3 | 3 | import com.uid2.operator.model.*; |
| 4 | +import com.uid2.operator.util.Tuple; |
4 | 5 | import com.uid2.operator.vertx.ClientInputValidationException; |
5 | 6 | import com.uid2.shared.Const.Data; |
6 | 7 | import com.uid2.shared.encryption.AesCbc; |
|
11 | 12 | import io.vertx.core.buffer.Buffer; |
12 | 13 | import io.micrometer.core.instrument.Counter; |
13 | 14 | import io.micrometer.core.instrument.Metrics; |
| 15 | +import software.amazon.awssdk.services.kms.endpoints.internal.Value; |
14 | 16 |
|
15 | 17 | import java.time.Instant; |
16 | 18 | import java.util.Base64; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.Map; |
17 | 21 |
|
18 | 22 | public class EncryptedTokenEncoder implements ITokenEncoder { |
19 | 23 | private final KeyManager keyManager; |
20 | 24 |
|
21 | 25 | public EncryptedTokenEncoder(KeyManager keyManager) { |
22 | 26 | this.keyManager = keyManager; |
23 | 27 | } |
| 28 | + private final Map<Integer, Counter> _refreshTokenVersionCounters = new HashMap<>(); |
24 | 29 |
|
25 | 30 | public byte[] encode(AdvertisingToken t, Instant asOf) { |
26 | 31 | final KeysetKey masterKey = this.keyManager.getMasterKey(asOf); |
@@ -260,23 +265,24 @@ public AdvertisingToken decodeAdvertisingTokenV3orV4(Buffer b, byte[] bytes, Tok |
260 | 265 | ); |
261 | 266 | } |
262 | 267 |
|
| 268 | + private void recordRefreshTokenVersionCount(int siteId, TokenVersion tokenVersion) { |
| 269 | + _refreshTokenVersionCounters.computeIfAbsent(siteId, id -> Counter |
| 270 | + .builder(String.format("uid2_refresh_token_%s_served_count", tokenVersion.toString().toLowerCase())) |
| 271 | + .description(String.format("Counter for the amount of refresh token %s served", tokenVersion.toString().toLowerCase())) |
| 272 | + .tags("site_id", String.valueOf(siteId)) |
| 273 | + .register(Metrics.globalRegistry)).increment(); |
| 274 | + |
| 275 | + } |
| 276 | + |
263 | 277 | public byte[] encode(RefreshToken t, Instant asOf) { |
264 | 278 | final KeysetKey serviceKey = this.keyManager.getRefreshKey(asOf); |
265 | 279 |
|
266 | 280 | switch (t.version) { |
267 | 281 | case V2: |
268 | | - var v2Builder = Counter |
269 | | - .builder("uid2_refresh_token_v2_served_count") |
270 | | - .description("Counter for the amount of refresh token v2 served").tags( |
271 | | - "timestamp", String.valueOf(asOf)); |
272 | | - v2Builder.register(Metrics.globalRegistry).increment(); |
| 282 | + recordRefreshTokenVersionCount(t.publisherIdentity.siteId, TokenVersion.V2); |
273 | 283 | return encodeV2(t, serviceKey); |
274 | 284 | case V3: |
275 | | - var v3Builder = Counter |
276 | | - .builder("uid2_refresh_token_v3_served_count") |
277 | | - .description("Counter for the amount of refresh token v3 served").tags( |
278 | | - "timestamp", String.valueOf(asOf)); |
279 | | - v3Builder.register(Metrics.globalRegistry).increment(); |
| 285 | + recordRefreshTokenVersionCount(t.publisherIdentity.siteId, TokenVersion.V3); |
280 | 286 | return encodeV3(t, serviceKey); |
281 | 287 | default: |
282 | 288 | throw new ClientInputValidationException("RefreshToken version " + t.version + " not supported"); |
|
0 commit comments