@@ -117,6 +117,7 @@ public class UIDOperatorVerticle extends AbstractVerticle {
117117 private final Map <String , Tuple .Tuple2 <Counter , Counter >> _identityMapUnmappedIdentifiers = new HashMap <>();
118118 private final Map <String , Counter > _identityMapRequestWithUnmapped = new HashMap <>();
119119 private final Map <Tuple .Tuple2 <String , String >, Counter > _clientVersions = new HashMap <>();
120+ private final Map <Tuple .Tuple2 <String , String >, Counter > _tokenValidateCounters = new HashMap <>();
120121
121122 private final Map <String , DistributionSummary > optOutStatusCounters = new HashMap <>();
122123 private final IdentityScope identityScope ;
@@ -210,7 +211,8 @@ public void start(Promise<Void> startPromise) throws Exception {
210211 this .identityScope ,
211212 this .saltRetrievalResponseHandler ,
212213 this .identityV3Enabled ,
213- this .uidInstanceIdProvider
214+ this .uidInstanceIdProvider ,
215+ this .keyManager
214216 );
215217
216218 final Router router = createRoutesSetup ();
@@ -233,13 +235,8 @@ public void start(Promise<Void> startPromise) throws Exception {
233235
234236 }
235237
236- private Router createRoutesSetup () throws IOException {
237- final Router router = Router .router (vertx );
238-
239- router .allowForward (AllowForwardHeaders .X_FORWARD );
240- router .route ().handler (new RequestCapturingHandler (siteProvider ));
241- router .route ().handler (new ClientVersionCapturingHandler ("static/js" , "*.js" , clientKeyProvider ));
242- router .route ().handler (CorsHandler .create ()
238+ private CorsHandler createCorsHandler () {
239+ return CorsHandler .create ()
243240 .addRelativeOrigin (".*." )
244241 .allowedMethod (io .vertx .core .http .HttpMethod .GET )
245242 .allowedMethod (io .vertx .core .http .HttpMethod .POST )
@@ -249,7 +246,17 @@ private Router createRoutesSetup() throws IOException {
249246 .allowedHeader ("Access-Control-Allow-Credentials" )
250247 .allowedHeader ("Access-Control-Allow-Origin" )
251248 .allowedHeader ("Access-Control-Allow-Headers" )
252- .allowedHeader ("Content-Type" ));
249+ .allowedHeader ("Content-Type" );
250+ }
251+
252+ private Router createRoutesSetup () throws IOException {
253+ final Router router = Router .router (vertx );
254+
255+ router .allowForward (AllowForwardHeaders .X_FORWARD );
256+ router .route ().handler (new RequestCapturingHandler (siteProvider ));
257+ router .route ().handler (new ClientVersionCapturingHandler ("static/js" , "*.js" , clientKeyProvider ));
258+ router .route (V2_TOKEN_VALIDATE .toString ()).handler (createCorsHandler ().allowedHeader ("Authorization" ));
259+ router .route ().handler (createCorsHandler ());
253260 router .route ().handler (new StatsCollectorHandler (_statsCollectorQueue , vertx ));
254261 router .route ("/static/*" ).handler (StaticHandler .create ("static" ));
255262 router .route ().handler (ctx -> {
@@ -807,6 +814,22 @@ private void recordOperatorServedSdkUsage(RoutingContext rc, Integer siteId, Str
807814 }
808815 }
809816
817+ private void recordTokenValidateStats (Integer siteId , String result ) {
818+ final String siteIdStr = siteId != null ? String .valueOf (siteId ) : "unknown" ;
819+ _tokenValidateCounters .computeIfAbsent (
820+ new Tuple .Tuple2 <>(siteIdStr , result ),
821+ tuple -> Counter
822+ .builder ("uid2_token_validate_total" )
823+ .description ("counter for token validate endpoint results" )
824+ .tags (
825+ "site_id" , tuple .getItem1 (),
826+ "site_name" , getSiteName (siteProvider , Integer .valueOf (tuple .getItem1 ())),
827+ "result" , tuple .getItem2 ()
828+ )
829+ .register (Metrics .globalRegistry )
830+ ).increment ();
831+ }
832+
810833 private void handleTokenRefreshV2 (RoutingContext rc ) {
811834 Integer siteId = null ;
812835 TokenResponseStatsCollector .PlatformType platformType = TokenResponseStatsCollector .PlatformType .Other ;
@@ -848,33 +871,38 @@ private void handleTokenRefreshV2(RoutingContext rc) {
848871 private void handleTokenValidateV2 (RoutingContext rc ) {
849872 RuntimeConfig config = this .getConfigFromRc (rc );
850873 IdentityEnvironment env = config .getIdentityEnvironment ();
874+ final Integer participantSiteId = AuthMiddleware .getAuthClient (rc ).getSiteId ();
851875
852876 try {
853877 final JsonObject req = (JsonObject ) rc .data ().get ("request" );
854878
855879 final InputUtil .InputVal input = getTokenInputV2 (req );
856880 if (!isTokenInputValid (input , rc )) {
881+ recordTokenValidateStats (participantSiteId , "invalid_input" );
857882 return ;
858883 }
859- if ((input .getIdentityType () == IdentityType .Email && Arrays .equals (ValidateIdentityForEmailHash , input .getIdentityInput ()))
860- || (input .getIdentityType () == IdentityType .Phone && Arrays .equals (ValidateIdentityForPhoneHash , input .getIdentityInput ()))) {
861- try {
862- final Instant now = Instant .now ();
863- final String token = req .getString ("token" );
864-
865- if (this .idService .advertisingTokenMatches (token , input .toUserIdentity (this .identityScope , 0 , now ), now , env )) {
866- ResponseUtil .SuccessV2 (rc , Boolean .TRUE );
867- } else {
868- ResponseUtil .SuccessV2 (rc , Boolean .FALSE );
869- }
870- } catch (Exception e ) {
871- ResponseUtil .SuccessV2 (rc , Boolean .FALSE );
872- }
873- } else {
884+
885+ final Instant now = Instant .now ();
886+ final String token = req .getString ("token" );
887+
888+ final TokenValidateResult result = this .idService .validateAdvertisingToken (participantSiteId , token , input .toUserIdentity (this .identityScope , 0 , now ), now , env );
889+
890+ if (result == TokenValidateResult .MATCH ) {
891+ recordTokenValidateStats (participantSiteId , "match" );
892+ ResponseUtil .SuccessV2 (rc , Boolean .TRUE );
893+ } else if (result == TokenValidateResult .MISMATCH ) {
894+ recordTokenValidateStats (participantSiteId , "mismatch" );
874895 ResponseUtil .SuccessV2 (rc , Boolean .FALSE );
896+ } else if (result == TokenValidateResult .UNAUTHORIZED ) {
897+ recordTokenValidateStats (participantSiteId , "unauthorized" );
898+ ResponseUtil .LogInfoAndSend400Response (rc , "Unauthorised to validate token" );
899+ } else if (result == TokenValidateResult .INVALID_TOKEN ) {
900+ recordTokenValidateStats (participantSiteId , "invalid_token" );
901+ ResponseUtil .LogInfoAndSend400Response (rc , "Invalid token" );
875902 }
876903 } catch (Exception e ) {
877- LOGGER .error ("Unknown error while validating token v2" , e );
904+ recordTokenValidateStats (participantSiteId , "error" );
905+ LOGGER .error ("Unknown error while validating token" , e );
878906 rc .fail (500 );
879907 }
880908 }
0 commit comments