mirror of
https://github.com/kuhyx/wake-alarm.git
synced 2026-11-22 16:22:18 +01:00
phone_app: add Google sign-in support for the Firebase sync account
Same pattern as todo, diet_guard_app, and workout_app: openFirebaseWithGoogle() (google_platform.dart abstracts the plugin's platform-channel dependence), isFirebaseConfigured() reading the stored session rather than the account marker alone, and the settings screen offering "Sign in with Google" ahead of the password form. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KdSp6Nbrs9xvsWkCMDN9a1
This commit is contained in:
parent
b1762fad29
commit
37a7829757
@ -134,6 +134,14 @@ repos:
|
||||
|
||||
- repo: local
|
||||
hooks:
|
||||
# Fails a commit that still points crdt_sync at a local path. Local
|
||||
# tests would go green against a library CI has never seen, silently.
|
||||
- id: crdt-sync-override
|
||||
name: no local crdt_sync dependency_override
|
||||
entry: bash scripts/check_no_crdt_sync_override.sh phone_app/pubspec.yaml
|
||||
language: system
|
||||
pass_filenames: false
|
||||
always_run: true
|
||||
- id: shellcheck
|
||||
name: shellcheck
|
||||
entry: bash -c 'printf "%s\0" "$@" | xargs -0 -n 40 shellcheck --severity=warning' --
|
||||
|
||||
@ -7,6 +7,7 @@ import 'package:url_launcher/url_launcher.dart';
|
||||
|
||||
import 'package:wake_alarm_sync/services/firebase_backend.dart';
|
||||
import 'package:wake_alarm_sync/services/github_device_auth.dart';
|
||||
import 'package:wake_alarm_sync/services/google_sign_in_backend.dart';
|
||||
import 'package:wake_alarm_sync/services/sync_settings.dart';
|
||||
import 'package:wake_alarm_sync/ui/theme.dart';
|
||||
|
||||
@ -18,7 +19,34 @@ import 'package:wake_alarm_sync/ui/theme.dart';
|
||||
/// competing with Firebase as a visible choice.
|
||||
class SettingsScreen extends StatefulWidget {
|
||||
/// Creates a [SettingsScreen].
|
||||
const SettingsScreen({super.key});
|
||||
const SettingsScreen({
|
||||
super.key,
|
||||
this.googleFirebaseFactory,
|
||||
this.googleAvailable,
|
||||
this.accountLoader,
|
||||
this.sessionProbe,
|
||||
});
|
||||
|
||||
/// Builds the Firebase backend via Google sign-in. Injected so tests need
|
||||
/// no platform channel -- the plugin reaches the OS account picker.
|
||||
final Future<FirebaseRestClient?> Function()? googleFirebaseFactory;
|
||||
|
||||
/// Whether to offer the Google button. Defaults to what the platform
|
||||
/// supports; injected by tests, whose host reports unsupported.
|
||||
final bool? googleAvailable;
|
||||
|
||||
/// Reads the stored Firebase account. Injected for the same reason as
|
||||
/// [googleFirebaseFactory]: the keystore is a platform channel.
|
||||
final Future<FirebaseAccount?> Function()? accountLoader;
|
||||
|
||||
/// Whether a Firebase session is stored. See [accountLoader].
|
||||
///
|
||||
/// Separate from [accountLoader] because the two answer different
|
||||
/// questions: the account marker is bookkeeping, the session is the
|
||||
/// credential. A device can hold the second without the first, and
|
||||
/// reporting only the first is what made a syncing phone read as
|
||||
/// "not connected".
|
||||
final Future<bool> Function()? sessionProbe;
|
||||
|
||||
@override
|
||||
State<SettingsScreen> createState() => _SettingsScreenState();
|
||||
@ -50,16 +78,81 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
// Reflect a previously-stored account, so a returning user sees the real
|
||||
// state instead of an empty form that looks unconfigured.
|
||||
final account = await loadAccount();
|
||||
// The stored session, not the account marker, decides "connected": a
|
||||
// Google sign-in leaves a refresh token that authenticates every request
|
||||
// even when no marker was written beside it.
|
||||
final connected = await (widget.sessionProbe ?? isFirebaseConfigured)();
|
||||
if (!mounted) return;
|
||||
if (account != null) _emailController.text = account.email;
|
||||
setState(() {
|
||||
_settings = s;
|
||||
_firebaseConnected = account != null;
|
||||
_firebaseConnected = connected;
|
||||
});
|
||||
}
|
||||
|
||||
/// Stores the typed account and signs in immediately, so a typo surfaces
|
||||
/// here rather than as a silent background failure at push time.
|
||||
|
||||
/// Signs in by picking a Google account -- the one-tap path.
|
||||
///
|
||||
/// A dismissed picker is not an error; a wrong-account sign-in reports why,
|
||||
/// because that is the failure that otherwise looks like a working sync
|
||||
/// which silently never syncs.
|
||||
Future<void> _connectGoogle() async {
|
||||
setState(() {
|
||||
_busy = true;
|
||||
_status = 'Signing in...';
|
||||
});
|
||||
try {
|
||||
final client =
|
||||
await (widget.googleFirebaseFactory ?? openFirebaseWithGoogle)();
|
||||
if (!mounted) return;
|
||||
if (client == null) {
|
||||
setState(() {
|
||||
_busy = false;
|
||||
_status = 'Google sign-in was cancelled.';
|
||||
});
|
||||
return;
|
||||
}
|
||||
// openFirebaseWithGoogle stored the account under the email Firebase
|
||||
// reported; reflect it rather than reading the (empty) form field.
|
||||
final account = await (widget.accountLoader ?? storedAccount)();
|
||||
// Report the persisted state, not the fact that the call returned a
|
||||
// client: a non-null client only means sign-in succeeded in that
|
||||
// moment, which is how four apps claimed "Connected" and then synced
|
||||
// over GitHub after the next restart.
|
||||
final connected = await (widget.sessionProbe ?? isFirebaseConfigured)();
|
||||
if (!mounted) return;
|
||||
if (account != null) _emailController.text = account.email;
|
||||
setState(() {
|
||||
_busy = false;
|
||||
_firebaseConnected = connected;
|
||||
_status = connected
|
||||
? 'Connected to Firebase.'
|
||||
: 'Signed in, but this device did not save the session - it will '
|
||||
'sync over GitHub after a restart. Try connecting again.';
|
||||
});
|
||||
} on FirebaseAuthError catch (error) {
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_busy = false;
|
||||
_firebaseConnected = false;
|
||||
_status = error.message;
|
||||
});
|
||||
} on Object catch (error) {
|
||||
// Broader than Exception on purpose: a missing platform binding raises
|
||||
// an Error, and anything escaping here leaves the button disabled and
|
||||
// the screen stuck on "Signing in..." forever -- which is exactly what
|
||||
// happened on the phone before storedAccount() replaced loadAccount().
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_busy = false;
|
||||
_firebaseConnected = false;
|
||||
_status = 'Google sign-in failed: $error';
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _connectFirebase() async {
|
||||
final email = _emailController.text.trim();
|
||||
final password = _passwordController.text;
|
||||
@ -229,6 +322,18 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
: const Icon(Icons.cloud_done),
|
||||
label: const Text('Connect Firebase'),
|
||||
),
|
||||
// One tap, no typing -- the path that matters after a
|
||||
// reinstall. Hidden where the platform has no programmatic
|
||||
// Google flow (see google_platform.dart); a button that
|
||||
// always failed would be worse than none.
|
||||
if (widget.googleAvailable ?? googleSignInSupported) ...[
|
||||
const SizedBox(height: 12),
|
||||
FilledButton.icon(
|
||||
onPressed: _busy ? null : _connectGoogle,
|
||||
icon: const Icon(Icons.account_circle),
|
||||
label: const Text('Sign in with Google'),
|
||||
),
|
||||
],
|
||||
],
|
||||
const SizedBox(height: 16),
|
||||
if (_status.isNotEmpty)
|
||||
|
||||
@ -12,8 +12,11 @@
|
||||
/// half (`wake_alarm/_sync.py`). On Android there is no such file.
|
||||
library;
|
||||
|
||||
import 'dart:developer';
|
||||
import 'package:crdt_sync/crdt_sync.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:wake_alarm_sync/services/google_sign_in_backend.dart';
|
||||
|
||||
/// The shared `kuhy-syncs` project.
|
||||
///
|
||||
@ -60,6 +63,14 @@ Future<FirebaseAccount?> loadAccount() async {
|
||||
Future<void> saveAccount(FirebaseAccount account) =>
|
||||
_secure.write(key: kFirebaseAccountKey, value: account.toJsonString());
|
||||
|
||||
/// Reads the account from the keystore only, with no wrapper fallback.
|
||||
///
|
||||
/// Callers reading back an account they just wrote use this rather than
|
||||
/// [loadAccount]: a fallback would be wrong there, and on Android the
|
||||
/// wrapper route resolves to `file:///` and throws instead of returning null.
|
||||
Future<FirebaseAccount?> storedAccount() async =>
|
||||
FirebaseAccount.tryParse(await _secure.read(key: kFirebaseAccountKey));
|
||||
|
||||
/// Forgets the account and any cached session.
|
||||
Future<void> clearAccount() async {
|
||||
await _secure.delete(key: kFirebaseAccountKey);
|
||||
@ -72,7 +83,13 @@ Future<void> clearAccount() async {
|
||||
/// token, so the usual path costs no authentication round trip.
|
||||
Future<FirebaseRestClient?> openFirebase() async {
|
||||
final account = await loadAccount();
|
||||
if (account == null) return null;
|
||||
if (account == null) {
|
||||
// A stored refresh token IS a signed-in device, even with no account
|
||||
// marker beside it. Treating the marker as the source of truth is what
|
||||
// made a phone with a live session sync over GitHub and 401 forever --
|
||||
// the credential was in the keystore the whole time, unused.
|
||||
return _clientFromStoredSession();
|
||||
}
|
||||
try {
|
||||
return await firebaseClientFor(
|
||||
config: kProject.configFor(account.email),
|
||||
@ -88,6 +105,105 @@ Future<FirebaseRestClient?> openFirebase() async {
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether this device can actually authenticate against Firebase.
|
||||
///
|
||||
/// True when either half of the state is present: the account marker
|
||||
/// [loadAccount] reads, or a stored refresh token. The token is the half that
|
||||
/// matters -- it is what signs requests -- so reporting the marker alone calls
|
||||
/// a working device "not connected", which is exactly how a phone that was in
|
||||
/// fact syncing looked broken.
|
||||
Future<bool> isFirebaseConfigured() async {
|
||||
try {
|
||||
final auth = FirebaseTokenProvider(
|
||||
apiKey: kProject.apiKey,
|
||||
store: credentialStore(),
|
||||
);
|
||||
if (await auth.hasSession()) return true;
|
||||
// storedAccount, not loadAccount: on Android the latter falls back to the
|
||||
// desktop wrapper's /sync-account route, which resolves to file:/// and
|
||||
// throws "No host specified in URI" -- observed on the phone, where it
|
||||
// turned a successful sign-in into "Google sign-in failed".
|
||||
if (await storedAccount() == null) return false;
|
||||
// A marker with no session behind it cannot sign a single request. Drop
|
||||
// just the marker so the settings screen offers a sign-in instead of a
|
||||
// dead banner -- not clearAccount(), which also sets the opt-out flag and
|
||||
// would stop the desktop wrapper re-provisioning after the next sign-in.
|
||||
await _secure.delete(key: kFirebaseAccountKey);
|
||||
return false;
|
||||
} on Object catch (error, stackTrace) {
|
||||
log(
|
||||
'session probe failed; reporting this device as not configured',
|
||||
level: 1000,
|
||||
error: error,
|
||||
stackTrace: stackTrace,
|
||||
);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Signs in with Google alone, for a device that has no account stored yet.
|
||||
///
|
||||
/// The one-tap path: [openFirebase] needs an account in the keystore to know
|
||||
/// which email to use, but a fresh install has none. The Google token carries
|
||||
/// the identity, so nothing has to be typed.
|
||||
///
|
||||
/// Returns null when the user dismisses the picker; throws [FirebaseAuthError]
|
||||
/// when Google succeeds but resolves to a uid the security rules do not pin —
|
||||
/// that would authenticate fine and then be denied every read and write, with
|
||||
/// no other symptom.
|
||||
Future<FirebaseRestClient?> openFirebaseWithGoogle({
|
||||
Future<String?> Function()? tokenFetcher,
|
||||
Future<void> Function(FirebaseAccount)? accountSaver,
|
||||
http.Client? httpClient,
|
||||
}) async {
|
||||
final token = await (tokenFetcher ?? googleIdToken)();
|
||||
if (token == null) return null;
|
||||
final auth = FirebaseTokenProvider(
|
||||
apiKey: kProject.apiKey,
|
||||
store: credentialStore(),
|
||||
httpClient: httpClient,
|
||||
);
|
||||
final email = await auth.signInWithGoogle(
|
||||
idToken: token,
|
||||
expectedUid: kSyncUid,
|
||||
);
|
||||
// Saved unconditionally, and deliberately not gated on `email`:
|
||||
// `signInWithIdp` omits that field whenever the Google account hides it, and
|
||||
// gating the write on it returns a working client while persisting nothing,
|
||||
// so the next launch looks unconfigured and falls back to GitHub-only.
|
||||
// The session is already durable here — signInWithGoogle stored the refresh
|
||||
// token — and that token, not the address, is the credential.
|
||||
await (accountSaver ?? saveAccount)(
|
||||
FirebaseAccount(email: email ?? '', password: ''),
|
||||
);
|
||||
return FirebaseRestClient(databaseUrl: kProject.databaseUrl, auth: auth);
|
||||
}
|
||||
|
||||
/// Returns a client built from the keystore's refresh token alone, or null.
|
||||
///
|
||||
/// The recovery path for a device that has a live session but no account
|
||||
/// marker -- the state a Google sign-in used to leave behind. Costs one
|
||||
/// keystore read and no network round trip when there is no session, so it is
|
||||
/// safe on the background-tick path [openFirebase] also serves.
|
||||
Future<FirebaseRestClient?> _clientFromStoredSession() async {
|
||||
try {
|
||||
final auth = FirebaseTokenProvider(
|
||||
apiKey: kProject.apiKey,
|
||||
store: credentialStore(),
|
||||
);
|
||||
if (!await auth.hasSession()) return null;
|
||||
return FirebaseRestClient(databaseUrl: kProject.databaseUrl, auth: auth);
|
||||
} on Object catch (error, stackTrace) {
|
||||
log(
|
||||
'stored-session recovery failed; falling back to the GitHub mirror',
|
||||
level: 1000,
|
||||
error: error,
|
||||
stackTrace: stackTrace,
|
||||
);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// coverage:ignore-end
|
||||
|
||||
/// Wraps [github] so Firebase is primary and GitHub is kept as a mirror.
|
||||
|
||||
11
phone_app/lib/services/google_platform.dart
Normal file
11
phone_app/lib/services/google_platform.dart
Normal file
@ -0,0 +1,11 @@
|
||||
/// Platform gate for the programmatic Google sign-in flow.
|
||||
///
|
||||
/// Conditional export because the answer differs per platform *and* because
|
||||
/// asking the plugin directly is unsafe: `supportsAuthenticate()` throws
|
||||
/// `UnimplementedError` where no implementation is registered -- an `Error`,
|
||||
/// not an `Exception`, so it escapes an ordinary catch and would take down
|
||||
/// the settings screen's `build()`.
|
||||
library;
|
||||
|
||||
export 'google_platform_io.dart'
|
||||
if (dart.library.js_interop) 'google_platform_web.dart';
|
||||
10
phone_app/lib/services/google_platform_io.dart
Normal file
10
phone_app/lib/services/google_platform_io.dart
Normal file
@ -0,0 +1,10 @@
|
||||
/// Whether the programmatic Google sign-in flow exists on this platform.
|
||||
///
|
||||
/// The io half: Android has the Credential Manager flow; Linux desktop does
|
||||
/// not, and this app's desktop build is the web one anyway.
|
||||
library;
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
/// True only on Android, the one platform shipping the programmatic flow.
|
||||
bool get googleSignInSupported => Platform.isAndroid;
|
||||
8
phone_app/lib/services/google_platform_web.dart
Normal file
8
phone_app/lib/services/google_platform_web.dart
Normal file
@ -0,0 +1,8 @@
|
||||
/// Whether the programmatic Google sign-in flow exists on this platform.
|
||||
///
|
||||
/// The web half: Google Identity Services signs in only through its own
|
||||
/// rendered button, so `authenticate()` throws `UnimplementedError` here.
|
||||
library;
|
||||
|
||||
/// Always false: the web plugin has no programmatic sign-in.
|
||||
bool get googleSignInSupported => false;
|
||||
122
phone_app/lib/services/google_sign_in_backend.dart
Normal file
122
phone_app/lib/services/google_sign_in_backend.dart
Normal file
@ -0,0 +1,122 @@
|
||||
/// One-tap Google sign-in, kept apart from the rest of the sync wiring.
|
||||
///
|
||||
/// Why this is its own file: `crdt_sync` is deliberately pure Dart (only
|
||||
/// `crypto` and `http`), because the same library runs on Linux desktop and
|
||||
/// headless under systemd, where `google_sign_in` does not exist. So the
|
||||
/// plugin lives here, in the app, and the library only ever receives a token
|
||||
/// string through a closure.
|
||||
///
|
||||
/// What this buys: on a fresh install the sync account is reached by tapping
|
||||
/// one account-picker row instead of typing a long password on a phone
|
||||
/// keyboard. The password path stays as the fallback and as the machine
|
||||
/// credential for desktop and the Python side.
|
||||
library;
|
||||
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:google_sign_in/google_sign_in.dart';
|
||||
import 'package:wake_alarm_sync/services/google_platform.dart' as platform_gate;
|
||||
|
||||
/// The account the security rules pin.
|
||||
///
|
||||
/// Not a secret -- it is already in the public `database.rules.json` -- but it
|
||||
/// is the value that makes a wrong-account sign-in *fail loudly* instead of
|
||||
/// authenticating fine and then being denied every read and write. See
|
||||
/// `crdt-sync/tool/link_google.py`, which is what put the Google identity on
|
||||
/// this uid in the first place.
|
||||
const kSyncUid = 'OvA2REQyLIhAHOEjzwS1o877rgG3';
|
||||
|
||||
/// The project's **Web** OAuth client id, used as the audience for ID
|
||||
/// tokens.
|
||||
///
|
||||
/// Public by design, exactly like the project's `apiKey`: it ships inside every
|
||||
/// APK, and the security rules -- not its secrecy -- protect the data.
|
||||
///
|
||||
/// A plain const rather than a `--dart-define`, deliberately. As a
|
||||
/// compile-time environment value it was empty in every build that matters:
|
||||
/// the phone-deploy skill and CI both run a bare `flutter build apk
|
||||
/// --release`, so released APKs would have shown a Google button that always
|
||||
/// reported "cancelled" -- a visible control that can never succeed, which is
|
||||
/// the exact thing the web platform gate exists to prevent.
|
||||
///
|
||||
/// Fill this in from Firebase console -> Project settings -> the `syncs-rest`
|
||||
/// Web app's OAuth client (type "Web application"). Android must request a
|
||||
/// token minted for the *web* client; an Android client id here yields a token
|
||||
/// Firebase rejects with `audience mismatch`.
|
||||
/// Empty until the Web OAuth client is registered; [googleSignInSupported] is
|
||||
/// false while it is, so the button stays hidden rather than failing.
|
||||
const kServerClientId =
|
||||
'845446124781-prdoherj0v64vc6egvvcp3l0693khaur.apps.googleusercontent.com';
|
||||
|
||||
/// Whether this platform can sign in programmatically.
|
||||
///
|
||||
/// False on web, where Google Identity Services only signs in through its own
|
||||
/// rendered button: `authenticate()` throws `UnimplementedError` there. The
|
||||
/// desktop build of this app *is* the web build, so without this check the
|
||||
/// "Sign in with Google" button would crash the settings screen on desktop
|
||||
/// rather than falling back; see `google_platform.dart` for why this is a
|
||||
/// platform check rather than a question asked of the plugin.
|
||||
///
|
||||
/// Also false when no client id is compiled in: a button that cannot possibly
|
||||
/// succeed is worse than no button, and without this check an unconfigured
|
||||
/// build reports "cancelled" and sends you debugging the OAuth console for a
|
||||
/// problem that is really a missing constant.
|
||||
bool get googleSignInSupported =>
|
||||
platform_gate.googleSignInSupported && kServerClientId.isNotEmpty;
|
||||
|
||||
/// Returns a Google ID token for the signed-in account, or null.
|
||||
///
|
||||
/// Null rather than throwing when the user dismisses the picker: cancelling is
|
||||
/// an ordinary outcome, and the caller falls back to the password path.
|
||||
///
|
||||
/// [signInFn] and [serverClientId] exist for tests, which cannot reach the
|
||||
/// platform channel the plugin uses.
|
||||
Future<String?> googleIdToken({
|
||||
Future<String?> Function()? signInFn,
|
||||
String serverClientId = kServerClientId,
|
||||
}) async {
|
||||
if (signInFn != null) return signInFn();
|
||||
if (serverClientId.isEmpty) {
|
||||
// A build without --dart-define=GOOGLE_SERVER_CLIENT_ID would otherwise
|
||||
// fail inside the plugin with a far less obvious message.
|
||||
log(
|
||||
'Google sign-in unavailable: GOOGLE_SERVER_CLIENT_ID was not set at '
|
||||
'build time; falling back to the password path',
|
||||
level: 900,
|
||||
);
|
||||
return null;
|
||||
}
|
||||
// The plugin reaches the OS account picker through a platform channel,
|
||||
// which `flutter test` has no binding for -- the same reason main.dart and
|
||||
// firebase_backend.dart's keystore adapters are excluded. Everything above
|
||||
// this point is pure Dart and is covered.
|
||||
// coverage:ignore-start
|
||||
try {
|
||||
await GoogleSignIn.instance.initialize(serverClientId: serverClientId);
|
||||
if (!GoogleSignIn.instance.supportsAuthenticate()) {
|
||||
// Web/desktop: sign-in must go through the rendered GIS button, which
|
||||
// this app does not host yet. Fall back rather than throwing.
|
||||
log(
|
||||
'Google sign-in is not available on this platform; falling back to '
|
||||
'the password path',
|
||||
level: 900,
|
||||
);
|
||||
return null;
|
||||
}
|
||||
final account = await GoogleSignIn.instance.authenticate();
|
||||
return account.authentication.idToken;
|
||||
} on GoogleSignInException catch (error, stackTrace) {
|
||||
// Includes the user simply dismissing the picker, which is not an error
|
||||
// worth surfacing -- but log it, because a *configuration* failure
|
||||
// (unregistered SHA-1, wrong client id) arrives through the same path and
|
||||
// is otherwise indistinguishable from "the user changed their mind".
|
||||
log(
|
||||
'Google sign-in did not complete',
|
||||
level: 900,
|
||||
error: error,
|
||||
stackTrace: stackTrace,
|
||||
);
|
||||
return null;
|
||||
}
|
||||
// coverage:ignore-end
|
||||
}
|
||||
@ -93,11 +93,11 @@ packages:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
path: crdt_sync_dart
|
||||
ref: "crdt_sync_dart-v0.6.0"
|
||||
resolved-ref: fc99eff00599a4c3195e4c1573ba08f15909a10d
|
||||
ref: "crdt_sync_dart-v0.10.0"
|
||||
resolved-ref: "5f64a8959b1db342da1f73157b1148aa0dfa38a8"
|
||||
url: "https://github.com/kuhyx/utils"
|
||||
source: git
|
||||
version: "0.6.0"
|
||||
version: "0.10.0"
|
||||
crypto:
|
||||
dependency: transitive
|
||||
description:
|
||||
@ -217,6 +217,54 @@ packages:
|
||||
description: flutter
|
||||
source: sdk
|
||||
version: "0.0.0"
|
||||
google_identity_services_web:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: google_identity_services_web
|
||||
sha256: "5d187c46dc59e02646e10fe82665fc3884a9b71bc1c90c2b8b749316d33ee454"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "0.3.3+1"
|
||||
google_sign_in:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: google_sign_in
|
||||
sha256: "521031b65853b4409b8213c0387d57edaad7e2a949ce6dea0d8b2afc9cb29763"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "7.2.0"
|
||||
google_sign_in_android:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: google_sign_in_android
|
||||
sha256: "57782125965ca87b03d42a147d40b046f1fd6a09141a58913e1b86671a5ef22e"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "7.2.16"
|
||||
google_sign_in_ios:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: google_sign_in_ios
|
||||
sha256: ac1e4c1205267cb7999d1d81333fccffdfda29e853f434bbaf71525498bb6950
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "6.3.0"
|
||||
google_sign_in_platform_interface:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: google_sign_in_platform_interface
|
||||
sha256: "7f59208c42b415a3cca203571128d6f84f885fead2d5b53eb65a9e27f2965bb5"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "3.1.0"
|
||||
google_sign_in_web:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: google_sign_in_web
|
||||
sha256: d473003eeca892f96a01a64fc803378be765071cb0c265ee872c7f8683245d14
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.1.3"
|
||||
hooks:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
||||
@ -12,12 +12,16 @@ dependencies:
|
||||
crdt_sync:
|
||||
git:
|
||||
url: https://github.com/kuhyx/utils
|
||||
ref: crdt_sync_dart-v0.6.0
|
||||
ref: crdt_sync_dart-v0.10.0
|
||||
path: crdt_sync_dart
|
||||
cupertino_icons: ^1.0.8
|
||||
flutter:
|
||||
sdk: flutter
|
||||
flutter_secure_storage: ^9.0.0
|
||||
|
||||
# One-tap sign-in for the sync account. App-side only: crdt_sync stays pure
|
||||
# Dart so it keeps working on Linux desktop and headless.
|
||||
google_sign_in: ^7.2.0
|
||||
http: ^1.2.0
|
||||
path: ^1.9.0
|
||||
path_provider: ^2.1.0
|
||||
|
||||
48
phone_app/test/fake_secure_storage.dart
Normal file
48
phone_app/test/fake_secure_storage.dart
Normal file
@ -0,0 +1,48 @@
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
/// In-memory fake for the `flutter_secure_storage` platform channel so tests
|
||||
/// never touch the real OS keystore. Install it from a test (or `setUp`) and it
|
||||
/// auto-removes on tear down.
|
||||
///
|
||||
/// Pass [throwing] to simulate a host with no secret service: every call raises
|
||||
/// a `PlatformException`, which exercises the plaintext-fallback paths.
|
||||
void installFakeSecureStorage({
|
||||
Map<String, String>? initial,
|
||||
bool throwing = false,
|
||||
}) {
|
||||
const channel = MethodChannel('plugins.it_nomads.com/flutter_secure_storage');
|
||||
final store = <String, String>{...?initial};
|
||||
final messenger =
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger;
|
||||
|
||||
Future<Object?>? handler(MethodCall call) async {
|
||||
if (throwing) {
|
||||
throw PlatformException(code: 'unavailable');
|
||||
}
|
||||
final args = (call.arguments as Map?) ?? const <Object?, Object?>{};
|
||||
final key = args['key'] as String?;
|
||||
switch (call.method) {
|
||||
case 'read':
|
||||
return store[key];
|
||||
case 'write':
|
||||
store[key!] = args['value']! as String;
|
||||
return null;
|
||||
case 'delete':
|
||||
store.remove(key);
|
||||
return null;
|
||||
case 'containsKey':
|
||||
return store.containsKey(key);
|
||||
case 'readAll':
|
||||
return Map<String, String>.from(store);
|
||||
case 'deleteAll':
|
||||
store.clear();
|
||||
return null;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
messenger.setMockMethodCallHandler(channel, handler);
|
||||
addTearDown(() => messenger.setMockMethodCallHandler(channel, null));
|
||||
}
|
||||
88
phone_app/test/firebase_backend_google_test.dart
Normal file
88
phone_app/test/firebase_backend_google_test.dart
Normal file
@ -0,0 +1,88 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:crdt_sync/crdt_sync.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:http/testing.dart' as http_testing;
|
||||
import 'package:wake_alarm_sync/services/firebase_backend.dart';
|
||||
import 'package:wake_alarm_sync/services/google_sign_in_backend.dart';
|
||||
|
||||
import 'fake_secure_storage.dart';
|
||||
|
||||
/// Answers a `signInWithIdp` call the way Firebase does.
|
||||
http_testing.MockClient _signsInAs(String email, {String uid = kSyncUid}) =>
|
||||
http_testing.MockClient(
|
||||
(request) async => http.Response(
|
||||
jsonEncode({
|
||||
'idToken': 'id',
|
||||
'refreshToken': 'refresh',
|
||||
'expiresIn': '3600',
|
||||
'email': email,
|
||||
'localId': uid,
|
||||
}),
|
||||
200,
|
||||
),
|
||||
);
|
||||
|
||||
void main() {
|
||||
// installFakeSecureStorage touches the test binary messenger, which only
|
||||
// exists once the binding is initialised.
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
// openFirebaseWithGoogle reaches the keystore through a platform channel;
|
||||
// the fake stands in so the account write is really exercised rather than
|
||||
// stubbed out at the call site.
|
||||
setUp(installFakeSecureStorage);
|
||||
|
||||
group('openFirebaseWithGoogle', () {
|
||||
test('returns null when the picker is dismissed', () async {
|
||||
// Cancelling is ordinary: no session, no account, no error.
|
||||
final saved = <FirebaseAccount>[];
|
||||
final client = await openFirebaseWithGoogle(
|
||||
tokenFetcher: () async => null,
|
||||
accountSaver: (account) async => saved.add(account),
|
||||
);
|
||||
|
||||
expect(client, isNull);
|
||||
expect(saved, isEmpty);
|
||||
});
|
||||
|
||||
test('stores the account under the email Firebase reports', () async {
|
||||
// The regression this test exists for. A fresh install has no email
|
||||
// anywhere on the device, so it must come from the sign-in response. An
|
||||
// earlier cut read it from the settings screen's text field -- empty on
|
||||
// exactly this path -- and persisted FirebaseAccount(email: ''); the
|
||||
// next launch then took the password branch with '' and failed.
|
||||
final saved = <FirebaseAccount>[];
|
||||
final client = await openFirebaseWithGoogle(
|
||||
tokenFetcher: () async => 'a-token',
|
||||
accountSaver: (account) async => saved.add(account),
|
||||
httpClient: _signsInAs('signed-in@example.com'),
|
||||
);
|
||||
|
||||
expect(client, isNotNull);
|
||||
expect(saved, hasLength(1));
|
||||
expect(saved.single.email, 'signed-in@example.com');
|
||||
// No password to store: this device authenticates with the refresh
|
||||
// token, and openFirebase() must not later try to sign in with ''.
|
||||
expect(saved.single.password, isEmpty);
|
||||
client!.close();
|
||||
});
|
||||
|
||||
test('stores nothing when Google resolves to the wrong uid', () async {
|
||||
// Authentication succeeds and the uid is wrong, which the security
|
||||
// rules would deny on every read and write. Failing here -- with
|
||||
// nothing persisted -- leaves the device retryable rather than stuck.
|
||||
final saved = <FirebaseAccount>[];
|
||||
await expectLater(
|
||||
openFirebaseWithGoogle(
|
||||
tokenFetcher: () async => 'a-token',
|
||||
accountSaver: (account) async => saved.add(account),
|
||||
httpClient: _signsInAs('someone@example.com', uid: 'a-different-uid'),
|
||||
),
|
||||
throwsA(isA<FirebaseAuthError>()),
|
||||
);
|
||||
expect(saved, isEmpty);
|
||||
});
|
||||
});
|
||||
}
|
||||
14
phone_app/test/google_platform_test.dart
Normal file
14
phone_app/test/google_platform_test.dart
Normal file
@ -0,0 +1,14 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:wake_alarm_sync/services/google_sign_in_backend.dart';
|
||||
|
||||
void main() {
|
||||
test('reports whether the programmatic Google flow exists here', () {
|
||||
// Android is the only platform shipping it. Asked as a platform question
|
||||
// rather than of the plugin, because GoogleSignIn.supportsAuthenticate()
|
||||
// itself throws UnimplementedError where no implementation is registered
|
||||
// -- an Error, not an Exception, so it would escape an ordinary catch and
|
||||
// take down the settings screen's build(). The test host is Linux, so
|
||||
// this is false here and the password path stays visible.
|
||||
expect(googleSignInSupported, isFalse);
|
||||
});
|
||||
}
|
||||
34
phone_app/test/google_sign_in_backend_test.dart
Normal file
34
phone_app/test/google_sign_in_backend_test.dart
Normal file
@ -0,0 +1,34 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:wake_alarm_sync/services/google_sign_in_backend.dart';
|
||||
|
||||
void main() {
|
||||
group('googleIdToken', () {
|
||||
test('returns the token the sign-in function yields', () async {
|
||||
// The injected path: proves the closure is what feeds crdt_sync, and
|
||||
// that no platform channel is touched to get there.
|
||||
expect(
|
||||
await googleIdToken(signInFn: () async => 'a-token'),
|
||||
'a-token',
|
||||
);
|
||||
});
|
||||
|
||||
test('passes a null cancellation straight through', () async {
|
||||
// Dismissing the picker is an ordinary outcome, not an error: the
|
||||
// caller falls back to the password path.
|
||||
expect(await googleIdToken(signInFn: () async => null), isNull);
|
||||
});
|
||||
|
||||
test('reports "not configured" when the client id is missing', () async {
|
||||
// Passed explicitly rather than relying on the const: an app whose
|
||||
// client id is filled in would otherwise reach the real plugin here.
|
||||
// The behaviour under test is the empty-id fallback itself.
|
||||
expect(await googleIdToken(serverClientId: ''), isNull);
|
||||
});
|
||||
|
||||
test('pins the uid the security rules expect', () {
|
||||
// If this ever drifts from database.rules.json, a Google sign-in would
|
||||
// be accepted here and then denied every read and write.
|
||||
expect(kSyncUid, 'OvA2REQyLIhAHOEjzwS1o877rgG3');
|
||||
});
|
||||
});
|
||||
}
|
||||
88
scripts/check_no_crdt_sync_override.sh
Executable file
88
scripts/check_no_crdt_sync_override.sh
Executable file
@ -0,0 +1,88 @@
|
||||
#!/bin/bash
|
||||
|
||||
# ============================================================================
|
||||
# Fail if a pubspec still overrides crdt_sync with a local path.
|
||||
#
|
||||
# The apps depend on crdt_sync as a *git dependency pinned to a tag*, so what
|
||||
# CI builds is whatever that tag points at. A `dependency_overrides:` entry
|
||||
# pointing at ~/utils/crdt_sync_dart is invaluable while developing an unreleased
|
||||
# library change -- and catastrophic if it survives the commit: local tests go
|
||||
# green against code CI has never seen, and the failure is silent in both
|
||||
# directions.
|
||||
#
|
||||
# A comment in the pubspec asking someone to remember is deployment hygiene,
|
||||
# not a fix. This is the gate.
|
||||
#
|
||||
# Usage: check_no_crdt_sync_override.sh [pubspec.yaml ...]
|
||||
# With no arguments, checks ./pubspec.yaml.
|
||||
# ============================================================================
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_NAME="$(basename "$0")"
|
||||
readonly SCRIPT_NAME
|
||||
|
||||
usage() {
|
||||
echo "Usage: $SCRIPT_NAME [pubspec.yaml ...]"
|
||||
echo " Fails when a pubspec overrides crdt_sync with a local path."
|
||||
exit 0
|
||||
}
|
||||
|
||||
# Returns 0 (found) when the file has a crdt_sync entry under
|
||||
# dependency_overrides. Deliberately a small state machine rather than a grep:
|
||||
# `path:` appears under plenty of other keys, and a false positive here blocks
|
||||
# a legitimate commit.
|
||||
has_override() {
|
||||
local file="$1"
|
||||
awk '
|
||||
# Track indent depth explicitly. An earlier version used an awk
|
||||
# interval expression ({1,4}) to spot a sibling key, which plain awk
|
||||
# does not enable by default -- so the check silently never fired and
|
||||
# reported every file clean. A safety check that fails open is worse
|
||||
# than none, so this version is written to be obvious instead of terse.
|
||||
function indent(line, n) {
|
||||
n = match(line, /[^ ]/)
|
||||
return (n == 0) ? 0 : n - 1
|
||||
}
|
||||
/^[[:space:]]*#/ { next } # comments
|
||||
/^[[:space:]]*$/ { next } # blank lines
|
||||
/^dependency_overrides:[[:space:]]*$/ { in_overrides = 1; next }
|
||||
/^[^[:space:]]/ { in_overrides = 0; in_crdt = 0; next }
|
||||
in_overrides {
|
||||
if ($0 ~ /^[[:space:]]+crdt_sync:[[:space:]]*$/) {
|
||||
in_crdt = 1
|
||||
crdt_indent = indent($0)
|
||||
next
|
||||
}
|
||||
# Any key at or above crdt_sync{s} own indent ends its block.
|
||||
if (in_crdt && indent($0) <= crdt_indent) { in_crdt = 0 }
|
||||
if (in_crdt && $0 ~ /^[[:space:]]+path:/) { found = 1 }
|
||||
}
|
||||
END { exit(found ? 0 : 1) }
|
||||
' "$file"
|
||||
}
|
||||
|
||||
main() {
|
||||
local -a files=("$@")
|
||||
if [[ ${#files[@]} -eq 0 ]]; then
|
||||
files=("pubspec.yaml")
|
||||
fi
|
||||
|
||||
local failed=0
|
||||
for file in "${files[@]}"; do
|
||||
[[ -f "$file" ]] || continue
|
||||
if has_override "$file"; then
|
||||
echo "Error: $file still has a local crdt_sync dependency_override." >&2
|
||||
echo " Remove it and bump the git 'ref:' to the released tag" >&2
|
||||
echo " before committing, or CI will build different code." >&2
|
||||
failed=1
|
||||
fi
|
||||
done
|
||||
return "$failed"
|
||||
}
|
||||
|
||||
if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
|
||||
usage
|
||||
fi
|
||||
|
||||
main "$@"
|
||||
Loading…
Reference in New Issue
Block a user