Search in sources :

Example 1 with AuthorizedRequest

use of com.okta.oidc.net.request.AuthorizedRequest in project okta-oidc-android by okta.

the class SyncSessionClientImplTest method userProfileRequest.

@Test
public void userProfileRequest() throws InterruptedException, JSONException, AuthorizationException, OktaRepository.EncryptionException {
    mOktaState.save(mTokenResponse);
    mEndPoint.enqueueUserInfoSuccess();
    AuthorizedRequest request = mSyncSessionClientImpl.userProfileRequest(mOktaState.getProviderConfiguration(), mTokenResponse);
    JSONObject result = request.executeRequest(mHttpClient);
    RecordedRequest recordedRequest = mEndPoint.takeRequest();
    assertThat(recordedRequest.getHeader("Authorization"), is("Bearer " + ACCESS_TOKEN));
    assertThat(recordedRequest.getHeader("Accept"), is(ConnectionParameters.JSON_CONTENT_TYPE));
    assertThat(recordedRequest.getPath(), equalTo("/userinfo"));
    assertNotNull(result);
    assertEquals("John Doe", result.getString("name"));
    assertEquals("Jimmy", result.getString("nickname"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) AuthorizedRequest(com.okta.oidc.net.request.AuthorizedRequest) JSONObject(org.json.JSONObject) Test(org.junit.Test)

Example 2 with AuthorizedRequest

use of com.okta.oidc.net.request.AuthorizedRequest in project okta-oidc-android by okta.

the class SyncSessionClientImplTest method userProfileRequestFailure.

@Test
public void userProfileRequestFailure() throws InterruptedException, AuthorizationException, OktaRepository.EncryptionException {
    mOktaState.save(mTokenResponse);
    mExpectedEx.expect(AuthorizationException.class);
    mEndPoint.enqueueReturnUnauthorizedRevoked();
    AuthorizedRequest request = mSyncSessionClientImpl.userProfileRequest(mOktaState.getProviderConfiguration(), mTokenResponse);
    JSONObject result = request.executeRequest(mHttpClient);
    RecordedRequest recordedRequest = mEndPoint.takeRequest();
    assertNull(result);
    assertThat(recordedRequest.getPath(), equalTo("/userinfo"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) AuthorizedRequest(com.okta.oidc.net.request.AuthorizedRequest) JSONObject(org.json.JSONObject) Test(org.junit.Test)

Example 3 with AuthorizedRequest

use of com.okta.oidc.net.request.AuthorizedRequest in project okta-oidc-android by okta.

the class SyncSessionClientImplTest method authorizedRequestFailure.

@Test
public void authorizedRequestFailure() throws InterruptedException, AuthorizationException, OktaRepository.EncryptionException {
    // use userinfo for generic authorized request
    mOktaState.save(mTokenResponse);
    mExpectedEx.expect(AuthorizationException.class);
    mEndPoint.enqueueReturnUnauthorizedRevoked();
    Uri uri = Uri.parse(mProviderConfig.userinfo_endpoint);
    HashMap<String, String> properties = new HashMap<>();
    properties.put("state", CUSTOM_STATE);
    AuthorizedRequest request = mSyncSessionClientImpl.createAuthorizedRequest(uri, properties, null, ConnectionParameters.RequestMethod.GET, mOktaState.getProviderConfiguration(), mTokenResponse);
    JSONObject result = request.executeRequest(mHttpClient);
    RecordedRequest recordedRequest = mEndPoint.takeRequest();
    assertThat(recordedRequest.getHeader("state"), is(CUSTOM_STATE));
    assertThat(recordedRequest.getHeader("Authorization"), is("Bearer " + ACCESS_TOKEN));
    assertThat(recordedRequest.getHeader("Accept"), is(ConnectionParameters.JSON_CONTENT_TYPE));
    assertThat(recordedRequest.getPath(), equalTo("/userinfo"));
    assertNull(result);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) AuthorizedRequest(com.okta.oidc.net.request.AuthorizedRequest) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) Uri(android.net.Uri) Test(org.junit.Test)

Example 4 with AuthorizedRequest

use of com.okta.oidc.net.request.AuthorizedRequest in project okta-oidc-android by okta.

the class SyncSessionClientImpl method getUserProfile.

@Override
public UserInfo getUserProfile() throws AuthorizationException {
    try {
        ProviderConfiguration providerConfiguration = mOktaState.getProviderConfiguration();
        TokenResponse tokenResponse = mOktaState.getTokenResponse();
        AuthorizedRequest request = userProfileRequest(providerConfiguration, tokenResponse);
        JSONObject userInfo = request.executeRequest(mHttpClient);
        mCurrentRequest.set(new WeakReference<>(request));
        return new UserInfo(userInfo);
    } catch (OktaRepository.EncryptionException e) {
        throw AuthorizationException.EncryptionErrors.byEncryptionException(e);
    }
}
Also used : AuthorizedRequest(com.okta.oidc.net.request.AuthorizedRequest) TokenResponse(com.okta.oidc.net.response.TokenResponse) JSONObject(org.json.JSONObject) OktaRepository(com.okta.oidc.storage.OktaRepository) UserInfo(com.okta.oidc.net.response.UserInfo) ProviderConfiguration(com.okta.oidc.net.request.ProviderConfiguration)

Example 5 with AuthorizedRequest

use of com.okta.oidc.net.request.AuthorizedRequest in project okta-oidc-android by okta.

the class SyncSessionClientImpl method authorizedRequest.

public JSONObject authorizedRequest(@NonNull Uri uri, @Nullable Map<String, String> properties, @Nullable Map<String, String> postParameters, @NonNull ConnectionParameters.RequestMethod method) throws AuthorizationException {
    try {
        ProviderConfiguration providerConfiguration = mOktaState.getProviderConfiguration();
        TokenResponse tokenResponse = mOktaState.getTokenResponse();
        AuthorizedRequest request = createAuthorizedRequest(uri, properties, postParameters, method, providerConfiguration, tokenResponse);
        mCurrentRequest.set(new WeakReference<>(request));
        return request.executeRequest(mHttpClient);
    } catch (OktaRepository.EncryptionException e) {
        throw AuthorizationException.EncryptionErrors.byEncryptionException(e);
    }
}
Also used : AuthorizedRequest(com.okta.oidc.net.request.AuthorizedRequest) TokenResponse(com.okta.oidc.net.response.TokenResponse) OktaRepository(com.okta.oidc.storage.OktaRepository) ProviderConfiguration(com.okta.oidc.net.request.ProviderConfiguration)

Aggregations

AuthorizedRequest (com.okta.oidc.net.request.AuthorizedRequest)7 JSONObject (org.json.JSONObject)5 Test (org.junit.Test)5 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)4 Uri (android.net.Uri)2 ProviderConfiguration (com.okta.oidc.net.request.ProviderConfiguration)2 TokenResponse (com.okta.oidc.net.response.TokenResponse)2 OktaRepository (com.okta.oidc.storage.OktaRepository)2 HashMap (java.util.HashMap)2 OIDCConfig (com.okta.oidc.OIDCConfig)1 SyncWebAuthClient (com.okta.oidc.clients.web.SyncWebAuthClient)1 UserInfo (com.okta.oidc.net.response.UserInfo)1 SharedPreferenceStorage (com.okta.oidc.storage.SharedPreferenceStorage)1 EncryptionManagerStub (com.okta.oidc.util.EncryptionManagerStub)1