| 1 | //go:build browser |
| 2 | |
| 3 | package home |
| 4 | |
| 5 | import ( |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "amadan.net/oficina/home/internal/home/sealtest" |
| 10 | "amadan.net/rastrillo/rastrillo/harness" |
| 11 | "amadan.net/rastrillo/rastrillo/lastsignin" |
| 12 | "github.com/chromedp/cdproto/emulation" |
| 13 | "github.com/chromedp/cdproto/network" |
| 14 | "github.com/chromedp/chromedp" |
| 15 | ) |
| 16 | |
| 17 | // The door in a real Chrome with a virtual authenticator. The email |
| 18 | // field's autofill (conditional UI) signs a passkey holder in by itself; |
| 19 | // from the chooser, a passkey is signed in by rastrillo's own module |
| 20 | // against Home's {ok,to} finish, landing where the sign-in was going. |
| 21 | // The next visit offers the passkey first, and its button is enabled by |
| 22 | // the module. With script off, that door says |
| 23 | // passkeys need JavaScript and its Other ways still opens the email way. |
| 24 | func TestDoorBrowserChooserPasskeyAndReturn(t *testing.T) { |
| 25 | p, ctx, _ := idBrowser(t) |
| 26 | s := signInFully(t, p.credential, sealtest.CanaryAddress) |
| 27 | idBrowserRun(t, ctx, idBrowserCookie(s), chromedp.Navigate(browserIssuer+"/passkey/enrol"), chromedp.WaitVisible("#passkey-enrol"), chromedp.Click("#passkey-enrol"), chromedp.WaitVisible(`form[action="/passkey/codes"]`)) |
| 28 | idBrowserRun(t, ctx, cdpStep(network.DeleteCookies, network.DeleteCookiesParams{Name: s.Name, URL: browserIssuer + "/"})) |
| 29 | |
| 30 | // First visit: the field, with passkeys offered in its autofill (the |
| 31 | // door's markup is checked in TestDoorStates; it is the only way in on |
| 32 | // this page that runs without a press). |
| 33 | idBrowserRun(t, ctx, chromedp.Navigate(browserIssuer+"/signin?return_to=%2Fpasskey%2Fcodes")) |
| 34 | // With one discoverable credential and presence simulated, the virtual |
| 35 | // authenticator answers the field's conditional request by itself: the |
| 36 | // module begins, gets, finishes against {ok,to} and navigates to where |
| 37 | // the sign-in was going, with nobody pressing anything. |
| 38 | idBrowserRun(t, ctx, chromedp.WaitVisible(`form[action="/passkey/codes"]`)) |
| 39 | var conditional bool |
| 40 | idBrowserRun(t, ctx, harness.Into(&conditional, chromedp.Evaluate[bool](`PublicKeyCredential.isConditionalMediationAvailable()`, chromedp.EvalAwaitPromise))) |
| 41 | if !conditional { |
| 42 | t.Fatal("this Chrome reports no conditional mediation") |
| 43 | } |
| 44 | var location string |
| 45 | idBrowserRun(t, ctx, harness.Into(&location, chromedp.Location())) |
| 46 | if !strings.HasPrefix(location, browserIssuer+"/passkey/codes") { |
| 47 | t.Fatal("conditional UI did not sign in to where the sign-in was going", location) |
| 48 | } |
| 49 | idBrowserRun(t, ctx, cdpStep(network.DeleteCookies, network.DeleteCookiesParams{Name: s.Name, URL: browserIssuer + "/"}), |
| 50 | cdpStep(network.DeleteCookies, network.DeleteCookiesParams{Name: jarCookie, URL: browserIssuer + "/"})) |
| 51 | |
| 52 | // This browser proved the address once (a sealed record, as a link |
| 53 | // sign-in writes). Other ways, then a passkey, then the button the |
| 54 | // module enables: the passkey's owner is the same person, so the |
| 55 | // address is kept. |
| 56 | remembered := sealedRecord(t, p.credential.App, lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: sealtest.CanaryAddress}) |
| 57 | idBrowserRun(t, ctx, idBrowserCookie(remembered), |
| 58 | chromedp.Navigate(browserIssuer+"/signin?return_to=%2Fpasskey%2Fcodes&ways=1&way=email"), chromedp.WaitVisible(`a[href*="way=passkey"]`), |
| 59 | chromedp.Click(`a[href*="way=passkey"]`), chromedp.WaitVisible(`button[data-rst-passkey]:enabled`), |
| 60 | chromedp.Click(`button[data-rst-passkey]`), chromedp.WaitVisible(`form[action="/passkey/codes"]`)) |
| 61 | idBrowserRun(t, ctx, harness.Into(&location, chromedp.Location())) |
| 62 | if !strings.HasPrefix(location, browserIssuer+"/passkey/codes") { |
| 63 | t.Fatal("the passkey sign-in lost where it was going", location) |
| 64 | } |
| 65 | |
| 66 | // The next visit offers the passkey first, enabled by the module. |
| 67 | idBrowserRun(t, ctx, cdpStep(network.DeleteCookies, network.DeleteCookiesParams{Name: s.Name, URL: browserIssuer + "/"}), |
| 68 | chromedp.Navigate(browserIssuer+"/signin"), chromedp.WaitVisible(`button[data-rst-passkey]:enabled`)) |
| 69 | var label string |
| 70 | idBrowserRun(t, ctx, harness.Into(&label, chromedp.Text(`button[data-rst-passkey]`))) |
| 71 | if !strings.Contains(label, "Sign in with your passkey") { |
| 72 | t.Fatal("the next visit does not offer the passkey first", label) |
| 73 | } |
| 74 | |
| 75 | // Script off: the noscript line, and Other ways opens the email way, |
| 76 | // the address kept. |
| 77 | idBrowserRun(t, ctx, cdpStep(emulation.SetScriptExecutionDisabled, emulation.SetScriptExecutionDisabledParams{Value: true}), |
| 78 | chromedp.Navigate(browserIssuer+"/signin"), chromedp.WaitVisible(`a[href*="ways=1"]`), |
| 79 | chromedp.Click(`a[href*="ways=1"]`), chromedp.WaitVisible(`a[href*="way=email"]`), |
| 80 | chromedp.Click(`a[href*="way=email"]`), chromedp.WaitVisible(`form[action="/signin"] button[type="submit"]`)) |
| 81 | var html string |
| 82 | idBrowserRun(t, ctx, harness.Into(&html, chromedp.OuterHTML("main"))) |
| 83 | if !strings.Contains(html, sealtest.CanaryAddress) { |
| 84 | t.Fatal("the email way lost the remembered address") |
| 85 | } |
| 86 | } |
| 87 | |