@@ -89,11 +89,21 @@ public long expireAfterRead(
8989 * @param token The token response to be cached
9090 */
9191 public static void put (Scopes scopes , HttpClient .TokenResponse token ) {
92- LOG .trace ("Caching token for scopes: {}" , scopes );
93- CACHE .put (scopes , token );
94- if (scopes .getService () != null ) {
95- LOG .trace ("Caching service for registry: {}" , scopes .getRegistry ());
96- SERVICE_CACHE .put (scopes .getRegistry (), scopes .getService ());
92+ Scopes newScopes = scopes .getService () != null ? scopes : scopes .withService (token .service ());
93+ LOG .trace ("Caching token for scopes: {}" , newScopes );
94+ CACHE .put (newScopes , token );
95+ if (newScopes .getService () != null ) {
96+ LOG .trace ("Caching service '{}' for registry '{}'" , newScopes .getService (), newScopes .getRegistry ());
97+ SERVICE_CACHE .put (scopes .getRegistry (), newScopes .getService ());
98+ }
99+ // Store global scopes if present for future lookups
100+ if (scopes .hasGlobalScopes ()) {
101+ Scopes newScopesWithService =
102+ scopes .withService (newScopes .getService ()).withOnlyGlobalScopes ();
103+ CACHE .put (newScopesWithService , token );
104+ Scopes newScopesWithoutGlobal =
105+ scopes .withService (newScopes .getService ()).withoutGlobalScopes ();
106+ CACHE .put (newScopesWithoutGlobal , token );
97107 }
98108 }
99109
@@ -103,16 +113,50 @@ public static void put(Scopes scopes, HttpClient.TokenResponse token) {
103113 * @return the token response associated with the scopes, or null if not found or expired
104114 */
105115 public static HttpClient .@ Nullable TokenResponse get (Scopes scopes ) {
116+ String service =
117+ scopes .getService () != null ? scopes .getService () : SERVICE_CACHE .getIfPresent (scopes .getRegistry ());
106118 HttpClient .TokenResponse token = CACHE .getIfPresent (scopes );
107- if (token == null ) {
108- // Lookup for specific service
109- String service = SERVICE_CACHE .getIfPresent (scopes .getRegistry ());
110- if (service == null ) {
111- return null ;
119+ if (token != null ) {
120+ LOG .trace ("Direct cache hit for scopes: {}" , scopes );
121+ return token ;
122+ }
123+ // Try lookup with service
124+ token = CACHE .getIfPresent (scopes .withService (service ));
125+ if (token != null ) {
126+ LOG .trace ("Cache lookup for scopes: {}, found with service '{}'" , scopes , service );
127+ return token ;
128+ }
129+ // Check englobing scopes
130+ if (scopes .isPullOnly () && !scopes .hasGlobalScopes ()) {
131+ Scopes newScopes = scopes .withService (service ).withAddedRegistryScopes (Scope .PUSH ); // Just add push scope
132+ token = CACHE .getIfPresent (newScopes );
133+ if (token != null ) {
134+ LOG .trace ("Cache lookup for scopes: {}, found with push scope" , scopes );
135+ return token ;
136+ }
137+ newScopes = newScopes .withService (service ).withAddedRegistryScopes (Scope .DELETE ); // Add delete scope
138+ token = CACHE .getIfPresent (newScopes );
139+ if (token != null ) {
140+ LOG .trace ("Cache lookup for scopes: {}, found with push and delete scopes" , scopes );
141+ return token ;
142+ }
143+ newScopes = newScopes .withService (service ).withRegistryScopes (Scope .ALL ); // All replace all scopes
144+ token = CACHE .getIfPresent (newScopes );
145+ if (token != null ) {
146+ LOG .trace ("Cache lookup for scopes: {}, found with all scopes" , scopes );
147+ return token ;
148+ }
149+ return null ;
150+ }
151+ if (scopes .hasGlobalScopes ()) {
152+ token = CACHE .getIfPresent (scopes .withOnlyGlobalScopes ());
153+ if (token != null ) {
154+ LOG .trace ("Cache lookup for scopes: {}, found with only global scopes" , scopes );
155+ return token ;
112156 }
113- return CACHE .getIfPresent (scopes .withService (service ));
114157 }
115- return token ;
158+ LOG .trace ("Cache miss for scopes: {}" , scopes );
159+ return null ;
116160 }
117161
118162 /**
0 commit comments