|
| 1 | +package io.getstream.chat.java.services.framework; |
| 2 | + |
| 3 | +import okhttp3.Call; |
| 4 | +import okhttp3.Request; |
| 5 | +import org.jetbrains.annotations.NotNull; |
| 6 | +import java.lang.reflect.Field; |
| 7 | +import java.lang.reflect.InvocationHandler; |
| 8 | +import java.lang.reflect.Method; |
| 9 | + |
| 10 | +/** |
| 11 | + * Dynamic proxy that intercepts Retrofit service calls and injects UserToken |
| 12 | + * into the request by modifying the internal rawCall field via reflection. |
| 13 | + * |
| 14 | + * This approach allows per-call authentication without creating multiple OkHttpClient |
| 15 | + * instances, making it suitable for multi-tenant systems with thousands of users. |
| 16 | + */ |
| 17 | +class UserTokenCallProxy implements InvocationHandler { |
| 18 | + private static volatile Field rawCallField; |
| 19 | + |
| 20 | + private final Call.Factory callFactory; |
| 21 | + private final Object delegate; |
| 22 | + private final UserToken token; |
| 23 | + |
| 24 | + UserTokenCallProxy(@NotNull Call.Factory callFactory, @NotNull Object delegate, @NotNull UserToken token) { |
| 25 | + this.callFactory = callFactory; |
| 26 | + this.delegate = delegate; |
| 27 | + this.token = token; |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { |
| 32 | + Object result = method.invoke(delegate, args); |
| 33 | + |
| 34 | + // If the result is a Retrofit Call, inject the user token |
| 35 | + if (result instanceof retrofit2.Call) { |
| 36 | + return injectTokenIntoCall((retrofit2.Call<?>) result); |
| 37 | + } |
| 38 | + |
| 39 | + return result; |
| 40 | + } |
| 41 | + |
| 42 | + private retrofit2.Call<?> injectTokenIntoCall(retrofit2.Call<?> originalCall) { |
| 43 | + retrofit2.Call<?> clonedCall = originalCall.clone(); |
| 44 | + |
| 45 | + try { |
| 46 | + // Cache field lookup for performance (double-checked locking) |
| 47 | + if (rawCallField == null) { |
| 48 | + synchronized (UserTokenCallProxy.class) { |
| 49 | + if (rawCallField == null) { |
| 50 | + rawCallField = clonedCall.getClass().getDeclaredField("rawCall"); |
| 51 | + rawCallField.setAccessible(true); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // Create new request with token tag |
| 57 | + Request newRequest = originalCall.request().newBuilder() |
| 58 | + .tag(UserToken.class, token) |
| 59 | + .build(); |
| 60 | + |
| 61 | + // Create new OkHttp call with modified request |
| 62 | + okhttp3.Call newOkHttpCall = callFactory.newCall(newRequest); |
| 63 | + |
| 64 | + // Inject the new call into the cloned Retrofit call |
| 65 | + rawCallField.set(clonedCall, newOkHttpCall); |
| 66 | + |
| 67 | + return clonedCall; |
| 68 | + } catch (NoSuchFieldException e) { |
| 69 | + // If Retrofit's internal structure changes, provide clear error message |
| 70 | + throw new RuntimeException( |
| 71 | + "Retrofit internal structure changed. Field 'rawCall' not found in " + |
| 72 | + clonedCall.getClass().getName() + ". Update client implementation.", e); |
| 73 | + } catch (IllegalAccessException e) { |
| 74 | + throw new RuntimeException("Failed to inject token into call", e); |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
0 commit comments