mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-12-23 00:51:01 +03:00
[FIX] Auto refresh
This commit is contained in:
parent
caebd6b5de
commit
55c77dd343
1 changed files with 33 additions and 25 deletions
|
@ -12,6 +12,7 @@
|
||||||
import pro.gravit.utils.helper.LogHelper;
|
import pro.gravit.utils.helper.LogHelper;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
@ -22,11 +23,11 @@ public abstract class Request<R extends WebSocketEvent> implements WebSocketRequ
|
||||||
private static final List<ExtendedTokenCallback> extendedTokenCallbacks = new ArrayList<>(4);
|
private static final List<ExtendedTokenCallback> extendedTokenCallbacks = new ArrayList<>(4);
|
||||||
private static final List<BiConsumer<String, AuthRequestEvent.OAuthRequestEvent>> oauthChangeHandlers = new ArrayList<>(4);
|
private static final List<BiConsumer<String, AuthRequestEvent.OAuthRequestEvent>> oauthChangeHandlers = new ArrayList<>(4);
|
||||||
|
|
||||||
private static RequestService requestService;
|
private static volatile RequestService requestService;
|
||||||
private static AuthRequestEvent.OAuthRequestEvent oauth;
|
private static volatile AuthRequestEvent.OAuthRequestEvent oauth;
|
||||||
private static Map<String, ExtendedToken> extendedTokens;
|
private static volatile Map<String, ExtendedToken> extendedTokens;
|
||||||
private static String authId;
|
private static volatile String authId;
|
||||||
private static long tokenExpiredTime;
|
private static volatile long tokenExpiredTime;
|
||||||
private static ScheduledExecutorService executorService;
|
private static ScheduledExecutorService executorService;
|
||||||
@LauncherNetworkAPI
|
@LauncherNetworkAPI
|
||||||
public final UUID requestUUID = UUID.randomUUID();
|
public final UUID requestUUID = UUID.randomUUID();
|
||||||
|
@ -104,14 +105,14 @@ public static void clearExtendedTokens() {
|
||||||
|
|
||||||
public static void addExtendedToken(String name, ExtendedToken token) {
|
public static void addExtendedToken(String name, ExtendedToken token) {
|
||||||
if (extendedTokens == null) {
|
if (extendedTokens == null) {
|
||||||
extendedTokens = new HashMap<>();
|
extendedTokens = new ConcurrentHashMap<>();
|
||||||
}
|
}
|
||||||
extendedTokens.put(name, token);
|
extendedTokens.put(name, token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void addAllExtendedToken(Map<String, ExtendedToken> map) {
|
public static void addAllExtendedToken(Map<String, ExtendedToken> map) {
|
||||||
if (extendedTokens == null) {
|
if (extendedTokens == null) {
|
||||||
extendedTokens = new HashMap<>();
|
extendedTokens = new ConcurrentHashMap<>();
|
||||||
}
|
}
|
||||||
extendedTokens.putAll(map);
|
extendedTokens.putAll(map);
|
||||||
}
|
}
|
||||||
|
@ -150,14 +151,17 @@ public static RequestRestoreReport restore() throws Exception {
|
||||||
return restore(false, false);
|
return restore(false, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Map<String, String> getExpiredExtendedTokens() {
|
private synchronized static Map<String, String> getExpiredExtendedTokens() {
|
||||||
Map<String, String> map = new HashMap<>();
|
Set<String> set = new HashSet<>();
|
||||||
for(Map.Entry<String, ExtendedToken> e : extendedTokens.entrySet()) {
|
for(Map.Entry<String, ExtendedToken> e : extendedTokens.entrySet()) {
|
||||||
if(e.getValue().expire != 0 && e.getValue().expire < System.currentTimeMillis()) {
|
if(e.getValue().expire != 0 && e.getValue().expire < System.currentTimeMillis()) {
|
||||||
map.put(e.getKey(), e.getValue().token);
|
set.add(e.getKey());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return map;
|
if(set.isEmpty()) {
|
||||||
|
return new HashMap<>();
|
||||||
|
}
|
||||||
|
return makeNewTokens(set);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static synchronized RequestRestoreReport restore(boolean needUserInfo, boolean refreshOnly) throws Exception {
|
public static synchronized RequestRestoreReport restore(boolean needUserInfo, boolean refreshOnly) throws Exception {
|
||||||
|
@ -180,19 +184,8 @@ public static synchronized RequestRestoreReport restore(boolean needUserInfo, bo
|
||||||
RestoreRequestEvent event = request.request();
|
RestoreRequestEvent event = request.request();
|
||||||
List<String> invalidTokens = null;
|
List<String> invalidTokens = null;
|
||||||
if (event.invalidTokens != null && event.invalidTokens.size() > 0) {
|
if (event.invalidTokens != null && event.invalidTokens.size() > 0) {
|
||||||
boolean needRequest = false;
|
Map<String, String> tokens = makeNewTokens(event.invalidTokens);
|
||||||
Map<String, String> tokens = new HashMap<>();
|
if (!tokens.isEmpty()) {
|
||||||
for (ExtendedTokenCallback cb : extendedTokenCallbacks) {
|
|
||||||
for (String tokenName : event.invalidTokens) {
|
|
||||||
ExtendedToken newToken = cb.tryGetNewToken(tokenName);
|
|
||||||
if (newToken != null) {
|
|
||||||
needRequest = true;
|
|
||||||
tokens.put(tokenName, newToken.token);
|
|
||||||
addExtendedToken(tokenName, newToken);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (needRequest) {
|
|
||||||
request = new RestoreRequest(authId, null, tokens, false);
|
request = new RestoreRequest(authId, null, tokens, false);
|
||||||
event = request.request();
|
event = request.request();
|
||||||
if (event.invalidTokens != null && event.invalidTokens.size() > 0) {
|
if (event.invalidTokens != null && event.invalidTokens.size() > 0) {
|
||||||
|
@ -204,6 +197,20 @@ public static synchronized RequestRestoreReport restore(boolean needUserInfo, bo
|
||||||
return new RequestRestoreReport(refreshed, invalidTokens, event.userInfo);
|
return new RequestRestoreReport(refreshed, invalidTokens, event.userInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private synchronized static Map<String, String> makeNewTokens(Collection<String> keys) {
|
||||||
|
Map<String, String> tokens = new HashMap<>();
|
||||||
|
for (ExtendedTokenCallback cb : extendedTokenCallbacks) {
|
||||||
|
for (String tokenName : keys) {
|
||||||
|
ExtendedToken newToken = cb.tryGetNewToken(tokenName);
|
||||||
|
if (newToken != null) {
|
||||||
|
tokens.put(tokenName, newToken.token);
|
||||||
|
addExtendedToken(tokenName, newToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return tokens;
|
||||||
|
}
|
||||||
|
|
||||||
public static void requestError(String message) throws RequestException {
|
public static void requestError(String message) throws RequestException {
|
||||||
throw new RequestException(message);
|
throw new RequestException(message);
|
||||||
}
|
}
|
||||||
|
@ -272,7 +279,8 @@ public static class ExtendedToken {
|
||||||
|
|
||||||
public ExtendedToken(String token, long expire) {
|
public ExtendedToken(String token, long expire) {
|
||||||
this.token = token;
|
this.token = token;
|
||||||
this.expire = expire;
|
long time = System.currentTimeMillis();
|
||||||
|
this.expire = expire < time/2 ? time+expire : expire;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue