11package com .uid2 .operator .vertx ;
22
3+ import com .uid2 .operator .util .Tuple ;
34import com .uid2 .shared .Const ;
5+ import com .uid2 .shared .auth .IAuthorizable ;
6+ import com .uid2 .shared .auth .IAuthorizableProvider ;
7+ import com .uid2 .shared .middleware .AuthMiddleware ;
48import io .micrometer .core .instrument .Counter ;
59import io .micrometer .core .instrument .Metrics ;
610import io .vertx .core .Handler ;
711import io .vertx .ext .web .RoutingContext ;
12+ import org .slf4j .Logger ;
13+ import org .slf4j .LoggerFactory ;
814
915import java .io .IOException ;
1016import java .nio .file .DirectoryStream ;
1117import java .nio .file .Files ;
1218import java .nio .file .Path ;
1319import java .nio .file .Paths ;
1420import java .util .HashMap ;
21+ import java .util .HashSet ;
1522import java .util .Map ;
23+ import java .util .Set ;
1624
1725public class ClientVersionCapturingHandler implements Handler <RoutingContext > {
18- private final Map <String , Counter > _clientVersionCounters = new HashMap <>();
26+ private static final Logger LOGGER = LoggerFactory .getLogger (ClientVersionCapturingHandler .class );
27+ private static final String BEARER_TOKEN_PREFIX = "bearer " ;
28+ private final Map <Tuple .Tuple2 <String , String >, Counter > _clientVersionCounters = new HashMap <>();
29+ private IAuthorizableProvider authKeyStore ;
30+ private final Set <String > versions = new HashSet <>();
1931
20- public ClientVersionCapturingHandler (String dir , String whitelistGlob ) throws IOException {
32+ public ClientVersionCapturingHandler (String dir , String whitelistGlob , IAuthorizableProvider authKeyStore ) throws IOException {
33+ this .authKeyStore = authKeyStore ;
2134 try (DirectoryStream <Path > dirStream = Files .newDirectoryStream (Paths .get (dir ), whitelistGlob )) {
2235 dirStream .forEach (path -> {
2336 final String version = getFileNameWithoutExtension (path );
24- final Counter counter = Counter
25- .builder ("uid2.client_sdk_versions" )
26- .description ("counter for how many http requests are processed per each client sdk version" )
27- .tags ("client_version" , version )
28- .register (Metrics .globalRegistry );
29- _clientVersionCounters .put (version , counter );
37+ versions .add (version );
3038 });
3139 }
3240 }
@@ -36,11 +44,22 @@ public void handle(RoutingContext context) {
3644 if (clientVersion == null ) {
3745 clientVersion = !context .queryParam ("client" ).isEmpty () ? context .queryParam ("client" ).get (0 ) : null ;
3846 }
39- if (clientVersion != null ) {
40- final Counter counter = _clientVersionCounters .get (clientVersion );
41- if (counter != null ) {
42- counter .increment ();
43- }
47+ String apiContact ;
48+ try {
49+ final String authHeaderValue = context .request ().getHeader ("Authorization" );
50+ final String authKey = extractBearerToken (authHeaderValue );
51+ final IAuthorizable profile = this .authKeyStore .get (authKey );
52+ apiContact = profile .getContact ();
53+ apiContact = apiContact == null ? "unknown" : apiContact ;
54+ } catch (Exception ex ) {
55+ apiContact = "unknown" ;
56+ }
57+ if (clientVersion != null && versions .contains (clientVersion )) {
58+ _clientVersionCounters .computeIfAbsent (new Tuple .Tuple2 <>(apiContact , clientVersion ), tuple -> Counter
59+ .builder ("uid2.client_sdk_versions" )
60+ .description ("counter for how many http requests are processed per each client sdk version" )
61+ .tags ("api_contact" , tuple .getItem1 (), "client_version" , tuple .getItem2 ())
62+ .register (Metrics .globalRegistry )).increment ();;
4463 }
4564 context .next ();
4665 }
@@ -49,4 +68,22 @@ private static String getFileNameWithoutExtension(Path path) {
4968 final String fileName = path .getFileName ().toString ();
5069 return fileName .indexOf ("." ) > 0 ? fileName .substring (0 , fileName .lastIndexOf ("." )) : fileName ;
5170 }
71+
72+ private static String extractBearerToken (final String headerValue ) {
73+ if (headerValue == null ) {
74+ return null ;
75+ }
76+
77+ final String v = headerValue .trim ();
78+ if (v .length () < BEARER_TOKEN_PREFIX .length ()) {
79+ return null ;
80+ }
81+
82+ final String givenPrefix = v .substring (0 , BEARER_TOKEN_PREFIX .length ());
83+
84+ if (!BEARER_TOKEN_PREFIX .equals (givenPrefix .toLowerCase ())) {
85+ return null ;
86+ }
87+ return v .substring (BEARER_TOKEN_PREFIX .length ());
88+ }
5289}
0 commit comments