diff --git a/pkg/coredata/session.go b/pkg/coredata/session.go index 3eab76468..108e085ee 100644 --- a/pkg/coredata/session.go +++ b/pkg/coredata/session.go @@ -22,11 +22,14 @@ package coredata import ( "context" + "database/sql/driver" "encoding" + "encoding/json" "errors" "fmt" "maps" "net" + "strings" "time" "github.com/jackc/pgx/v5" @@ -56,11 +59,75 @@ type ( Sessions []*Session - SessionData struct{} + SessionData struct { + BoundHost string `json:"bound_host,omitempty"` + } AuthMethod string ) +func NormalizeBoundHost(host string) string { + return strings.ToLower(strings.TrimSpace(host)) +} + +func SessionDataForHost(host string) SessionData { + return SessionData{ + BoundHost: NormalizeBoundHost(host), + } +} + +func (d SessionData) IsDomainBound() bool { + return d.BoundHost != "" +} + +func (d SessionData) MatchesBoundHost(requestHost string) bool { + if !d.IsDomainBound() { + return false + } + + return NormalizeBoundHost(d.BoundHost) == NormalizeBoundHost(requestHost) +} + +func (d SessionData) Value() (driver.Value, error) { + data, err := json.Marshal(d) + if err != nil { + return nil, fmt.Errorf("cannot marshal session data: %w", err) + } + + return data, nil +} + +func (d *SessionData) Scan(value any) error { + if value == nil { + *d = SessionData{} + + return nil + } + + var data []byte + + switch v := value.(type) { + case string: + data = []byte(v) + case []byte: + data = v + default: + return fmt.Errorf("cannot scan session data: unsupported type %T", value) + } + + if len(data) == 0 { + *d = SessionData{} + + return nil + } + + if err := json.Unmarshal(data, d); err != nil { + return fmt.Errorf("cannot unmarshal session data: %w", err) + } + + return nil +} + const ( AuthMethodMagicLink AuthMethod = "MAGIC_LINK" AuthMethodPassword AuthMethod = "PASSWORD" diff --git a/pkg/coredata/session_data_test.go b/pkg/coredata/session_data_test.go new file mode 100644 index 000000000..8ca60e4fe --- /dev/null +++ b/pkg/coredata/session_data_test.go @@ -0,0 +1,42 @@ +// Copyright (c) 2026 Probo Inc . +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice appear in all copies. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH +// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR +// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +// PERFORMANCE OF THIS SOFTWARE. + +package coredata + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestSessionDataForHost(t *testing.T) { + t.Parallel() + + data := SessionDataForHost(" Portal.Example.COM ") + + assert.Equal(t, "portal.example.com", data.BoundHost) + assert.True(t, data.IsDomainBound()) + assert.True(t, data.MatchesBoundHost("portal.example.com")) + assert.True(t, data.MatchesBoundHost("PORTAL.example.com")) + assert.False(t, data.MatchesBoundHost("other.example.com")) +} + +func TestSessionData_MatchesBoundHost_Unbound(t *testing.T) { + t.Parallel() + + data := SessionData{} + + assert.False(t, data.IsDomainBound()) + assert.False(t, data.MatchesBoundHost("portal.example.com")) +}