Files
foundry-core/internal/auth/session.go
codex-bot e626a2359e
Foundry Go checks / check (push) Successful in 1m42s
Foundry Go checks / check (pull_request) Successful in 1m51s
Add authenticated profiles and prefill Google account names
2026-11-05 18:17:44 +08:00

28 lines
721 B
Go

package auth
import (
"context"
"database/sql"
"errors"
"strings"
)
var ErrInvalidSession = errors.New("invalid session")
// Authenticate resolves only an unexpired session belonging to this project.
func Authenticate(ctx context.Context, db *sql.DB, project, authorization string) (string, error) {
value, ok := strings.CutPrefix(authorization, "Bearer ")
if !ok || !tokenPattern.MatchString(value) {
return "", ErrInvalidSession
}
var id string
err := db.QueryRowContext(ctx, `
SELECT account_id::text FROM project_sessions
WHERE project=$1 AND token_hash=$2 AND expires_at>now()
`, project, digest(value)).Scan(&id)
if errors.Is(err, sql.ErrNoRows) {
return "", ErrInvalidSession
}
return id, err
}