Melora Hugues
d5aa640df0
Because polyculeconnect is a OIDC proxy, we need to know which auth backend to use. This is provided using a query param or a form, so we need to get it from our own middleware. This commit adds the following elements: - basic DB storage for the backends - support for DB migrations and a first test migration (not definitive) - middleware to get the backend from the request and put it in the context - test that the backend exists in the auth flow
46 lines
1.1 KiB
Go
46 lines
1.1 KiB
Go
package backend
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/model"
|
|
)
|
|
|
|
var ErrNotFound = errors.New("backend not found")
|
|
|
|
// const backendRows = `"id", "name", "oidc_id", "oidc_secret"`
|
|
const backendRows = `"id", "name"`
|
|
|
|
type BackendDB interface {
|
|
GetBackendByName(ctx context.Context, name string) (*model.Backend, error)
|
|
}
|
|
|
|
type sqlBackendDB struct {
|
|
db *sql.DB
|
|
}
|
|
|
|
func backendFromRow(row sql.Row) (*model.Backend, error) {
|
|
var res model.Backend
|
|
|
|
if err := row.Scan(&res.ID, &res.Name); err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, fmt.Errorf("invalid format for backend: %w", err)
|
|
}
|
|
return &res, nil
|
|
}
|
|
|
|
func (db *sqlBackendDB) GetBackendByName(ctx context.Context, name string) (*model.Backend, error) {
|
|
query := fmt.Sprintf(`SELECT %s FROM "backend" WHERE "name" = ?`, backendRows)
|
|
fmt.Println(query, name)
|
|
row := db.db.QueryRowContext(ctx, query, name)
|
|
return backendFromRow(*row)
|
|
}
|
|
|
|
func New(db *sql.DB) *sqlBackendDB {
|
|
return &sqlBackendDB{db: db}
|
|
}
|