diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d45f0e..9351a84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## [Unreleased] +- Every waozi.xyz site with its own key store, such as Breeze and Werk, follows the shared sign-in and sign-out. A site signed in to a different account offers Continue as @name. + - In Google Play builds, optionally save encrypted account backups to Google Drive and choose the same backup in another app or phone. Local backup files remain available. - Continue as @name on a computer where another waozi app is signed in, using one shared account file. diff --git a/README.md b/README.md index 8a1b94d..140769d 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,10 @@ https://account.waozi.xyz/share.js and calls `WaoziAccount.attach(Module)` before starting gives Entity the shared key as `Module.sharedAccountKey`; a site without its own account signs in with it, and signing in, creating an account, linking a device or logging out is passed back through -`Module.shareAccount`. A site keeps its own account if it already has one. +`Module.shareAccount`. Hosts with their own key store follow it through +`ManagementProvider` without extra code. A site signed in to a different account +keeps it and offers "Continue as @name", because that key may be its only copy. +Signing out on one site signs that account out of all of them. Signing in takes the backup first and asks for its password only if it has one, keeping the still-sealed file so a mistyped password can be retried. New backups diff --git a/src/account_store.zi b/src/account_store.zi index 386e237..1b5676d 100644 --- a/src/account_store.zi +++ b/src/account_store.zi @@ -148,30 +148,8 @@ AccountStoreDeliverFile :: (context: *void, path: string) -> bool { // the page's share.js reads it into Module.sharedAccountKey before the app // starts and passes changes on through Module.shareAccount(key, owner, only_if_empty). // Other hosts have no shared sign-in. -AccountStoreSharedKey :: (output: []u8) -> string { - #if #defined(PLATFORM_WEB) { - scope := WebScope() - defer WebEnd(scope) - module := WebGetObject(WebGlobal, WebName("Module")) - if module == 0 || WebFailed() { return "" } - handle := WebAsObject(WebGet(module, WebName("sharedAccountKey"))) - if handle == 0 || WebFailed() || WebTypeOf(handle) != WebTypeString { return "" } - length := WebCopyString(handle, output) - if length <= 0 || length >= cast(s32)output.count { return "" } - output[length] = cast(u8)0 - return TextUntilNul(output) - } - return "" -} -AccountStoreShare :: (key: string, owner: string, only_if_empty: bool) { - #if #defined(PLATFORM_WEB) { - scope := WebScope() - defer WebEnd(scope) - module := WebGetObject(WebGlobal, WebName("Module")) - if module == 0 || WebFailed() || WebTypeOf(WebGetObject(module, WebName("shareAccount"))) != WebTypeFunction { return } - unused WebCall3(module, WebName("shareAccount"), WebText(key), WebText(owner), WebBool(only_if_empty)) - } -} +AccountStoreSharedKey :: (output: []u8) -> string { return SharedWebText("sharedAccountKey", output) } +AccountStoreShare :: (key: string, owner: string, only_if_empty: bool) { SharedWebShare(key, owner, only_if_empty) } AccountStorePersist :: () -> bool { #if #defined(PLATFORM_WEB) { @@ -541,7 +519,7 @@ AccountStoreStartAt :: (root: string, client_id: string) -> bool { } unused CopyCString(TextUntilNul(account_recovery.keys.public_id[:]), account_profile.owner_id[:]) unused SharedProfileLoad(TextUntilNul(account_recovery.keys.public_id[:]), *account_profile) - account_provider = ManagementProvider.{.snapshot = AccountStoreSnapshot, .load = AccountStoreLoad, + account_provider = ManagementProvider.{.shared_web_files = true, .snapshot = AccountStoreSnapshot, .load = AccountStoreLoad, .apply = AccountStoreApply, .action = AccountStoreAction, .client = AccountStoreClient, .choose_file = AccountStoreChooseFile, .deliver_file = AccountStoreDeliverFile} AccountStoreReadAlias() diff --git a/src/entity.zi b/src/entity.zi index ea8e1cd..8188ede 100644 --- a/src/entity.zi +++ b/src/entity.zi @@ -533,6 +533,8 @@ AccountSummary :: (x: float32, y: *float32, width: float32) { if management.account { PublicIdLine(705, Rectangle.{x, y.*, width, 44.0 * scale}) y.* += 56.0 * scale + // Another waozi.xyz site is signed in to a different account. + if CanManage(ACCOUNT_CONTINUE) && ListRow(8801, ContinueLabel(), "", x, y, width) { Manage("continue") } return } padding := 16.0 * scale diff --git a/src/native_management.zi b/src/native_management.zi index 0c69af3..f5d7ec3 100644 --- a/src/native_management.zi +++ b/src/native_management.zi @@ -80,6 +80,10 @@ ManagementProvider :: struct { online_reply: [ACCOUNT_BACKUP_BODY_MAX]u8 // The account shared with the other waozi apps on this device. shared_name: [SHARED_ACCOUNT_NAME_MAX]u8 + // share.js keeps this host's account files in step with the shared web + // sign-in, so the provider leaves that to it. + shared_web_files: bool + shared_web_tried: [65]u8 shared_owner: [65]u8 shared_alias: [40]u8 shared_next: s64 @@ -125,7 +129,7 @@ ProviderState :: (provider: *ManagementProvider) -> Management { if provider.has_incoming { state.candidate_id = provider.incoming.keys.public_id } state.password_needed = provider.held_backup[0] != cast(u8)0 state.backed_up = state.account && provider.backed_up && TextUntilNul(state.public_id[:]) == TextUntilNul(provider.backed_up_owner[:]) - if !state.account && provider.shared_name[0] != cast(u8)0 { + if provider.shared_name[0] != cast(u8)0 { state.shared_name = provider.shared_name state.capabilities |= ACCOUNT_CONTINUE } @@ -175,6 +179,9 @@ ProviderApplyIncoming :: (provider: *ManagementProvider, choice: s32) { result := provider.apply(provider.context, *provider.incoming, choice) if result == 2 { ProviderFinish(provider, "switch", "Choose what to do with the local records"); return } if result != 1 { ProviderFinish(provider, "error", "Could not save the account"); return } + // A sign-in here signs in the other waozi.xyz sites too. + ProviderSharedWebOffer(provider, *provider.incoming, false) + provider.shared_name = .{} RecoveryClear(*provider.incoming) provider.has_incoming = false ProviderFinish(provider, "ok", ifx TextUntilNul(provider.queue.request.operation[:]) == "create" then "Account created" else "Signed in") @@ -337,6 +344,10 @@ ProviderSharedPump :: (provider: *ManagementProvider) { now := UnixNow() if now < provider.shared_next { return } provider.shared_next = now + 2 + #if #defined(PLATFORM_WEB) { + ProviderSharedWebPump(provider) + return + } root_buffer: [4096]u8 root := SharedAccountRoot(root_buffer[:]) if root == "" { return } @@ -358,11 +369,87 @@ ProviderSharedPump :: (provider: *ManagementProvider) { } } +// The name to offer for the shared web account: the name in its public +// profile, or its short ID. +ProviderSharedWebName :: (provider: *ManagementProvider, owner: string) { + document: [65536]u8 + state: EntityState + text := SharedWebText("sharedProfileDocument", document[:]) + if text != "" && EntityDecode(text, *state) && TextUntilNul(state.owner_id[:]) == owner && state.name[0] != cast(u8)0 { + provider.shared_name = .{} + unused CopyCString(TextUntilNul(state.name[:]), provider.shared_name[:]) + return + } + provider.shared_name = .{} + unused CopyCString(owner, provider.shared_name[:]) +} + +// Passes this host's own sign-in on to the other waozi.xyz sites. +ProviderSharedWebOffer :: (provider: *ManagementProvider, recovery: *Recovery, only_if_empty: bool) { + #if #defined(PLATFORM_WEB) { + if provider.shared_web_files { return } + text: [ACCOUNT_EXPORT_ENCRYPTED_TEXT_SIZE]u8 + defer { SecureWipe(*text[0], text.count) } + if RecoveryEncode(recovery, "", text[:]) { + SharedWebShare(TextUntilNul(text[:]), TextUntilNul(recovery.keys.public_id[:]), only_if_empty) + } + } +} + +// Hosts with their own key store, such as Breeze and Werk, follow the shared +// web sign-in here. Until the hub answers, each keeps working on its own. +ProviderSharedWebPump :: (provider: *ManagementProvider) { + if provider.shared_web_files || !SharedWebFlag("sharedAccountReady") || provider.queue.pending { return } + key_buffer: [ACCOUNT_EXPORT_ENCRYPTED_TEXT_SIZE]u8 + defer { SecureWipe(*key_buffer[0], key_buffer.count) } + owner_buffer: [65]u8 + gone_buffer: [65]u8 + key := SharedWebText("sharedAccountKey", key_buffer[:]) + owner := SharedWebText("sharedAccountOwner", owner_buffer[:]) + state := ProviderState(provider) + local := ifx state.account then TextUntilNul(state.public_id[:]) else "" + decision := SharedWebDecide(local, key, owner, SharedWebFlag("sharedAccountInitialized"), + SharedWebText("sharedAccountSignedOut", gone_buffer[:])) + if decision != SHARED_WEB_OFFER && decision != SHARED_WEB_ADOPT { provider.shared_name = .{} } + if decision == SHARED_WEB_SEED { + if TextUntilNul(provider.shared_web_tried[:]) == local { return } + provider.shared_web_tried = state.public_id + recovery: Recovery + defer { RecoveryClear(*recovery) } + if provider.load != null && provider.load(provider.context, *recovery) { ProviderSharedWebOffer(provider, *recovery, true) } + } else if decision == SHARED_WEB_OFFER { + ProviderSharedWebName(provider, owner) + } else if decision == SHARED_WEB_ADOPT { + if TextUntilNul(provider.shared_web_tried[:]) == owner { return } + unused CopyCString(owner, provider.shared_web_tried[:]) + incoming: Recovery + defer { RecoveryClear(*incoming) } + if provider.apply == null || !RecoveryDecode(key, "", *incoming) || + TextUntilNul(incoming.keys.public_id[:]) != owner { return } + // Local records from another owner are the owner's choice to make. + if provider.apply(provider.context, *incoming, 0) != 1 { ProviderSharedWebName(provider, owner) } + } else if decision == SHARED_WEB_SIGN_OUT { + request: ManagementRequest + unused CopyCString("logout", request.operation[:]) + unused CopyCString(local, request.owner_id[:]) + if provider.action != null && provider.action(provider.context, *request) == 1 { + ProviderBridgeSignOut(provider) + ProviderSignOutClear(provider, local, false) + } + } +} + ProviderContinue :: (provider: *ManagementProvider) { root_buffer: [4096]u8 incoming: Recovery defer { RecoveryClear(*incoming) } - if !SharedAccountRead(SharedAccountRoot(root_buffer[:]), *incoming) { + web_key: [ACCOUNT_EXPORT_ENCRYPTED_TEXT_SIZE]u8 + defer { SecureWipe(*web_key[0], web_key.count) } + shared_key := SharedWebText("sharedAccountKey", web_key[:]) + available := false + if shared_key != "" { available = RecoveryDecode(shared_key, "", *incoming) } + else { available = SharedAccountRead(SharedAccountRoot(root_buffer[:]), *incoming) } + if !available { provider.shared_name = .{} ProviderFinish(provider, "error", "That account is no longer signed in on this device") return @@ -410,8 +497,11 @@ ProviderSignOutFirst :: (provider: *ManagementProvider) { } // Nothing of the account stays in memory once it is signed out here. -ProviderSignOutClear :: (provider: *ManagementProvider, owner: string) { +ProviderSignOutClear :: (provider: *ManagementProvider, owner: string, share: bool = true) { root_buffer: [4096]u8 + if share && !provider.shared_web_files { SharedWebShare("", owner, false) } + provider.shared_web_tried = .{} + provider.shared_name = .{} unused SharedAccountRemove(SharedAccountRoot(root_buffer[:]), owner) provider.shared_owner = .{} provider.shared_next = 0 diff --git a/src/shared_account.zi b/src/shared_account.zi index 967e65e..97b8bae 100644 --- a/src/shared_account.zi +++ b/src/shared_account.zi @@ -12,8 +12,70 @@ #import "std/file" #import "std/byte_text_linux" +#if #defined(PLATFORM_WEB) { + #import "std/web" +} + SHARED_ACCOUNT_NAME_MAX :: 80 +// What a site with its own key store does about the waozi.xyz shared sign-in. +SHARED_WEB_NOTHING :: 0 +SHARED_WEB_SEED :: 1 +SHARED_WEB_ADOPT :: 2 +SHARED_WEB_OFFER :: 3 +SHARED_WEB_SIGN_OUT :: 4 + +// The shared sign-in wins on a site with no account. A different key already +// here may be its only copy, so that one is offered, never overwritten. Only +// the account that was signed out leaves; a key made here while the hub was +// out of reach stays. +SharedWebDecide :: (local_owner: string, shared_key: string, shared_owner: string, + initialized: bool, signed_out: string) -> s32 { + if local_owner == "" { return ifx shared_key == "" then SHARED_WEB_NOTHING else SHARED_WEB_ADOPT } + if shared_key != "" { return ifx shared_owner == local_owner then SHARED_WEB_NOTHING else SHARED_WEB_OFFER } + if !initialized { return SHARED_WEB_SEED } + return ifx signed_out == local_owner then SHARED_WEB_SIGN_OUT else SHARED_WEB_NOTHING +} + +// A text field share.js placed on the page's Module, or "" anywhere else. +SharedWebText :: (name: string, output: []u8) -> string { + #if #defined(PLATFORM_WEB) { + scope := WebScope() + defer WebEnd(scope) + module := WebGetObject(WebGlobal, WebName("Module")) + if module == 0 || WebFailed() { return "" } + handle := WebAsObject(WebGet(module, WebName(name))) + if handle == 0 || WebFailed() || WebTypeOf(handle) != WebTypeString { return "" } + length := WebCopyString(handle, output) + if length <= 0 || length >= cast(s32)output.count { return "" } + output[length] = cast(u8)0 + return TextUntilNul(output) + } + return "" +} + +SharedWebFlag :: (name: string) -> bool { + #if #defined(PLATFORM_WEB) { + scope := WebScope() + defer WebEnd(scope) + module := WebGetObject(WebGlobal, WebName("Module")) + if module == 0 || WebFailed() { return false } + return WebAsBool(WebGet(module, WebName(name))) + } + return false +} + +// An empty key signs that owner out of every site. +SharedWebShare :: (key: string, owner: string, only_if_empty: bool) { + #if #defined(PLATFORM_WEB) { + scope := WebScope() + defer WebEnd(scope) + module := WebGetObject(WebGlobal, WebName("Module")) + if module == 0 || WebFailed() || WebTypeOf(WebGetObject(module, WebName("shareAccount"))) != WebTypeFunction { return } + unused WebCall3(module, WebName("shareAccount"), WebText(key), WebText(owner), WebBool(only_if_empty)) + } +} + // Follows the same folder rules as NativeRoot. WAOZI_ACCOUNT_DATA gives tests // and separated profiles their own place. SharedAccountRoot :: (output: []u8) -> string { diff --git a/tests/management_test.zi b/tests/management_test.zi index 8e8e955..97aa6c0 100644 --- a/tests/management_test.zi +++ b/tests/management_test.zi @@ -406,7 +406,21 @@ AccountBackupCheck :: (fixture: *Recovery) -> s32 { // One shared account: offered once, never replaced by another, removed only by // its own logout, and a corrupt file offers nothing. +SharedWebDecideCheck :: () -> bool { + one := "1111111111111111111111111111111111111111111111111111111111111111" + two := "2222222222222222222222222222222222222222222222222222222222222222" + return SharedWebDecide("", "", "", false, "") == SHARED_WEB_NOTHING && + SharedWebDecide("", "key", one, true, "") == SHARED_WEB_ADOPT && + SharedWebDecide(one, "key", one, true, "") == SHARED_WEB_NOTHING && + SharedWebDecide(one, "key", two, true, "") == SHARED_WEB_OFFER && + SharedWebDecide(one, "", "", false, "") == SHARED_WEB_SEED && + SharedWebDecide(one, "", "", true, one) == SHARED_WEB_SIGN_OUT && + SharedWebDecide(one, "", "", true, two) == SHARED_WEB_NOTHING && + SharedWebDecide(one, "", "", true, "") == SHARED_WEB_NOTHING +} + SharedAccountCheck :: (fixture: *Recovery) -> s32 { + if !SharedWebDecideCheck() { return 549 } root := "build/shared-account-test" key_path: [256]u8 name_path: [256]u8