Aggiunto interceptor per il refresh del token
This commit is contained in:
parent
56f14c064e
commit
0da31ef866
8
.idea/deploymentTargetSelector.xml
generated
8
.idea/deploymentTargetSelector.xml
generated
@ -7,6 +7,14 @@
|
||||
</SelectionState>
|
||||
<SelectionState runConfigName="app">
|
||||
<option name="selectionMode" value="DROPDOWN" />
|
||||
<DropdownSelection timestamp="2024-05-07T15:55:53.714883500Z">
|
||||
<Target type="DEFAULT_BOOT">
|
||||
<handle>
|
||||
<DeviceId pluginId="PhysicalDevice" identifier="serial=23324B6867" />
|
||||
</handle>
|
||||
</Target>
|
||||
</DropdownSelection>
|
||||
<DialogSelection />
|
||||
</SelectionState>
|
||||
</selectionStates>
|
||||
</component>
|
||||
|
||||
@ -19,6 +19,7 @@ import it.integry.integrywmsnative.core.data_store.db.AppDatabase;
|
||||
import it.integry.integrywmsnative.core.ean128.Ean128Service;
|
||||
import it.integry.integrywmsnative.core.menu.MenuRESTConsumer;
|
||||
import it.integry.integrywmsnative.core.menu.MenuService;
|
||||
import it.integry.integrywmsnative.core.rest.AuthInterceptor;
|
||||
import it.integry.integrywmsnative.core.rest.RESTBuilder;
|
||||
import it.integry.integrywmsnative.core.rest.consumers.ArticoloRESTConsumer;
|
||||
import it.integry.integrywmsnative.core.rest.consumers.AziendaRESTConsumer;
|
||||
@ -50,7 +51,7 @@ import it.integry.integrywmsnative.core.settings.SettingsManager;
|
||||
import it.integry.integrywmsnative.core.sound.SoundAlertService;
|
||||
import it.integry.integrywmsnative.core.update.UpdatesManager;
|
||||
import it.integry.integrywmsnative.gest.contab_doc_interni.rest.DocInterniRESTConsumer;
|
||||
import it.integry.integrywmsnative.gest.login.rest.AuthRESTConsumer;
|
||||
import it.integry.integrywmsnative.gest.login.rest.AuthenticationRESTConsumer;
|
||||
import it.integry.integrywmsnative.gest.prod_fabbisogno_linee_prod.rest.ProdFabbisognoLineeProdRESTConsumer;
|
||||
import it.integry.integrywmsnative.view.dialogs.DialogProgressView;
|
||||
import it.integry.integrywmsnative.view.dialogs.input_quantity_v2.DialogInputQuantityV2View;
|
||||
@ -96,8 +97,8 @@ public class MainApplicationModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
MainContext providesMainContextNew(MenuService menuService, AppDatabase appDatabase, SystemRESTConsumer systemRESTConsumer, AuthRESTConsumer authRESTConsumer) {
|
||||
return new MainContext(mApplication.getApplicationContext(), menuService, appDatabase, systemRESTConsumer, authRESTConsumer);
|
||||
MainContext providesMainContextNew(MenuService menuService, AppDatabase appDatabase, SystemRESTConsumer systemRESTConsumer, AuthenticationRESTConsumer authenticationRESTConsumer) {
|
||||
return new MainContext(mApplication.getApplicationContext(), menuService, appDatabase, systemRESTConsumer, authenticationRESTConsumer);
|
||||
}
|
||||
|
||||
@Provides
|
||||
@ -117,8 +118,8 @@ public class MainApplicationModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
RESTBuilder providesRESTBuilder() {
|
||||
return new RESTBuilder();
|
||||
RESTBuilder providesRESTBuilder(AuthInterceptor authInterceptor) {
|
||||
return new RESTBuilder(authInterceptor);
|
||||
}
|
||||
|
||||
@Provides
|
||||
|
||||
@ -19,7 +19,7 @@ import it.integry.integrywmsnative.core.rest.consumers.SystemRESTConsumer;
|
||||
import it.integry.integrywmsnative.core.rest.watcher.ServerStatusChecker;
|
||||
import it.integry.integrywmsnative.core.settings.SettingsManager;
|
||||
import it.integry.integrywmsnative.core.utility.UtilityThread;
|
||||
import it.integry.integrywmsnative.gest.login.rest.AuthRESTConsumer;
|
||||
import it.integry.integrywmsnative.gest.login.rest.AuthenticationRESTConsumer;
|
||||
|
||||
@Singleton
|
||||
public class MainContext {
|
||||
@ -28,16 +28,16 @@ public class MainContext {
|
||||
private final MenuService menuService;
|
||||
private final AppDatabase appDatabase;
|
||||
private final SystemRESTConsumer systemRESTConsumer;
|
||||
private final AuthRESTConsumer authRESTConsumer;
|
||||
private final AuthenticationRESTConsumer authenticationRESTConsumer;
|
||||
|
||||
private Listener mListener;
|
||||
|
||||
public MainContext(Context applicationContext, MenuService menuService, AppDatabase appDatabase, SystemRESTConsumer systemRESTConsumer, AuthRESTConsumer authRESTConsumer) {
|
||||
public MainContext(Context applicationContext, MenuService menuService, AppDatabase appDatabase, SystemRESTConsumer systemRESTConsumer, AuthenticationRESTConsumer authenticationRESTConsumer) {
|
||||
this.applicationContext = applicationContext;
|
||||
this.menuService = menuService;
|
||||
this.appDatabase = appDatabase;
|
||||
this.systemRESTConsumer = systemRESTConsumer;
|
||||
this.authRESTConsumer = authRESTConsumer;
|
||||
this.authenticationRESTConsumer = authenticationRESTConsumer;
|
||||
}
|
||||
|
||||
public void init() {
|
||||
@ -67,7 +67,7 @@ public class MainContext {
|
||||
|
||||
|
||||
private void initAuthSession(Runnable onComplete) {
|
||||
this.authRESTConsumer.me(obj -> {
|
||||
this.authenticationRESTConsumer.me(obj -> {
|
||||
onComplete.run();
|
||||
}, ex -> {
|
||||
if (mListener != null) mListener.onError(new SpannedString(ex.getMessage()));
|
||||
|
||||
@ -2,23 +2,58 @@ package it.integry.integrywmsnative.core.rest;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.auth0.android.jwt.JWT;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import it.integry.integrywmsnative.gest.login.rest.AuthRESTConsumer;
|
||||
import it.integry.integrywmsnative.core.settings.SettingsManager;
|
||||
import it.integry.integrywmsnative.core.utility.UtilityDate;
|
||||
import it.integry.integrywmsnative.gest.login.rest.RefreshRESTConsumer;
|
||||
import okhttp3.Interceptor;
|
||||
import okhttp3.Response;
|
||||
|
||||
public class AuthInterceptor implements Interceptor {
|
||||
|
||||
private final AuthRESTConsumer authRESTConsumer;
|
||||
private final RefreshRESTConsumer refreshRESTConsumer;
|
||||
|
||||
public AuthInterceptor(AuthRESTConsumer authRESTConsumer) {
|
||||
this.authRESTConsumer = authRESTConsumer;
|
||||
public AuthInterceptor(RefreshRESTConsumer refreshRESTConsumer) {
|
||||
this.refreshRESTConsumer = refreshRESTConsumer;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Response intercept(@NonNull Chain chain) throws IOException {
|
||||
return null;
|
||||
var originalRequest = chain.request();
|
||||
var accessToken = SettingsManager.i().getUserSession().getAccessToken();
|
||||
var accessTokenExpiryDate = SettingsManager.i().getUserSession().getAccessTokenExpiryDate();
|
||||
|
||||
if(accessToken != null && (accessTokenExpiryDate == null || UtilityDate.getNowTime().isAfter(accessTokenExpiryDate))) {
|
||||
|
||||
// Make the token refresh request
|
||||
try {
|
||||
var response = refreshRESTConsumer.makeSynchronousRefreshTokenRequest();
|
||||
|
||||
JWT jwt = new JWT(response.getAccessToken());
|
||||
|
||||
SettingsManager.i().getUserSession()
|
||||
.setAccessToken(response.getAccessToken())
|
||||
.setAccessTokenExpiryDate(UtilityDate.toLocalDateTime(jwt.getExpiresAt()))
|
||||
.setRefreshToken(response.getRefreshToken())
|
||||
.setRefreshTokenExpiryDate(response.getExpiryDate());
|
||||
|
||||
SettingsManager.update();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
|
||||
// Add the access token to the request header
|
||||
var authorizedRequest = originalRequest.newBuilder()
|
||||
.header("Authorization", "Bearer " + SettingsManager.i().getUserSession().getAccessToken())
|
||||
.build();
|
||||
|
||||
return chain.proceed(authorizedRequest);
|
||||
} else
|
||||
return chain.proceed(originalRequest);
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@ package it.integry.integrywmsnative.core.rest;
|
||||
import java.io.IOException;
|
||||
|
||||
import it.integry.integrywmsnative.core.settings.SettingsManager;
|
||||
import it.integry.integrywmsnative.core.utility.UtilityDate;
|
||||
import okhttp3.HttpUrl;
|
||||
import okhttp3.Interceptor;
|
||||
import okhttp3.Request;
|
||||
@ -18,19 +19,30 @@ public class HttpInterceptor implements Interceptor {
|
||||
public Response intercept(Chain chain) throws IOException {
|
||||
|
||||
final String PROFILE_DB = SettingsManager.i().getUserSession() == null ? null : SettingsManager.i().getUserSession().getProfileDB();
|
||||
final String ACCESS_TOKEN = SettingsManager.i().getUserSession().getAccessToken();
|
||||
final String APP_TOKEN = "fa3a21af-606b-4129-a22b-aedc2a52c7b6";
|
||||
|
||||
String accessToken = null;
|
||||
|
||||
if(SettingsManager.i().getUserSession().getAccessTokenExpiryDate() != null &&
|
||||
UtilityDate.getNowTime().isBefore(SettingsManager.i().getUserSession().getAccessTokenExpiryDate())) {
|
||||
accessToken = SettingsManager.i().getUserSession().getAccessToken();
|
||||
}
|
||||
|
||||
final Request request = chain.request();
|
||||
final HttpUrl url = request.url().newBuilder()
|
||||
.addQueryParameter("profileDb", PROFILE_DB)
|
||||
.build();
|
||||
|
||||
final Request newRequest = chain.request().newBuilder()
|
||||
.addHeader("Authorization", "Bearer " + ACCESS_TOKEN)
|
||||
Request.Builder builder = chain.request().newBuilder()
|
||||
.addHeader("Content-Type", "application/json")
|
||||
.addHeader("Accept", "*/*")
|
||||
.addHeader("x-app-token", APP_TOKEN)
|
||||
.addHeader("x-app-token", APP_TOKEN);
|
||||
|
||||
if(accessToken != null)
|
||||
builder
|
||||
.addHeader("Authorization", "Bearer " + accessToken);
|
||||
|
||||
var newRequest = builder
|
||||
.url(url)
|
||||
.build();
|
||||
|
||||
|
||||
@ -24,7 +24,12 @@ import retrofit2.converter.gson.GsonConverterFactory;
|
||||
@Singleton
|
||||
public class RESTBuilder {
|
||||
|
||||
private static final boolean ADD_LOGGER_INTERCEPTOR = false;
|
||||
private final boolean ADD_LOGGER_INTERCEPTOR = false;
|
||||
private final AuthInterceptor authInterceptor;
|
||||
|
||||
public RESTBuilder(AuthInterceptor authInterceptor) {
|
||||
this.authInterceptor = authInterceptor;
|
||||
}
|
||||
|
||||
public <T> T getService(final Class<T> service) {
|
||||
return getService(service, SettingsManager.i().getServer().getProtocol(), SettingsManager.i().getServer().getHost(), SettingsManager.i().getServer().getPort(), true);
|
||||
@ -53,6 +58,7 @@ public class RESTBuilder {
|
||||
|
||||
clientBuilder.retryOnConnectionFailure(true);
|
||||
|
||||
clientBuilder.addInterceptor(authInterceptor);
|
||||
if (addInterceptors) clientBuilder.addInterceptor(new HttpInterceptor());
|
||||
if (ADD_LOGGER_INTERCEPTOR) clientBuilder.addInterceptor(new HttpLoggerInterceptor());
|
||||
|
||||
|
||||
@ -80,6 +80,7 @@ public class SettingsModel {
|
||||
public static class UserSession {
|
||||
|
||||
private String accessToken;
|
||||
private LocalDateTime accessTokenExpiryDate;
|
||||
private String refreshToken;
|
||||
private LocalDateTime refreshTokenExpiryDate;
|
||||
private String deviceId;
|
||||
@ -159,6 +160,15 @@ public class SettingsModel {
|
||||
this.defaultOrdinamentoPickingAccettazioneBolle = defaultOrdinamentoPickingAccettazioneBolle;
|
||||
return this;
|
||||
}
|
||||
|
||||
public LocalDateTime getAccessTokenExpiryDate() {
|
||||
return accessTokenExpiryDate;
|
||||
}
|
||||
|
||||
public UserSession setAccessTokenExpiryDate(LocalDateTime accessTokenExpiryDate) {
|
||||
this.accessTokenExpiryDate = accessTokenExpiryDate;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
public Server getServer() {
|
||||
|
||||
@ -1,12 +1,16 @@
|
||||
package it.integry.integrywmsnative.gest.login;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
import it.integry.integrywmsnative.core.rest.AuthInterceptor;
|
||||
import it.integry.integrywmsnative.core.rest.RESTBuilder;
|
||||
import it.integry.integrywmsnative.core.rest.consumers.SystemRESTConsumer;
|
||||
import it.integry.integrywmsnative.gest.login.rest.AuthRESTConsumer;
|
||||
import it.integry.integrywmsnative.gest.login.rest.AuthenticationRESTConsumer;
|
||||
import it.integry.integrywmsnative.gest.login.rest.RefreshRESTConsumer;
|
||||
import it.integry.integrywmsnative.gest.login.viewmodel.LoginViewModel;
|
||||
|
||||
@Module(subcomponents = LoginComponent.class)
|
||||
@ -14,13 +18,25 @@ public class LoginModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
AuthRESTConsumer provideLoginRESTConsumer(RESTBuilder restBuilder) {
|
||||
return new AuthRESTConsumer(restBuilder);
|
||||
AuthenticationRESTConsumer provideAuthenticationRESTConsumer(RESTBuilder restBuilder) {
|
||||
return new AuthenticationRESTConsumer(restBuilder);
|
||||
}
|
||||
|
||||
@Provides
|
||||
LoginViewModel providesSpedizioneViewModel(SystemRESTConsumer systemRESTConsumer, AuthRESTConsumer authRESTConsumer) {
|
||||
return new LoginViewModel(systemRESTConsumer, authRESTConsumer);
|
||||
@Singleton
|
||||
AuthInterceptor provideAuthInterceptor(RefreshRESTConsumer refreshRESTConsumer) {
|
||||
return new AuthInterceptor(refreshRESTConsumer);
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
RefreshRESTConsumer provideRefreshRESTConsumer(ExecutorService executorService) {
|
||||
return new RefreshRESTConsumer(executorService);
|
||||
}
|
||||
|
||||
@Provides
|
||||
LoginViewModel providesLoginViewModel(SystemRESTConsumer systemRESTConsumer, AuthenticationRESTConsumer authenticationRESTConsumer) {
|
||||
return new LoginViewModel(systemRESTConsumer, authenticationRESTConsumer);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -2,7 +2,7 @@ package it.integry.integrywmsnative.gest.login.dto;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
public class LoginJwtResponseDTO {
|
||||
public class AuthenticationJwtResponseDTO {
|
||||
|
||||
private String accessToken;
|
||||
private String refreshToken;
|
||||
@ -14,7 +14,7 @@ public class LoginJwtResponseDTO {
|
||||
return accessToken;
|
||||
}
|
||||
|
||||
public LoginJwtResponseDTO setAccessToken(String accessToken) {
|
||||
public AuthenticationJwtResponseDTO setAccessToken(String accessToken) {
|
||||
this.accessToken = accessToken;
|
||||
return this;
|
||||
}
|
||||
@ -23,7 +23,7 @@ public class LoginJwtResponseDTO {
|
||||
return refreshToken;
|
||||
}
|
||||
|
||||
public LoginJwtResponseDTO setRefreshToken(String refreshToken) {
|
||||
public AuthenticationJwtResponseDTO setRefreshToken(String refreshToken) {
|
||||
this.refreshToken = refreshToken;
|
||||
return this;
|
||||
}
|
||||
@ -32,7 +32,7 @@ public class LoginJwtResponseDTO {
|
||||
return expiryDate;
|
||||
}
|
||||
|
||||
public LoginJwtResponseDTO setExpiryDate(LocalDateTime expiryDate) {
|
||||
public AuthenticationJwtResponseDTO setExpiryDate(LocalDateTime expiryDate) {
|
||||
this.expiryDate = expiryDate;
|
||||
return this;
|
||||
}
|
||||
@ -41,7 +41,7 @@ public class LoginJwtResponseDTO {
|
||||
return expireIn;
|
||||
}
|
||||
|
||||
public LoginJwtResponseDTO setExpireIn(long expireIn) {
|
||||
public AuthenticationJwtResponseDTO setExpireIn(long expireIn) {
|
||||
this.expireIn = expireIn;
|
||||
return this;
|
||||
}
|
||||
@ -13,21 +13,20 @@ import it.integry.integrywmsnative.core.rest.RESTBuilder;
|
||||
import it.integry.integrywmsnative.core.rest.consumers._BaseRESTConsumer;
|
||||
import it.integry.integrywmsnative.core.rest.model.ServiceRESTResponse;
|
||||
import it.integry.integrywmsnative.gest.login.dto.LoginAziendaDTO;
|
||||
import it.integry.integrywmsnative.gest.login.dto.LoginJwtResponseDTO;
|
||||
import it.integry.integrywmsnative.gest.login.dto.AuthenticationJwtResponseDTO;
|
||||
import it.integry.integrywmsnative.gest.login.dto.LoginRequestDTO;
|
||||
import it.integry.integrywmsnative.gest.login.dto.RefreshRequestDTO;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
|
||||
@Singleton
|
||||
public class AuthRESTConsumer extends _BaseRESTConsumer {
|
||||
public class AuthenticationRESTConsumer extends _BaseRESTConsumer {
|
||||
|
||||
|
||||
private final RESTBuilder restBuilder;
|
||||
|
||||
@Inject
|
||||
public AuthRESTConsumer(RESTBuilder restBuilder) {
|
||||
public AuthenticationRESTConsumer(RESTBuilder restBuilder) {
|
||||
this.restBuilder = restBuilder;
|
||||
}
|
||||
|
||||
@ -71,7 +70,7 @@ public class AuthRESTConsumer extends _BaseRESTConsumer {
|
||||
}
|
||||
|
||||
|
||||
public void authenticate(String protocol, String host, int port, String username, String password, String profileDb, String deviceSalt, RunnableArgs<LoginJwtResponseDTO> onComplete, RunnableArgs<Exception> onFailed) {
|
||||
public void authenticate(String protocol, String host, int port, String username, String password, String profileDb, String deviceSalt, RunnableArgs<AuthenticationJwtResponseDTO> onComplete, RunnableArgs<Exception> onFailed) {
|
||||
AuthenticationRESTConsumerService service = restBuilder.getService(AuthenticationRESTConsumerService.class, protocol, host, port, false);
|
||||
|
||||
LoginRequestDTO loginRequestDTO = new LoginRequestDTO()
|
||||
@ -85,12 +84,12 @@ public class AuthRESTConsumer extends _BaseRESTConsumer {
|
||||
.enqueue(new Callback<>() {
|
||||
|
||||
@Override
|
||||
public void onResponse(Call<ServiceRESTResponse<LoginJwtResponseDTO>> call, Response<ServiceRESTResponse<LoginJwtResponseDTO>> response) {
|
||||
public void onResponse(Call<ServiceRESTResponse<AuthenticationJwtResponseDTO>> call, Response<ServiceRESTResponse<AuthenticationJwtResponseDTO>> response) {
|
||||
analyzeAnswer(response, "login", onComplete, onFailed);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<ServiceRESTResponse<LoginJwtResponseDTO>> call, final Throwable t) {
|
||||
public void onFailure(Call<ServiceRESTResponse<AuthenticationJwtResponseDTO>> call, final Throwable t) {
|
||||
Log.e("login", t.toString());
|
||||
onFailed.run(new Exception(t));
|
||||
}
|
||||
@ -98,28 +97,6 @@ public class AuthRESTConsumer extends _BaseRESTConsumer {
|
||||
}
|
||||
|
||||
|
||||
public void refreshToken(String protocol, String host, int port, String username, String password, String profileDb, String deviceSalt, RunnableArgs<LoginJwtResponseDTO> onComplete, RunnableArgs<Exception> onFailed) {
|
||||
AuthenticationRESTConsumerService service = restBuilder.getService(AuthenticationRESTConsumerService.class);
|
||||
|
||||
RefreshRequestDTO refreshRequest = new RefreshRequestDTO()
|
||||
.setRefreshToken(username);
|
||||
|
||||
service
|
||||
.refresh(refreshRequest)
|
||||
.enqueue(new Callback<>() {
|
||||
|
||||
@Override
|
||||
public void onResponse(Call<ServiceRESTResponse<LoginJwtResponseDTO>> call, Response<ServiceRESTResponse<LoginJwtResponseDTO>> response) {
|
||||
analyzeAnswer(response, "refresh", onComplete, onFailed);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<ServiceRESTResponse<LoginJwtResponseDTO>> call, final Throwable t) {
|
||||
Log.e("refresh", t.toString());
|
||||
onFailed.run(new Exception(t));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public void retrieveAvailableProfiles(String protocol, String host, int port, String username, RunnableArgs<List<String>> onComplete, RunnableArgs<Exception> onFailed) {
|
||||
@ -5,10 +5,9 @@ import java.util.List;
|
||||
|
||||
import it.integry.integrywmsnative.core.rest.model.ServiceRESTResponse;
|
||||
import it.integry.integrywmsnative.gest.login.dto.LoginAziendaDTO;
|
||||
import it.integry.integrywmsnative.gest.login.dto.LoginJwtResponseDTO;
|
||||
import it.integry.integrywmsnative.gest.login.dto.AuthenticationJwtResponseDTO;
|
||||
import it.integry.integrywmsnative.gest.login.dto.LoginRequestDTO;
|
||||
import it.integry.integrywmsnative.gest.login.dto.LoginResponseDTO;
|
||||
import it.integry.integrywmsnative.gest.login.dto.RefreshRequestDTO;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.http.Body;
|
||||
import retrofit2.http.GET;
|
||||
@ -25,10 +24,7 @@ public interface AuthenticationRESTConsumerService {
|
||||
Call<ServiceRESTResponse<LoginResponseDTO>> login(@Body LoginRequestDTO loginRequestDTO);
|
||||
|
||||
@POST("auth/login")
|
||||
Call<ServiceRESTResponse<LoginJwtResponseDTO>> loginNew(@Body LoginRequestDTO loginRequestDTO);
|
||||
|
||||
@POST("auth/refresh")
|
||||
Call<ServiceRESTResponse<LoginJwtResponseDTO>> refresh(@Body RefreshRequestDTO refreshRequestDTO);
|
||||
Call<ServiceRESTResponse<AuthenticationJwtResponseDTO>> loginNew(@Body LoginRequestDTO loginRequestDTO);
|
||||
|
||||
@GET("auth/me")
|
||||
Call<ServiceRESTResponse<Object>> me();
|
||||
|
||||
@ -0,0 +1,95 @@
|
||||
package it.integry.integrywmsnative.gest.login.rest;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import it.integry.integrywmsnative.core.expansion.RunnableArgs;
|
||||
import it.integry.integrywmsnative.core.rest.HttpInterceptor;
|
||||
import it.integry.integrywmsnative.core.rest.RESTBuilder;
|
||||
import it.integry.integrywmsnative.core.rest.consumers._BaseRESTConsumer;
|
||||
import it.integry.integrywmsnative.core.rest.model.ServiceRESTResponse;
|
||||
import it.integry.integrywmsnative.core.settings.SettingsManager;
|
||||
import it.integry.integrywmsnative.core.utility.UtilityGson;
|
||||
import it.integry.integrywmsnative.gest.login.dto.AuthenticationJwtResponseDTO;
|
||||
import it.integry.integrywmsnative.gest.login.dto.RefreshRequestDTO;
|
||||
import okhttp3.OkHttpClient;
|
||||
import retrofit2.Response;
|
||||
import retrofit2.Retrofit;
|
||||
import retrofit2.converter.gson.GsonConverterFactory;
|
||||
|
||||
@Singleton
|
||||
public class RefreshRESTConsumer extends _BaseRESTConsumer {
|
||||
|
||||
private final ExecutorService executorService;
|
||||
|
||||
public RefreshRESTConsumer(ExecutorService executorService) {
|
||||
this.executorService = executorService;
|
||||
}
|
||||
|
||||
public AuthenticationJwtResponseDTO makeSynchronousRefreshTokenRequest() throws Exception {
|
||||
RefreshRESTConsumerService service = getService(
|
||||
RefreshRESTConsumerService.class,
|
||||
SettingsManager.i().getServer().getProtocol(),
|
||||
SettingsManager.i().getServer().getHost(),
|
||||
SettingsManager.i().getServer().getPort(),
|
||||
true,
|
||||
true,
|
||||
30);
|
||||
|
||||
RefreshRequestDTO refreshRequest = new RefreshRequestDTO()
|
||||
.setRefreshToken(SettingsManager.i().getUserSession().getRefreshToken());
|
||||
|
||||
Response<ServiceRESTResponse<AuthenticationJwtResponseDTO>> response = service
|
||||
.refresh(refreshRequest)
|
||||
.execute();
|
||||
|
||||
return analyzeAnswer(response, "refreshToken");
|
||||
}
|
||||
|
||||
public void makeRefreshTokenRequest(final Runnable onComplete, final RunnableArgs<Exception> onError) {
|
||||
executorService.execute(() -> {
|
||||
try {
|
||||
makeSynchronousRefreshTokenRequest();
|
||||
|
||||
if (onComplete != null)
|
||||
onComplete.run();
|
||||
|
||||
} catch (Exception ex) {
|
||||
if (onError != null) onError.run(ex);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private <T> T getService(final Class<T> service, String protocol, String host, int port, boolean addInterceptors, boolean addEmsApi, int timeout) {
|
||||
OkHttpClient.Builder clientBuilder = RESTBuilder.getDefaultHttpClient();
|
||||
|
||||
clientBuilder.connectTimeout(timeout, TimeUnit.SECONDS);
|
||||
clientBuilder.readTimeout(timeout, TimeUnit.SECONDS);
|
||||
clientBuilder.writeTimeout(timeout, TimeUnit.SECONDS);
|
||||
|
||||
clientBuilder.retryOnConnectionFailure(true);
|
||||
|
||||
if (addInterceptors) clientBuilder.addInterceptor(new HttpInterceptor());
|
||||
|
||||
OkHttpClient client = clientBuilder.build();
|
||||
|
||||
String endpoint = protocol + "://" + host + (port > 0 ? ":" + port : "") + "/" + (addEmsApi ? "ems-api/" : "");
|
||||
|
||||
|
||||
Gson gson = UtilityGson.createObject();
|
||||
|
||||
Retrofit retrofit = new Retrofit.Builder()
|
||||
.addConverterFactory(GsonConverterFactory.create(gson))
|
||||
.baseUrl(endpoint)
|
||||
.client(client)
|
||||
.build();
|
||||
|
||||
return retrofit.create(service);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,17 @@
|
||||
package it.integry.integrywmsnative.gest.login.rest;
|
||||
|
||||
import it.integry.integrywmsnative.core.rest.model.ServiceRESTResponse;
|
||||
import it.integry.integrywmsnative.gest.login.dto.AuthenticationJwtResponseDTO;
|
||||
import it.integry.integrywmsnative.gest.login.dto.RefreshRequestDTO;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.http.Body;
|
||||
import retrofit2.http.POST;
|
||||
|
||||
public interface RefreshRESTConsumerService {
|
||||
|
||||
|
||||
|
||||
@POST("auth/refresh")
|
||||
Call<ServiceRESTResponse<AuthenticationJwtResponseDTO>> refresh(@Body RefreshRequestDTO refreshRequestDTO);
|
||||
|
||||
}
|
||||
@ -15,25 +15,26 @@ import it.integry.integrywmsnative.core.expansion.RunnableArgsss;
|
||||
import it.integry.integrywmsnative.core.interfaces.viewmodel_listeners.ILoadingListener;
|
||||
import it.integry.integrywmsnative.core.rest.consumers.SystemRESTConsumer;
|
||||
import it.integry.integrywmsnative.core.settings.SettingsManager;
|
||||
import it.integry.integrywmsnative.core.utility.UtilityDate;
|
||||
import it.integry.integrywmsnative.core.utility.UtilityServer;
|
||||
import it.integry.integrywmsnative.core.utility.UtilityString;
|
||||
import it.integry.integrywmsnative.gest.login.dto.AuthTokenClaimsDTO;
|
||||
import it.integry.integrywmsnative.gest.login.exception.InvalidServerCodAziendaException;
|
||||
import it.integry.integrywmsnative.gest.login.exception.InvalidUserDepositException;
|
||||
import it.integry.integrywmsnative.gest.login.rest.AuthRESTConsumer;
|
||||
import it.integry.integrywmsnative.gest.login.rest.AuthenticationRESTConsumer;
|
||||
|
||||
public class LoginViewModel {
|
||||
|
||||
|
||||
private final SystemRESTConsumer mSystemRESTConsumer;
|
||||
private final AuthRESTConsumer mAuthRESTConsumer;
|
||||
private final AuthenticationRESTConsumer mAuthenticationRESTConsumer;
|
||||
|
||||
private Listener mListener;
|
||||
|
||||
@Inject
|
||||
public LoginViewModel(SystemRESTConsumer systemRESTConsumer, AuthRESTConsumer authRESTConsumer) {
|
||||
public LoginViewModel(SystemRESTConsumer systemRESTConsumer, AuthenticationRESTConsumer authenticationRESTConsumer) {
|
||||
this.mSystemRESTConsumer = systemRESTConsumer;
|
||||
this.mAuthRESTConsumer = authRESTConsumer;
|
||||
this.mAuthenticationRESTConsumer = authenticationRESTConsumer;
|
||||
}
|
||||
|
||||
|
||||
@ -77,7 +78,7 @@ public class LoginViewModel {
|
||||
|
||||
|
||||
private void retrieveServerData(String codAzienda, RunnableArgsss<String, String, Integer> onComplete) {
|
||||
mAuthRESTConsumer.retrieveServerData(codAzienda, value -> {
|
||||
mAuthenticationRESTConsumer.retrieveServerData(codAzienda, value -> {
|
||||
|
||||
final String serverEndpoint = value.getEndpointRestApi();
|
||||
|
||||
@ -102,7 +103,7 @@ public class LoginViewModel {
|
||||
}
|
||||
|
||||
private void retrieveAvailableProfiles(String protocol, String host, int port, String username, RunnableArgs<String> onComplete) {
|
||||
mAuthRESTConsumer.retrieveAvailableProfiles(protocol, host, port, username, availableProfiles -> {
|
||||
mAuthenticationRESTConsumer.retrieveAvailableProfiles(protocol, host, port, username, availableProfiles -> {
|
||||
|
||||
if (availableProfiles == null || availableProfiles.isEmpty()) {
|
||||
this.sendError(new Exception("Non è stato trovato alcun profilo per l'utente: " + username));
|
||||
@ -123,7 +124,7 @@ public class LoginViewModel {
|
||||
}
|
||||
|
||||
private void authenticate(String protocol, String host, int port, String username, String password, String profileDb, String deviceSalt, RunnableArgs<String> onComplete) {
|
||||
mAuthRESTConsumer.authenticate(protocol, host, port, username, password, profileDb, deviceSalt, sessionData -> {
|
||||
mAuthenticationRESTConsumer.authenticate(protocol, host, port, username, password, profileDb, deviceSalt, sessionData -> {
|
||||
|
||||
JWT jwt = new JWT(sessionData.getAccessToken());
|
||||
var claims = jwt.getClaims();
|
||||
@ -134,6 +135,7 @@ public class LoginViewModel {
|
||||
.setProfileDB(profileDb)
|
||||
.setDeviceId(deviceSalt)
|
||||
.setAccessToken(sessionData.getAccessToken())
|
||||
.setAccessTokenExpiryDate(UtilityDate.toLocalDateTime(jwt.getExpiresAt()))
|
||||
.setRefreshToken(sessionData.getRefreshToken())
|
||||
.setRefreshTokenExpiryDate(sessionData.getExpiryDate());
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user