| 1 | package oficinaid |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/rastrillo/rastrillo/auth" |
| 5 | "context" |
| 6 | "database/sql" |
| 7 | "database/sql/driver" |
| 8 | "errors" |
| 9 | "io" |
| 10 | "net/http" |
| 11 | "net/http/httptest" |
| 12 | "testing" |
| 13 | "time" |
| 14 | ) |
| 15 | |
| 16 | // This driver fails the first identity lookup and succeeds on an unsafe retry. |
| 17 | type transientDriver struct{} |
| 18 | type transientConn struct{ queries int } |
| 19 | type transientRows struct{ done bool } |
| 20 | |
| 21 | func (transientDriver) Open(string) (driver.Conn, error) { return &transientConn{}, nil } |
| 22 | func (*transientConn) Prepare(string) (driver.Stmt, error) { |
| 23 | return nil, errors.New("unexpected prepare") |
| 24 | } |
| 25 | func (*transientConn) Close() error { return nil } |
| 26 | func (*transientConn) Begin() (driver.Tx, error) { return nil, errors.New("unexpected transaction") } |
| 27 | func (c *transientConn) QueryContext(context.Context, string, []driver.NamedValue) (driver.Rows, error) { |
| 28 | c.queries++ |
| 29 | if c.queries == 1 { |
| 30 | return nil, errors.New("temporary lookup failure") |
| 31 | } |
| 32 | return &transientRows{}, nil |
| 33 | } |
| 34 | func (*transientRows) Columns() []string { |
| 35 | return []string{"subject", "method", "auth_time", "created_at", "expires_at"} |
| 36 | } |
| 37 | func (*transientRows) Close() error { return nil } |
| 38 | func (r *transientRows) Next(out []driver.Value) error { |
| 39 | if r.done { |
| 40 | return io.EOF |
| 41 | } |
| 42 | r.done = true |
| 43 | now := time.Now().UTC() |
| 44 | copy(out, []driver.Value{"pr_aaaaaaaaaaaaaaaaaaaaaaaaaa", Method, now.Format(time.RFC3339), now.Format(time.RFC3339), now.Add(time.Hour).Format(time.RFC3339)}) |
| 45 | return nil |
| 46 | } |
| 47 | func init() { sql.Register("oficina_id_transient_lookup", transientDriver{}) } |
| 48 | func TestIDLookupFailureCannotRetryIntoLocalPath(t *testing.T) { |
| 49 | d, e := sql.Open("oficina_id_transient_lookup", "") |
| 50 | if e != nil { |
| 51 | t.Fatal(e) |
| 52 | } |
| 53 | defer d.Close() |
| 54 | d.SetMaxOpenConns(1) |
| 55 | authentication, e := auth.New(auth.Config{DB: d, Origin: "https://equipo.example", InstanceKey: "test-identity-cookie-key", ProofOff: true}) |
| 56 | if e != nil { |
| 57 | t.Fatal(e) |
| 58 | } |
| 59 | h := &HTTP{Store: &Store{DB: d}, Auth: authentication, Render: func(w http.ResponseWriter, _ *http.Request, _ View, status int) { w.WriteHeader(status) }} |
| 60 | req := httptest.NewRequest("GET", "https://equipo.example/work", nil) |
| 61 | req.AddCookie(&http.Cookie{Name: authentication.SessionCookie(), Value: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}) |
| 62 | next := false |
| 63 | h.Authenticate(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { next = true })).ServeHTTP(httptest.NewRecorder(), req) |
| 64 | if next { |
| 65 | t.Fatal("failed identity lookup retried into unresolved Home subject") |
| 66 | } |
| 67 | } |
| 68 | |