Search in sources :

Example 1 with LoginService

use of com.gh4a.ServiceFactory.LoginService in project gh4a by slapperwan.

the class UserPasswordLoginDialogFragment method makeRequestSingle.

private Single<LoginService.AuthorizationRequest> makeRequestSingle() {
    String description = "Octodroid - " + Build.MANUFACTURER + " " + Build.MODEL;
    String fingerprint = getHashedDeviceId();
    LoginService service = getService();
    String scopes = getArguments().getString("scopes");
    return service.getAuthorizations().map(ApiHelpers::throwOnFailure).compose(RxUtils::doInBackground).retryWhen(handler -> handler.flatMap(error -> {
        if (error instanceof ApiRequestException) {
            ApiRequestException are = (ApiRequestException) error;
            if (are.getStatus() == 401 && are.getResponse().message().contains("OTP code")) {
                mWaitingForOtpCode = true;
                mHandler.post(() -> updateContainerVisibility(false));
                // getAuthorizations() doesn't trigger the OTP SMS for whatever reason,
                // so make a dummy create request (which we know will fail) just to
                // actually trigger SMS sending
                LoginService.AuthorizationRequest dummyRequest = new LoginService.AuthorizationRequest("", "dummy", "");
                service.createAuthorization(dummyRequest).compose(RxUtils::doInBackground).subscribe(ignoredResponse -> {
                }, ignoredError -> {
                });
            }
        }
        if (!mWaitingForOtpCode) {
            mRetryProcessor.onError(error);
        }
        return mRetryProcessor;
    })).compose(RxUtils.filter(authorization -> {
        String note = authorization.note();
        return note != null && note.startsWith(description);
    })).flatMap(existingAuthorizations -> {
        Single<Void> deleteSingle = null;
        Iterator<LoginService.AuthorizationResponse> iter = existingAuthorizations.iterator();
        while (iter.hasNext()) {
            LoginService.AuthorizationResponse auth = iter.next();
            if (fingerprint.equals(auth.fingerprint())) {
                deleteSingle = service.deleteAuthorization(auth.id()).map(ApiHelpers::throwOnFailure).compose(RxUtils::doInBackground);
                iter.remove();
            }
        }
        String finalDescription = description;
        if (!existingAuthorizations.isEmpty()) {
            finalDescription += " #" + (existingAuthorizations.size() + 1);
        }
        LoginService.AuthorizationRequest request = new LoginService.AuthorizationRequest(scopes, finalDescription, fingerprint);
        if (deleteSingle != null) {
            return deleteSingle.map(response -> request);
        } else {
            return Single.just(request);
        }
    });
}
Also used : Context(android.content.Context) Bundle(android.os.Bundle) MessageDigest(java.security.MessageDigest) Dialog(android.app.Dialog) NonNull(android.support.annotation.NonNull) DialogFragment(android.support.v4.app.DialogFragment) Single(io.reactivex.Single) Editable(android.text.Editable) TextInputLayout(android.support.design.widget.TextInputLayout) User(com.meisolsson.githubsdk.model.User) Locale(java.util.Locale) Handler(android.os.Handler) R(com.gh4a.R) View(android.view.View) Button(android.widget.Button) Settings(android.provider.Settings) ApiRequestException(com.gh4a.ApiRequestException) Build(android.os.Build) DialogInterface(android.content.DialogInterface) ApiHelpers(com.gh4a.utils.ApiHelpers) Iterator(java.util.Iterator) LayoutInflater(android.view.LayoutInflater) TextUtils(android.text.TextUtils) UserService(com.meisolsson.githubsdk.service.users.UserService) IdRes(android.support.annotation.IdRes) AlertDialog(android.support.v7.app.AlertDialog) RxUtils(com.gh4a.utils.RxUtils) Pair(android.support.v4.util.Pair) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) TextInputEditText(android.support.design.widget.TextInputEditText) ServiceFactory(com.gh4a.ServiceFactory) PublishProcessor(io.reactivex.processors.PublishProcessor) UnsupportedEncodingException(java.io.UnsupportedEncodingException) LoginService(com.gh4a.ServiceFactory.LoginService) TextWatcher(android.text.TextWatcher) RxUtils(com.gh4a.utils.RxUtils) LoginService(com.gh4a.ServiceFactory.LoginService) ApiRequestException(com.gh4a.ApiRequestException) ApiHelpers(com.gh4a.utils.ApiHelpers)

Aggregations

Dialog (android.app.Dialog)1 Context (android.content.Context)1 DialogInterface (android.content.DialogInterface)1 Build (android.os.Build)1 Bundle (android.os.Bundle)1 Handler (android.os.Handler)1 Settings (android.provider.Settings)1 IdRes (android.support.annotation.IdRes)1 NonNull (android.support.annotation.NonNull)1 TextInputEditText (android.support.design.widget.TextInputEditText)1 TextInputLayout (android.support.design.widget.TextInputLayout)1 DialogFragment (android.support.v4.app.DialogFragment)1 Pair (android.support.v4.util.Pair)1 AlertDialog (android.support.v7.app.AlertDialog)1 Editable (android.text.Editable)1 TextUtils (android.text.TextUtils)1 TextWatcher (android.text.TextWatcher)1 LayoutInflater (android.view.LayoutInflater)1 View (android.view.View)1 Button (android.widget.Button)1