use of retrofit2.adapter.rxjava.HttpException in project Varis-Android by dkhmelenko.
the class AuthPresenter method isTwoFactorAuthRequired.
private boolean isTwoFactorAuthRequired(HttpException exception) {
Response response = exception.response();
boolean twoFactorAuthRequired = false;
for (String header : response.headers().names()) {
if (GithubApiService.TWO_FACTOR_HEADER.equals(header)) {
twoFactorAuthRequired = true;
break;
}
}
return response.code() == HttpURLConnection.HTTP_UNAUTHORIZED && twoFactorAuthRequired;
}
use of retrofit2.adapter.rxjava.HttpException in project Varis-Android by dkhmelenko.
the class TestAuthPresenter method testTwoFactorAuth.
@Test
public void testTwoFactorAuth() {
final String login = "login";
final String password = "password";
String auth = EncryptionUtils.generateBasicAuthorization(login, password);
// rules for throwing a request for 2-factor auth
final String expectedUrl = "https://sample.org";
Request rawRequest = new Request.Builder().url(expectedUrl).build();
okhttp3.Response rawResponse = new okhttp3.Response.Builder().request(rawRequest).message("no body").protocol(Protocol.HTTP_1_1).code(401).header(GithubApiService.TWO_FACTOR_HEADER, "required").build();
Response response = Response.error(ResponseBody.create(null, ""), rawResponse);
HttpException exception = new HttpException(response);
when(mGitHubRestClient.getApiService().createNewAuthorization(eq(auth), any(AuthorizationRequest.class))).thenReturn(Single.error(exception));
mAuthPresenter.login(login, password);
verify(mAuthView).showTwoFactorAuth();
// rules for handling 2-factor auth continuation
final String securityCode = "123456";
final String gitHubToken = "gitHubToken";
Authorization authorization = new Authorization();
authorization.setToken(gitHubToken);
authorization.setId(1L);
when(mGitHubRestClient.getApiService().createNewAuthorization(eq(auth), eq(securityCode), any(AuthorizationRequest.class))).thenReturn(Single.just(authorization));
final String accessToken = "token";
AccessTokenRequest request = new AccessTokenRequest();
request.setGithubToken(gitHubToken);
AccessToken token = new AccessToken();
token.setAccessToken(accessToken);
when(mTravisRestClient.getApiService().auth(request)).thenReturn(Single.just(token));
when(mGitHubRestClient.getApiService().deleteAuthorization(auth, String.valueOf(authorization.getId()))).thenReturn(null);
mAuthPresenter.twoFactorAuth(securityCode);
verify(mAuthView, times(2)).hideProgress();
verify(mAuthView).finishView();
}
use of retrofit2.adapter.rxjava.HttpException in project Varis-Android by dkhmelenko.
the class AuthPresenter method isTwoFactorAuthRequired.
private boolean isTwoFactorAuthRequired(HttpException exception) {
Response response = exception.response();
boolean twoFactorAuthRequired = false;
for (String header : response.headers().names()) {
if (GithubApiService.TWO_FACTOR_HEADER.equals(header)) {
twoFactorAuthRequired = true;
break;
}
}
return response.code() == HttpURLConnection.HTTP_UNAUTHORIZED && twoFactorAuthRequired;
}
use of retrofit2.adapter.rxjava.HttpException in project Varis-Android by dkhmelenko.
the class AuthPresenter method doLogin.
private void doLogin(Single<Authorization> authorizationJob) {
Disposable subscription = authorizationJob.flatMap(this::doAuthorization).doOnSuccess(this::saveAccessToken).doAfterSuccess(accessToken -> cleanUpAfterAuthorization()).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe((authorization, throwable) -> {
getView().hideProgress();
if (throwable == null) {
getView().finishView();
} else {
if (throwable instanceof HttpException && isTwoFactorAuthRequired((HttpException) throwable)) {
mSecurityCodeInput = true;
getView().showTwoFactorAuth();
} else {
getView().showErrorMessage(throwable.getMessage());
}
}
});
mSubscriptions.add(subscription);
}
use of retrofit2.adapter.rxjava.HttpException in project SmartCampus by Vegen.
the class HttpError method getErrorCode.
public static int getErrorCode(Throwable throwable) {
if (throwable instanceof HttpException) {
try {
HttpException httpException = (HttpException) throwable;
String errorMsg = httpException.response().errorBody().string();
HashMap data = new Gson().fromJson(errorMsg, HashMap.class);
if (data.containsKey("msg")) {
return ((Double) data.get("code")).intValue();
} else {
return -1;
}
} catch (IOException e) {
LogUtils.e(e.getMessage());
} catch (JsonSyntaxException e) {
LogUtils.e(e.getMessage());
return -1;
} catch (Exception e) {
return -1;
}
}
return -1;
}
Aggregations