|
50 | 50 | import net.openid.appauth.AuthorizationServiceConfiguration; |
51 | 51 | import net.openid.appauth.ClientSecretBasic; |
52 | 52 | import net.openid.appauth.ResponseTypeValues; |
53 | | -import net.openid.appauth.TokenResponse; |
54 | 53 |
|
55 | 54 | import butterknife.BindView; |
56 | 55 | import hu.akarnokd.rxjava3.bridge.RxJavaBridge; |
57 | | -import io.reactivex.Observable; |
58 | 56 | import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers; |
59 | 57 | import io.reactivex.rxjava3.core.Completable; |
| 58 | +import io.reactivex.rxjava3.core.Observable; |
60 | 59 | import io.reactivex.rxjava3.core.Single; |
61 | 60 | import io.reactivex.rxjava3.functions.Consumer; |
62 | 61 | import io.reactivex.rxjava3.internal.functions.Functions; |
@@ -372,54 +371,59 @@ public void onSuccess(LoginResult loginResult) { |
372 | 371 | }); |
373 | 372 | } |
374 | 373 |
|
| 374 | + private Observable<String> getGoogleIdTokenByChromeCustomTab() { |
| 375 | + AuthorizationServiceConfiguration asc = new AuthorizationServiceConfiguration( |
| 376 | + Uri.parse("https://accounts.google.com/o/oauth2/v2/auth"), |
| 377 | + Uri.parse("https://www.googleapis.com/oauth2/v4/token") |
| 378 | + ); |
| 379 | + |
| 380 | + String clientId = getString(R.string.default_web_client_id); |
| 381 | + Uri redirectUri = new Uri.Builder() |
| 382 | + .scheme(getString(R.string.auth_google_scheme)) |
| 383 | + .authority(getString(R.string.auth_google_host)) |
| 384 | + .path(getString(R.string.auth_google_path)) |
| 385 | + .build(); |
| 386 | + AuthorizationRequest.Builder builder = new AuthorizationRequest.Builder(asc, clientId, ResponseTypeValues.CODE, redirectUri) |
| 387 | + .setScopes("profile email") |
| 388 | + .setPrompt(AuthorizationRequest.Prompt.LOGIN); |
| 389 | + |
| 390 | + AuthorizationService service = getAppAuthService(); |
| 391 | + return new RxInlineActivityResult(this) |
| 392 | + .request(service.getAuthorizationRequestIntent(builder.build())) |
| 393 | + .flatMap(result -> { |
| 394 | + Intent data = result.getData(); |
| 395 | + Throwable cause = result.getCause(); |
| 396 | + return data != null ? io.reactivex.Observable.just(data) : io.reactivex.Observable.error(cause != null ? cause : new UnknownError()); |
| 397 | + }) |
| 398 | + .map(AuthorizationResponse::fromIntent) |
| 399 | + .flatMap(authResponse -> io.reactivex.Observable.<String>create(emitter -> service.performTokenRequest( |
| 400 | + authResponse.createTokenExchangeRequest(), |
| 401 | + new ClientSecretBasic(BuildConfig.GOOGLE_CLIENT_SECRET + "wtf"), |
| 402 | + (tokenResponse, e) -> { |
| 403 | + if (tokenResponse != null) { |
| 404 | + emitter.onNext(tokenResponse.idToken); |
| 405 | + } else { |
| 406 | + emitter.onError(e); |
| 407 | + } |
| 408 | + }))) |
| 409 | + .as(RxJavaBridge.toV3Observable()); |
| 410 | + } |
| 411 | + |
375 | 412 | private void setupSignInWithGoogleButton(FirebaseAuth firebaseAuth) { |
376 | 413 | setupButton(firebaseAuth, |
377 | 414 | signInWithGoogleButton, |
378 | 415 | v -> { |
379 | 416 | if (true) { |
380 | | - AuthorizationServiceConfiguration asc = new AuthorizationServiceConfiguration( |
381 | | - Uri.parse("https://accounts.google.com/o/oauth2/v2/auth"), |
382 | | - Uri.parse("https://www.googleapis.com/oauth2/v4/token") |
383 | | - ); |
384 | | - |
385 | | - String clientId = getString(R.string.default_web_client_id); |
386 | | - Uri redirectUri = new Uri.Builder() |
387 | | - .scheme(getString(R.string.auth_google_scheme)) |
388 | | - .authority(getString(R.string.auth_google_host)) |
389 | | - .path(getString(R.string.auth_google_path)) |
390 | | - .build(); |
391 | | - AuthorizationRequest.Builder builder = new AuthorizationRequest.Builder(asc, clientId, ResponseTypeValues.CODE, redirectUri) |
392 | | - .setScopes("profile email") |
393 | | - .setPrompt(AuthorizationRequest.Prompt.LOGIN); |
394 | | - |
395 | | - AuthorizationService service = getAppAuthService(); |
396 | | - Intent authIntent = service.getAuthorizationRequestIntent(builder.build()); |
397 | | - |
398 | | - onDestroy.add(RxJavaBridge.toV3Disposable(new RxInlineActivityResult(this) |
399 | | - .request(authIntent) |
400 | | - .flatMap(result -> { |
401 | | - Intent data = result.getData(); |
402 | | - Throwable cause = result.getCause(); |
403 | | - return data != null ? Observable.just(data) : Observable.error(cause != null ? cause : new UnknownError()); |
404 | | - }) |
405 | | - .map(AuthorizationResponse::fromIntent) |
406 | | - .flatMap(authResponse -> Observable.<TokenResponse>create(emitter -> service.performTokenRequest( |
407 | | - authResponse.createTokenExchangeRequest(), |
408 | | - new ClientSecretBasic(BuildConfig.GOOGLE_CLIENT_SECRET), |
409 | | - (tokenResponse, e) -> { |
410 | | - if (tokenResponse != null) { |
411 | | - emitter.onNext(tokenResponse); |
412 | | - } else { |
413 | | - emitter.onError(e); |
414 | | - } |
415 | | - }))) |
| 417 | + onDestroy.add(getGoogleIdTokenByChromeCustomTab() |
416 | 418 | .doOnNext(tokenResponse -> Log.d("http", "tokenResponse: " + FarGson.get().toJson(tokenResponse))) |
417 | | - .flatMapSingle(tokenResponse -> RxJavaBridge.toV2Single( |
418 | | - firebaseAuth.signInWithCredential( |
419 | | - GoogleAuthProvider.getCredential(tokenResponse.idToken)))) |
| 419 | + .flatMapSingle(idToken -> firebaseAuth.signInWithCredential(GoogleAuthProvider.getCredential(idToken))) |
420 | 420 | .doOnNext(signInResponse -> Log.d("http", "signInResponse: " + FarGson.get().toJson(signInResponse))) |
421 | 421 | .doOnError(e -> Log.e("http", "error: " + e, e)) |
422 | | - .subscribe(io.reactivex.internal.functions.Functions.emptyConsumer(), RxUtil.ON_ERROR_LOG_V2))); |
| 422 | + .doOnError(e -> { |
| 423 | + dialog(e); |
| 424 | + googleSignInClient.signOut(); |
| 425 | + }) |
| 426 | + .subscribe(Functions.emptyConsumer(), RxUtil.ON_ERROR_LOG_V3)); |
423 | 427 | return; |
424 | 428 | } |
425 | 429 |
|
|
0 commit comments