Search in sources :

Example 1 with AccessToken

use of com.khmelenko.lab.varis.network.response.AccessToken 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 {
            HttpException httpException = (HttpException) throwable;
            if (isTwoFactorAuthRequired(httpException)) {
                mSecurityCodeInput = true;
                getView().showTwoFactorAuth();
            } else {
                getView().showErrorMessage(throwable.getMessage());
            }
        }
    });
    mSubscriptions.add(subscription);
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) HttpURLConnection(java.net.HttpURLConnection) AccessToken(com.khmelenko.lab.varis.network.response.AccessToken) AppSettings(com.khmelenko.lab.varis.storage.AppSettings) Arrays(java.util.Arrays) HttpException(retrofit2.HttpException) TextUtils(android.text.TextUtils) Response(retrofit2.Response) MvpPresenter(com.khmelenko.lab.varis.mvp.MvpPresenter) GithubApiService(com.khmelenko.lab.varis.network.retrofit.github.GithubApiService) Single(io.reactivex.Single) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) EncryptionUtils(com.khmelenko.lab.varis.util.EncryptionUtils) Inject(javax.inject.Inject) List(java.util.List) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) TravisRestClient(com.khmelenko.lab.varis.network.retrofit.travis.TravisRestClient) StringUtils(com.khmelenko.lab.varis.util.StringUtils) AuthView(com.khmelenko.lab.varis.view.AuthView) AccessTokenRequest(com.khmelenko.lab.varis.network.request.AccessTokenRequest) AuthorizationRequest(com.khmelenko.lab.varis.network.request.AuthorizationRequest) Schedulers(io.reactivex.schedulers.Schedulers) Authorization(com.khmelenko.lab.varis.network.response.Authorization) GitHubRestClient(com.khmelenko.lab.varis.network.retrofit.github.GitHubRestClient) HttpException(retrofit2.HttpException)

Example 2 with AccessToken

use of com.khmelenko.lab.varis.network.response.AccessToken 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
    Header header = new Header(GithubApiService.TWO_FACTOR_HEADER, "required");
    List<Header> headers = new ArrayList<>();
    headers.add(header);
    Response response = new Response("https://github.com", 401, "twoFactorAuth", headers, null);
    TaskError taskError = new TaskError(401, "twoFactorAuth");
    taskError.setResponse(response);
    TaskException exception = new TaskException(taskError);
    when(mGitHubRestClient.getApiService().createNewAuthorization(eq(auth), any(AuthorizationRequest.class))).thenThrow(exception);
    mAuthPresenter.login(login, password);
    verify(mTaskManager).createNewAuthorization(eq(auth), any(AuthorizationRequest.class));
    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(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(token);
    when(mGitHubRestClient.getApiService().deleteAuthorization(auth, String.valueOf(authorization.getId()))).thenReturn(null);
    mAuthPresenter.twoFactorAuth(securityCode);
    verify(mTaskManager).startAuth(gitHubToken);
    verify(mTaskManager).deleteAuthorization(auth, String.valueOf(authorization.getId()), securityCode);
    verify(mAuthView, times(2)).hideProgress();
    verify(mAuthView).finishView();
}
Also used : Response(retrofit.client.Response) Authorization(com.khmelenko.lab.varis.network.response.Authorization) AuthorizationRequest(com.khmelenko.lab.varis.network.request.AuthorizationRequest) Header(retrofit.client.Header) TaskException(com.khmelenko.lab.varis.task.TaskException) AccessToken(com.khmelenko.lab.varis.network.response.AccessToken) ArrayList(java.util.ArrayList) TaskError(com.khmelenko.lab.varis.task.TaskError) AccessTokenRequest(com.khmelenko.lab.varis.network.request.AccessTokenRequest) Test(org.junit.Test)

Example 3 with AccessToken

use of com.khmelenko.lab.varis.network.response.AccessToken in project Varis-Android by dkhmelenko.

the class TestAuthPresenter method testLoginSuccess.

@Test
public void testLoginSuccess() {
    final String login = "login";
    final String password = "password";
    String auth = EncryptionUtils.generateBasicAuthorization(login, password);
    final String gitHubToken = "gitHubToken";
    Authorization authorization = new Authorization();
    authorization.setToken(gitHubToken);
    authorization.setId(1L);
    when(mGitHubRestClient.getApiService().createNewAuthorization(eq(auth), any(AuthorizationRequest.class))).thenReturn(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(token);
    when(mGitHubRestClient.getApiService().deleteAuthorization(auth, String.valueOf(authorization.getId()))).thenReturn(null);
    mAuthPresenter.login(login, password);
    verify(mTaskManager).createNewAuthorization(eq(auth), any(AuthorizationRequest.class));
    verify(mTaskManager).startAuth(gitHubToken);
    verify(mTaskManager).deleteAuthorization(auth, String.valueOf(authorization.getId()));
    verify(mAuthView).hideProgress();
    verify(mAuthView).finishView();
}
Also used : Authorization(com.khmelenko.lab.varis.network.response.Authorization) AuthorizationRequest(com.khmelenko.lab.varis.network.request.AuthorizationRequest) AccessToken(com.khmelenko.lab.varis.network.response.AccessToken) AccessTokenRequest(com.khmelenko.lab.varis.network.request.AccessTokenRequest) Test(org.junit.Test)

Aggregations

AccessTokenRequest (com.khmelenko.lab.varis.network.request.AccessTokenRequest)3 AuthorizationRequest (com.khmelenko.lab.varis.network.request.AuthorizationRequest)3 AccessToken (com.khmelenko.lab.varis.network.response.AccessToken)3 Authorization (com.khmelenko.lab.varis.network.response.Authorization)3 Test (org.junit.Test)2 TextUtils (android.text.TextUtils)1 MvpPresenter (com.khmelenko.lab.varis.mvp.MvpPresenter)1 GitHubRestClient (com.khmelenko.lab.varis.network.retrofit.github.GitHubRestClient)1 GithubApiService (com.khmelenko.lab.varis.network.retrofit.github.GithubApiService)1 TravisRestClient (com.khmelenko.lab.varis.network.retrofit.travis.TravisRestClient)1 AppSettings (com.khmelenko.lab.varis.storage.AppSettings)1 TaskError (com.khmelenko.lab.varis.task.TaskError)1 TaskException (com.khmelenko.lab.varis.task.TaskException)1 EncryptionUtils (com.khmelenko.lab.varis.util.EncryptionUtils)1 StringUtils (com.khmelenko.lab.varis.util.StringUtils)1 AuthView (com.khmelenko.lab.varis.view.AuthView)1 Single (io.reactivex.Single)1 AndroidSchedulers (io.reactivex.android.schedulers.AndroidSchedulers)1 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)1 Disposable (io.reactivex.disposables.Disposable)1