use of com.okta.oidc.RequestCallback in project okta-oidc-android by okta.
the class SampleActivity method onSignIn.
@Override
public void onSignIn(String username, String password) {
mSignInDialog.dismiss();
if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
mTvStatus.setText("Invalid username or password");
return;
}
showNetworkProgress(true);
mExecutor.submit(() -> {
try {
if (mAuthenticationClient == null) {
return;
}
mAuthenticationClient.authenticate(username, password.toCharArray(), null, new AuthenticationStateHandlerAdapter() {
@Override
public void handleUnknown(AuthenticationResponse authenticationResponse) {
SampleActivity.this.runOnUiThread(() -> {
showNetworkProgress(false);
mTvStatus.setText(authenticationResponse.getStatus().name());
});
}
@Override
public void handleLockedOut(AuthenticationResponse lockedOut) {
SampleActivity.this.runOnUiThread(() -> {
showNetworkProgress(false);
mTvStatus.setText("Account locked out");
});
}
@Override
public void handleSuccess(AuthenticationResponse successResponse) {
String sessionToken = successResponse.getSessionToken();
mAuthClient.signIn(sessionToken, mPayload, new RequestCallback<Result, AuthorizationException>() {
@Override
public void onSuccess(@NonNull Result result) {
mTvStatus.setText("authentication authorized");
mIsSessionSignIn = true;
showAuthenticatedMode();
showNetworkProgress(false);
}
@Override
public void onError(String error, AuthorizationException exception) {
mTvStatus.setText(error);
}
});
}
});
} catch (AuthenticationException e) {
Log.e(TAG, Log.getStackTraceString(e));
runOnUiThread(() -> {
showNetworkProgress(false);
mTvStatus.setText(e.getMessage());
});
}
});
}
use of com.okta.oidc.RequestCallback in project okta-oidc-android by okta.
the class SessionClientImpl method refreshToken.
public void refreshToken(final RequestCallback<Tokens, AuthorizationException> cb) {
// Wrap the callback from the app because we want to be consistent in
// returning a Tokens object instead of a TokenResponse.
boolean isEmpty;
if (Thread.holdsLock(refreshTokenRequestCallbacks)) {
throw new RuntimeException("refreshToken can't be called from callback.");
}
CallbackWrapper<Tokens, AuthorizationException> wrapper = new CallbackWrapper<>(cb);
synchronized (refreshTokenRequestCallbacks) {
isEmpty = refreshTokenRequestCallbacks.isEmpty();
refreshTokenRequestCallbacks.add(wrapper);
}
if (isEmpty) {
executeSerial(wrapper, () -> {
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
try {
Tokens result = mSyncSessionClient.refreshToken();
mDispatcher.submitResults(() -> {
synchronized (refreshTokenRequestCallbacks) {
for (RequestCallback<Tokens, AuthorizationException> callback : refreshTokenRequestCallbacks) {
callback.onSuccess(result);
}
refreshTokenRequestCallbacks.clear();
}
});
} catch (AuthorizationException ae) {
mDispatcher.submitResults(() -> {
synchronized (refreshTokenRequestCallbacks) {
for (RequestCallback<Tokens, AuthorizationException> callback : refreshTokenRequestCallbacks) {
callback.onError(ae.error, ae);
}
refreshTokenRequestCallbacks.clear();
}
});
} catch (Exception ex) {
mDispatcher.submitResults(() -> {
synchronized (refreshTokenRequestCallbacks) {
for (RequestCallback<Tokens, AuthorizationException> callback : refreshTokenRequestCallbacks) {
callback.onError(ex.getMessage(), new AuthorizationException(ex.getMessage(), ex));
}
refreshTokenRequestCallbacks.clear();
}
});
}
});
}
}
use of com.okta.oidc.RequestCallback in project okta-oidc-android by okta.
the class SessionClientImplTest method refreshTokenFailureInParallelCallbackCycle.
@Test
public void refreshTokenFailureInParallelCallbackCycle() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
String nonce = CodeVerifierUtil.generateRandomState();
String jws = TestValues.getJwt(mEndPoint.getUrl(), nonce, mConfig.getClientId());
mEndPoint.enqueueTokenSuccess(jws);
MockRequestCallback<Tokens, AuthorizationException> errorCb = new MockRequestCallback<>(latch);
RequestCallback<Tokens, AuthorizationException> cb = new RequestCallback<Tokens, AuthorizationException>() {
@Override
public void onSuccess(@NonNull Tokens result) {
try {
mSessionClient.refreshToken(errorCb);
} catch (RuntimeException runtimeException) {
assertEquals(runtimeException.getMessage(), "refreshToken can't be called from callback.");
latch.countDown();
}
}
@Override
public void onError(String error, AuthorizationException exception) {
fail();
}
};
mSessionClient.refreshToken(cb);
latch.await();
}
Aggregations