| 1 | package home |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "net/url" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "amadan.net/oficina/home/internal/home/sealtest" |
| 12 | ) |
| 13 | |
| 14 | func TestIDLocalSignoutWithHangingIssuer(t *testing.T) { |
| 15 | accepted := make(chan struct{}) |
| 16 | release := make(chan struct{}) |
| 17 | issuer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { close(accepted); <-release })) |
| 18 | defer issuer.Close() |
| 19 | defer close(release) |
| 20 | p := newExchangePair(t) |
| 21 | credential, directory := signedInPair(t, p) |
| 22 | p.directory.origins.Credential = issuer.URL |
| 23 | r := exchangeRequest(p.directory, "POST", directoryOrigin+"/signout", directoryOrigin, url.Values{}, directory) |
| 24 | if r.Code != 303 || r.Header().Get("Location") != "/signed-out" { |
| 25 | t.Fatal("local signout depends on hanging issuer", r.Code, r.Header().Get("Location")) |
| 26 | } |
| 27 | if s, ok := p.directory.sessions.From(func() *http.Request { |
| 28 | r := httptest.NewRequest("GET", directoryOrigin+"/", nil) |
| 29 | r.AddCookie(directory) |
| 30 | return r |
| 31 | }()); ok || s.Subject != "" { |
| 32 | t.Fatal("local session not revoked") |
| 33 | } |
| 34 | page := exchangeRequest(p.directory, "GET", directoryOrigin+"/signed-out", "", nil, r.Result().Cookies()...) |
| 35 | if page.Code != 200 { |
| 36 | t.Fatal("local landing failed") |
| 37 | } |
| 38 | ctx, cancel := context.WithCancel(context.Background()) |
| 39 | defer cancel() |
| 40 | req, _ := http.NewRequestWithContext(ctx, "POST", signoutFormAction(t, page.Body.String()), nil) |
| 41 | done := make(chan struct{}) |
| 42 | go func() { |
| 43 | defer close(done) |
| 44 | resp, _ := issuer.Client().Do(req) |
| 45 | if resp != nil { |
| 46 | resp.Body.Close() |
| 47 | } |
| 48 | }() |
| 49 | select { |
| 50 | case <-accepted: |
| 51 | case <-time.After(time.Second): |
| 52 | t.Fatal("issuer did not accept connection") |
| 53 | } |
| 54 | // The actual page form is now waiting on the issuer; local pages still |
| 55 | // respond and the copied directory session remains revoked. |
| 56 | again := exchangeRequest(p.directory, "GET", directoryOrigin+"/signed-out", "", nil) |
| 57 | if again.Code != 200 { |
| 58 | t.Fatal("hanging form blocked local landing") |
| 59 | } |
| 60 | local := httptest.NewRequest("GET", directoryOrigin+"/", nil) |
| 61 | local.AddCookie(directory) |
| 62 | if _, ok := p.directory.sessions.From(local); ok { |
| 63 | t.Fatal("hanging issuer restored local session") |
| 64 | } |
| 65 | // A credential-origin sign-out also lands on its own origin. |
| 66 | end := exchangeRequest(p.credential.App, "POST", Origin+"/signout", Origin, url.Values{}, credential) |
| 67 | if end.Code != 303 || end.Header().Get("Location") != Origin+"/signed-out" { |
| 68 | t.Fatal("credential signout leaves its origin") |
| 69 | } |
| 70 | if page := exchangeRequest(p.credential.App, "GET", Origin+"/signed-out", "", nil); page.Code != 200 { |
| 71 | t.Fatal("credential landing missing") |
| 72 | } |
| 73 | cancel() |
| 74 | select { |
| 75 | case <-done: |
| 76 | case <-time.After(time.Second): |
| 77 | t.Fatal("issuer probe failed to cancel") |
| 78 | } |
| 79 | } |
| 80 | func TestIDSigninRemainsLocal(t *testing.T) { |
| 81 | p := newExchangePair(t) |
| 82 | s := signInFully(t, p.credential, sealtest.CanaryAddress) |
| 83 | if s == nil { |
| 84 | t.Fatal("local signin failed") |
| 85 | } |
| 86 | } |
| 87 | |