Search in sources :

Example 1 with AuthenticatedUserModel

use of org.hisp.dhis.android.core.user.AuthenticatedUserModel in project dhis2-android-sdk by dhis2.

the class BasicAuthenticator method intercept.

@Override
public Response intercept(Chain chain) throws IOException {
    String authorizationHeader = chain.request().header(AUTHORIZATION);
    if (authorizationHeader != null) {
        // authorization header has already been set
        return chain.proceed(chain.request());
    }
    List<AuthenticatedUserModel> authenticatedUsers = authenticatedUserStore.query();
    if (authenticatedUsers.isEmpty()) {
        // have any users authenticated
        return chain.proceed(chain.request());
    }
    // retrieve first user and pass in his / her credentials
    Request request = chain.request().newBuilder().addHeader(AUTHORIZATION, String.format(Locale.US, BASIC_CREDENTIALS, authenticatedUsers.get(0).credentials())).build();
    return chain.proceed(request);
}
Also used : AuthenticatedUserModel(org.hisp.dhis.android.core.user.AuthenticatedUserModel) Request(okhttp3.Request)

Example 2 with AuthenticatedUserModel

use of org.hisp.dhis.android.core.user.AuthenticatedUserModel in project dhis2-android-sdk by dhis2.

the class BasicAuthenticatorShould method return_null_when_server_take_request_with_authenticate_with_empty_list.

@Test
public void return_null_when_server_take_request_with_authenticate_with_empty_list() throws IOException, InterruptedException {
    when(authenticatedUserStore.query()).thenReturn(new ArrayList<AuthenticatedUserModel>());
    okHttpClient.newCall(new Request.Builder().url(mockWebServer.url("/api/me/")).build()).execute();
    RecordedRequest recordedRequest = mockWebServer.takeRequest();
    assertThat(recordedRequest.getHeader("Authorization")).isNull();
}
Also used : AuthenticatedUserModel(org.hisp.dhis.android.core.user.AuthenticatedUserModel) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 3 with AuthenticatedUserModel

use of org.hisp.dhis.android.core.user.AuthenticatedUserModel in project dhis2-android-sdk by dhis2.

the class BasicAuthenticatorShould method return_test_and_user_when_server_take_request.

@Test
public void return_test_and_user_when_server_take_request() throws IOException, InterruptedException {
    AuthenticatedUserModel authenticatedUserModel = AuthenticatedUserModel.builder().user("test_user").credentials(base64("test_user", "test_password")).build();
    when(authenticatedUserStore.query()).thenReturn(Arrays.asList(authenticatedUserModel));
    okHttpClient.newCall(new Request.Builder().url(mockWebServer.url("/api/me/")).build()).execute();
    RecordedRequest recordedRequest = mockWebServer.takeRequest();
    assertThat(recordedRequest.getHeader("Authorization")).isEqualTo(basic("test_user", "test_password"));
}
Also used : AuthenticatedUserModel(org.hisp.dhis.android.core.user.AuthenticatedUserModel) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Aggregations

AuthenticatedUserModel (org.hisp.dhis.android.core.user.AuthenticatedUserModel)3 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2 Test (org.junit.Test)2 Request (okhttp3.Request)1