| 1 | package personapi |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/base64" |
| 6 | "errors" |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "amadan.net/oficina/equipo/internal/equipo/kernel" |
| 14 | "amadan.net/rastrillo/rastrillo/sessions" |
| 15 | "gorm.io/gorm" |
| 16 | ) |
| 17 | |
| 18 | func TestNativeDeviceRejectsUnverifiedPrincipal(t *testing.T) { |
| 19 | s := &DeviceStore{Invalid: errors.New("invalid"), NativeSession: func(*gorm.DB, string) (sessions.Session, error) { |
| 20 | t.Fatal("unverified session reached lookup") |
| 21 | return sessions.Session{}, nil |
| 22 | }, Now: time.Now} |
| 23 | key := base64.RawURLEncoding.EncodeToString(make([]byte, 32)) |
| 24 | for _, p := range []PersonPrincipal{{}, {Address: "person", CredentialID: "paired", SessionHash: "hash"}, {Address: "person", Pending: true, SessionHash: "hash"}, {Address: "person"}} { |
| 25 | _, err := s.RegisterNative(WithVerifiedPrincipal(context.Background(), p), kernel.Actor{Address: p.Address}, key, "phone") |
| 26 | if !errors.Is(err, ErrUnauthenticated) { |
| 27 | t.Fatalf("accepted unverified principal: %v", err) |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | func TestNativeDeviceRouteRejectsBearer(t *testing.T) { |
| 33 | a := Authenticator{Origin: "https://equipo.example", Forbidden: errors.New("forbidden"), Cookie: func(*http.Request) (PersonPrincipal, error) { |
| 34 | return PersonPrincipal{Address: "person", SessionEpoch: 1, SessionHash: "hash"}, nil |
| 35 | }, Bearer: func(context.Context, string) (PersonPrincipal, error) { |
| 36 | return PersonPrincipal{Address: "person", SessionEpoch: 1, CredentialID: "paired", SessionHash: "hash"}, nil |
| 37 | }, Member: func(*http.Request, PersonPrincipal) error { return nil }} |
| 38 | r := httptest.NewRequest("POST", "https://equipo.example/api/v1/auth/devices/native", nil) |
| 39 | r.Header.Set("Authorization", "Bearer paired") |
| 40 | if _, err := a.AuthenticateRoute(r, Route{Operation: "native_device"}); !errors.Is(err, ErrUnauthenticated) { |
| 41 | t.Fatal("device bearer registered native credential", err) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // A session from Oficina ID lives 7 days and its devices expire with it. |
| 46 | // Approving a paired device would hand out a 30-day credential from it. |
| 47 | func TestNativeSessionNeverApprovesAPairedDevice(t *testing.T) { |
| 48 | forbidden := errors.New("forbidden") |
| 49 | s := &DeviceStore{Invalid: errors.New("invalid"), Forbidden: forbidden, Now: time.Now, Write: func(context.Context, func(*gorm.DB) error) error { |
| 50 | t.Fatal("native session reached the approval write") |
| 51 | return nil |
| 52 | }} |
| 53 | p := PersonPrincipal{Address: "person", SessionEpoch: 1, SessionHash: "hash"} |
| 54 | err := s.Approve(WithVerifiedPrincipal(context.Background(), p), kernel.Actor{Address: "person"}, "device", "ABCDEFGH", kernel.Mutation{RequestID: "request", Revision: 1}) |
| 55 | if !errors.Is(err, forbidden) { |
| 56 | t.Fatalf("native session approved a device: %v", err) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func TestNativeSessionNeverApprovesFromTheBrowserForm(t *testing.T) { |
| 61 | forbidden := errors.New("forbidden") |
| 62 | var failed error |
| 63 | b := &DeviceBrowser{Invalid: errors.New("invalid"), Forbidden: forbidden, Cookie: func(*http.Request) (PersonPrincipal, error) { |
| 64 | return PersonPrincipal{Address: "person", SessionEpoch: 1, SessionHash: "hash"}, nil |
| 65 | }, Approve: func(context.Context, kernel.Actor, string, string, kernel.Mutation) error { |
| 66 | t.Fatal("native session reached approval") |
| 67 | return nil |
| 68 | }, Failure: func(_ http.ResponseWriter, _ *http.Request, err error) { failed = err }} |
| 69 | r := httptest.NewRequest("POST", "https://equipo.example/auth/devices/device/approve", strings.NewReader("typed_code=ABCDEFGH&request_id=request&revision=1")) |
| 70 | r.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 71 | b.ApproveForm(httptest.NewRecorder(), r) |
| 72 | if !errors.Is(failed, forbidden) { |
| 73 | t.Fatalf("native session approved from the browser: %v", failed) |
| 74 | } |
| 75 | } |
| 76 | |